Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
06/24/2011 06:57:51 PM (14 years ago)
Author:
boonebgorges
Message:

Introduces bp_x_option() wrapper functions, along with bp_get_option_blog_id() to allow for fully filterable option storage. Refactors internal option storage to use new API functions. Fixes bp-pages for BP_ENABLE_MULTIBLOG. Fixes #3261

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core/bp-core-functions.php

    r4558 r4559  
    11<?php
     2
     3/**
     4 * Retrieve an option
     5 *
     6 * This is a wrapper for get_blog_option(), which in turn stores settings data (such as bp-pages)
     7 * on the appropriate blog, given your current setup.
     8 *
     9 * The 'bp_get_option' filter is primarily for backward-compatibility.
     10 *
     11 * @package BuddyPress
     12 * @since 1.3
     13 *
     14 * @uses bp_get_option_blog_id()
     15 * @param str $option_name The option to be retrieved
     16 * @param str $default Optional. Default value to be returned if the option isn't set
     17 * @return mixed The value for the option
     18 */
     19function bp_get_option( $option_name, $default = false ) {
     20    $value = get_blog_option( bp_get_option_blog_id( $option_name ), $option_name, $default );
     21   
     22    return apply_filters( 'bp_get_option', $value );
     23}
     24
     25/**
     26 * Save an option
     27 *
     28 * This is a wrapper for update_blog_option(), which in turn stores settings data (such as bp-pages)
     29 * on the appropriate blog, given your current setup.
     30 *
     31 * @package BuddyPress
     32 * @since 1.3
     33 *
     34 * @uses bp_get_option_blog_id()
     35 * @param str $option_name The option key to be set
     36 * @param str $value The value to be set
     37 */
     38function bp_update_option( $option_name, $value ) {
     39    // update_blog_option() does not return anything on success/failure, so neither can we
     40    update_blog_option( bp_get_option_blog_id( $option_name ), $option_name, $value );
     41}
     42
     43/**
     44 * Delete an option
     45 *
     46 * This is a wrapper for delete_blog_option(), which in turn deletes settings data (such as
     47 * bp-pages) on the appropriate blog, given your current setup.
     48 *
     49 * @package BuddyPress
     50 * @since 1.3
     51 *
     52 * @uses bp_get_option_blog_id()
     53 * @param str $option_name The option key to be set
     54 */
     55function bp_delete_option( $option_name ) {
     56    // update_blog_option() does not return anything on success/failure, so neither can we
     57    delete_blog_option( bp_get_option_blog_id( $option_name ), $option_name );
     58}
     59
     60/**
     61 * Retrieve the filterable blog_id of the blog where the option is question is saved
     62 *
     63 * Since BP 1.3, BuddyPress has stored all of its settings in blog options tables, as opposed to
     64 * sitemeta. This makes it easier for non-standard setups (like BP_ENABLE_MULTIBLOG and
     65 * multinetwork BP) to save and access options in a consistent and logical way.
     66 *
     67 * By default, nearly all settings are stored in the options table of BP_ROOT_BLOG. The one
     68 * exception is when BP_ENABLE_MULTIBLOG is enabled. In this case, bp-pages - the list of pages that
     69 * are associated with BP top-level components - must be specific to each blog in the network. If
     70 * you are building a plugin that requires an option (either a BP-native option, or your own custom
     71 * option) to be specific to each blog in a network, filter 'bp_blog_specific_options' and add your
     72 * option's name. This will allow you to use bp_get_option() and bp_update_option() seamlessly.
     73 *
     74 * @package BuddyPress
     75 * @since 1.3
     76 *
     77 * @see bp_get_option()
     78 * @see bp_update_option()
     79 * @uses apply_filters() Filter bp_get_option_blog_id to change this setting
     80 * @return int $blog_id
     81 */
     82function bp_get_option_blog_id( $option_name ) {
     83    $blog_specific_options = apply_filters( 'bp_blog_specific_options', array(
     84        'bp-pages'
     85    ) );
     86   
     87    if ( in_array( $option_name, $blog_specific_options ) ) {
     88        if ( defined( 'BP_ENABLE_MULTIBLOG' ) && BP_ENABLE_MULTIBLOG ) {
     89            $blog_id = get_current_blog_id();
     90        } else {
     91            $blog_id = BP_ROOT_BLOG;
     92        }
     93    } else {
     94        $blog_id = BP_ROOT_BLOG;
     95    }
     96
     97    return apply_filters( 'bp_get_option_blog_id', $blog_id );
     98}
    299
    3100/**
     
    23120 */
    24121function bp_core_get_page_meta() {
    25     $page_ids = get_site_option( 'bp-pages' );
    26 
    27     $is_enable_multiblog = is_multisite() && defined( 'BP_ENABLE_MULTIBLOG' ) && BP_ENABLE_MULTIBLOG ? true : false;
    28 
    29     $page_blog_id = $is_enable_multiblog ? get_current_blog_id() : BP_ROOT_BLOG;
    30 
    31     // Upgrading from an earlier version of BP pre-1.3
    32     if ( empty( $page_ids ) || isset( $page_ids['members'] ) ) {
    33         if ( empty( $page_ids ) ) {
    34             // We're probably coming from an old multisite install
    35             $old_page_ids = get_blog_option( $page_blog_id, 'bp-pages' );
    36         } else {
    37             // We're probably coming from an old single-WP install
    38             $old_page_ids = $page_ids;
    39         }
    40 
    41         /**
    42          * If $page_ids is found in a blog_option, and it's formatted in the new way (keyed
    43          * by blog_id), it means that this is an MS upgrade. Return false and let the
    44          * upgrade wizard handle the migration.
    45          */
    46         if ( !isset( $old_page_ids['members'] ) )
    47             return false;
    48 
    49         // Finally, move the page ids over to site options
    50         $new_page_ids = array(
    51             $page_blog_id => $old_page_ids
    52         );
    53 
    54         update_site_option( 'bp-pages', $new_page_ids );
    55     }
    56 
    57     $blog_page_ids = !empty( $page_ids[$page_blog_id] ) ? $page_ids[$page_blog_id] : false;
    58 
    59     return apply_filters( 'bp_core_get_page_meta', $blog_page_ids );
     122    $page_ids = bp_get_option( 'bp-pages' );
     123 
     124    // Upgrading from an earlier version of BP pre-1.3
     125    if ( !isset( $page_ids['members'] ) && $ms_page_ids = get_site_option( 'bp-pages' ) ) {
     126        $is_enable_multiblog = is_multisite() && defined( 'BP_ENABLE_MULTIBLOG' ) && BP_ENABLE_MULTIBLOG ? true : false;
     127 
     128        $page_blog_id = $is_enable_multiblog ? get_current_blog_id() : BP_ROOT_BLOG;
     129
     130        if ( isset( $ms_page_ids[$page_blog_id] ) ) {
     131            $page_ids = $ms_page_ids[$page_blog_id];
     132
     133            bp_update_option( 'bp-pages', $page_ids );
     134        }
     135    }
     136   
     137    return apply_filters( 'bp_core_get_page_meta', $page_ids );
    60138}
    61139
     
    72150 */
    73151function bp_core_update_page_meta( $blog_page_ids ) {
    74     if ( !$page_ids = get_site_option( 'bp-pages' ) )
    75         $page_ids = array();
    76 
    77     // Generally, we key by the BP_ROOT_BLOG. Exception: when BP_ENABLE_MULTIBLOG is turned on
    78     $key = is_multisite() && defined( 'BP_ENABLE_MULTIBLOG' ) && BP_ENABLE_MULTIBLOG ? get_current_blog_id() : BP_ROOT_BLOG;
    79 
    80     $page_ids[$key] = $blog_page_ids;
    81 
    82     update_site_option( 'bp-pages', $page_ids );
     152    bp_update_option( 'bp-pages', $blog_page_ids );
    83153}
    84154
     
    875945 *
    876946 * @package BuddyPress Core
     947 * @todo Does this need to be here anymore after the introduction of bp_get_option etc?
    877948 */
    878949function bp_core_activate_site_options( $keys = array() ) {
     
    884955        foreach ( $keys as $key => $default ) {
    885956            if ( empty( $bp->site_options[ $key ] ) ) {
    886                 $bp->site_options[ $key ] = get_blog_option( BP_ROOT_BLOG, $key, $default );
    887 
    888                 if ( !update_site_option( $key, $bp->site_options[ $key ] ) )
     957                $bp->site_options[ $key ] = bp_get_option( $key, $default );
     958
     959                if ( !bp_update_option( $key, $bp->site_options[ $key ] ) )
    889960                    $errors = true;
    890961            }
     
    908979    global $bp, $wpdb;
    909980
    910     // These options come from the options table in WP single, and sitemeta in MS
     981    // These options come from the options table
    911982    $site_options = apply_filters( 'bp_core_site_options', array(
    912983        'bp-deactivated-components',
     
    9361007    $meta_keys = "'" . implode( "','", (array)$site_options ) ."'";
    9371008
    938     if ( is_multisite() )
    939         $site_meta = $wpdb->get_results( "SELECT meta_key AS name, meta_value AS value FROM {$wpdb->sitemeta} WHERE meta_key IN ({$meta_keys}) AND site_id = {$wpdb->siteid}" );
    940     else
    941         $site_meta = $wpdb->get_results( "SELECT option_name AS name, option_value AS value FROM {$wpdb->options} WHERE option_name IN ({$meta_keys})" );
     1009    $site_meta = $wpdb->get_results( "SELECT option_name AS name, option_value AS value FROM {$wpdb->options} WHERE option_name IN ({$meta_keys})" );
     1010   
     1011    // Backward compatibility - moves sitemeta to the blog
     1012    if ( empty( $site_meta ) || ( count( $site_meta ) < count( $site_options ) ) ) {
     1013        if ( is_multisite() ) {
     1014            $ms_site_meta = $wpdb->get_results( "SELECT meta_key AS name, meta_value AS value FROM {$wpdb->sitemeta} WHERE meta_key IN ({$meta_keys}) AND site_id = {$wpdb->siteid}" );
     1015        } else {
     1016            $ms_site_meta = $wpdb->get_results( "SELECT option_name AS name, option_value AS value FROM {$wpdb->options} WHERE option_name IN ({$meta_keys})" );
     1017        }
     1018       
     1019        $settings_to_move = array(
     1020            'bp-deactivated-components',
     1021            'bp-blogs-first-install',
     1022            'bp-disable-blog-forum-comments',
     1023            'bp-xprofile-base-group-name',
     1024            'bp-xprofile-fullname-field-name',
     1025            'bp-disable-profile-sync',
     1026            'bp-disable-avatar-uploads',
     1027            'bp-disable-account-deletion',
     1028            'bp-disable-forum-directory',
     1029            'bp-disable-blogforum-comments',
     1030            'bb-config-location',
     1031            'hide-loggedout-adminbar',
     1032        );
     1033       
     1034        foreach( (array)$ms_site_meta as $meta ) { 
     1035            if ( isset( $meta->name ) && in_array( $meta->name, $settings_to_move ) ) {
     1036                bp_update_option( $meta->name, $meta->value );
     1037                       
     1038                if ( empty( $site_meta[$meta->name] ) ) {
     1039                    $site_meta[$meta->name] = $meta->value;
     1040                }
     1041            }
     1042        }
     1043    }
    9421044
    9431045    $root_blog_meta_keys  = "'" . implode( "','", (array)$root_blog_options ) ."'";
     
    9491051        if ( !empty( $meta ) ) {
    9501052            foreach( (array)$meta as $meta_item ) {
    951                 $site_options[$meta_item->name] = $meta_item->value;
     1053                if ( isset( $meta_item->name ) )
     1054                    $site_options[$meta_item->name] = $meta_item->value;
    9521055            }
    9531056        }
Note: See TracChangeset for help on using the changeset viewer.