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