Make WordPress Core

Changeset 12615


Ignore:
Timestamp:
01/06/2010 11:57:35 PM (17 years ago)
Author:
ryan
Message:

Make *_site_option() multisite awate. see #11644

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/functions.php

    r12584 r12615  
    32583258
    32593259function get_site_option( $key, $default = false, $use_cache = true ) {
     3260        global $wpdb;
     3261
    32603262        // Allow plugins to short-circuit site options.
    32613263        $pre = apply_filters( 'pre_site_option_' . $key, false );
    … …  
    32633265                return $pre;
    32643266
    3265         $value = get_option($key, $default);
     3267        if ( !is_multisite() ) {
     3268                $value = get_option($key, $default);
     3269        } else {
     3270                $cache_key = "{$wpdb->siteid}:$key";
     3271
     3272                if ( $use_cache == true && $value = wp_cache_get($cache_key, 'site-options') )
     3273                        return $value;
     3274
     3275                $value = $wpdb->get_var( $wpdb->prepare("SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $key, $wpdb->siteid) );
     3276
     3277                if ( is_null($value) )
     3278                        $value = $default;
     3279
     3280                $value = maybe_unserialize( $value );
     3281
     3282                wp_cache_set( $cache_key, $value, 'site-options' );
     3283        }
    32663284
    32673285        return apply_filters( 'site_option_' . $key, $value );
    … …  
    32703288// expects $key, $value not to be SQL escaped
    32713289function add_site_option( $key, $value ) {
     3290        global $wpdb;
     3291
    32723292        $value = apply_filters( 'pre_add_site_option_' . $key, $value );
    3273         $result =  add_option($key, $value);
     3293
     3294        if ( !is_multisite() ) {
     3295                $result =  add_option($key, $value);
     3296        } else {
     3297                $cache_key = "{$wpdb->siteid}:$key";
     3298
     3299                if ( $wpdb->get_row( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $key, $wpdb->siteid ) ) )
     3300                        return update_site_option( $key, $value );
     3301
     3302                $value = sanitize_option( $key, $value );
     3303                wp_cache_set( $cache_key, $value, 'site-options');
     3304
     3305                $value = maybe_serialize($value);
     3306
     3307                $wpdb->insert( $wpdb->sitemeta, array('site_id' => $wpdb->siteid, 'meta_key' => $key, 'meta_value' => $value) );
     3308        }
     3309
    32743310        do_action( "add_site_option_{$key}", $key, $value );
     3311        do_action( "add_site_option", $key, $value );
     3312
    32753313        return $result;
    32763314}
    32773315
    32783316function delete_site_option( $key ) {
    3279         $result = delete_option($key);
     3317        global $wpdb;
     3318
     3319        //wpmu_protect_special_option( $key ); @todo
     3320
     3321        do_action( 'pre_delete_site_option_' . $key );
     3322
     3323        if ( !is_multisite() ) {
     3324                $result = delete_option($key);
     3325        } else {
     3326                $option = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $key, $wpdb->siteid ) );
     3327                if ( is_null( $option ) || !$option->meta_id )
     3328                        return false;
     3329                $cache_key = "{$wpdb->siteid}:$key";
     3330                wp_cache_delete( $cache_key, 'site-options' );
     3331
     3332                $result = $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $key, $wpdb->siteid ) );
     3333        }
     3334
    32803335        do_action( "delete_site_option_{$key}", $key );
     3336        do_action( "delete_site_option", $key );
    32813337        return $result;
    32823338}
    … …  
    32843340// expects $key, $value not to be SQL escaped
    32853341function update_site_option( $key, $value ) {
     3342        global $wpdb;
     3343
    32863344        $oldvalue = get_site_option( $key );
    32873345        $value = apply_filters( 'pre_update_site_option_' . $key, $value, $oldvalue );
    3288         $result = update_option($key, $value);
     3346
     3347        if ( $value == $oldvalue )
     3348                return false;
     3349
     3350        if ( !is_multisite() ) {
     3351                $result = update_option($key, $value);
     3352        } else {
     3353                $cache_key = "{$wpdb->siteid}:$key";
     3354
     3355                if ( $value && !$wpdb->get_row( $wpdb->prepare("SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $key, $wpdb->siteid) ) )
     3356                        return add_site_option( $key, $value );
     3357                $value = sanitize_option( $key, $value );
     3358                wp_cache_set( $cache_key, $value, 'site-options' );
     3359
     3360                $value = maybe_serialize($value);
     3361                $result = $wpdb->update( $wpdb->sitemeta, array('meta_value' => $value), array('site_id' => $wpdb->siteid, 'meta_key' => $key) );
     3362        }
     3363
    32893364        do_action( "update_site_option_{$key}", $key, $value );
     3365        do_action( "update_site_option", $key, $value );
    32903366        return $result;
    32913367}
Note: See TracChangeset for help on using the changeset viewer.