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
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
| 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.