Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
02/13/2014 03:50:39 AM (12 years ago)
Author:
boonebgorges
Message:

Migrate xprofilemeta functions to the WP metadata API

The case of XProfile metadata is complex, because xprofile groups, fields, and
data all store their metadata in a single table, differentiated by object_type.
This complication requires additional filters on WP's core metadata queries.

See #4551

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-xprofile/bp-xprofile-filters.php

    r7812 r7862  
    262262}
    263263add_filter( 'bp_user_query_populate_extras', 'bp_xprofile_filter_user_query_populate_extras', 2, 2 );
     264
     265/**
     266 * Filter meta queries to modify for the xprofile data schema.
     267 *
     268 * @since BuddyPress (2.0.0)
     269 *
     270 * @access private Do not use.
     271 *
     272 * @param string $q SQL query.
     273 * @return string
     274 */
     275function bp_xprofile_filter_meta_query( $q ) {
     276        global $wpdb;
     277
     278        // Get the first word of the command
     279        preg_match( '/^(\S+)/', $q, $first_word_matches );
     280
     281        if ( empty( $first_word_matches[0] ) ) {
     282                return $q;
     283        }
     284
     285        // Get the field type
     286        preg_match( '/xprofile_(group|field|data)_id/', $q, $matches );
     287
     288        if ( empty( $matches[0] ) || empty( $matches[1] ) ) {
     289                return $q;
     290        }
     291
     292        switch ( $first_word_matches[0] ) {
     293
     294                /**
     295                 * SELECT:
     296                 * - replace 'xprofile_{fieldtype}_id' with 'object_id'
     297                 * - ensure that 'object_id' is aliased to 'xprofile_{fieldtype}_id',
     298                 *   because update_meta_cache() needs the column name to parse
     299                 *   the query results
     300                 * - append the 'object type' WHERE clause
     301                 */
     302                case 'SELECT' :
     303                        $q = str_replace(
     304                                array(
     305                                        $matches[0],
     306                                        'SELECT object_id',
     307                                        'WHERE ',
     308                                ),
     309                                array(
     310                                        'object_id',
     311                                        'SELECT object_id AS ' . $matches[0],
     312                                        $wpdb->prepare( 'WHERE object_type = %s AND ', $matches[1] ),
     313                                ),
     314                                $q
     315                        );
     316                        break;
     317
     318                /**
     319                 * UPDATE and DELETE:
     320                 * - replace 'xprofile_{fieldtype}_id' with 'object_id'
     321                 * - append the 'object type' WHERE clause
     322                 */
     323                case 'UPDATE' :
     324                case 'DELETE' :
     325                        $q = str_replace(
     326                                array(
     327                                        $matches[0],
     328                                        'WHERE ',
     329                                ),
     330                                array(
     331                                        'object_id',
     332                                        $wpdb->prepare( 'WHERE object_type = %s AND ', $matches[1] ),
     333                                ),
     334                                $q
     335                        );
     336                        break;
     337
     338                /**
     339                 * UPDATE and DELETE:
     340                 * - replace 'xprofile_{fieldtype}_id' with 'object_id'
     341                 * - ensure that the object_type field gets filled in
     342                 */
     343                case 'INSERT' :
     344                        $q = str_replace(
     345                                array(
     346                                        '`' . $matches[0] . '`',
     347                                        'VALUES (',
     348                                ),
     349                                array(
     350                                        '`object_type`,`object_id`',
     351                                        $wpdb->prepare( "VALUES (%s,", $matches[1] ),
     352                                ),
     353                                $q
     354                        );
     355                        break;
     356        }
     357
     358        return $q;
     359}
Note: See TracChangeset for help on using the changeset viewer.