Example #1
0
/**
 * Add filters to each BuddyPress option and allow them to be overloaded from
 * inside the $bbp->options array.
 *
 * @since BuddyPress (1.6)
 *
 * @uses bp_get_default_options() To get default options
 * @uses add_filter() To add filters to 'pre_option_{$key}'
 * @uses do_action() Calls 'bp_add_option_filters'
 */
function bp_setup_option_filters()
{
    // Get the default options and values
    $options = bp_get_default_options();
    // Add filters to each BuddyPress option
    foreach ($options as $key => $value) {
        add_filter('pre_option_' . $key, 'bp_pre_get_option');
    }
    // Allow previously activated plugins to append their own options.
    do_action('bp_setup_option_filters');
}
Example #2
0
/**
 * Fetch global BP options.
 *
 * BuddyPress uses common options to store configuration settings. Many of these
 * settings are needed at run time. Instead of fetching them all and adding many
 * initial queries to each page load, let's fetch them all in one go.
 *
 * @todo Use settings API and audit these methods.
 *
 * @return array $root_blog_options_meta List of options.
 */
function bp_core_get_root_options()
{
    global $wpdb;
    // Get all the BuddyPress settings, and a few useful WP ones too
    $root_blog_options = bp_get_default_options();
    $root_blog_options['registration'] = '0';
    $root_blog_options['avatar_default'] = 'mysteryman';
    $root_blog_option_keys = array_keys($root_blog_options);
    // Do some magic to get all the root blog options in 1 swoop
    // Check cache first - We cache here instead of using the standard WP
    // settings cache because the current blog may not be the root blog,
    // and it's not practical to access the cache across blogs
    $root_blog_options_meta = wp_cache_get('root_blog_options', 'bp');
    if (false === $root_blog_options_meta) {
        $blog_options_keys = "'" . join("', '", (array) $root_blog_option_keys) . "'";
        $blog_options_table = bp_is_multiblog_mode() ? $wpdb->options : $wpdb->get_blog_prefix(bp_get_root_blog_id()) . 'options';
        $blog_options_query = "SELECT option_name AS name, option_value AS value FROM {$blog_options_table} WHERE option_name IN ( {$blog_options_keys} )";
        $root_blog_options_meta = $wpdb->get_results($blog_options_query);
        // On Multisite installations, some options must always be fetched from sitemeta
        if (is_multisite()) {
            /**
             * Filters multisite options retrieved from sitemeta.
             *
             * @since BuddyPress (1.5.0)
             *
             * @param array $value Array of multisite options from sitemeta table.
             */
            $network_options = apply_filters('bp_core_network_options', array('tags_blog_id' => '0', 'sitewide_tags_blog' => '', 'registration' => '0', 'fileupload_maxk' => '1500'));
            $current_site = get_current_site();
            $network_option_keys = array_keys($network_options);
            $sitemeta_options_keys = "'" . join("', '", (array) $network_option_keys) . "'";
            $sitemeta_options_query = $wpdb->prepare("SELECT meta_key AS name, meta_value AS value FROM {$wpdb->sitemeta} WHERE meta_key IN ( {$sitemeta_options_keys} ) AND site_id = %d", $current_site->id);
            $network_options_meta = $wpdb->get_results($sitemeta_options_query);
            // Sitemeta comes second in the merge, so that network 'registration' value wins
            $root_blog_options_meta = array_merge($root_blog_options_meta, $network_options_meta);
        }
        // Missing some options, so do some one-time fixing
        if (empty($root_blog_options_meta) || count($root_blog_options_meta) < count($root_blog_option_keys)) {
            // Get a list of the keys that are already populated
            $existing_options = array();
            foreach ($root_blog_options_meta as $already_option) {
                $existing_options[$already_option->name] = $already_option->value;
            }
            // Unset the query - We'll be resetting it soon
            unset($root_blog_options_meta);
            // Loop through options
            foreach ($root_blog_options as $old_meta_key => $old_meta_default) {
                if (isset($existing_options[$old_meta_key])) {
                    continue;
                }
                // Get old site option
                if (is_multisite()) {
                    $old_meta_value = get_site_option($old_meta_key);
                }
                // No site option so look in root blog
                if (empty($old_meta_value)) {
                    $old_meta_value = bp_get_option($old_meta_key, $old_meta_default);
                }
                // Update the root blog option
                bp_update_option($old_meta_key, $old_meta_value);
                // Update the global array
                $root_blog_options_meta[$old_meta_key] = $old_meta_value;
                // Clear out the value for the next time around
                unset($old_meta_value);
            }
            $root_blog_options_meta = array_merge($root_blog_options_meta, $existing_options);
            unset($existing_options);
            // We're all matched up
        } else {
            // Loop through our results and make them usable
            foreach ($root_blog_options_meta as $root_blog_option) {
                $root_blog_options[$root_blog_option->name] = $root_blog_option->value;
            }
            // Copy the options no the return val
            $root_blog_options_meta = $root_blog_options;
            // Clean up our temporary copy
            unset($root_blog_options);
        }
        wp_cache_set('root_blog_options', $root_blog_options_meta, 'bp');
    }
    /**
     * Filters the global BP options.
     *
     * @since BuddyPress (1.5.0)
     *
     * @param array $root_blog_options_meta Array of global BP options.
     */
    return apply_filters('bp_core_get_root_options', $root_blog_options_meta);
}
Example #3
0
/**
 * Clear the root_blog_options cache when any of its options are updated.
 *
 * @since BuddyPress (2.0.0)
 *
 * @param string $option Option name.
 */
