Make WordPress Core

Ticket #18244: update_option.diff

File update_option.diff, 1.9 KB (added by wpdavis, 14 years ago)
  • wp-includes/functions.php

     
    507507 *
    508508 * @param string $option Option name. Expected to not be SQL-escaped.
    509509 * @param mixed $newvalue Option value. Expected to not be SQL-escaped.
     510 * @param bool $autoload Optional. Default is enabled, but will not overwrite previous value unless forced. Whether to load the option when WordPress starts up.
    510511 * @return bool False if value was not updated and true if value was updated.
    511512 */
    512 function update_option( $option, $newvalue ) {
     513function update_option( $option, $newvalue, $autoload = 'default' ) {
    513514        global $wpdb;
    514515
     516        // If autoload is not set explicitly, do not overwrite previous value
     517        if( $autoload == 'default' ) {
     518                $replace_autoload = false;
     519                $autoload = 'yes';
     520        } else {
     521                $replace_autoload = true;
     522        }
     523        $autoload = ( 'no' === $autoload ) ? 'no' : 'yes';
     524
    515525        $option = trim($option);
    516526        if ( empty($option) )
    517527                return false;
     
    530540                return false;
    531541
    532542        if ( false === $oldvalue )
    533                 return add_option( $option, $newvalue );
     543                return add_option( $option, $newvalue, '', $autoload );
    534544
    535545        $notoptions = wp_cache_get( 'notoptions', 'options' );
    536546        if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
     
    551561                        wp_cache_set( $option, $_newvalue, 'options' );
    552562                }
    553563        }
     564       
     565        $updates = array( 'option_value' => $newvalue );
     566        if( $replace_autoload )
     567                $updates[ 'autoload' ] = $autoload;
     568       
     569        $result = $wpdb->update( $wpdb->options, $updates, array( 'option_name' => $option ) );
    554570
    555         $result = $wpdb->update( $wpdb->options, array( 'option_value' => $newvalue ), array( 'option_name' => $option ) );
    556 
    557571        if ( $result ) {
    558572                do_action( "update_option_{$option}", $oldvalue, $_newvalue );
    559573                do_action( 'updated_option', $option, $oldvalue, $_newvalue );