Changeset 1320

Show
Ignore:
Timestamp:
06/04/08 16:01:24 (3 months ago)
Author:
donncha
Message:

WordPress?.com object cache merge

Files:

Legend:

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

    r1311 r1320  
    198198        global $wpdb, $switched, $current_blog; 
    199199 
     200        $wpdb->hide_errors(); 
    200201        // Allow plugins to short-circuit options. 
    201202        $pre = apply_filters( 'pre_option_' . $setting, false ); 
     
    203204                return $pre; 
    204205 
    205         global $blog_id; 
    206         if( $wpdb->blogid != $blog_id && function_exists('error_log') ) { 
    207                 $msg = "{$_SERVER[ 'HTTP_HOST' ]}{$_SERVER[ 'REQUEST_URI' ]} blog_id changed without calling switch_to_blog(). Current value: $blog_id"; 
    208                 if( defined( 'ERRORLOGFILE' ) ) { 
    209                         error_log( $msg, 3, CONSTANT( 'ERRORLOGFILE' ) ); 
    210                 } else { 
    211                         error_log( $msg ); 
    212                 } 
    213         } 
    214         if ( $switched != false || defined('WP_INSTALLING') != false ) { 
    215                 //wp_cache_delete($setting, 'options'); 
    216                 //wp_cache_delete('notoptions', 'options'); 
    217                 //wp_cache_delete('alloptions', 'options'); 
    218         } 
    219  
    220         // prevent non-existent options from triggering multiple queries 
    221         $notoptions = wp_cache_get( 'notoptions', 'options' ); 
    222         if ( isset( $notoptions[$setting] ) ) 
    223                 return false; 
    224  
    225         $alloptions = wp_load_alloptions(); 
    226  
    227         if ( isset( $alloptions[$setting] ) ) { 
    228                 $value = $alloptions[$setting]; 
    229         } else { 
    230                 $value = wp_cache_get( $setting, 'options' ); 
    231  
    232                 if ( false === $value ) { 
    233                         if ( defined( 'WP_INSTALLING' ) ) 
    234                                 $supress = $wpdb->suppress_errors(); 
    235                         // expected_slashed ($setting) 
    236                         $row = $wpdb->get_row( "SELECT option_value FROM $wpdb->options WHERE option_name = '$setting' LIMIT 1" ); 
    237                         if ( defined( 'WP_INSTALLING' ) ) 
    238                                 $wpdb->suppress_errors($suppress); 
    239  
    240                         if ( is_object( $row) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values 
    241                                 $value = $row->option_value; 
    242                                 wp_cache_add( $setting, $value, 'options' ); 
    243                         } else { // option does not exist, so we must cache its non-existence 
    244                                 $notoptions[$setting] = true; 
    245                                 wp_cache_set( 'notoptions', $notoptions, 'options' ); 
    246                                 return false; 
    247                         } 
    248                 } 
    249         } 
     206        $value = _get_option_cache( $setting ); 
     207        if ( false === $value ) 
     208                        return false; 
    250209 
    251210        // If home is not set use siteurl. 
     
    255214        if ( in_array( $setting, array('siteurl', 'home', 'category_base', 'tag_base') ) ) 
    256215                $value = untrailingslashit( $value ); 
     216 
     217        if (! unserialize($value) ) 
     218                $value = stripslashes( $value ); 
    257219 
    258220        return apply_filters( 'option_' . $setting, maybe_unserialize( $value ) ); 
     
    291253function wp_load_alloptions() { 
    292254        global $wpdb; 
    293  
    294         $alloptions = wp_cache_get( 'alloptions', 'options' ); 
    295  
    296         if ( !$alloptions ) { 
    297                 $suppress = $wpdb->suppress_errors(); 
    298                 if ( !$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) ) 
    299                         $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" ); 
    300                 $wpdb->suppress_errors($suppress); 
    301                 $alloptions = array(); 
    302                 foreach ( (array) $alloptions_db as $o ) 
    303                         $alloptions[$o->option_name] = $o->option_value; 
    304                 wp_cache_add( 'alloptions', $alloptions, 'options' ); 
    305         } 
    306         return $alloptions; 
    307 
    308  
     255        global $_wp_alloptions; 
     256        global $blog_id; 
     257 
     258        if ( !empty($_wp_alloptions[$blog_id]) ) 
     259                return $_wp_alloptions[$blog_id]; 
     260 
     261        $alloptions = wp_cache_get('alloptions', 'options'); 
     262 
     263        if ( false !== $alloptions ) { 
     264                $_wp_alloptions[$blog_id] = $alloptions; 
     265                return $alloptions; 
     266        } 
     267 
     268        $_wp_alloptions[$blog_id] = array(); 
     269 
     270        $suppress = $wpdb->suppress_errors(); 
     271        if ( !$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) ) 
     272                $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" ); 
     273        $wpdb->suppress_errors($suppress); 
     274        foreach ( (array) $alloptions_db as $o ) 
     275                $_wp_alloptions[$blog_id][$o->option_name] = $o->option_value; 
     276 
     277        wp_cache_set('alloptions', $_wp_alloptions[$blog_id], 'options'); 
     278 
     279        return $_wp_alloptions[$blog_id]; 
     280
     281 
     282function _get_option_cache( $setting ) { 
     283        global $_wp_alloptions; 
     284        global $blog_id; 
     285 
     286        wp_load_alloptions(); 
     287 
     288        if ( isset($_wp_alloptions[$blog_id][$setting]) ) 
     289                return $_wp_alloptions[$blog_id][$setting]; 
     290 
     291        return false; 
     292
     293 
     294function _set_option_cache( $setting, $value ) { 
     295        global $_wp_alloptions; 
     296        global $blog_id; 
     297 
     298        wp_load_alloptions(); 
     299 
     300        $_wp_alloptions[$blog_id][$setting] = $value; 
     301 
     302        wp_cache_delete('alloptions', 'options'); 
     303
     304 
     305function _delete_option_cache( $setting ) { 
     306        global $_wp_alloptions; 
     307        global $blog_id; 
     308 
     309        wp_load_alloptions(); 
     310 
     311        if ( isset($_wp_alloptions[$blog_id][$setting]) ) 
     312                unset($_wp_alloptions[$blog_id][$setting]); 
     313 
     314        wp_cache_delete('alloptions', 'options'); 
     315
    309316 
    310317// expects $option_name to NOT be SQL-escaped 
     
    327334        } 
    328335 
    329         $notoptions = wp_cache_get( 'notoptions', 'options' ); 
    330         if ( is_array( $notoptions ) && isset( $notoptions[$option_name] ) ) { 
    331                 unset( $notoptions[$option_name] ); 
    332                 wp_cache_set( 'notoptions', $notoptions, 'options' ); 
    333         } 
    334  
    335336        $_newvalue = $newvalue; 
    336337        $newvalue = maybe_serialize( $newvalue ); 
    337338 
    338         $alloptions = wp_load_alloptions(); 
    339         if ( isset( $alloptions[$option_name] ) ) { 
    340                 $alloptions[$option_name] = $newvalue; 
    341                 wp_cache_set( 'alloptions', $alloptions, 'options' ); 
    342         } else { 
    343                 wp_cache_set( $option_name, $newvalue, 'options' ); 
    344         } 
     339        _set_option_cache($option_name, $newvalue); 
    345340 
    346341        $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->options SET option_value = %s WHERE option_name = %s", $newvalue, $option_name ) ); 
     
    362357        $value = sanitize_option( $name, $value ); 
    363358 
    364         // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query 
    365         $notoptions = wp_cache_get( 'notoptions', 'options' ); 
    366         if ( !is_array( $notoptions ) || !isset( $notoptions[$name] ) ) 
    367                 if ( false !== get_option( $safe_name ) ) 
    368                         return; 
     359        if ( false !== get_option( $safe_name ) ) 
     360                return; 
    369361 
    370362        $value = maybe_serialize( $value ); 
    371363        $autoload = ( 'no' === $autoload ) ? 'no' : 'yes'; 
    372364 
    373         if ( 'yes' == $autoload ) { 
    374                 $alloptions = wp_load_alloptions(); 
    375                 $alloptions[$name] = $value; 
    376                 wp_cache_set( 'alloptions', $alloptions, 'options' ); 
    377         } else { 
    378                 wp_cache_set( $name, $value, 'options' ); 
    379         } 
    380  
    381         // This option exists now 
    382         $notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh 
    383         if ( is_array( $notoptions ) && isset( $notoptions[$name] ) ) { 
    384                 unset( $notoptions[$name] ); 
    385                 wp_cache_set( 'notoptions', $notoptions, 'options' ); 
    386         } 
     365        _set_option_cache( $name, $value ); 
    387366 
    388367        $wpdb->query( $wpdb->prepare( "INSERT INTO $wpdb->options (option_name, option_value, autoload) VALUES (%s, %s, %s)", $name, $value, $autoload ) ); 
     
    397376 
    398377        wp_protect_special_option( $name ); 
     378 
     379        _delete_option_cache( $name ); 
    399380 
    400381        // Get the ID, if no ID then return 
     
    403384        if ( is_null($option) || !$option->option_id ) 
    404385                return false; 
     386 
    405387        // expected_slashed ($name) 
    406388        $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name = '$name'" ); 
    407         if ( 'yes' == $option->autoload ) { 
    408                 $alloptions = wp_load_alloptions(); 
    409                 if ( isset( $alloptions[$name] ) ) { 
    410                         unset( $alloptions[$name] ); 
    411                         wp_cache_set( 'alloptions', $alloptions, 'options' ); 
    412                 } 
    413         } else { 
    414                 wp_cache_delete( $name, 'options' ); 
    415         } 
     389 
    416390        return true; 
    417391} 
     
    955929function do_robots() { 
    956930        global $current_blog; 
    957         header( 'Content-Type: text/plain; charset=utf-8' ); 
    958  
    959         do_action('do_robotstxt'); 
     931 
     932        header('Content-Type: text/plain; charset=utf-8'); 
     933 
     934        do_action( 'do_robotstxt' ); 
    960935 
    961936        if ( '0' == $current_blog->public ) { 
     
    11921167                return $upload; 
    11931168        } 
     1169 
    11941170        $filename = wp_unique_filename( $upload['path'], $name ); 
    11951171 
     
    14561432} 
    14571433 
    1458  
    14591434function _mce_set_direction( $input ) { 
    14601435        global $wp_locale; 
     
    14681443        return $input; 
    14691444} 
    1470  
    14711445 
    14721446function smilies_init() {