Skip to:
Content

BuddyPress.org

Changeset 9693


Ignore:
Timestamp:
04/03/2015 10:11:15 PM (5 years ago)
Author:
johnjamesjacoby
Message:

XProfile: Improvements to bp_xprofile_update_meta_cache():

  • Cleaner logic for deciding whether a query is necessary
  • Bail if: no query necessary, no where conditions, no uncached data, no data retrieved
  • Avoid possible debug notice if $where_sql was never defined
  • Fewer indentations and less needless execution via improved sanity checks

See #6347.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-xprofile/bp-xprofile-cache.php

    r9676 r9693  
    5656    global $wpdb;
    5757
     58    // Bail if no objects
    5859    if ( empty( $object_ids ) ) {
    5960        return false;
    6061    }
    6162
    62     // $object_ids is a multi-dimensional array
     63    $bp = buddypress();
     64
     65    // Define the array where uncached object IDs will be stored
    6366    $uncached_object_ids = array(
    64         'group' => array(),
    65         'field' => array(),
    66         'data'  => array(),
     67        'group',
     68        'field',
     69        'data'
    6770    );
    6871
     72    // Define the cache groups for the 3 types of XProfile metadata
    6973    $cache_groups = array(
    7074        'group' => 'xprofile_group_meta',
     
    7377    );
    7478
     79    // No reason to query yet
    7580    $do_query = false;
    76     foreach ( $uncached_object_ids as $object_type => $uncached_object_type_ids ) {
    77         if ( ! empty( $object_ids[ $object_type ] ) ) {
    78 
    79             // Sanitize $object_ids passed to the function
    80             $object_type_ids = wp_parse_id_list( $object_ids[ $object_type ] );
    81 
    82             // Get non-cached IDs for each object type
    83             $uncached_object_ids[ $object_type ] = bp_get_non_cached_ids( $object_type_ids, $cache_groups[ $object_type ] );
    84 
    85             // Set the flag to do the meta query
    86             if ( ! empty( $uncached_object_ids[ $object_type ] ) && ! $do_query ) {
    87                 $do_query = true;
    88             }
    89         }
    90     }
    91 
    92     // If there are uncached items, go ahead with the query
    93     if ( true === $do_query ) {
    94         $where = array();
    95         foreach ( $uncached_object_ids as $otype => $oids ) {
    96             if ( empty( $oids ) ) {
    97                 continue;
    98             }
    99 
    100             $oids_sql = implode( ',', wp_parse_id_list( $oids ) );
    101             $where[]  = $wpdb->prepare( "( object_type = %s AND object_id IN ({$oids_sql}) )", $otype );
    102         }
    103         $where_sql = implode( " OR ", $where );
    104     }
    105 
    106 
    107     $bp        = buddypress();
    108     $cache     = array();
     81
     82    // Loop through object types and look for uncached data
     83    foreach ( $uncached_object_ids as $object_type ) {
     84
     85        // Skip if empty object type
     86        if ( empty( $object_ids[ $object_type ] ) ) {
     87            continue;
     88        }
     89
     90        // Sanitize $object_ids passed to the function
     91        $object_type_ids = wp_parse_id_list( $object_ids[ $object_type ] );
     92
     93        // Get non-cached IDs for each object type
     94        $uncached_object_ids[ $object_type ] = bp_get_non_cached_ids( $object_type_ids, $cache_groups[ $object_type ] );
     95
     96        // Set the flag to do the meta query
     97        if ( ! empty( $uncached_object_ids[ $object_type ] ) && ( false === $do_query ) ) {
     98            $do_query = true;
     99        }
     100    }
     101
     102    // Bail if no uncached items
     103    if ( false === $do_query ) {
     104        return;
     105    }
     106
     107    // Setup where conditions for query
     108    $where_sql        = '';
     109    $where_conditions = array();
     110
     111    // Loop through uncached objects and prepare to query for them
     112    foreach ( $uncached_object_ids as $otype => $oids ) {
     113
     114        // Skip empty object IDs
     115        if ( empty( $oids ) ) {
     116            continue;
     117        }
     118
     119        // Compile WHERE query conditions for uncached metadata
     120        $oids_sql           = implode( ',', wp_parse_id_list( $oids ) );
     121        $where_conditions[] = $wpdb->prepare( "( object_type = %s AND object_id IN ({$oids_sql}) )", $otype );
     122    }
     123
     124    // Bail if no where conditions
     125    if ( empty( $where_conditions ) ) {
     126        return;
     127    }
     128
     129    // Setup the WHERE query part
     130    $where_sql = implode( " OR ", $where_conditions );
     131
     132    // Attempt to query meta values
    109133    $meta_list = $wpdb->get_results( "SELECT object_id, object_type, meta_key, meta_value FROM {$bp->profile->table_name_meta} WHERE {$where_sql}" );
    110134
    111     if ( ! empty( $meta_list ) ) {
    112         foreach ( $meta_list as $meta ) {
    113             $oid    = $meta->object_id;
    114             $otype  = $meta->object_type;
    115             $okey   = $meta->meta_key;
    116             $ovalue = $meta->meta_value;
    117 
    118             // Force subkeys to be array type
    119             if ( ! isset( $cache[ $otype ][ $oid ] ) || ! is_array( $cache[ $otype ][ $oid ] ) ) {
    120                 $cache[ $otype ][ $oid ] = array();
    121             }
    122 
    123             if ( ! isset( $cache[ $otype ][ $oid ][ $okey ] ) || ! is_array( $cache[ $otype ][ $oid ][ $okey ] ) ) {
    124                 $cache[ $otype ][ $oid ][ $okey ] = array();
    125             }
    126 
    127             // Add to the cache array
    128             $cache[ $otype ][ $oid ][ $okey ][] = maybe_unserialize( $ovalue );
    129         }
    130 
    131         foreach ( $cache as $object_type => $object_caches ) {
    132             $cache_group = $cache_groups[ $object_type ];
    133             foreach ( $object_caches as $object_id => $object_cache ) {
    134                 wp_cache_set( $object_id, $object_cache, $cache_group );
    135             }
    136         }
    137     }
    138 
    139     return;
     135    // Bail if no results found
     136    if ( empty( $meta_list ) || is_wp_error( $meta_list ) ) {
     137        return;
     138    }
     139
     140    // Setup empty cache array
     141    $cache = array();
     142
     143    // Loop through metas
     144    foreach ( $meta_list as $meta ) {
     145        $oid    = $meta->object_id;
     146        $otype  = $meta->object_type;
     147        $okey   = $meta->meta_key;
     148        $ovalue = $meta->meta_value;
     149
     150        // Force subkeys to be array type
     151        if ( ! isset( $cache[ $otype ][ $oid ] ) || ! is_array( $cache[ $otype ][ $oid ] ) ) {
     152            $cache[ $otype ][ $oid ] = array();
     153        }
     154
     155        if ( ! isset( $cache[ $otype ][ $oid ][ $okey ] ) || ! is_array( $cache[ $otype ][ $oid ][ $okey ] ) ) {
     156            $cache[ $otype ][ $oid ][ $okey ] = array();
     157        }
     158
     159        // Add to the cache array
     160        $cache[ $otype ][ $oid ][ $okey ][] = maybe_unserialize( $ovalue );
     161    }
     162
     163    // Loop through data and cache to the appropriate object
     164    foreach ( $cache as $object_type => $object_caches ) {
     165
     166        // Determine the cache group for this data
     167        $cache_group = $cache_groups[ $object_type ];
     168
     169        // Loop through objects and cache appropriately
     170        foreach ( $object_caches as $object_id => $object_cache ) {
     171            wp_cache_set( $object_id, $object_cache, $cache_group );
     172        }
     173    }
    140174}
    141175
Note: See TracChangeset for help on using the changeset viewer.