Skip to:
Content

BuddyPress.org


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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.