Ticket #26394: 26394.4.diff
File 26394.4.diff, 3.7 KB (added by , 10 years ago) |
---|
-
src/wp-includes/option.php
221 221 * to set whether an option is autoloaded, then you need to use the add_option(). 222 222 * 223 223 * @since 1.0.0 224 * @since 4.2.0 The `$autoload` parameter was added. 224 225 * 225 226 * @param string $option Option name. Expected to not be SQL-escaped. 226 227 * @param mixed $value Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped. 228 * @param string Optional. $autoload Autoload by default, while allowing it to be changed. 227 229 * @return bool False if value was not updated and true if value was updated. 228 230 */ 229 function update_option( $option, $value ) {231 function update_option( $option, $value, $autoload = 'yes' ) { 230 232 global $wpdb; 231 233 232 $option = trim( $option);233 if ( empty( $option) )234 $option = trim( $option ); 235 if ( empty( $option ) ) { 234 236 return false; 237 } 235 238 236 239 wp_protect_special_option( $option ); 237 240 238 if ( is_object( $value ) ) 241 if ( is_object( $value ) ) { 239 242 $value = clone $value; 243 } 240 244 241 245 $value = sanitize_option( $option, $value ); 242 246 $old_value = get_option( $option ); … … 265 269 $value = apply_filters( 'pre_update_option', $value, $option, $old_value ); 266 270 267 271 // If the new and old values are the same, no need to update. 268 if ( $value === $old_value ) 272 if ( $value === $old_value ) { 269 273 return false; 274 } 270 275 271 276 /** This filter is documented in wp-includes/option.php */ 272 if ( apply_filters( 'default_option_' . $option, false ) === $old_value ) 273 return add_option( $option, $value ); 277 if ( apply_filters( 'default_option_' . $option, false ) === $old_value ) { 278 return add_option( $option, $value, '', $autoload ); 279 } 274 280 275 281 $serialized_value = maybe_serialize( $value ); 276 282 … … 286 292 do_action( 'update_option', $option, $old_value, $value ); 287 293 288 294 $result = $wpdb->update( $wpdb->options, array( 'option_value' => $serialized_value ), array( 'option_name' => $option ) ); 289 if ( ! $result ) 295 if ( ! $result ) { 290 296 return false; 297 } 291 298 292 299 $notoptions = wp_cache_get( 'notoptions', 'options' ); 293 300 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) { -
tests/phpunit/tests/option/option.php
149 149 $actual = $wpdb->get_row( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s LIMIT 1", $name ) ); 150 150 $this->assertEquals( $expected, $actual->autoload ); 151 151 } 152 153 function test_option_update_default() { 154 global $wpdb; 155 wp_cache_flush(); 156 update_option( 'test_update_option_default', 'value' ); 157 wp_cache_flush(); 158 $before = $wpdb->num_queries; 159 $value = get_option( 'test_update_option_default' ); 160 $after = $wpdb->num_queries; 161 $this->assertEquals( ++$before, $after ); 162 $this->assertEquals( $value, 'value' ); 163 } 164 165 function test_option_update_w_autoload() { 166 global $wpdb; 167 wp_cache_flush(); 168 update_option( 'test_update_option_default', 'value', 'yes' ); 169 wp_cache_flush(); 170 $before = $wpdb->num_queries; 171 $value = get_option( 'test_update_option_default' ); 172 $after = $wpdb->num_queries; 173 $this->assertEquals( ++$before, $after ); 174 $this->assertEquals( $value, 'value' ); 175 } 176 177 function test_option_update_wo_autoload() { 178 global $wpdb; 179 wp_cache_flush(); 180 update_option( 'test_update_option_default', 'value', 'no' ); 181 wp_cache_flush(); 182 $before = $wpdb->num_queries; 183 $value = get_option( 'test_update_option_default' ); 184 $after = $wpdb->num_queries; 185 $this->assertEquals( 2 + $before, $after ); 186 $this->assertEquals( $value, 'value' ); 187 } 152 188 }