Skip to:
Content

BuddyPress.org

Changeset 7781


Ignore:
Timestamp:
02/04/2014 02:19:11 AM (11 years ago)
Author:
boonebgorges
Message:

Add caching support for default xprofile field visibility settings

These settings rarely change, and so are good candidates for persistent caching

See #1332

Location:
trunk/bp-xprofile
Files:
2 edited

Legend:

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

    r6342 r7781  
    1818    wp_cache_delete( 'xprofile_group_' . $group_obj->id, 'bp' );
    1919}
     20add_action( 'xprofile_groups_deleted_group', 'xprofile_clear_profile_groups_object_cache' );
     21add_action( 'xprofile_groups_saved_group',   'xprofile_clear_profile_groups_object_cache' );
    2022
    2123function xprofile_clear_profile_data_object_cache( $group_id ) {
    2224    wp_cache_delete( 'bp_user_fullname_' . bp_loggedin_user_id(), 'bp' );
    2325}
     26add_action( 'xprofile_updated_profile', 'xprofile_clear_profile_data_object_cache'   );
    2427
    25 // List actions to clear object caches on
    26 add_action( 'xprofile_groups_deleted_group', 'xprofile_clear_profile_groups_object_cache' );
    27 add_action( 'xprofile_groups_saved_group',   'xprofile_clear_profile_groups_object_cache' );
    28 add_action( 'xprofile_updated_profile',      'xprofile_clear_profile_data_object_cache'   );
     28/**
     29 * Clear caches when a field object is modified.
     30 *
     31 * @since BuddyPress (2.0.0)
     32 *
     33 * @param BP_XProfile_Field
     34 */
     35function xprofile_clear_profile_field_object_cache( $field_obj ) {
     36    // Clear default visibility level cache
     37    wp_cache_delete( 'xprofile_default_visibility_levels', 'bp' );
     38}
     39add_action( 'xprofile_fields_saved_field', 'xprofile_clear_profile_field_object_cache' );
     40add_aciton( 'xprofile_fields_deleted_field', 'xprofile_clear_profile_field_object_cache' );
    2941
    3042// List actions to clear super cached pages on, if super cache is installed
  • trunk/bp-xprofile/bp-xprofile-classes.php

    r7780 r7781  
    389389
    390390    /**
    391      * Fetch the admin-set preferences for all fields
    392      *
    393      * @since BuddyPress (1.6)
    394      *
    395      * @return array $default_visibility_levels An array, keyed by field_id, of default
    396      *   visibility level + allow_custom (whether the admin allows this field to be set by user)
     391     * Fetch the admin-set preferences for all fields.
     392     *
     393     * @since BuddyPress (1.6.0)
     394     *
     395     * @return array $default_visibility_levels An array, keyed by
     396     *         field_id, of default visibility level + allow_custom
     397     *         (whether the admin allows this field to be set by user)
    397398     */
    398399    public static function fetch_default_visibility_levels() {
    399400        global $wpdb, $bp;
    400401
    401         $levels = $wpdb->get_results( "SELECT object_id, meta_key, meta_value FROM {$bp->profile->table_name_meta} WHERE object_type = 'field' AND ( meta_key = 'default_visibility' OR meta_key = 'allow_custom_visibility' )" );
    402 
    403         // Arrange so that the field id is the key and the visibility level the value
    404         $default_visibility_levels = array();
    405         foreach( $levels as $level ) {
    406             if ( 'default_visibility' == $level->meta_key ) {
    407                 $default_visibility_levels[$level->object_id]['default'] = $level->meta_value;
    408             } else if ( 'allow_custom_visibility' == $level->meta_key ) {
    409                 $default_visibility_levels[$level->object_id]['allow_custom'] = $level->meta_value;
    410             }
     402        $default_visibility_levels = wp_cache_get( 'xprofile_default_visibility_levels', 'bp' );
     403
     404        if ( false === $default_visibility_levels ) {
     405            $levels = $wpdb->get_results( "SELECT object_id, meta_key, meta_value FROM {$bp->profile->table_name_meta} WHERE object_type = 'field' AND ( meta_key = 'default_visibility' OR meta_key = 'allow_custom_visibility' )" );
     406
     407            // Arrange so that the field id is the key and the visibility level the value
     408            $default_visibility_levels = array();
     409            foreach ( $levels as $level ) {
     410                if ( 'default_visibility' == $level->meta_key ) {
     411                    $default_visibility_levels[ $level->object_id ]['default'] = $level->meta_value;
     412                } else if ( 'allow_custom_visibility' == $level->meta_key ) {
     413                    $default_visibility_levels[ $level->object_id ]['allow_custom'] = $level->meta_value;
     414                }
     415            }
     416
     417            wp_cache_set( 'xprofile_default_visibility_levels', $default_visibility_levels, 'bp' );
    411418        }
    412419
Note: See TracChangeset for help on using the changeset viewer.