| [ Index ] |
PHP Cross Reference of WordPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Option API 4 * 5 * @package WordPress 6 */ 7 8 /** 9 * Retrieve option value based on name of option. 10 * 11 * If the option does not exist or does not have a value, then the return value 12 * will be false. This is useful to check whether you need to install an option 13 * and is commonly used during installation of plugin options and to test 14 * whether upgrading is required. 15 * 16 * If the option was serialized then it will be unserialized when it is returned. 17 * 18 * @since 1.5.0 19 * @package WordPress 20 * @subpackage Option 21 * @uses apply_filters() Calls 'pre_option_$option' before checking the option. 22 * Any value other than false will "short-circuit" the retrieval of the option 23 * and return the returned value. You should not try to override special options, 24 * but you will not be prevented from doing so. 25 * @uses apply_filters() Calls 'option_$option', after checking the option, with 26 * the option value. 27 * 28 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped. 29 * @param mixed $default Optional. Default value to return if the option does not exist. 30 * @return mixed Value set for the option. 31 */ 32 function get_option( $option, $default = false ) { 33 global $wpdb; 34 35 // Allow plugins to short-circuit options. 36 $pre = apply_filters( 'pre_option_' . $option, false ); 37 if ( false !== $pre ) 38 return $pre; 39 40 $option = trim($option); 41 if ( empty($option) ) 42 return false; 43 44 if ( defined( 'WP_SETUP_CONFIG' ) ) 45 return false; 46 47 if ( ! defined( 'WP_INSTALLING' ) ) { 48 // prevent non-existent options from triggering multiple queries 49 $notoptions = wp_cache_get( 'notoptions', 'options' ); 50 if ( isset( $notoptions[$option] ) ) 51 return apply_filters( 'default_option_' . $option, $default ); 52 53 $alloptions = wp_load_alloptions(); 54 55 if ( isset( $alloptions[$option] ) ) { 56 $value = $alloptions[$option]; 57 } else { 58 $value = wp_cache_get( $option, 'options' ); 59 60 if ( false === $value ) { 61 $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) ); 62 63 // Has to be get_row instead of get_var because of funkiness with 0, false, null values 64 if ( is_object( $row ) ) { 65 $value = $row->option_value; 66 wp_cache_add( $option, $value, 'options' ); 67 } else { // option does not exist, so we must cache its non-existence 68 $notoptions[$option] = true; 69 wp_cache_set( 'notoptions', $notoptions, 'options' ); 70 return apply_filters( 'default_option_' . $option, $default ); 71 } 72 } 73 } 74 } else { 75 $suppress = $wpdb->suppress_errors(); 76 $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) ); 77 $wpdb->suppress_errors( $suppress ); 78 if ( is_object( $row ) ) 79 $value = $row->option_value; 80 else 81 return apply_filters( 'default_option_' . $option, $default ); 82 } 83 84 // If home is not set use siteurl. 85 if ( 'home' == $option && '' == $value ) 86 return get_option( 'siteurl' ); 87 88 if ( in_array( $option, array('siteurl', 'home', 'category_base', 'tag_base') ) ) 89 $value = untrailingslashit( $value ); 90 91 return apply_filters( 'option_' . $option, maybe_unserialize( $value ) ); 92 } 93 94 /** 95 * Protect WordPress special option from being modified. 96 * 97 * Will die if $option is in protected list. Protected options are 'alloptions' 98 * and 'notoptions' options. 99 * 100 * @since 2.2.0 101 * @package WordPress 102 * @subpackage Option 103 * 104 * @param string $option Option name. 105 */ 106 function wp_protect_special_option( $option ) { 107 $protected = array( 'alloptions', 'notoptions' ); 108 if ( in_array( $option, $protected ) ) 109 wp_die( sprintf( __( '%s is a protected WP option and may not be modified' ), esc_html( $option ) ) ); 110 } 111 112 /** 113 * Print option value after sanitizing for forms. 114 * 115 * @uses attr Sanitizes value. 116 * @since 1.5.0 117 * @package WordPress 118 * @subpackage Option 119 * 120 * @param string $option Option name. 121 */ 122 function form_option( $option ) { 123 echo esc_attr( get_option( $option ) ); 124 } 125 126 /** 127 * Loads and caches all autoloaded options, if available or all options. 128 * 129 * @since 2.2.0 130 * @package WordPress 131 * @subpackage Option 132 * 133 * @return array List of all options. 134 */ 135 function wp_load_alloptions() { 136 global $wpdb; 137 138 if ( !defined( 'WP_INSTALLING' ) || !is_multisite() ) 139 $alloptions = wp_cache_get( 'alloptions', 'options' ); 140 else 141 $alloptions = false; 142 143 if ( !$alloptions ) { 144 $suppress = $wpdb->suppress_errors(); 145 if ( !$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) ) 146 $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" ); 147 $wpdb->suppress_errors($suppress); 148 $alloptions = array(); 149 foreach ( (array) $alloptions_db as $o ) { 150 $alloptions[$o->option_name] = $o->option_value; 151 } 152 if ( !defined( 'WP_INSTALLING' ) || !is_multisite() ) 153 wp_cache_add( 'alloptions', $alloptions, 'options' ); 154 } 155 156 return $alloptions; 157 } 158 159 /** 160 * Loads and caches certain often requested site options if is_multisite() and a persistent cache is not being used. 161 * 162 * @since 3.0.0 163 * @package WordPress 164 * @subpackage Option 165 * 166 * @param int $site_id Optional site ID for which to query the options. Defaults to the current site. 167 */ 168 function wp_load_core_site_options( $site_id = null ) { 169 global $wpdb, $_wp_using_ext_object_cache; 170 171 if ( !is_multisite() || $_wp_using_ext_object_cache || defined( 'WP_INSTALLING' ) ) 172 return; 173 174 if ( empty($site_id) ) 175 $site_id = $wpdb->siteid; 176 177 $core_options = array('site_name', 'siteurl', 'active_sitewide_plugins', '_site_transient_timeout_theme_roots', '_site_transient_theme_roots', 'site_admins', 'can_compress_scripts', 'global_terms_enabled' ); 178 179 $core_options_in = "'" . implode("', '", $core_options) . "'"; 180 $options = $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value FROM $wpdb->sitemeta WHERE meta_key IN ($core_options_in) AND site_id = %d", $site_id) ); 181 182 foreach ( $options as $option ) { 183 $key = $option->meta_key; 184 $cache_key = "{$site_id}:$key"; 185 $option->meta_value = maybe_unserialize( $option->meta_value ); 186 187 wp_cache_set( $cache_key, $option->meta_value, 'site-options' ); 188 } 189 } 190 191 /** 192 * Update the value of an option that was already added. 193 * 194 * You do not need to serialize values. If the value needs to be serialized, then 195 * it will be serialized before it is inserted into the database. Remember, 196 * resources can not be serialized or added as an option. 197 * 198 * If the option does not exist, then the option will be added with the option 199 * value, but you will not be able to set whether it is autoloaded. If you want 200 * to set whether an option is autoloaded, then you need to use the add_option(). 201 * 202 * @since 1.0.0 203 * @package WordPress 204 * @subpackage Option 205 * 206 * @uses apply_filters() Calls 'pre_update_option_$option' hook to allow overwriting the 207 * option value to be stored. 208 * @uses do_action() Calls 'update_option' hook before updating the option. 209 * @uses do_action() Calls 'update_option_$option' and 'updated_option' hooks on success. 210 * 211 * @param string $option Option name. Expected to not be SQL-escaped. 212 * @param mixed $newvalue Option value. Expected to not be SQL-escaped. 213 * @return bool False if value was not updated and true if value was updated. 214 */ 215 function update_option( $option, $newvalue ) { 216 global $wpdb; 217 218 $option = trim($option); 219 if ( empty($option) ) 220 return false; 221 222 wp_protect_special_option( $option ); 223 224 if ( is_object($newvalue) ) 225 $newvalue = clone $newvalue; 226 227 $newvalue = sanitize_option( $option, $newvalue ); 228 $oldvalue = get_option( $option ); 229 $newvalue = apply_filters( 'pre_update_option_' . $option, $newvalue, $oldvalue ); 230 231 // If the new and old values are the same, no need to update. 232 if ( $newvalue === $oldvalue ) 233 return false; 234 235 if ( false === $oldvalue ) 236 return add_option( $option, $newvalue ); 237 238 $notoptions = wp_cache_get( 'notoptions', 'options' ); 239 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) { 240 unset( $notoptions[$option] ); 241 wp_cache_set( 'notoptions', $notoptions, 'options' ); 242 } 243 244 $_newvalue = $newvalue; 245 $newvalue = maybe_serialize( $newvalue ); 246 247 do_action( 'update_option', $option, $oldvalue, $_newvalue ); 248 if ( ! defined( 'WP_INSTALLING' ) ) { 249 $alloptions = wp_load_alloptions(); 250 if ( isset( $alloptions[$option] ) ) { 251 $alloptions[$option] = $_newvalue; 252 wp_cache_set( 'alloptions', $alloptions, 'options' ); 253 } else { 254 wp_cache_set( $option, $_newvalue, 'options' ); 255 } 256 } 257 258 $result = $wpdb->update( $wpdb->options, array( 'option_value' => $newvalue ), array( 'option_name' => $option ) ); 259 260 if ( $result ) { 261 do_action( "update_option_{$option}", $oldvalue, $_newvalue ); 262 do_action( 'updated_option', $option, $oldvalue, $_newvalue ); 263 return true; 264 } 265 return false; 266 } 267 268 /** 269 * Add a new option. 270 * 271 * You do not need to serialize values. If the value needs to be serialized, then 272 * it will be serialized before it is inserted into the database. Remember, 273 * resources can not be serialized or added as an option. 274 * 275 * You can create options without values and then update the values later. 276 * Existing options will not be updated and checks are performed to ensure that you 277 * aren't adding a protected WordPress option. Care should be taken to not name 278 * options the same as the ones which are protected. 279 * 280 * @package WordPress 281 * @subpackage Option 282 * @since 1.0.0 283 * 284 * @uses do_action() Calls 'add_option' hook before adding the option. 285 * @uses do_action() Calls 'add_option_$option' and 'added_option' hooks on success. 286 * 287 * @param string $option Name of option to add. Expected to not be SQL-escaped. 288 * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped. 289 * @param mixed $deprecated Optional. Description. Not used anymore. 290 * @param bool $autoload Optional. Default is enabled. Whether to load the option when WordPress starts up. 291 * @return bool False if option was not added and true if option was added. 292 */ 293 function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) { 294 global $wpdb; 295 296 if ( !empty( $deprecated ) ) 297 _deprecated_argument( __FUNCTION__, '2.3' ); 298 299 $option = trim($option); 300 if ( empty($option) ) 301 return false; 302 303 wp_protect_special_option( $option ); 304 305 if ( is_object($value) ) 306 $value = clone $value; 307 308 $value = sanitize_option( $option, $value ); 309 310 // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query 311 $notoptions = wp_cache_get( 'notoptions', 'options' ); 312 if ( !is_array( $notoptions ) || !isset( $notoptions[$option] ) ) 313 if ( false !== get_option( $option ) ) 314 return false; 315 316 $_value = $value; 317 $value = maybe_serialize( $value ); 318 $autoload = ( 'no' === $autoload ) ? 'no' : 'yes'; 319 do_action( 'add_option', $option, $_value ); 320 if ( ! defined( 'WP_INSTALLING' ) ) { 321 if ( 'yes' == $autoload ) { 322 $alloptions = wp_load_alloptions(); 323 $alloptions[$option] = $value; 324 wp_cache_set( 'alloptions', $alloptions, 'options' ); 325 } else { 326 wp_cache_set( $option, $value, 'options' ); 327 } 328 } 329 330 // This option exists now 331 $notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh 332 if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) { 333 unset( $notoptions[$option] ); 334 wp_cache_set( 'notoptions', $notoptions, 'options' ); 335 } 336 337 $result = $wpdb->query( $wpdb->prepare( "INSERT INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, %s) ON DUPLICATE KEY UPDATE `option_name` = VALUES(`option_name`), `option_value` = VALUES(`option_value`), `autoload` = VALUES(`autoload`)", $option, $value, $autoload ) ); 338 339 if ( $result ) { 340 do_action( "add_option_{$option}", $option, $_value ); 341 do_action( 'added_option', $option, $_value ); 342 return true; 343 } 344 return false; 345 } 346 347 /** 348 * Removes option by name. Prevents removal of protected WordPress options. 349 * 350 * @package WordPress 351 * @subpackage Option 352 * @since 1.2.0 353 * 354 * @uses do_action() Calls 'delete_option' hook before option is deleted. 355 * @uses do_action() Calls 'deleted_option' and 'delete_option_$option' hooks on success. 356 * 357 * @param string $option Name of option to remove. Expected to not be SQL-escaped. 358 * @return bool True, if option is successfully deleted. False on failure. 359 */ 360 function delete_option( $option ) { 361 global $wpdb; 362 363 wp_protect_special_option( $option ); 364 365 // Get the ID, if no ID then return 366 $row = $wpdb->get_row( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) ); 367 if ( is_null( $row ) ) 368 return false; 369 do_action( 'delete_option', $option ); 370 $result = $wpdb->delete( $wpdb->options, array( 'option_name' => $option ) ); 371 if ( ! defined( 'WP_INSTALLING' ) ) { 372 if ( 'yes' == $row->autoload ) { 373 $alloptions = wp_load_alloptions(); 374 if ( is_array( $alloptions ) && isset( $alloptions[$option] ) ) { 375 unset( $alloptions[$option] ); 376 wp_cache_set( 'alloptions', $alloptions, 'options' ); 377 } 378 } else { 379 wp_cache_delete( $option, 'options' ); 380 } 381 } 382 if ( $result ) { 383 do_action( "delete_option_$option", $option ); 384 do_action( 'deleted_option', $option ); 385 return true; 386 } 387 return false; 388 } 389 390 /** 391 * Delete a transient. 392 * 393 * @since 2.8.0 394 * @package WordPress 395 * @subpackage Transient 396 * 397 * @uses do_action() Calls 'delete_transient_$transient' hook before transient is deleted. 398 * @uses do_action() Calls 'deleted_transient' hook on success. 399 * 400 * @param string $transient Transient name. Expected to not be SQL-escaped. 401 * @return bool true if successful, false otherwise 402 */ 403 function delete_transient( $transient ) { 404 global $_wp_using_ext_object_cache; 405 406 do_action( 'delete_transient_' . $transient, $transient ); 407 408 if ( $_wp_using_ext_object_cache ) { 409 $result = wp_cache_delete( $transient, 'transient' ); 410 } else { 411 $option_timeout = '_transient_timeout_' . $transient; 412 $option = '_transient_' . $transient; 413 $result = delete_option( $option ); 414 if ( $result ) 415 delete_option( $option_timeout ); 416 } 417 418 if ( $result ) 419 do_action( 'deleted_transient', $transient ); 420 return $result; 421 } 422 423 /** 424 * Get the value of a transient. 425 * 426 * If the transient does not exist or does not have a value, then the return value 427 * will be false. 428 * 429 * @uses apply_filters() Calls 'pre_transient_$transient' hook before checking the transient. 430 * Any value other than false will "short-circuit" the retrieval of the transient 431 * and return the returned value. 432 * @uses apply_filters() Calls 'transient_$option' hook, after checking the transient, with 433 * the transient value. 434 * 435 * @since 2.8.0 436 * @package WordPress 437 * @subpackage Transient 438 * 439 * @param string $transient Transient name. Expected to not be SQL-escaped 440 * @return mixed Value of transient 441 */ 442 function get_transient( $transient ) { 443 global $_wp_using_ext_object_cache; 444 445 $pre = apply_filters( 'pre_transient_' . $transient, false ); 446 if ( false !== $pre ) 447 return $pre; 448 449 if ( $_wp_using_ext_object_cache ) { 450 $value = wp_cache_get( $transient, 'transient' ); 451 } else { 452 $transient_option = '_transient_' . $transient; 453 if ( ! defined( 'WP_INSTALLING' ) ) { 454 // If option is not in alloptions, it is not autoloaded and thus has a timeout 455 $alloptions = wp_load_alloptions(); 456 if ( !isset( $alloptions[$transient_option] ) ) { 457 $transient_timeout = '_transient_timeout_' . $transient; 458 if ( get_option( $transient_timeout ) < time() ) { 459 delete_option( $transient_option ); 460 delete_option( $transient_timeout ); 461 return false; 462 } 463 } 464 } 465 466 $value = get_option( $transient_option ); 467 } 468 469 return apply_filters( 'transient_' . $transient, $value ); 470 } 471 472 /** 473 * Set/update the value of a transient. 474 * 475 * You do not need to serialize values. If the value needs to be serialized, then 476 * it will be serialized before it is set. 477 * 478 * @since 2.8.0 479 * @package WordPress 480 * @subpackage Transient 481 * 482 * @uses apply_filters() Calls 'pre_set_transient_$transient' hook to allow overwriting the 483 * transient value to be stored. 484 * @uses do_action() Calls 'set_transient_$transient' and 'setted_transient' hooks on success. 485 * 486 * @param string $transient Transient name. Expected to not be SQL-escaped. 487 * @param mixed $value Transient value. Expected to not be SQL-escaped. 488 * @param int $expiration Time until expiration in seconds, default 0 489 * @return bool False if value was not set and true if value was set. 490 */ 491 function set_transient( $transient, $value, $expiration = 0 ) { 492 global $_wp_using_ext_object_cache; 493 494 $value = apply_filters( 'pre_set_transient_' . $transient, $value ); 495 496 if ( $_wp_using_ext_object_cache ) { 497 $result = wp_cache_set( $transient, $value, 'transient', $expiration ); 498 } else { 499 $transient_timeout = '_transient_timeout_' . $transient; 500 $transient = '_transient_' . $transient; 501 if ( false === get_option( $transient ) ) { 502 $autoload = 'yes'; 503 if ( $expiration ) { 504 $autoload = 'no'; 505 add_option( $transient_timeout, time() + $expiration, '', 'no' ); 506 } 507 $result = add_option( $transient, $value, '', $autoload ); 508 } else { 509 if ( $expiration ) 510 update_option( $transient_timeout, time() + $expiration ); 511 $result = update_option( $transient, $value ); 512 } 513 } 514 if ( $result ) { 515 do_action( 'set_transient_' . $transient ); 516 do_action( 'setted_transient', $transient ); 517 } 518 return $result; 519 } 520 521 /** 522 * Saves and restores user interface settings stored in a cookie. 523 * 524 * Checks if the current user-settings cookie is updated and stores it. When no 525 * cookie exists (different browser used), adds the last saved cookie restoring 526 * the settings. 527 * 528 * @package WordPress 529 * @subpackage Option 530 * @since 2.7.0 531 */ 532 function wp_user_settings() { 533 534 if ( ! is_admin() ) 535 return; 536 537 if ( defined('DOING_AJAX') ) 538 return; 539 540 if ( ! $user = wp_get_current_user() ) 541 return; 542 543 $settings = get_user_option( 'user-settings', $user->ID ); 544 545 if ( isset( $_COOKIE['wp-settings-' . $user->ID] ) ) { 546 $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user->ID] ); 547 548 if ( ! empty( $cookie ) && strpos( $cookie, '=' ) ) { 549 if ( $cookie == $settings ) 550 return; 551 552 $last_time = (int) get_user_option( 'user-settings-time', $user->ID ); 553 $saved = isset( $_COOKIE['wp-settings-time-' . $user->ID]) ? preg_replace( '/[^0-9]/', '', $_COOKIE['wp-settings-time-' . $user->ID] ) : 0; 554 555 if ( $saved > $last_time ) { 556 update_user_option( $user->ID, 'user-settings', $cookie, false ); 557 update_user_option( $user->ID, 'user-settings-time', time() - 5, false ); 558 return; 559 } 560 } 561 } 562 563 setcookie( 'wp-settings-' . $user->ID, $settings, time() + 31536000, SITECOOKIEPATH ); 564 setcookie( 'wp-settings-time-' . $user->ID, time(), time() + 31536000, SITECOOKIEPATH ); 565 $_COOKIE['wp-settings-' . $user->ID] = $settings; 566 } 567 568 /** 569 * Retrieve user interface setting value based on setting name. 570 * 571 * @package WordPress 572 * @subpackage Option 573 * @since 2.7.0 574 * 575 * @param string $name The name of the setting. 576 * @param string $default Optional default value to return when $name is not set. 577 * @return mixed the last saved user setting or the default value/false if it doesn't exist. 578 */ 579 function get_user_setting( $name, $default = false ) { 580 581 $all = get_all_user_settings(); 582 583 return isset($all[$name]) ? $all[$name] : $default; 584 } 585 586 /** 587 * Add or update user interface setting. 588 * 589 * Both $name and $value can contain only ASCII letters, numbers and underscores. 590 * This function has to be used before any output has started as it calls setcookie(). 591 * 592 * @package WordPress 593 * @subpackage Option 594 * @since 2.8.0 595 * 596 * @param string $name The name of the setting. 597 * @param string $value The value for the setting. 598 * @return bool true if set successfully/false if not. 599 */ 600 function set_user_setting( $name, $value ) { 601 602 if ( headers_sent() ) 603 return false; 604 605 $all = get_all_user_settings(); 606 $name = preg_replace( '/[^A-Za-z0-9_]+/', '', $name ); 607 608 if ( empty($name) ) 609 return false; 610 611 $all[$name] = $value; 612 613 return wp_set_all_user_settings($all); 614 } 615 616 /** 617 * Delete user interface settings. 618 * 619 * Deleting settings would reset them to the defaults. 620 * This function has to be used before any output has started as it calls setcookie(). 621 * 622 * @package WordPress 623 * @subpackage Option 624 * @since 2.7.0 625 * 626 * @param mixed $names The name or array of names of the setting to be deleted. 627 * @return bool true if deleted successfully/false if not. 628 */ 629 function delete_user_setting( $names ) { 630 631 if ( headers_sent() ) 632 return false; 633 634 $all = get_all_user_settings(); 635 $names = (array) $names; 636 637 foreach ( $names as $name ) { 638 if ( isset($all[$name]) ) { 639 unset($all[$name]); 640 $deleted = true; 641 } 642 } 643 644 if ( isset($deleted) ) 645 return wp_set_all_user_settings($all); 646 647 return false; 648 } 649 650 /** 651 * Retrieve all user interface settings. 652 * 653 * @package WordPress 654 * @subpackage Option 655 * @since 2.7.0 656 * 657 * @return array the last saved user settings or empty array. 658 */ 659 function get_all_user_settings() { 660 global $_updated_user_settings; 661 662 if ( ! $user = wp_get_current_user() ) 663 return array(); 664 665 if ( isset($_updated_user_settings) && is_array($_updated_user_settings) ) 666 return $_updated_user_settings; 667 668 $all = array(); 669 if ( isset($_COOKIE['wp-settings-' . $user->ID]) ) { 670 $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user->ID] ); 671 672 if ( $cookie && strpos($cookie, '=') ) // the '=' cannot be 1st char 673 parse_str($cookie, $all); 674 675 } else { 676 $option = get_user_option('user-settings', $user->ID); 677 if ( $option && is_string($option) ) 678 parse_str( $option, $all ); 679 } 680 681 return $all; 682 } 683 684 /** 685 * Private. Set all user interface settings. 686 * 687 * @package WordPress 688 * @subpackage Option 689 * @since 2.8.0 690 * 691 * @param unknown $all 692 * @return bool 693 */ 694 function wp_set_all_user_settings($all) { 695 global $_updated_user_settings; 696 697 if ( ! $user = wp_get_current_user() ) 698 return false; 699 700 $_updated_user_settings = $all; 701 $settings = ''; 702 foreach ( $all as $k => $v ) { 703 $v = preg_replace( '/[^A-Za-z0-9_]+/', '', $v ); 704 $settings .= $k . '=' . $v . '&'; 705 } 706 707 $settings = rtrim($settings, '&'); 708 709 update_user_option( $user->ID, 'user-settings', $settings, false ); 710 update_user_option( $user->ID, 'user-settings-time', time(), false ); 711 712 return true; 713 } 714 715 /** 716 * Delete the user settings of the current user. 717 * 718 * @package WordPress 719 * @subpackage Option 720 * @since 2.7.0 721 */ 722 function delete_all_user_settings() { 723 if ( ! $user = wp_get_current_user() ) 724 return; 725 726 update_user_option( $user->ID, 'user-settings', '', false ); 727 setcookie('wp-settings-' . $user->ID, ' ', time() - 31536000, SITECOOKIEPATH); 728 } 729 730 /** 731 * Retrieve site option value based on name of option. 732 * 733 * @see get_option() 734 * @package WordPress 735 * @subpackage Option 736 * @since 2.8.0 737 * 738 * @uses apply_filters() Calls 'pre_site_option_$option' before checking the option. 739 * Any value other than false will "short-circuit" the retrieval of the option 740 * and return the returned value. 741 * @uses apply_filters() Calls 'site_option_$option', after checking the option, with 742 * the option value. 743 * 744 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped. 745 * @param mixed $default Optional value to return if option doesn't exist. Default false. 746 * @param bool $use_cache Whether to use cache. Multisite only. Default true. 747 * @return mixed Value set for the option. 748 */ 749 function get_site_option( $option, $default = false, $use_cache = true ) { 750 global $wpdb; 751 752 // Allow plugins to short-circuit site options. 753 $pre = apply_filters( 'pre_site_option_' . $option, false ); 754 if ( false !== $pre ) 755 return $pre; 756 757 if ( ! is_multisite() ) { 758 $default = apply_filters( 'default_site_option_' . $option, $default ); 759 $value = get_option($option, $default); 760 } else { 761 $cache_key = "{$wpdb->siteid}:$option"; 762 if ( $use_cache ) 763 $value = wp_cache_get($cache_key, 'site-options'); 764 765 if ( !isset($value) || (false === $value) ) { 766 $row = $wpdb->get_row( $wpdb->prepare("SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) ); 767 768 // Has to be get_row instead of get_var because of funkiness with 0, false, null values 769 if ( is_object( $row ) ) { 770 $value = $row->meta_value; 771 $value = maybe_unserialize( $value ); 772 wp_cache_set( $cache_key, $value, 'site-options' ); 773 } else { 774 $value = apply_filters( 'default_site_option_' . $option, $default ); 775 } 776 } 777 } 778 779 return apply_filters( 'site_option_' . $option, $value ); 780 } 781 782 /** 783 * Add a new site option. 784 * 785 * Existing options will not be updated. Note that prior to 3.3 this wasn't the case. 786 * 787 * @see add_option() 788 * @package WordPress 789 * @subpackage Option 790 * @since 2.8.0 791 * 792 * @uses apply_filters() Calls 'pre_add_site_option_$option' hook to allow overwriting the 793 * option value to be stored. 794 * @uses do_action() Calls 'add_site_option_$option' and 'add_site_option' hooks on success. 795 * 796 * @param string $option Name of option to add. Expected to not be SQL-escaped. 797 * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped. 798 * @return bool False if option was not added and true if option was added. 799 */ 800 function add_site_option( $option, $value ) { 801 global $wpdb; 802 803 $value = apply_filters( 'pre_add_site_option_' . $option, $value ); 804 805 if ( !is_multisite() ) { 806 $result = add_option( $option, $value ); 807 } else { 808 $cache_key = "{$wpdb->siteid}:$option"; 809 810 if ( false !== get_site_option( $option ) ) 811 return false; 812 813 $value = sanitize_option( $option, $value ); 814 wp_cache_set( $cache_key, $value, 'site-options' ); 815 816 $_value = $value; 817 $value = maybe_serialize( $value ); 818 $result = $wpdb->insert( $wpdb->sitemeta, array('site_id' => $wpdb->siteid, 'meta_key' => $option, 'meta_value' => $value ) ); 819 $value = $_value; 820 } 821 822 if ( $result ) { 823 do_action( "add_site_option_{$option}", $option, $value ); 824 do_action( "add_site_option", $option, $value ); 825 return true; 826 } 827 return false; 828 } 829 830 /** 831 * Removes site option by name. 832 * 833 * @see delete_option() 834 * @package WordPress 835 * @subpackage Option 836 * @since 2.8.0 837 * 838 * @uses do_action() Calls 'pre_delete_site_option_$option' hook before option is deleted. 839 * @uses do_action() Calls 'delete_site_option' and 'delete_site_option_$option' 840 * hooks on success. 841 * 842 * @param string $option Name of option to remove. Expected to not be SQL-escaped. 843 * @return bool True, if succeed. False, if failure. 844 */ 845 function delete_site_option( $option ) { 846 global $wpdb; 847 848 // ms_protect_special_option( $option ); @todo 849 850 do_action( 'pre_delete_site_option_' . $option ); 851 852 if ( !is_multisite() ) { 853 $result = delete_option( $option ); 854 } else { 855 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) ); 856 if ( is_null( $row ) || !$row->meta_id ) 857 return false; 858 $cache_key = "{$wpdb->siteid}:$option"; 859 wp_cache_delete( $cache_key, 'site-options' ); 860 861 $result = $wpdb->delete( $wpdb->sitemeta, array( 'meta_key' => $option, 'site_id' => $wpdb->siteid ) ); 862 } 863 864 if ( $result ) { 865 do_action( "delete_site_option_{$option}", $option ); 866 do_action( "delete_site_option", $option ); 867 return true; 868 } 869 return false; 870 } 871 872 /** 873 * Update the value of a site option that was already added. 874 * 875 * @see update_option() 876 * @since 2.8.0 877 * @package WordPress 878 * @subpackage Option 879 * 880 * @uses apply_filters() Calls 'pre_update_site_option_$option' hook to allow overwriting the 881 * option value to be stored. 882 * @uses do_action() Calls 'update_site_option_$option' and 'update_site_option' hooks on success. 883 * 884 * @param string $option Name of option. Expected to not be SQL-escaped. 885 * @param mixed $value Option value. Expected to not be SQL-escaped. 886 * @return bool False if value was not updated and true if value was updated. 887 */ 888 function update_site_option( $option, $value ) { 889 global $wpdb; 890 891 $oldvalue = get_site_option( $option ); 892 $value = apply_filters( 'pre_update_site_option_' . $option, $value, $oldvalue ); 893 894 if ( $value === $oldvalue ) 895 return false; 896 897 if ( false === $oldvalue ) 898 return add_site_option( $option, $value ); 899 900 if ( !is_multisite() ) { 901 $result = update_option( $option, $value ); 902 } else { 903 $value = sanitize_option( $option, $value ); 904 $cache_key = "{$wpdb->siteid}:$option"; 905 wp_cache_set( $cache_key, $value, 'site-options' ); 906 907 $_value = $value; 908 $value = maybe_serialize( $value ); 909 $result = $wpdb->update( $wpdb->sitemeta, array( 'meta_value' => $value ), array( 'site_id' => $wpdb->siteid, 'meta_key' => $option ) ); 910 $value = $_value; 911 } 912 913 if ( $result ) { 914 do_action( "update_site_option_{$option}", $option, $value, $oldvalue ); 915 do_action( "update_site_option", $option, $value, $oldvalue ); 916 return true; 917 } 918 return false; 919 } 920 921 /** 922 * Delete a site transient. 923 * 924 * @since 2.9.0 925 * @package WordPress 926 * @subpackage Transient 927 * 928 * @uses do_action() Calls 'delete_site_transient_$transient' hook before transient is deleted. 929 * @uses do_action() Calls 'deleted_site_transient' hook on success. 930 * 931 * @param string $transient Transient name. Expected to not be SQL-escaped. 932 * @return bool True if successful, false otherwise 933 */ 934 function delete_site_transient( $transient ) { 935 global $_wp_using_ext_object_cache; 936 937 do_action( 'delete_site_transient_' . $transient, $transient ); 938 if ( $_wp_using_ext_object_cache ) { 939 $result = wp_cache_delete( $transient, 'site-transient' ); 940 } else { 941 $option_timeout = '_site_transient_timeout_' . $transient; 942 $option = '_site_transient_' . $transient; 943 $result = delete_site_option( $option ); 944 if ( $result ) 945 delete_site_option( $option_timeout ); 946 } 947 if ( $result ) 948 do_action( 'deleted_site_transient', $transient ); 949 return $result; 950 } 951 952 /** 953 * Get the value of a site transient. 954 * 955 * If the transient does not exist or does not have a value, then the return value 956 * will be false. 957 * 958 * @see get_transient() 959 * @since 2.9.0 960 * @package WordPress 961 * @subpackage Transient 962 * 963 * @uses apply_filters() Calls 'pre_site_transient_$transient' hook before checking the transient. 964 * Any value other than false will "short-circuit" the retrieval of the transient 965 * and return the returned value. 966 * @uses apply_filters() Calls 'site_transient_$option' hook, after checking the transient, with 967 * the transient value. 968 * 969 * @param string $transient Transient name. Expected to not be SQL-escaped. 970 * @return mixed Value of transient 971 */ 972 function get_site_transient( $transient ) { 973 global $_wp_using_ext_object_cache; 974 975 $pre = apply_filters( 'pre_site_transient_' . $transient, false ); 976 if ( false !== $pre ) 977 return $pre; 978 979 if ( $_wp_using_ext_object_cache ) { 980 $value = wp_cache_get( $transient, 'site-transient' ); 981 } else { 982 // Core transients that do not have a timeout. Listed here so querying timeouts can be avoided. 983 $no_timeout = array('update_core', 'update_plugins', 'update_themes'); 984 $transient_option = '_site_transient_' . $transient; 985 if ( ! in_array( $transient, $no_timeout ) ) { 986 $transient_timeout = '_site_transient_timeout_' . $transient; 987 $timeout = get_site_option( $transient_timeout ); 988 if ( false !== $timeout && $timeout < time() ) { 989 delete_site_option( $transient_option ); 990 delete_site_option( $transient_timeout ); 991 return false; 992 } 993 } 994 995 $value = get_site_option( $transient_option ); 996 } 997 998 return apply_filters( 'site_transient_' . $transient, $value ); 999 } 1000 1001 /** 1002 * Set/update the value of a site transient. 1003 * 1004 * You do not need to serialize values, if the value needs to be serialize, then 1005 * it will be serialized before it is set. 1006 * 1007 * @see set_transient() 1008 * @since 2.9.0 1009 * @package WordPress 1010 * @subpackage Transient 1011 * 1012 * @uses apply_filters() Calls 'pre_set_site_transient_$transient' hook to allow overwriting the 1013 * transient value to be stored. 1014 * @uses do_action() Calls 'set_site_transient_$transient' and 'setted_site_transient' hooks on success. 1015 * 1016 * @param string $transient Transient name. Expected to not be SQL-escaped. 1017 * @param mixed $value Transient value. Expected to not be SQL-escaped. 1018 * @param int $expiration Time until expiration in seconds, default 0 1019 * @return bool False if value was not set and true if value was set. 1020 */ 1021 function set_site_transient( $transient, $value, $expiration = 0 ) { 1022 global $_wp_using_ext_object_cache; 1023 1024 $value = apply_filters( 'pre_set_site_transient_' . $transient, $value ); 1025 1026 if ( $_wp_using_ext_object_cache ) { 1027 $result = wp_cache_set( $transient, $value, 'site-transient', $expiration ); 1028 } else { 1029 $transient_timeout = '_site_transient_timeout_' . $transient; 1030 $transient = '_site_transient_' . $transient; 1031 if ( false === get_site_option( $transient ) ) { 1032 if ( $expiration ) 1033 add_site_option( $transient_timeout, time() + $expiration ); 1034 $result = add_site_option( $transient, $value ); 1035 } else { 1036 if ( $expiration ) 1037 update_site_option( $transient_timeout, time() + $expiration ); 1038 $result = update_site_option( $transient, $value ); 1039 } 1040 } 1041 if ( $result ) { 1042 do_action( 'set_site_transient_' . $transient ); 1043 do_action( 'setted_site_transient', $transient ); 1044 } 1045 return $result; 1046 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Fri May 25 03:56:23 2012 | Hosted by follow the white rabbit. |