function bp_core_clear_root_options_cache($option)
{
    $keys = array_keys(bp_get_default_options());
    $keys = array_merge($keys, array('registration', 'avatar_default', 'tags_blog_id', 'sitewide_tags_blog', 'registration', 'fileupload_mask'));
    if (in_array($option, $keys)) {
        wp_cache_delete('root_blog_options', 'bp');
    }
}
Example #4
0
 /**
  * @group bp_core_get_root_options
  */
 public function test_bp_core_get_root_options_cache_invalidate()
 {
     $keys = array_keys(bp_get_default_options());
     $keys[] = 'registration';
     $keys[] = 'avatar_default';
     foreach ($keys as $key) {
         // prime cache
         $root_options = bp_core_get_root_options();
         bp_update_option($key, 'foo');
         $this->assertFalse(wp_cache_get('root_blog_options', 'bp'), 'Cache not invalidated after updating "' . $key . '"');
     }
     if (is_multisite()) {
         $ms_keys = array('tags_blog_id', 'sitewide_tags_blog', 'registration', 'fileupload_mask');
         foreach ($ms_keys as $ms_key) {
             $root_options = bp_core_get_root_options();
             update_site_option($ms_key, 'foooooooo');
             $this->assertFalse(wp_cache_get('root_blog_options', 'bp'), 'Cache not invalidated after updating "' . $ms_key . '"');
         }
     }
 }
/**
 * Fetch global BP options.
 *
 * BuddyPress uses common options to store configuration settings. Many of these
 * settings are needed at run time. Instead of fetching them all and adding many
 * initial queries to each page load, let's fetch them all in one go.
 *
 * @since 1.5.0
 *
 * @todo Use settings API and audit these methods.
 *
 * @return array $root_blog_options_meta List of options.
 */
function bp_core_get_root_options()
{
    global $wpdb;
    // Get all the BuddyPress settings, and a few useful WP ones too.
    $root_blog_options = bp_get_default_options();
    $root_blog_options['registration'] = '0';
    $root_blog_options['avatar_default'] = 'mysteryman';
    $root_blog_option_keys = array_keys($root_blog_options);
    // Do some magic to get all the root blog options in 1 swoop
    // Check cache first - We cache here instead of using the standard WP
    // settings cache because the current blog may not be the root blog,
    // and it's not practical to access the cache across blogs.
    $root_blog_options_meta = wp_cache_get('root_blog_options', 'bp');
    if (false === $root_blog_options_meta) {
        $blog_options_keys = "'" . join("', '", (array) $root_blog_option_keys) . "'";
        $blog_options_table = bp_is_multiblog_mode() ? $wpdb->options : $wpdb->get_blog_prefix(bp_get_root_blog_id()) . 'options';
        $blog_options_query = "SELECT option_name AS name, option_value AS value FROM {$blog_options_table} WHERE option_name IN ( {$blog_options_keys} )";
        $root_blog_options_meta = $wpdb->get_results($blog_options_query);
        // On Multisite installations, some options must always be fetched from sitemeta.
        if (is_multisite()) {
            /**
             * Filters multisite options retrieved from sitemeta.
             *
             * @since 1.5.0
             *
             * @param array $value Array of multisite options from sitemeta table.
             */
            $network_options = apply_filters('bp_core_network_options', array('tags_blog_id' => '0', 'sitewide_tags_blog' => '', 'registration' => '0', 'fileupload_maxk' => '1500'));
            $current_site = get_current_site();
            $network_option_keys = array_keys($network_options);
            $sitemeta_options_keys = "'" . join("', '", (array) $network_option_keys) . "'";
            $sitemeta_options_query = $wpdb->prepare("SELECT meta_key AS name, meta_value AS value FROM {$wpdb->sitemeta} WHERE meta_key IN ( {$sitemeta_options_keys} ) AND site_id = %d", $current_site->id);
            $network_options_meta = $wpdb->get_results($sitemeta_options_query);
            // Sitemeta comes second in the merge, so that network 'registration' value wins.
            $root_blog_options_meta = array_merge($root_blog_options_meta, $network_options_meta);
        }
        // Loop through our results and make them usable.
        foreach ($root_blog_options_meta as $root_blog_option) {
            $root_blog_options[$root_blog_option->name] = $root_blog_option->value;
        }
        // Copy the options no the return val.
        $root_blog_options_meta = $root_blog_options;
        // Clean up our temporary copy.
        unset($root_blog_options);
        wp_cache_set('root_blog_options', $root_blog_options_meta, 'bp');
    }
    /**
     * Filters the global BP options.
     *
     * @since 1.5.0
     *
     * @param array $root_blog_options_meta Array of global BP options.
     */
    return apply_filters('bp_core_get_root_options', $root_blog_options_meta);
}
/**
 * Clear the root_blog_options cache when any of its options are updated.
 *
 * @since 2.0.0
 *
 * @param string $option Option name.
 */
function bp_core_clear_root_options_cache($option)
{
    foreach (array('add_option', 'add_site_option', 'update_option', 'update_site_option') as $action) {
        remove_action($action, 'bp_core_clear_root_options_cache');
    }
    // Surrounding code prevents infinite loops on WP < 4.4.
    $keys = array_keys(bp_get_default_options());
    foreach (array('add_option', 'add_site_option', 'update_option', 'update_site_option') as $action) {
        add_action($action, 'bp_core_clear_root_options_cache');
    }
    $keys = array_merge($keys, array('registration', 'avatar_default', 'tags_blog_id', 'sitewide_tags_blog', 'registration', 'fileupload_mask'));
    if (in_array($option, $keys)) {
        wp_cache_delete('root_blog_options', 'bp');
    }
}