Changeset 7914 for trunk/bp-xprofile/bp-xprofile-cache.php
- Timestamp:
- 02/17/2014 07:42:03 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/bp-xprofile/bp-xprofile-cache.php
r7812 r7914 2 2 3 3 /** 4 * BuddyPress XProfile Template Tags4 * BuddyPress XProfile Caching Functions 5 5 * 6 6 * Caching functions handle the clearing of cached objects and pages on specific … … 8 8 * 9 9 * @package BuddyPress 10 * @subpackage XProfileTemplate11 10 */ 12 11 13 12 // Exit if accessed directly 14 13 if ( !defined( 'ABSPATH' ) ) exit; 14 15 /** 16 * Slurp up xprofilemeta for a specified set of profile objects. 17 * 18 * We do not use bp_update_meta_cache() for the xprofile component. This is 19 * because the xprofile component has three separate object types (group, 20 * field, and data) and three corresponding cache groups. Using the technique 21 * in bp_update_meta_cache(), pre-fetching would take three separate database 22 * queries. By grouping them together, we can reduce the required queries to 23 * one. 24 * 25 * This function is called within a bp_has_profile() loop. 26 * 27 * @since BuddyPress (2.0.0) 28 * 29 * @param array $object_ids Multi-dimensional array of object_ids, keyed by 30 * object type ('group', 'field', 'data') 31 */ 32 function bp_xprofile_update_meta_cache( $object_ids = array(), $user_id = 0 ) { 33 global $wpdb; 34 35 if ( empty( $object_ids ) ) { 36 return false; 37 } 38 39 // $object_ids is a multi-dimensional array 40 $uncached_object_ids = array( 41 'group' => array(), 42 'field' => array(), 43 'data' => array(), 44 ); 45 46 $cache_groups = array( 47 'group' => 'xprofile_group_meta', 48 'field' => 'xprofile_field_meta', 49 'data' => 'xprofile_data_meta', 50 ); 51 52 $do_query = false; 53 foreach ( $uncached_object_ids as $object_type => $uncached_object_type_ids ) { 54 if ( ! empty( $object_ids[ $object_type ] ) ) { 55 // Sanitize $object_ids passed to the function 56 $object_type_ids = wp_parse_id_list( $object_ids[ $object_type ] ); 57 58 // Get non-cached IDs for each object type 59 $uncached_object_ids[ $object_type ] = bp_get_non_cached_ids( $object_type_ids, $cache_groups[ $object_type ] ); 60 61 // Set the flag to do the meta query 62 if ( ! empty( $uncached_object_ids[ $object_type ] ) && ! $do_query ) { 63 $do_query = true; 64 } 65 } 66 } 67 68 // If there are uncached items, go ahead with the query 69 if ( $do_query ) { 70 $where = array(); 71 foreach ( $uncached_object_ids as $otype => $oids ) { 72 if ( empty( $oids ) ) { 73 continue; 74 } 75 76 $oids_sql = implode( ',', wp_parse_id_list( $oids ) ); 77 $where[] = $wpdb->prepare( "( object_type = %s AND object_id IN ({$oids_sql}) )", $otype ); 78 } 79 $where_sql = implode( " OR ", $where ); 80 } 81 82 83 $bp = buddypress(); 84 $meta_list = $wpdb->get_results( "SELECT object_id, object_type, meta_key, meta_value FROM {$bp->profile->table_name_meta} WHERE {$where_sql}" ); 85 86 if ( ! empty( $meta_list ) ) { 87 $object_type_caches = array( 88 'group' => array(), 89 'field' => array(), 90 'data' => array(), 91 ); 92 93 foreach ( $meta_list as $meta ) { 94 $oid = $meta->object_id; 95 $otype = $meta->object_type; 96 $okey = $meta->meta_key; 97 $ovalue = $meta->meta_value; 98 99 // Force subkeys to be array type 100 if ( ! isset( $cache[ $otype ][ $oid ] ) || ! is_array( $cache[ $otype ][ $oid ] ) ) { 101 $cache[ $otype ][ $oid ] = array(); 102 } 103 104 if ( ! isset( $cache[ $otype ][ $oid ][ $okey ] ) || ! is_array( $cache[ $otype ][ $oid ][ $okey ] ) ) { 105 $cache[ $otype ][ $oid ][ $okey ] = array(); 106 } 107 108 // Add to the cache array 109 $cache[ $otype ][ $oid ][ $okey ][] = maybe_unserialize( $ovalue ); 110 } 111 112 foreach ( $cache as $object_type => $object_caches ) { 113 $cache_group = $cache_groups[ $object_type ]; 114 foreach ( $object_caches as $object_id => $object_cache ) { 115 wp_cache_set( $object_id, $object_cache, $cache_group ); 116 } 117 } 118 } 119 120 return; 121 } 15 122 16 123 function xprofile_clear_profile_groups_object_cache( $group_obj ) {
Note: See TracChangeset
for help on using the changeset viewer.