bp_xprofile_filter_meta_query( string $q )

Filter meta queries to modify for the xprofile data schema.

Description

Parameters

$q

(Required) SQL query.

Return

(string)

Source

File: bp-xprofile/bp-xprofile-filters.php

554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
function bp_xprofile_filter_meta_query( $q ) {
    global $wpdb;
 
    $raw_q = $q;
 
    /*
     * Replace quoted content with __QUOTE__ to avoid false positives.
     * This regular expression will match nested quotes.
     */
    $quoted_regex = "/'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'/s";
    preg_match_all( $quoted_regex, $q, $quoted_matches );
    $q = preg_replace( $quoted_regex, '__QUOTE__', $q );
 
    // Get the first word of the command.
    preg_match( '/^(\S+)/', $q, $first_word_matches );
 
    if ( empty( $first_word_matches[0] ) ) {
        return $raw_q;
    }
 
    // Get the field type.
    preg_match( '/xprofile_(group|field|data)_id/', $q, $matches );
 
    if ( empty( $matches[0] ) || empty( $matches[1] ) ) {
        return $raw_q;
    }
 
    switch ( $first_word_matches[0] ) {
 
        /**
         * SELECT:
         * - replace 'xprofile_{fieldtype}_id' with 'object_id'
         * - ensure that 'object_id' is aliased to 'xprofile_{fieldtype}_id',
         *   because update_meta_cache() needs the column name to parse
         *   the query results
         * - append the 'object type' WHERE clause
         */
        case 'SELECT' :
            $q = str_replace(
                array(
                    $matches[0],
                    'SELECT object_id',
                    'WHERE ',
                ),
                array(
                    'object_id',
                    'SELECT object_id AS ' . $matches[0],
                    $wpdb->prepare( 'WHERE object_type = %s AND ', $matches[1] ),
                ),
                $q
            );
            break;
 
        /**
         * UPDATE and DELETE:
         * - replace 'xprofile_{fieldtype}_id' with 'object_id'
         * - append the 'object type' WHERE clause
         */
        case 'UPDATE' :
        case 'DELETE' :
            $q = str_replace(
                array(
                    $matches[0],
                    'WHERE ',
                ),
                array(
                    'object_id',
                    $wpdb->prepare( 'WHERE object_type = %s AND ', $matches[1] ),
                ),
                $q
            );
            break;
 
        /**
         * UPDATE and DELETE:
         * - replace 'xprofile_{fieldtype}_id' with 'object_id'
         * - ensure that the object_type field gets filled in
         */
        case 'INSERT' :
            $q = str_replace(
                array(
                    '`' . $matches[0] . '`',
                    'VALUES (',
                ),
                array(
                    '`object_type`,`object_id`',
                    $wpdb->prepare( "VALUES (%s,", $matches[1] ),
                ),
                $q
            );
            break;
    }
 
    // Put quoted content back into the string.
    if ( ! empty( $quoted_matches[0] ) ) {
        for ( $i = 0; $i < count( $quoted_matches[0] ); $i++ ) {
            $quote_pos = strpos( $q, '__QUOTE__' );
            $q = substr_replace( $q, $quoted_matches[0][ $i ], $quote_pos, 9 );
        }
    }
 
    return $q;
}

Changelog

Changelog
Version Description
BuddyPress 2.0.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.