[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

/wp-includes/ -> option.php (source)

   1  <?php
   2  /**
   3   * Option API
   4   *
   5   * @package WordPress
   6   * @subpackage Option
   7   */
   8  
   9  /**
  10   * Retrieves an option value based on an option name.
  11   *
  12   * If the option does not exist or does not have a value, then the return value
  13   * will be false. This is useful to check whether you need to install an option
  14   * and is commonly used during installation of plugin options and to test
  15   * whether upgrading is required.
  16   *
  17   * If the option was serialized then it will be unserialized when it is returned.
  18   *
  19   * Any scalar values will be returned as strings. You may coerce the return type of
  20   * a given option by registering an {@see 'option_$option'} filter callback.
  21   *
  22   * @since 1.5.0
  23   *
  24   * @global wpdb $wpdb WordPress database abstraction object.
  25   *
  26   * @param string $option  Name of option to retrieve. Expected to not be SQL-escaped.
  27   * @param mixed  $default Optional. Default value to return if the option does not exist.
  28   * @return mixed Value set for the option.
  29   */
  30  function get_option( $option, $default = false ) {
  31      global $wpdb;
  32  
  33      $option = trim( $option );
  34      if ( empty( $option ) ) {
  35          return false;
  36      }
  37  
  38      /**
  39       * Filters the value of an existing option before it is retrieved.
  40       *
  41       * The dynamic portion of the hook name, `$option`, refers to the option name.
  42       *
  43       * Passing a truthy value to the filter will short-circuit retrieving
  44       * the option value, returning the passed value instead.
  45       *
  46       * @since 1.5.0
  47       * @since 4.4.0 The `$option` parameter was added.
  48       * @since 4.9.0 The `$default` parameter was added.
  49       *
  50       * @param bool|mixed $pre_option The value to return instead of the option value. This differs from
  51       *                               `$default`, which is used as the fallback value in the event the option
  52       *                               doesn't exist elsewhere in get_option(). Default false (to skip past the
  53       *                               short-circuit).
  54       * @param string     $option     Option name.
  55       * @param mixed      $default    The fallback value to return if the option does not exist.
  56       *                               Default is false.
  57       */
  58      $pre = apply_filters( "pre_option_{$option}", false, $option, $default );
  59  
  60      if ( false !== $pre ) {
  61          return $pre;
  62      }
  63  
  64      if ( defined( 'WP_SETUP_CONFIG' ) ) {
  65          return false;
  66      }
  67  
  68      // Distinguish between `false` as a default, and not passing one.
  69      $passed_default = func_num_args() > 1;
  70  
  71      if ( ! wp_installing() ) {
  72          // prevent non-existent options from triggering multiple queries
  73          $notoptions = wp_cache_get( 'notoptions', 'options' );
  74          if ( isset( $notoptions[ $option ] ) ) {
  75              /**
  76               * Filters the default value for an option.
  77               *
  78               * The dynamic portion of the hook name, `$option`, refers to the option name.
  79               *
  80               * @since 3.4.0
  81               * @since 4.4.0 The `$option` parameter was added.
  82               * @since 4.7.0 The `$passed_default` parameter was added to distinguish between a `false` value and the default parameter value.
  83               *
  84               * @param mixed  $default The default value to return if the option does not exist
  85               *                        in the database.
  86               * @param string $option  Option name.
  87               * @param bool   $passed_default Was `get_option()` passed a default value?
  88               */
  89              return apply_filters( "default_option_{$option}", $default, $option, $passed_default );
  90          }
  91  
  92          $alloptions = wp_load_alloptions();
  93  
  94          if ( isset( $alloptions[ $option ] ) ) {
  95              $value = $alloptions[ $option ];
  96          } else {
  97              $value = wp_cache_get( $option, 'options' );
  98  
  99              if ( false === $value ) {
 100                  $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
 101  
 102                  // Has to be get_row instead of get_var because of funkiness with 0, false, null values
 103                  if ( is_object( $row ) ) {
 104                      $value = $row->option_value;
 105                      wp_cache_add( $option, $value, 'options' );
 106                  } else { // option does not exist, so we must cache its non-existence
 107                      if ( ! is_array( $notoptions ) ) {
 108                          $notoptions = array();
 109                      }
 110                      $notoptions[ $option ] = true;
 111                      wp_cache_set( 'notoptions', $notoptions, 'options' );
 112  
 113                      /** This filter is documented in wp-includes/option.php */
 114                      return apply_filters( "default_option_{$option}", $default, $option, $passed_default );
 115                  }
 116              }
 117          }
 118      } else {
 119          $suppress = $wpdb->suppress_errors();
 120          $row      = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
 121          $wpdb->suppress_errors( $suppress );
 122          if ( is_object( $row ) ) {
 123              $value = $row->option_value;
 124          } else {
 125              /** This filter is documented in wp-includes/option.php */
 126              return apply_filters( "default_option_{$option}", $default, $option, $passed_default );
 127          }
 128      }
 129  
 130      // If home is not set use siteurl.
 131      if ( 'home' == $option && '' == $value ) {
 132          return get_option( 'siteurl' );
 133      }
 134  
 135      if ( in_array( $option, array( 'siteurl', 'home', 'category_base', 'tag_base' ) ) ) {
 136          $value = untrailingslashit( $value );
 137      }
 138  
 139      /**
 140       * Filters the value of an existing option.
 141       *
 142       * The dynamic portion of the hook name, `$option`, refers to the option name.
 143       *
 144       * @since 1.5.0 As 'option_' . $setting
 145       * @since 3.0.0
 146       * @since 4.4.0 The `$option` parameter was added.
 147       *
 148       * @param mixed  $value  Value of the option. If stored serialized, it will be
 149       *                       unserialized prior to being returned.
 150       * @param string $option Option name.
 151       */
 152      return apply_filters( "option_{$option}", maybe_unserialize( $value ), $option );
 153  }
 154  
 155  /**
 156   * Protect WordPress special option from being modified.
 157   *
 158   * Will die if $option is in protected list. Protected options are 'alloptions'
 159   * and 'notoptions' options.
 160   *
 161   * @since 2.2.0
 162   *
 163   * @param string $option Option name.
 164   */
 165  function wp_protect_special_option( $option ) {
 166      if ( 'alloptions' === $option || 'notoptions' === $option ) {
 167          wp_die(
 168              sprintf(
 169                  /* translators: %s: Option name. */
 170                  __( '%s is a protected WP option and may not be modified' ),
 171                  esc_html( $option )
 172              )
 173          );
 174      }
 175  }
 176  
 177  /**
 178   * Print option value after sanitizing for forms.
 179   *
 180   * @since 1.5.0
 181   *
 182   * @param string $option Option name.
 183   */
 184  function form_option( $option ) {
 185      echo esc_attr( get_option( $option ) );
 186  }
 187  
 188  /**
 189   * Loads and caches all autoloaded options, if available or all options.
 190   *
 191   * @since 2.2.0
 192   * @since 5.4.0 The `$force_cache` parameter was added.
 193   *
 194   * @global wpdb $wpdb WordPress database abstraction object.
 195   *
 196   * @param bool $force_cache Optional. Whether to force an update of the local cache
 197   *                          from the persistent cache. Default false.
 198   * @return array List of all options.
 199   */
 200  function wp_load_alloptions( $force_cache = false ) {
 201      global $wpdb;
 202  
 203      if ( ! wp_installing() || ! is_multisite() ) {
 204          $alloptions = wp_cache_get( 'alloptions', 'options', $force_cache );
 205      } else {
 206          $alloptions = false;
 207      }
 208  
 209      if ( ! $alloptions ) {
 210          $suppress      = $wpdb->suppress_errors();
 211          $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" );
 212          if ( ! $alloptions_db ) {
 213              $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
 214          }
 215          $wpdb->suppress_errors( $suppress );
 216  
 217          $alloptions = array();
 218          foreach ( (array) $alloptions_db as $o ) {
 219              $alloptions[ $o->option_name ] = $o->option_value;
 220          }
 221  
 222          if ( ! wp_installing() || ! is_multisite() ) {
 223              /**
 224               * Filters all options before caching them.
 225               *
 226               * @since 4.9.0
 227               *
 228               * @param array $alloptions Array with all options.
 229               */
 230              $alloptions = apply_filters( 'pre_cache_alloptions', $alloptions );
 231              wp_cache_add( 'alloptions', $alloptions, 'options' );
 232          }
 233      }
 234  
 235      /**
 236       * Filters all options after retrieving them.
 237       *
 238       * @since 4.9.0
 239       *
 240       * @param array $alloptions Array with all options.
 241       */
 242      return apply_filters( 'alloptions', $alloptions );
 243  }
 244  
 245  /**
 246   * Loads and caches certain often requested site options if is_multisite() and a persistent cache is not being used.
 247   *
 248   * @since 3.0.0
 249   *
 250   * @global wpdb $wpdb WordPress database abstraction object.
 251   *
 252   * @param int $network_id Optional site ID for which to query the options. Defaults to the current site.
 253   */
 254  function wp_load_core_site_options( $network_id = null ) {
 255      global $wpdb;
 256  
 257      if ( ! is_multisite() || wp_using_ext_object_cache() || wp_installing() ) {
 258          return;
 259      }
 260  
 261      if ( empty( $network_id ) ) {
 262          $network_id = get_current_network_id();
 263      }
 264  
 265      $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' );
 266  
 267      $core_options_in = "'" . implode( "', '", $core_options ) . "'";
 268      $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", $network_id ) );
 269  
 270      foreach ( $options as $option ) {
 271          $key                = $option->meta_key;
 272          $cache_key          = "{$network_id}:$key";
 273          $option->meta_value = maybe_unserialize( $option->meta_value );
 274  
 275          wp_cache_set( $cache_key, $option->meta_value, 'site-options' );
 276      }
 277  }
 278  
 279  /**
 280   * Update the value of an option that was already added.
 281   *
 282   * You do not need to serialize values. If the value needs to be serialized, then
 283   * it will be serialized before it is inserted into the database. Remember,
 284   * resources can not be serialized or added as an option.
 285   *
 286   * If the option does not exist, then the option will be added with the option value,
 287   * with an `$autoload` value of 'yes'.
 288  
 289   * This function is designed to work with or without a logged-in user. In terms of security,
 290   * plugin developers should check the current user's capabilities before updating any options.
 291   *
 292   * @since 1.0.0
 293   * @since 4.2.0 The `$autoload` parameter was added.
 294   *
 295   * @global wpdb $wpdb WordPress database abstraction object.
 296   *
 297   * @param string      $option   Option name. Expected to not be SQL-escaped.
 298   * @param mixed       $value    Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
 299   * @param string|bool $autoload Optional. Whether to load the option when WordPress starts up. For existing options,
 300   *                              `$autoload` can only be updated using `update_option()` if `$value` is also changed.
 301   *                              Accepts 'yes'|true to enable or 'no'|false to disable. For non-existent options,
 302   *                              the default value is 'yes'. Default null.
 303   * @return bool False if value was not updated and true if value was updated.
 304   */
 305  function update_option( $option, $value, $autoload = null ) {
 306      global $wpdb;
 307  
 308      $option = trim( $option );
 309      if ( empty( $option ) ) {
 310          return false;
 311      }
 312  
 313      wp_protect_special_option( $option );
 314  
 315      if ( is_object( $value ) ) {
 316          $value = clone $value;
 317      }
 318  
 319      $value     = sanitize_option( $option, $value );
 320      $old_value = get_option( $option );
 321  
 322      /**
 323       * Filters a specific option before its value is (maybe) serialized and updated.
 324       *
 325       * The dynamic portion of the hook name, `$option`, refers to the option name.
 326       *
 327       * @since 2.6.0
 328       * @since 4.4.0 The `$option` parameter was added.
 329       *
 330       * @param mixed  $value     The new, unserialized option value.
 331       * @param mixed  $old_value The old option value.
 332       * @param string $option    Option name.
 333       */
 334      $value = apply_filters( "pre_update_option_{$option}", $value, $old_value, $option );
 335  
 336      /**
 337       * Filters an option before its value is (maybe) serialized and updated.
 338       *
 339       * @since 3.9.0
 340       *
 341       * @param mixed  $value     The new, unserialized option value.
 342       * @param string $option    Name of the option.
 343       * @param mixed  $old_value The old option value.
 344       */
 345      $value = apply_filters( 'pre_update_option', $value, $option, $old_value );
 346  
 347      /*
 348       * If the new and old values are the same, no need to update.
 349       *
 350       * Unserialized values will be adequate in most cases. If the unserialized
 351       * data differs, the (maybe) serialized data is checked to avoid
 352       * unnecessary database calls for otherwise identical object instances.
 353       *
 354       * See https://core.trac.wordpress.org/ticket/38903
 355       */
 356      if ( $value === $old_value || maybe_serialize( $value ) === maybe_serialize( $old_value ) ) {
 357          return false;
 358      }
 359  
 360      /** This filter is documented in wp-includes/option.php */
 361      if ( apply_filters( "default_option_{$option}", false, $option, false ) === $old_value ) {
 362          // Default setting for new options is 'yes'.
 363          if ( null === $autoload ) {
 364              $autoload = 'yes';
 365          }
 366  
 367          return add_option( $option, $value, '', $autoload );
 368      }
 369  
 370      $serialized_value = maybe_serialize( $value );
 371  
 372      /**
 373       * Fires immediately before an option value is updated.
 374       *
 375       * @since 2.9.0
 376       *
 377       * @param string $option    Name of the option to update.
 378       * @param mixed  $old_value The old option value.
 379       * @param mixed  $value     The new option value.
 380       */
 381      do_action( 'update_option', $option, $old_value, $value );
 382  
 383      $update_args = array(
 384          'option_value' => $serialized_value,
 385      );
 386  
 387      if ( null !== $autoload ) {
 388          $update_args['autoload'] = ( 'no' === $autoload || false === $autoload ) ? 'no' : 'yes';
 389      }
 390  
 391      $result = $wpdb->update( $wpdb->options, $update_args, array( 'option_name' => $option ) );
 392      if ( ! $result ) {
 393          return false;
 394      }
 395  
 396      $notoptions = wp_cache_get( 'notoptions', 'options' );
 397      if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
 398          unset( $notoptions[ $option ] );
 399          wp_cache_set( 'notoptions', $notoptions, 'options' );
 400      }
 401  
 402      if ( ! wp_installing() ) {
 403          $alloptions = wp_load_alloptions( true );
 404          if ( isset( $alloptions[ $option ] ) ) {
 405              $alloptions[ $option ] = $serialized_value;
 406              wp_cache_set( 'alloptions', $alloptions, 'options' );
 407          } else {
 408              wp_cache_set( $option, $serialized_value, 'options' );
 409          }
 410      }
 411  
 412      /**
 413       * Fires after the value of a specific option has been successfully updated.
 414       *
 415       * The dynamic portion of the hook name, `$option`, refers to the option name.
 416       *
 417       * @since 2.0.1
 418       * @since 4.4.0 The `$option` parameter was added.
 419       *
 420       * @param mixed  $old_value The old option value.
 421       * @param mixed  $value     The new option value.
 422       * @param string $option    Option name.
 423       */
 424      do_action( "update_option_{$option}", $old_value, $value, $option );
 425  
 426      /**
 427       * Fires after the value of an option has been successfully updated.
 428       *
 429       * @since 2.9.0
 430       *
 431       * @param string $option    Name of the updated option.
 432       * @param mixed  $old_value The old option value.
 433       * @param mixed  $value     The new option value.
 434       */
 435      do_action( 'updated_option', $option, $old_value, $value );
 436      return true;
 437  }
 438  
 439  /**
 440   * Add a new option.
 441   *
 442   * You do not need to serialize values. If the value needs to be serialized, then
 443   * it will be serialized before it is inserted into the database. Remember,
 444   * resources can not be serialized or added as an option.
 445   *
 446   * You can create options without values and then update the values later.
 447   * Existing options will not be updated and checks are performed to ensure that you
 448   * aren't adding a protected WordPress option. Care should be taken to not name
 449   * options the same as the ones which are protected.
 450   *
 451   * @since 1.0.0
 452   *
 453   * @global wpdb $wpdb WordPress database abstraction object.
 454   *
 455   * @param string         $option      Name of option to add. Expected to not be SQL-escaped.
 456   * @param mixed          $value       Optional. Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
 457   * @param string         $deprecated  Optional. Description. Not used anymore.
 458   * @param string|bool    $autoload    Optional. Whether to load the option when WordPress starts up.
 459   *                                    Default is enabled. Accepts 'no' to disable for legacy reasons.
 460   * @return bool False if option was not added and true if option was added.
 461   */
 462  function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) {
 463      global $wpdb;
 464  
 465      if ( ! empty( $deprecated ) ) {
 466          _deprecated_argument( __FUNCTION__, '2.3.0' );
 467      }
 468  
 469      $option = trim( $option );
 470      if ( empty( $option ) ) {
 471          return false;
 472      }
 473  
 474      wp_protect_special_option( $option );
 475  
 476      if ( is_object( $value ) ) {
 477          $value = clone $value;
 478      }
 479  
 480      $value = sanitize_option( $option, $value );
 481  
 482      // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
 483      $notoptions = wp_cache_get( 'notoptions', 'options' );
 484      if ( ! is_array( $notoptions ) || ! isset( $notoptions[ $option ] ) ) {
 485          /** This filter is documented in wp-includes/option.php */
 486          if ( apply_filters( "default_option_{$option}", false, $option, false ) !== get_option( $option ) ) {
 487              return false;
 488          }
 489      }
 490  
 491      $serialized_value = maybe_serialize( $value );
 492      $autoload         = ( 'no' === $autoload || false === $autoload ) ? 'no' : 'yes';
 493  
 494      /**
 495       * Fires before an option is added.
 496       *
 497       * @since 2.9.0
 498       *
 499       * @param string $option Name of the option to add.
 500       * @param mixed  $value  Value of the option.
 501       */
 502      do_action( 'add_option', $option, $value );
 503  
 504      $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 ) );
 505      if ( ! $result ) {
 506          return false;
 507      }
 508  
 509      if ( ! wp_installing() ) {
 510          if ( 'yes' == $autoload ) {
 511              $alloptions            = wp_load_alloptions( true );
 512              $alloptions[ $option ] = $serialized_value;
 513              wp_cache_set( 'alloptions', $alloptions, 'options' );
 514          } else {
 515              wp_cache_set( $option, $serialized_value, 'options' );
 516          }
 517      }
 518  
 519      // This option exists now
 520      $notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh
 521      if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
 522          unset( $notoptions[ $option ] );
 523          wp_cache_set( 'notoptions', $notoptions, 'options' );
 524      }
 525  
 526      /**
 527       * Fires after a specific option has been added.
 528       *
 529       * The dynamic portion of the hook name, `$option`, refers to the option name.
 530       *
 531       * @since 2.5.0 As "add_option_{$name}"
 532       * @since 3.0.0
 533       *
 534       * @param string $option Name of the option to add.
 535       * @param mixed  $value  Value of the option.
 536       */
 537      do_action( "add_option_{$option}", $option, $value );
 538  
 539      /**
 540       * Fires after an option has been added.
 541       *
 542       * @since 2.9.0
 543       *
 544       * @param string $option Name of the added option.
 545       * @param mixed  $value  Value of the option.
 546       */
 547      do_action( 'added_option', $option, $value );
 548      return true;
 549  }
 550  
 551  /**
 552   * Removes option by name. Prevents removal of protected WordPress options.
 553   *
 554   * @since 1.2.0
 555   *
 556   * @global wpdb $wpdb WordPress database abstraction object.
 557   *
 558   * @param string $option Name of option to remove. Expected to not be SQL-escaped.
 559   * @return bool True, if option is successfully deleted. False on failure.
 560   */
 561  function delete_option( $option ) {
 562      global $wpdb;
 563  
 564      $option = trim( $option );
 565      if ( empty( $option ) ) {
 566          return false;
 567      }
 568  
 569      wp_protect_special_option( $option );
 570  
 571      // Get the ID, if no ID then return
 572      $row = $wpdb->get_row( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) );
 573      if ( is_null( $row ) ) {
 574          return false;
 575      }
 576  
 577      /**
 578       * Fires immediately before an option is deleted.
 579       *
 580       * @since 2.9.0
 581       *
 582       * @param string $option Name of the option to delete.
 583       */
 584      do_action( 'delete_option', $option );
 585  
 586      $result = $wpdb->delete( $wpdb->options, array( 'option_name' => $option ) );
 587      if ( ! wp_installing() ) {
 588          if ( 'yes' == $row->autoload ) {
 589              $alloptions = wp_load_alloptions( true );
 590              if ( is_array( $alloptions ) && isset( $alloptions[ $option ] ) ) {
 591                  unset( $alloptions[ $option ] );
 592                  wp_cache_set( 'alloptions', $alloptions, 'options' );
 593              }
 594          } else {
 595              wp_cache_delete( $option, 'options' );
 596          }
 597      }
 598      if ( $result ) {
 599  
 600          /**
 601           * Fires after a specific option has been deleted.
 602           *
 603           * The dynamic portion of the hook name, `$option`, refers to the option name.
 604           *
 605           * @since 3.0.0
 606           *
 607           * @param string $option Name of the deleted option.
 608           */
 609          do_action( "delete_option_{$option}", $option );
 610  
 611          /**
 612           * Fires after an option has been deleted.
 613           *
 614           * @since 2.9.0
 615           *
 616           * @param string $option Name of the deleted option.
 617           */
 618          do_action( 'deleted_option', $option );
 619          return true;
 620      }
 621      return false;
 622  }
 623  
 624  /**
 625   * Delete a transient.
 626   *
 627   * @since 2.8.0
 628   *
 629   * @param string $transient Transient name. Expected to not be SQL-escaped.
 630   * @return bool true if successful, false otherwise
 631   */
 632  function delete_transient( $transient ) {
 633  
 634      /**
 635       * Fires immediately before a specific transient is deleted.
 636       *
 637       * The dynamic portion of the hook name, `$transient`, refers to the transient name.
 638       *
 639       * @since 3.0.0
 640       *
 641       * @param string $transient Transient name.
 642       */
 643      do_action( "delete_transient_{$transient}", $transient );
 644  
 645      if ( wp_using_ext_object_cache() ) {
 646          $result = wp_cache_delete( $transient, 'transient' );
 647      } else {
 648          $option_timeout = '_transient_timeout_' . $transient;
 649          $option         = '_transient_' . $transient;
 650          $result         = delete_option( $option );
 651          if ( $result ) {
 652              delete_option( $option_timeout );
 653          }
 654      }
 655  
 656      if ( $result ) {
 657  
 658          /**
 659           * Fires after a transient is deleted.
 660           *
 661           * @since 3.0.0
 662           *
 663           * @param string $transient Deleted transient name.
 664           */
 665          do_action( 'deleted_transient', $transient );
 666      }
 667  
 668      return $result;
 669  }
 670  
 671  /**
 672   * Get the value of a transient.
 673   *
 674   * If the transient does not exist, does not have a value, or has expired,
 675   * then the return value will be false.
 676   *
 677   * @since 2.8.0
 678   *
 679   * @param string $transient Transient name. Expected to not be SQL-escaped.
 680   * @return mixed Value of transient.
 681   */
 682  function get_transient( $transient ) {
 683  
 684      /**
 685       * Filters the value of an existing transient.
 686       *
 687       * The dynamic portion of the hook name, `$transient`, refers to the transient name.
 688       *
 689       * Passing a truthy value to the filter will effectively short-circuit retrieval
 690       * of the transient, returning the passed value instead.
 691       *
 692       * @since 2.8.0
 693       * @since 4.4.0 The `$transient` parameter was added
 694       *
 695       * @param mixed  $pre_transient The default value to return if the transient does not exist.
 696       *                              Any value other than false will short-circuit the retrieval
 697       *                              of the transient, and return the returned value.
 698       * @param string $transient     Transient name.
 699       */
 700      $pre = apply_filters( "pre_transient_{$transient}", false, $transient );
 701      if ( false !== $pre ) {
 702          return $pre;
 703      }
 704  
 705      if ( wp_using_ext_object_cache() ) {
 706          $value = wp_cache_get( $transient, 'transient' );
 707      } else {
 708          $transient_option = '_transient_' . $transient;
 709          if ( ! wp_installing() ) {
 710              // If option is not in alloptions, it is not autoloaded and thus has a timeout
 711              $alloptions = wp_load_alloptions();
 712              if ( ! isset( $alloptions[ $transient_option ] ) ) {
 713                  $transient_timeout = '_transient_timeout_' . $transient;
 714                  $timeout           = get_option( $transient_timeout );
 715                  if ( false !== $timeout && $timeout < time() ) {
 716                      delete_option( $transient_option );
 717                      delete_option( $transient_timeout );
 718                      $value = false;
 719                  }
 720              }
 721          }
 722  
 723          if ( ! isset( $value ) ) {
 724              $value = get_option( $transient_option );
 725          }
 726      }
 727  
 728      /**
 729       * Filters an existing transient's value.
 730       *
 731       * The dynamic portion of the hook name, `$transient`, refers to the transient name.
 732       *
 733       * @since 2.8.0
 734       * @since 4.4.0 The `$transient` parameter was added
 735       *
 736       * @param mixed  $value     Value of transient.
 737       * @param string $transient Transient name.
 738       */
 739      return apply_filters( "transient_{$transient}", $value, $transient );
 740  }
 741  
 742  /**
 743   * Set/update the value of a transient.
 744   *
 745   * You do not need to serialize values. If the value needs to be serialized, then
 746   * it will be serialized before it is set.
 747   *
 748   * @since 2.8.0
 749   *
 750   * @param string $transient  Transient name. Expected to not be SQL-escaped. Must be
 751   *                           172 characters or fewer in length.
 752   * @param mixed  $value      Transient value. Must be serializable if non-scalar.
 753   *                           Expected to not be SQL-escaped.
 754   * @param int    $expiration Optional. Time until expiration in seconds. Default 0 (no expiration).
 755   * @return bool False if value was not set and true if value was set.
 756   */
 757  function set_transient( $transient, $value, $expiration = 0 ) {
 758  
 759      $expiration = (int) $expiration;
 760  
 761      /**
 762       * Filters a specific transient before its value is set.
 763       *
 764       * The dynamic portion of the hook name, `$transient`, refers to the transient name.
 765       *
 766       * @since 3.0.0
 767       * @since 4.2.0 The `$expiration` parameter was added.
 768       * @since 4.4.0 The `$transient` parameter was added.
 769       *
 770       * @param mixed  $value      New value of transient.
 771       * @param int    $expiration Time until expiration in seconds.
 772       * @param string $transient  Transient name.
 773       */
 774      $value = apply_filters( "pre_set_transient_{$transient}", $value, $expiration, $transient );
 775  
 776      /**
 777       * Filters the expiration for a transient before its value is set.
 778       *
 779       * The dynamic portion of the hook name, `$transient`, refers to the transient name.
 780       *
 781       * @since 4.4.0
 782       *
 783       * @param int    $expiration Time until expiration in seconds. Use 0 for no expiration.
 784       * @param mixed  $value      New value of transient.
 785       * @param string $transient  Transient name.
 786       */
 787      $expiration = apply_filters( "expiration_of_transient_{$transient}", $expiration, $value, $transient );
 788  
 789      if ( wp_using_ext_object_cache() ) {
 790          $result = wp_cache_set( $transient, $value, 'transient', $expiration );
 791      } else {
 792          $transient_timeout = '_transient_timeout_' . $transient;
 793          $transient_option  = '_transient_' . $transient;
 794          if ( false === get_option( $transient_option ) ) {
 795              $autoload = 'yes';
 796              if ( $expiration ) {
 797                  $autoload = 'no';
 798                  add_option( $transient_timeout, time() + $expiration, '', 'no' );
 799              }
 800              $result = add_option( $transient_option, $value, '', $autoload );
 801          } else {
 802              // If expiration is requested, but the transient has no timeout option,
 803              // delete, then re-create transient rather than update.
 804              $update = true;
 805              if ( $expiration ) {
 806                  if ( false === get_option( $transient_timeout ) ) {
 807                      delete_option( $transient_option );
 808                      add_option( $transient_timeout, time() + $expiration, '', 'no' );
 809                      $result = add_option( $transient_option, $value, '', 'no' );
 810                      $update = false;
 811                  } else {
 812                      update_option( $transient_timeout, time() + $expiration );
 813                  }
 814              }
 815              if ( $update ) {
 816                  $result = update_option( $transient_option, $value );
 817              }
 818          }
 819      }
 820  
 821      if ( $result ) {
 822  
 823          /**
 824           * Fires after the value for a specific transient has been set.
 825           *
 826           * The dynamic portion of the hook name, `$transient`, refers to the transient name.
 827           *
 828           * @since 3.0.0
 829           * @since 3.6.0 The `$value` and `$expiration` parameters were added.
 830           * @since 4.4.0 The `$transient` parameter was added.
 831           *
 832           * @param mixed  $value      Transient value.
 833           * @param int    $expiration Time until expiration in seconds.
 834           * @param string $transient  The name of the transient.
 835           */
 836          do_action( "set_transient_{$transient}", $value, $expiration, $transient );
 837  
 838          /**
 839           * Fires after the value for a transient has been set.
 840           *
 841           * @since 3.0.0
 842           * @since 3.6.0 The `$value` and `$expiration` parameters were added.
 843           *
 844           * @param string $transient  The name of the transient.
 845           * @param mixed  $value      Transient value.
 846           * @param int    $expiration Time until expiration in seconds.
 847           */
 848          do_action( 'setted_transient', $transient, $value, $expiration );
 849      }
 850      return $result;
 851  }
 852  
 853  /**
 854   * Deletes all expired transients.
 855   *
 856   * The multi-table delete syntax is used to delete the transient record
 857   * from table a, and the corresponding transient_timeout record from table b.
 858   *
 859   * @since 4.9.0
 860   *
 861   * @param bool $force_db Optional. Force cleanup to run against the database even when an external object cache is used.
 862   */
 863  function delete_expired_transients( $force_db = false ) {
 864      global $wpdb;
 865  
 866      if ( ! $force_db && wp_using_ext_object_cache() ) {
 867          return;
 868      }
 869  
 870      $wpdb->query(
 871          $wpdb->prepare(
 872              "DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
 873              WHERE a.option_name LIKE %s
 874              AND a.option_name NOT LIKE %s
 875              AND b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) )
 876              AND b.option_value < %d",
 877              $wpdb->esc_like( '_transient_' ) . '%',
 878              $wpdb->esc_like( '_transient_timeout_' ) . '%',
 879              time()
 880          )
 881      );
 882  
 883      if ( ! is_multisite() ) {
 884          // non-Multisite stores site transients in the options table.
 885          $wpdb->query(
 886              $wpdb->prepare(
 887                  "DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b
 888                  WHERE a.option_name LIKE %s
 889                  AND a.option_name NOT LIKE %s
 890                  AND b.option_name = CONCAT( '_site_transient_timeout_', SUBSTRING( a.option_name, 17 ) )
 891                  AND b.option_value < %d",
 892                  $wpdb->esc_like( '_site_transient_' ) . '%',
 893                  $wpdb->esc_like( '_site_transient_timeout_' ) . '%',
 894                  time()
 895              )
 896          );
 897      } elseif ( is_multisite() && is_main_site() && is_main_network() ) {
 898          // Multisite stores site transients in the sitemeta table.
 899          $wpdb->query(
 900              $wpdb->prepare(
 901                  "DELETE a, b FROM {$wpdb->sitemeta} a, {$wpdb->sitemeta} b
 902                  WHERE a.meta_key LIKE %s
 903                  AND a.meta_key NOT LIKE %s
 904                  AND b.meta_key = CONCAT( '_site_transient_timeout_', SUBSTRING( a.meta_key, 17 ) )
 905                  AND b.meta_value < %d",
 906                  $wpdb->esc_like( '_site_transient_' ) . '%',
 907                  $wpdb->esc_like( '_site_transient_timeout_' ) . '%',
 908                  time()
 909              )
 910          );
 911      }
 912  }
 913  
 914  /**
 915   * Saves and restores user interface settings stored in a cookie.
 916   *
 917   * Checks if the current user-settings cookie is updated and stores it. When no
 918   * cookie exists (different browser used), adds the last saved cookie restoring
 919   * the settings.
 920   *
 921   * @since 2.7.0
 922   */
 923  function wp_user_settings() {
 924  
 925      if ( ! is_admin() || wp_doing_ajax() ) {
 926          return;
 927      }
 928  
 929      $user_id = get_current_user_id();
 930      if ( ! $user_id ) {
 931          return;
 932      }
 933  
 934      if ( ! is_user_member_of_blog() ) {
 935          return;
 936      }
 937  
 938      $settings = (string) get_user_option( 'user-settings', $user_id );
 939  
 940      if ( isset( $_COOKIE[ 'wp-settings-' . $user_id ] ) ) {
 941          $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE[ 'wp-settings-' . $user_id ] );
 942  
 943          // No change or both empty
 944          if ( $cookie == $settings ) {
 945              return;
 946          }
 947  
 948          $last_saved = (int) get_user_option( 'user-settings-time', $user_id );
 949          $current    = isset( $_COOKIE[ 'wp-settings-time-' . $user_id ] ) ? preg_replace( '/[^0-9]/', '', $_COOKIE[ 'wp-settings-time-' . $user_id ] ) : 0;
 950  
 951          // The cookie is newer than the saved value. Update the user_option and leave the cookie as-is
 952          if ( $current > $last_saved ) {
 953              update_user_option( $user_id, 'user-settings', $cookie, false );
 954              update_user_option( $user_id, 'user-settings-time', time() - 5, false );
 955              return;
 956          }
 957      }
 958  
 959      // The cookie is not set in the current browser or the saved value is newer.
 960      $secure = ( 'https' === parse_url( admin_url(), PHP_URL_SCHEME ) );
 961      setcookie( 'wp-settings-' . $user_id, $settings, time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure );
 962      setcookie( 'wp-settings-time-' . $user_id, time(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure );
 963      $_COOKIE[ 'wp-settings-' . $user_id ] = $settings;
 964  }
 965  
 966  /**
 967   * Retrieve user interface setting value based on setting name.
 968   *
 969   * @since 2.7.0
 970   *
 971   * @param string $name    The name of the setting.
 972   * @param string $default Optional default value to return when $name is not set.
 973   * @return mixed the last saved user setting or the default value/false if it doesn't exist.
 974   */
 975  function get_user_setting( $name, $default = false ) {
 976      $all_user_settings = get_all_user_settings();
 977  
 978      return isset( $all_user_settings[ $name ] ) ? $all_user_settings[ $name ] : $default;
 979  }
 980  
 981  /**
 982   * Add or update user interface setting.
 983   *
 984   * Both $name and $value can contain only ASCII letters, numbers, hyphens, and underscores.
 985   *
 986   * This function has to be used before any output has started as it calls setcookie().
 987   *
 988   * @since 2.8.0
 989   *
 990   * @param string $name  The name of the setting.
 991   * @param string $value The value for the setting.
 992   * @return bool|null True if set successfully, false if not. Null if the current user can't be established.
 993   */
 994  function set_user_setting( $name, $value ) {
 995      if ( headers_sent() ) {
 996          return false;
 997      }
 998  
 999      $all_user_settings          = get_all_user_settings();
1000      $all_user_settings[ $name ] = $value;
1001  
1002      return wp_set_all_user_settings( $all_user_settings );
1003  }
1004  
1005  /**
1006   * Delete user interface settings.
1007   *
1008   * Deleting settings would reset them to the defaults.
1009   *
1010   * This function has to be used before any output has started as it calls setcookie().
1011   *
1012   * @since 2.7.0
1013   *
1014   * @param string $names The name or array of names of the setting to be deleted.
1015   * @return bool|null True if deleted successfully, false if not. Null if the current user can't be established.
1016   */
1017  function delete_user_setting( $names ) {
1018      if ( headers_sent() ) {
1019          return false;
1020      }
1021  
1022      $all_user_settings = get_all_user_settings();
1023      $names             = (array) $names;
1024      $deleted           = false;
1025  
1026      foreach ( $names as $name ) {
1027          if ( isset( $all_user_settings[ $name ] ) ) {
1028              unset( $all_user_settings[ $name ] );
1029              $deleted = true;
1030          }
1031      }
1032  
1033      if ( $deleted ) {
1034          return wp_set_all_user_settings( $all_user_settings );
1035      }
1036  
1037      return false;
1038  }
1039  
1040  /**
1041   * Retrieve all user interface settings.
1042   *
1043   * @since 2.7.0
1044   *
1045   * @global array $_updated_user_settings
1046   *
1047   * @return array the last saved user settings or empty array.
1048   */
1049  function get_all_user_settings() {
1050      global $_updated_user_settings;
1051  
1052      $user_id = get_current_user_id();
1053      if ( ! $user_id ) {
1054          return array();
1055      }
1056  
1057      if ( isset( $_updated_user_settings ) && is_array( $_updated_user_settings ) ) {
1058          return $_updated_user_settings;
1059      }
1060  
1061      $user_settings = array();
1062  
1063      if ( isset( $_COOKIE[ 'wp-settings-' . $user_id ] ) ) {
1064          $cookie = preg_replace( '/[^A-Za-z0-9=&_-]/', '', $_COOKIE[ 'wp-settings-' . $user_id ] );
1065  
1066          if ( strpos( $cookie, '=' ) ) { // '=' cannot be 1st char
1067              parse_str( $cookie, $user_settings );
1068          }
1069      } else {
1070          $option = get_user_option( 'user-settings', $user_id );
1071  
1072          if ( $option && is_string( $option ) ) {
1073              parse_str( $option, $user_settings );
1074          }
1075      }
1076  
1077      $_updated_user_settings = $user_settings;
1078      return $user_settings;
1079  }
1080  
1081  /**
1082   * Private. Set all user interface settings.
1083   *
1084   * @since 2.8.0
1085   * @access private
1086   *
1087   * @global array $_updated_user_settings
1088   *
1089   * @param array $user_settings User settings.
1090   * @return bool|null False if the current user can't be found, null if the current
1091   *                   user is not a super admin or a member of the site, otherwise true.
1092   */
1093  function wp_set_all_user_settings( $user_settings ) {
1094      global $_updated_user_settings;
1095  
1096      $user_id = get_current_user_id();
1097      if ( ! $user_id ) {
1098          return false;
1099      }
1100  
1101      if ( ! is_user_member_of_blog() ) {
1102          return;
1103      }
1104  
1105      $settings = '';
1106      foreach ( $user_settings as $name => $value ) {
1107          $_name  = preg_replace( '/[^A-Za-z0-9_-]+/', '', $name );
1108          $_value = preg_replace( '/[^A-Za-z0-9_-]+/', '', $value );
1109  
1110          if ( ! empty( $_name ) ) {
1111              $settings .= $_name . '=' . $_value . '&';
1112          }
1113      }
1114  
1115      $settings = rtrim( $settings, '&' );
1116      parse_str( $settings, $_updated_user_settings );
1117  
1118      update_user_option( $user_id, 'user-settings', $settings, false );
1119      update_user_option( $user_id, 'user-settings-time', time(), false );
1120  
1121      return true;
1122  }
1123  
1124  /**
1125   * Delete the user settings of the current user.
1126   *
1127   * @since 2.7.0
1128   */
1129  function delete_all_user_settings() {
1130      $user_id = get_current_user_id();
1131      if ( ! $user_id ) {
1132          return;
1133      }
1134  
1135      update_user_option( $user_id, 'user-settings', '', false );
1136      setcookie( 'wp-settings-' . $user_id, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH );
1137  }
1138  
1139  /**
1140   * Retrieve an option value for the current network based on name of option.
1141   *
1142   * @since 2.8.0
1143   * @since 4.4.0 The `$use_cache` parameter was deprecated.
1144   * @since 4.4.0 Modified into wrapper for get_network_option()
1145   *
1146   * @see get_network_option()
1147   *
1148   * @param string $option     Name of option to retrieve. Expected to not be SQL-escaped.
1149   * @param mixed  $default    Optional value to return if option doesn't exist. Default false.
1150   * @param bool   $deprecated Whether to use cache. Multisite only. Always set to true.
1151   * @return mixed Value set for the option.
1152   */
1153  function get_site_option( $option, $default = false, $deprecated = true ) {
1154      return get_network_option( null, $option, $default );
1155  }
1156  
1157  /**
1158   * Add a new option for the current network.
1159   *
1160   * Existing options will not be updated. Note that prior to 3.3 this wasn't the case.
1161   *
1162   * @since 2.8.0
1163   * @since 4.4.0 Modified into wrapper for add_network_option()
1164   *
1165   * @see add_network_option()
1166   *
1167   * @param string $option Name of option to add. Expected to not be SQL-escaped.
1168   * @param mixed  $value  Option value, can be anything. Expected to not be SQL-escaped.
1169   * @return bool False if the option was not added. True if the option was added.
1170   */
1171  function add_site_option( $option, $value ) {
1172      return add_network_option( null, $option, $value );
1173  }
1174  
1175  /**
1176   * Removes a option by name for the current network.
1177   *
1178   * @since 2.8.0
1179   * @since 4.4.0 Modified into wrapper for delete_network_option()
1180   *
1181   * @see delete_network_option()
1182   *
1183   * @param string $option Name of option to remove. Expected to not be SQL-escaped.
1184   * @return bool True, if succeed. False, if failure.
1185   */
1186  function delete_site_option( $option ) {
1187      return delete_network_option( null, $option );
1188  }
1189  
1190  /**
1191   * Update the value of an option that was already added for the current network.
1192   *
1193   * @since 2.8.0
1194   * @since 4.4.0 Modified into wrapper for update_network_option()
1195   *
1196   * @see update_network_option()
1197   *
1198   * @param string $option Name of option. Expected to not be SQL-escaped.
1199   * @param mixed  $value  Option value. Expected to not be SQL-escaped.
1200   * @return bool False if value was not updated. True if value was updated.
1201   */
1202  function update_site_option( $option, $value ) {
1203      return update_network_option( null, $option, $value );
1204  }
1205  
1206  /**
1207   * Retrieve a network's option value based on the option name.
1208   *
1209   * @since 4.4.0
1210   *
1211   * @see get_option()
1212   *
1213   * @global wpdb $wpdb WordPress database abstraction object.
1214   *
1215   * @param int      $network_id ID of the network. Can be null to default to the current network ID.
1216   * @param string   $option     Name of option to retrieve. Expected to not be SQL-escaped.
1217   * @param mixed    $default    Optional. Value to return if the option doesn't exist. Default false.
1218   * @return mixed Value set for the option.
1219   */
1220  function get_network_option( $network_id, $option, $default = false ) {
1221      global $wpdb;
1222  
1223      if ( $network_id && ! is_numeric( $network_id ) ) {
1224          return false;
1225      }
1226  
1227      $network_id = (int) $network_id;
1228  
1229      // Fallback to the current network if a network ID is not specified.
1230      if ( ! $network_id ) {
1231          $network_id = get_current_network_id();
1232      }
1233  
1234      /**
1235       * Filters an existing network option before it is retrieved.
1236       *
1237       * The dynamic portion of the hook name, `$option`, refers to the option name.
1238       *
1239       * Passing a truthy value to the filter will effectively short-circuit retrieval,
1240       * returning the passed value instead.
1241       *
1242       * @since 2.9.0 As 'pre_site_option_' . $key
1243       * @since 3.0.0
1244       * @since 4.4.0 The `$option` parameter was added.
1245       * @since 4.7.0 The `$network_id` parameter was added.
1246       * @since 4.9.0 The `$default` parameter was added.
1247       *
1248       * @param mixed  $pre_option The value to return instead of the option value. This differs from
1249       *                           `$default`, which is used as the fallback value in the event the
1250       *                           option doesn't exist elsewhere in get_network_option(). Default
1251       *                           is false (to skip past the short-circuit).
1252       * @param string $option     Option name.
1253       * @param int    $network_id ID of the network.
1254       * @param mixed  $default    The fallback value to return if the option does not exist.
1255       *                           Default is false.
1256       */
1257      $pre = apply_filters( "pre_site_option_{$option}", false, $option, $network_id, $default );
1258  
1259      if ( false !== $pre ) {
1260          return $pre;
1261      }
1262  
1263      // prevent non-existent options from triggering multiple queries
1264      $notoptions_key = "$network_id:notoptions";
1265      $notoptions     = wp_cache_get( $notoptions_key, 'site-options' );
1266  
1267      if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
1268  
1269          /**
1270           * Filters a specific default network option.
1271           *
1272           * The dynamic portion of the hook name, `$option`, refers to the option name.
1273           *
1274           * @since 3.4.0
1275           * @since 4.4.0 The `$option` parameter was added.
1276           * @since 4.7.0 The `$network_id` parameter was added.
1277           *
1278           * @param mixed  $default    The value to return if the site option does not exist
1279           *                           in the database.
1280           * @param string $option     Option name.
1281           * @param int    $network_id ID of the network.
1282           */
1283          return apply_filters( "default_site_option_{$option}", $default, $option, $network_id );
1284      }
1285  
1286      if ( ! is_multisite() ) {
1287          /** This filter is documented in wp-includes/option.php */
1288          $default = apply_filters( 'default_site_option_' . $option, $default, $option, $network_id );
1289          $value   = get_option( $option, $default );
1290      } else {
1291          $cache_key = "$network_id:$option";
1292          $value     = wp_cache_get( $cache_key, 'site-options' );
1293  
1294          if ( ! isset( $value ) || false === $value ) {
1295              $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $network_id ) );
1296  
1297              // Has to be get_row instead of get_var because of funkiness with 0, false, null values
1298              if ( is_object( $row ) ) {
1299                  $value = $row->meta_value;
1300                  $value = maybe_unserialize( $value );
1301                  wp_cache_set( $cache_key, $value, 'site-options' );
1302              } else {
1303                  if ( ! is_array( $notoptions ) ) {
1304                      $notoptions = array();
1305                  }
1306                  $notoptions[ $option ] = true;
1307                  wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
1308  
1309                  /** This filter is documented in wp-includes/option.php */
1310                  $value = apply_filters( 'default_site_option_' . $option, $default, $option, $network_id );
1311              }
1312          }
1313      }
1314  
1315      if ( ! is_array( $notoptions ) ) {
1316          $notoptions = array();
1317          wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
1318      }
1319  
1320      /**
1321       * Filters the value of an existing network option.
1322       *
1323       * The dynamic portion of the hook name, `$option`, refers to the option name.
1324       *
1325       * @since 2.9.0 As 'site_option_' . $key
1326       * @since 3.0.0
1327       * @since 4.4.0 The `$option` parameter was added.
1328       * @since 4.7.0 The `$network_id` parameter was added.
1329       *
1330       * @param mixed  $value      Value of network option.
1331       * @param string $option     Option name.
1332       * @param int    $network_id ID of the network.
1333       */
1334      return apply_filters( "site_option_{$option}", $value, $option, $network_id );
1335  }
1336  
1337  /**
1338   * Add a new network option.
1339   *
1340   * Existing options will not be updated.
1341   *
1342   * @since 4.4.0
1343   *
1344   * @see add_option()
1345   *
1346   * @global wpdb $wpdb WordPress database abstraction object.
1347   *
1348   * @param int    $network_id ID of the network. Can be null to default to the current network ID.
1349   * @param string $option     Name of option to add. Expected to not be SQL-escaped.
1350   * @param mixed  $value      Option value, can be anything. Expected to not be SQL-escaped.
1351   * @return bool False if option was not added and true if option was added.
1352   */
1353  function add_network_option( $network_id, $option, $value ) {
1354      global $wpdb;
1355  
1356      if ( $network_id && ! is_numeric( $network_id ) ) {
1357          return false;
1358      }
1359  
1360      $network_id = (int) $network_id;
1361  
1362      // Fallback to the current network if a network ID is not specified.
1363      if ( ! $network_id ) {
1364          $network_id = get_current_network_id();
1365      }
1366  
1367      wp_protect_special_option( $option );
1368  
1369      /**
1370       * Filters the value of a specific network option before it is added.
1371       *
1372       * The dynamic portion of the hook name, `$option`, refers to the option name.
1373       *
1374       * @since 2.9.0 As 'pre_add_site_option_' . $key
1375       * @since 3.0.0
1376       * @since 4.4.0 The `$option` parameter was added.
1377       * @since 4.7.0 The `$network_id` parameter was added.
1378       *
1379       * @param mixed  $value      Value of network option.
1380       * @param string $option     Option name.
1381       * @param int    $network_id ID of the network.
1382       */
1383      $value = apply_filters( "pre_add_site_option_{$option}", $value, $option, $network_id );
1384  
1385      $notoptions_key = "$network_id:notoptions";
1386  
1387      if ( ! is_multisite() ) {
1388          $result = add_option( $option, $value, '', 'no' );
1389      } else {
1390          $cache_key = "$network_id:$option";
1391  
1392          // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
1393          $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
1394          if ( ! is_array( $notoptions ) || ! isset( $notoptions[ $option ] ) ) {
1395              if ( false !== get_network_option( $network_id, $option, false ) ) {
1396                  return false;
1397              }
1398          }
1399  
1400          $value = sanitize_option( $option, $value );
1401  
1402          $serialized_value = maybe_serialize( $value );
1403          $result           = $wpdb->insert(
1404              $wpdb->sitemeta,
1405              array(
1406                  'site_id'    => $network_id,
1407                  'meta_key'   => $option,
1408                  'meta_value' => $serialized_value,
1409              )
1410          );
1411  
1412          if ( ! $result ) {
1413              return false;
1414          }
1415  
1416          wp_cache_set( $cache_key, $value, 'site-options' );
1417  
1418          // This option exists now
1419          $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); // yes, again... we need it to be fresh
1420          if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
1421              unset( $notoptions[ $option ] );
1422              wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
1423          }
1424      }
1425  
1426      if ( $result ) {
1427  
1428          /**
1429           * Fires after a specific network option has been successfully added.
1430           *
1431           * The dynamic portion of the hook name, `$option`, refers to the option name.
1432           *
1433           * @since 2.9.0 As "add_site_option_{$key}"
1434           * @since 3.0.0
1435           * @since 4.7.0 The `$network_id` parameter was added.
1436           *
1437           * @param string $option     Name of the network option.
1438           * @param mixed  $value      Value of the network option.
1439           * @param int    $network_id ID of the network.
1440           */
1441          do_action( "add_site_option_{$option}", $option, $value, $network_id );
1442  
1443          /**
1444           * Fires after a network option has been successfully added.
1445           *
1446           * @since 3.0.0
1447           * @since 4.7.0 The `$network_id` parameter was added.
1448           *
1449           * @param string $option     Name of the network option.
1450           * @param mixed  $value      Value of the network option.
1451           * @param int    $network_id ID of the network.
1452           */
1453          do_action( 'add_site_option', $option, $value, $network_id );
1454  
1455          return true;
1456      }
1457  
1458      return false;
1459  }
1460  
1461  /**
1462   * Removes a network option by name.
1463   *
1464   * @since 4.4.0
1465   *
1466   * @see delete_option()
1467   *
1468   * @global wpdb $wpdb WordPress database abstraction object.
1469   *
1470   * @param int    $network_id ID of the network. Can be null to default to the current network ID.
1471   * @param string $option     Name of option to remove. Expected to not be SQL-escaped.
1472   * @return bool True, if succeed. False, if failure.
1473   */
1474  function delete_network_option( $network_id, $option ) {
1475      global $wpdb;
1476  
1477      if ( $network_id && ! is_numeric( $network_id ) ) {
1478          return false;
1479      }
1480  
1481      $network_id = (int) $network_id;
1482  
1483      // Fallback to the current network if a network ID is not specified.
1484      if ( ! $network_id ) {
1485          $network_id = get_current_network_id();
1486      }
1487  
1488      /**
1489       * Fires immediately before a specific network option is deleted.
1490       *
1491       * The dynamic portion of the hook name, `$option`, refers to the option name.
1492       *
1493       * @since 3.0.0
1494       * @since 4.4.0 The `$option` parameter was added.
1495       * @since 4.7.0 The `$network_id` parameter was added.
1496       *
1497       * @param string $option     Option name.
1498       * @param int    $network_id ID of the network.
1499       */
1500      do_action( "pre_delete_site_option_{$option}", $option, $network_id );
1501  
1502      if ( ! is_multisite() ) {
1503          $result = delete_option( $option );
1504      } else {
1505          $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $network_id ) );
1506          if ( is_null( $row ) || ! $row->meta_id ) {
1507              return false;
1508          }
1509          $cache_key = "$network_id:$option";
1510          wp_cache_delete( $cache_key, 'site-options' );
1511  
1512          $result = $wpdb->delete(
1513              $wpdb->sitemeta,
1514              array(
1515                  'meta_key' => $option,
1516                  'site_id'  => $network_id,
1517              )
1518          );
1519      }
1520  
1521      if ( $result ) {
1522  
1523          /**
1524           * Fires after a specific network option has been deleted.
1525           *
1526           * The dynamic portion of the hook name, `$option`, refers to the option name.
1527           *
1528           * @since 2.9.0 As "delete_site_option_{$key}"
1529           * @since 3.0.0
1530           * @since 4.7.0 The `$network_id` parameter was added.
1531           *
1532           * @param string $option     Name of the network option.
1533           * @param int    $network_id ID of the network.
1534           */
1535          do_action( "delete_site_option_{$option}", $option, $network_id );
1536  
1537          /**
1538           * Fires after a network option has been deleted.
1539           *
1540           * @since 3.0.0
1541           * @since 4.7.0 The `$network_id` parameter was added.
1542           *
1543           * @param string $option     Name of the network option.
1544           * @param int    $network_id ID of the network.
1545           */
1546          do_action( 'delete_site_option', $option, $network_id );
1547  
1548          return true;
1549      }
1550  
1551      return false;
1552  }
1553  
1554  /**
1555   * Update the value of a network option that was already added.
1556   *
1557   * @since 4.4.0
1558   *
1559   * @see update_option()
1560   *
1561   * @global wpdb $wpdb WordPress database abstraction object.
1562   *
1563   * @param int      $network_id ID of the network. Can be null to default to the current network ID.
1564   * @param string   $option     Name of option. Expected to not be SQL-escaped.
1565   * @param mixed    $value      Option value. Expected to not be SQL-escaped.
1566   * @return bool False if value was not updated and true if value was updated.
1567   */
1568  function update_network_option( $network_id, $option, $value ) {
1569      global $wpdb;
1570  
1571      if ( $network_id && ! is_numeric( $network_id ) ) {
1572          return false;
1573      }
1574  
1575      $network_id = (int) $network_id;
1576  
1577      // Fallback to the current network if a network ID is not specified.
1578      if ( ! $network_id ) {
1579          $network_id = get_current_network_id();
1580      }
1581  
1582      wp_protect_special_option( $option );
1583  
1584      $old_value = get_network_option( $network_id, $option, false );
1585  
1586      /**
1587       * Filters a specific network option before its value is updated.
1588       *
1589       * The dynamic portion of the hook name, `$option`, refers to the option name.
1590       *
1591       * @since 2.9.0 As 'pre_update_site_option_' . $key
1592       * @since 3.0.0
1593       * @since 4.4.0 The `$option` parameter was added.
1594       * @since 4.7.0 The `$network_id` parameter was added.
1595       *
1596       * @param mixed  $value      New value of the network option.
1597       * @param mixed  $old_value  Old value of the network option.
1598       * @param string $option     Option name.
1599       * @param int    $network_id ID of the network.
1600       */
1601      $value = apply_filters( "pre_update_site_option_{$option}", $value, $old_value, $option, $network_id );
1602  
1603      /*
1604       * If the new and old values are the same, no need to update.
1605       *
1606       * Unserialized values will be adequate in most cases. If the unserialized
1607       * data differs, the (maybe) serialized data is checked to avoid
1608       * unnecessary database calls for otherwise identical object instances.
1609       *
1610       * See https://core.trac.wordpress.org/ticket/44956
1611       */
1612      if ( $value === $old_value || maybe_serialize( $value ) === maybe_serialize( $old_value ) ) {
1613          return false;
1614      }
1615  
1616      if ( false === $old_value ) {
1617          return add_network_option( $network_id, $option, $value );
1618      }
1619  
1620      $notoptions_key = "$network_id:notoptions";
1621      $notoptions     = wp_cache_get( $notoptions_key, 'site-options' );
1622      if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
1623          unset( $notoptions[ $option ] );
1624          wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
1625      }
1626  
1627      if ( ! is_multisite() ) {
1628          $result = update_option( $option, $value, 'no' );
1629      } else {
1630          $value = sanitize_option( $option, $value );
1631  
1632          $serialized_value = maybe_serialize( $value );
1633          $result           = $wpdb->update(
1634              $wpdb->sitemeta,
1635              array( 'meta_value' => $serialized_value ),
1636              array(
1637                  'site_id'  => $network_id,
1638                  'meta_key' => $option,
1639              )
1640          );
1641  
1642          if ( $result ) {
1643              $cache_key = "$network_id:$option";
1644              wp_cache_set( $cache_key, $value, 'site-options' );
1645          }
1646      }
1647  
1648      if ( $result ) {
1649  
1650          /**
1651           * Fires after the value of a specific network option has been successfully updated.
1652           *
1653           * The dynamic portion of the hook name, `$option`, refers to the option name.
1654           *
1655           * @since 2.9.0 As "update_site_option_{$key}"
1656           * @since 3.0.0
1657           * @since 4.7.0 The `$network_id` parameter was added.
1658           *
1659           * @param string $option     Name of the network option.
1660           * @param mixed  $value      Current value of the network option.
1661           * @param mixed  $old_value  Old value of the network option.
1662           * @param int    $network_id ID of the network.
1663           */
1664          do_action( "update_site_option_{$option}", $option, $value, $old_value, $network_id );
1665  
1666          /**
1667           * Fires after the value of a network option has been successfully updated.
1668           *
1669           * @since 3.0.0
1670           * @since 4.7.0 The `$network_id` parameter was added.
1671           *
1672           * @param string $option     Name of the network option.
1673           * @param mixed  $value      Current value of the network option.
1674           * @param mixed  $old_value  Old value of the network option.
1675           * @param int    $network_id ID of the network.
1676           */
1677          do_action( 'update_site_option', $option, $value, $old_value, $network_id );
1678  
1679          return true;
1680      }
1681  
1682      return false;
1683  }
1684  
1685  /**
1686   * Delete a site transient.
1687   *
1688   * @since 2.9.0
1689   *
1690   * @param string $transient Transient name. Expected to not be SQL-escaped.
1691   * @return bool True if successful, false otherwise
1692   */
1693  function delete_site_transient( $transient ) {
1694  
1695      /**
1696       * Fires immediately before a specific site transient is deleted.
1697       *
1698       * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1699       *
1700       * @since 3.0.0
1701       *
1702       * @param string $transient Transient name.
1703       */
1704      do_action( "delete_site_transient_{$transient}", $transient );
1705  
1706      if ( wp_using_ext_object_cache() ) {
1707          $result = wp_cache_delete( $transient, 'site-transient' );
1708      } else {
1709          $option_timeout = '_site_transient_timeout_' . $transient;
1710          $option         = '_site_transient_' . $transient;
1711          $result         = delete_site_option( $option );
1712          if ( $result ) {
1713              delete_site_option( $option_timeout );
1714          }
1715      }
1716      if ( $result ) {
1717  
1718          /**
1719           * Fires after a transient is deleted.
1720           *
1721           * @since 3.0.0
1722           *
1723           * @param string $transient Deleted transient name.
1724           */
1725          do_action( 'deleted_site_transient', $transient );
1726      }
1727  
1728      return $result;
1729  }
1730  
1731  /**
1732   * Get the value of a site transient.
1733   *
1734   * If the transient does not exist, does not have a value, or has expired,
1735   * then the return value will be false.
1736   *
1737   * @since 2.9.0
1738   *
1739   * @see get_transient()
1740   *
1741   * @param string $transient Transient name. Expected to not be SQL-escaped.
1742   * @return mixed Value of transient.
1743   */
1744  function get_site_transient( $transient ) {
1745  
1746      /**
1747       * Filters the value of an existing site transient.
1748       *
1749       * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1750       *
1751       * Passing a truthy value to the filter will effectively short-circuit retrieval,
1752       * returning the passed value instead.
1753       *
1754       * @since 2.9.0
1755       * @since 4.4.0 The `$transient` parameter was added.
1756       *
1757       * @param mixed  $pre_site_transient The default value to return if the site transient does not exist.
1758       *                                   Any value other than false will short-circuit the retrieval
1759       *                                   of the transient, and return the returned value.
1760       * @param string $transient          Transient name.
1761       */
1762      $pre = apply_filters( "pre_site_transient_{$transient}", false, $transient );
1763  
1764      if ( false !== $pre ) {
1765          return $pre;
1766      }
1767  
1768      if ( wp_using_ext_object_cache() ) {
1769          $value = wp_cache_get( $transient, 'site-transient' );
1770      } else {
1771          // Core transients that do not have a timeout. Listed here so querying timeouts can be avoided.
1772          $no_timeout       = array( 'update_core', 'update_plugins', 'update_themes' );
1773          $transient_option = '_site_transient_' . $transient;
1774          if ( ! in_array( $transient, $no_timeout ) ) {
1775              $transient_timeout = '_site_transient_timeout_' . $transient;
1776              $timeout           = get_site_option( $transient_timeout );
1777              if ( false !== $timeout && $timeout < time() ) {
1778                  delete_site_option( $transient_option );
1779                  delete_site_option( $transient_timeout );
1780                  $value = false;
1781              }
1782          }
1783  
1784          if ( ! isset( $value ) ) {
1785              $value = get_site_option( $transient_option );
1786          }
1787      }
1788  
1789      /**
1790       * Filters the value of an existing site transient.
1791       *
1792       * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1793       *
1794       * @since 2.9.0
1795       * @since 4.4.0 The `$transient` parameter was added.
1796       *
1797       * @param mixed  $value     Value of site transient.
1798       * @param string $transient Transient name.
1799       */
1800      return apply_filters( "site_transient_{$transient}", $value, $transient );
1801  }
1802  
1803  /**
1804   * Set/update the value of a site transient.
1805   *
1806   * You do not need to serialize values, if the value needs to be serialize, then
1807   * it will be serialized before it is set.
1808   *
1809   * @since 2.9.0
1810   *
1811   * @see set_transient()
1812   *
1813   * @param string $transient  Transient name. Expected to not be SQL-escaped. Must be
1814   *                           167 characters or fewer in length.
1815   * @param mixed  $value      Transient value. Expected to not be SQL-escaped.
1816   * @param int    $expiration Optional. Time until expiration in seconds. Default 0 (no expiration).
1817   * @return bool False if value was not set and true if value was set.
1818   */
1819  function set_site_transient( $transient, $value, $expiration = 0 ) {
1820  
1821      /**
1822       * Filters the value of a specific site transient before it is set.
1823       *
1824       * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1825       *
1826       * @since 3.0.0
1827       * @since 4.4.0 The `$transient` parameter was added.
1828       *
1829       * @param mixed  $value     New value of site transient.
1830       * @param string $transient Transient name.
1831       */
1832      $value = apply_filters( "pre_set_site_transient_{$transient}", $value, $transient );
1833  
1834      $expiration = (int) $expiration;
1835  
1836      /**
1837       * Filters the expiration for a site transient before its value is set.
1838       *
1839       * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1840       *
1841       * @since 4.4.0
1842       *
1843       * @param int    $expiration Time until expiration in seconds. Use 0 for no expiration.
1844       * @param mixed  $value      New value of site transient.
1845       * @param string $transient  Transient name.
1846       */
1847      $expiration = apply_filters( "expiration_of_site_transient_{$transient}", $expiration, $value, $transient );
1848  
1849      if ( wp_using_ext_object_cache() ) {
1850          $result = wp_cache_set( $transient, $value, 'site-transient', $expiration );
1851      } else {
1852          $transient_timeout = '_site_transient_timeout_' . $transient;
1853          $option            = '_site_transient_' . $transient;
1854          if ( false === get_site_option( $option ) ) {
1855              if ( $expiration ) {
1856                  add_site_option( $transient_timeout, time() + $expiration );
1857              }
1858              $result = add_site_option( $option, $value );
1859          } else {
1860              if ( $expiration ) {
1861                  update_site_option( $transient_timeout, time() + $expiration );
1862              }
1863              $result = update_site_option( $option, $value );
1864          }
1865      }
1866      if ( $result ) {
1867  
1868          /**
1869           * Fires after the value for a specific site transient has been set.
1870           *
1871           * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1872           *
1873           * @since 3.0.0
1874           * @since 4.4.0 The `$transient` parameter was added
1875           *
1876           * @param mixed  $value      Site transient value.
1877           * @param int    $expiration Time until expiration in seconds.
1878           * @param string $transient  Transient name.
1879           */
1880          do_action( "set_site_transient_{$transient}", $value, $expiration, $transient );
1881  
1882          /**
1883           * Fires after the value for a site transient has been set.
1884           *
1885           * @since 3.0.0
1886           *
1887           * @param string $transient  The name of the site transient.
1888           * @param mixed  $value      Site transient value.
1889           * @param int    $expiration Time until expiration in seconds.
1890           */
1891          do_action( 'setted_site_transient', $transient, $value, $expiration );
1892      }
1893      return $result;
1894  }
1895  
1896  /**
1897   * Register default settings available in WordPress.
1898   *
1899   * The settings registered here are primarily useful for the REST API, so this
1900   * does not encompass all settings available in WordPress.
1901   *
1902   * @since 4.7.0
1903   */
1904  function register_initial_settings() {
1905      register_setting(
1906          'general',
1907          'blogname',
1908          array(
1909              'show_in_rest' => array(
1910                  'name' => 'title',
1911              ),
1912              'type'         => 'string',
1913              'description'  => __( 'Site title.' ),
1914          )
1915      );
1916  
1917      register_setting(
1918          'general',
1919          'blogdescription',
1920          array(
1921              'show_in_rest' => array(
1922                  'name' => 'description',
1923              ),
1924              'type'         => 'string',
1925              'description'  => __( 'Site tagline.' ),
1926          )
1927      );
1928  
1929      if ( ! is_multisite() ) {
1930          register_setting(
1931              'general',
1932              'siteurl',
1933              array(
1934                  'show_in_rest' => array(
1935                      'name'   => 'url',
1936                      'schema' => array(
1937                          'format' => 'uri',
1938                      ),
1939                  ),
1940                  'type'         => 'string',
1941                  'description'  => __( 'Site URL.' ),
1942              )
1943          );
1944      }
1945  
1946      if ( ! is_multisite() ) {
1947          register_setting(
1948              'general',
1949              'admin_email',
1950              array(
1951                  'show_in_rest' => array(
1952                      'name'   => 'email',
1953                      'schema' => array(
1954                          'format' => 'email',
1955                      ),
1956                  ),
1957                  'type'         => 'string',
1958                  'description'  => __( 'This address is used for admin purposes, like new user notification.' ),
1959              )
1960          );
1961      }
1962  
1963      register_setting(
1964          'general',
1965          'timezone_string',
1966          array(
1967              'show_in_rest' => array(
1968                  'name' => 'timezone',
1969              ),
1970              'type'         => 'string',
1971              'description'  => __( 'A city in the same timezone as you.' ),
1972          )
1973      );
1974  
1975      register_setting(
1976          'general',
1977          'date_format',
1978          array(
1979              'show_in_rest' => true,
1980              'type'         => 'string',
1981              'description'  => __( 'A date format for all date strings.' ),
1982          )
1983      );
1984  
1985      register_setting(
1986          'general',
1987          'time_format',
1988          array(
1989              'show_in_rest' => true,
1990              'type'         => 'string',
1991              'description'  => __( 'A time format for all time strings.' ),
1992          )
1993      );
1994  
1995      register_setting(
1996          'general',
1997          'start_of_week',
1998          array(
1999              'show_in_rest' => true,
2000              'type'         => 'integer',
2001              'description'  => __( 'A day number of the week that the week should start on.' ),
2002          )
2003      );
2004  
2005      register_setting(
2006          'general',
2007          'WPLANG',
2008          array(
2009              'show_in_rest' => array(
2010                  'name' => 'language',
2011              ),
2012              'type'         => 'string',
2013              'description'  => __( 'WordPress locale code.' ),
2014              'default'      => 'en_US',
2015          )
2016      );
2017  
2018      register_setting(
2019          'writing',
2020          'use_smilies',
2021          array(
2022              'show_in_rest' => true,
2023              'type'         => 'boolean',
2024              'description'  => __( 'Convert emoticons like :-) and :-P to graphics on display.' ),
2025              'default'      => true,
2026          )
2027      );
2028  
2029      register_setting(
2030          'writing',
2031          'default_category',
2032          array(
2033              'show_in_rest' => true,
2034              'type'         => 'integer',
2035              'description'  => __( 'Default post category.' ),
2036          )
2037      );
2038  
2039      register_setting(
2040          'writing',
2041          'default_post_format',
2042          array(
2043              'show_in_rest' => true,
2044              'type'         => 'string',
2045              'description'  => __( 'Default post format.' ),
2046          )
2047      );
2048  
2049      register_setting(
2050          'reading',
2051          'posts_per_page',
2052          array(
2053              'show_in_rest' => true,
2054              'type'         => 'integer',
2055              'description'  => __( 'Blog pages show at most.' ),
2056              'default'      => 10,
2057          )
2058      );
2059  
2060      register_setting(
2061          'discussion',
2062          'default_ping_status',
2063          array(
2064              'show_in_rest' => array(
2065                  'schema' => array(
2066                      'enum' => array( 'open', 'closed' ),
2067                  ),
2068              ),
2069              'type'         => 'string',
2070              'description'  => __( 'Allow link notifications from other blogs (pingbacks and trackbacks) on new articles.' ),
2071          )
2072      );
2073  
2074      register_setting(
2075          'discussion',
2076          'default_comment_status',
2077          array(
2078              'show_in_rest' => array(
2079                  'schema' => array(
2080                      'enum' => array( 'open', 'closed' ),
2081                  ),
2082              ),
2083              'type'         => 'string',
2084              'description'  => __( 'Allow people to submit comments on new posts.' ),
2085          )
2086      );
2087  }
2088  
2089  /**
2090   * Register a setting and its data.
2091   *
2092   * @since 2.7.0
2093   * @since 4.7.0 `$args` can be passed to set flags on the setting, similar to `register_meta()`.
2094   *
2095   * @global array $new_whitelist_options
2096   * @global array $wp_registered_settings
2097   *
2098   * @param string $option_group A settings group name. Should correspond to a whitelisted option key name.
2099   *  Default whitelisted option key names include "general," "discussion," and "reading," among others.
2100   * @param string $option_name The name of an option to sanitize and save.
2101   * @param array  $args {
2102   *     Data used to describe the setting when registered.
2103   *
2104   *     @type string   $type              The type of data associated with this setting.
2105   *                                       Valid values are 'string', 'boolean', 'integer', and 'number'.
2106   *     @type string   $description       A description of the data attached to this setting.
2107   *     @type callable $sanitize_callback A callback function that sanitizes the option's value.
2108   *     @type bool     $show_in_rest      Whether data associated with this setting should be included in the REST API.
2109   *     @type mixed    $default           Default value when calling `get_option()`.
2110   * }
2111   */
2112  function register_setting( $option_group, $option_name, $args = array() ) {
2113      global $new_whitelist_options, $wp_registered_settings;
2114  
2115      $defaults = array(
2116          'type'              => 'string',
2117          'group'             => $option_group,
2118          'description'       => '',
2119          'sanitize_callback' => null,
2120          'show_in_rest'      => false,
2121      );
2122  
2123      // Back-compat: old sanitize callback is added.
2124      if ( is_callable( $args ) ) {
2125          $args = array(
2126              'sanitize_callback' => $args,
2127          );
2128      }
2129  
2130      /**
2131       * Filters the registration arguments when registering a setting.
2132       *
2133       * @since 4.7.0
2134       *
2135       * @param array  $args         Array of setting registration arguments.
2136       * @param array  $defaults     Array of default arguments.
2137       * @param string $option_group Setting group.
2138       * @param string $option_name  Setting name.
2139       */
2140      $args = apply_filters( 'register_setting_args', $args, $defaults, $option_group, $option_name );
2141      $args = wp_parse_args( $args, $defaults );
2142  
2143      if ( ! is_array( $wp_registered_settings ) ) {
2144          $wp_registered_settings = array();
2145      }
2146  
2147      if ( 'misc' == $option_group ) {
2148          _deprecated_argument(
2149              __FUNCTION__,
2150              '3.0.0',
2151              sprintf(
2152                  /* translators: %s: misc */
2153                  __( 'The "%s" options group has been removed. Use another settings group.' ),
2154                  'misc'
2155              )
2156          );
2157          $option_group = 'general';
2158      }
2159  
2160      if ( 'privacy' == $option_group ) {
2161          _deprecated_argument(
2162              __FUNCTION__,
2163              '3.5.0',
2164              sprintf(
2165                  /* translators: %s: privacy */
2166                  __( 'The "%s" options group has been removed. Use another settings group.' ),
2167                  'privacy'
2168              )
2169          );
2170          $option_group = 'reading';
2171      }
2172  
2173      $new_whitelist_options[ $option_group ][] = $option_name;
2174      if ( ! empty( $args['sanitize_callback'] ) ) {
2175          add_filter( "sanitize_option_{$option_name}", $args['sanitize_callback'] );
2176      }
2177      if ( array_key_exists( 'default', $args ) ) {
2178          add_filter( "default_option_{$option_name}", 'filter_default_option', 10, 3 );
2179      }
2180  
2181      $wp_registered_settings[ $option_name ] = $args;
2182  }
2183  
2184  /**
2185   * Unregister a setting.
2186   *
2187   * @since 2.7.0
2188   * @since 4.7.0 `$sanitize_callback` was deprecated. The callback from `register_setting()` is now used instead.
2189   *
2190   * @global array $new_whitelist_options
2191   * @global array $wp_registered_settings
2192   *
2193   * @param string   $option_group      The settings group name used during registration.
2194   * @param string   $option_name       The name of the option to unregister.
2195   * @param callable $deprecated        Deprecated.
2196   */
2197  function unregister_setting( $option_group, $option_name, $deprecated = '' ) {
2198      global $new_whitelist_options, $wp_registered_settings;
2199  
2200      if ( 'misc' == $option_group ) {
2201          _deprecated_argument(
2202              __FUNCTION__,
2203              '3.0.0',
2204              sprintf(
2205                  /* translators: %s: misc */
2206                  __( 'The "%s" options group has been removed. Use another settings group.' ),
2207                  'misc'
2208              )
2209          );
2210          $option_group = 'general';
2211      }
2212  
2213      if ( 'privacy' == $option_group ) {
2214          _deprecated_argument(
2215              __FUNCTION__,
2216              '3.5.0',
2217              sprintf(
2218                  /* translators: %s: privacy */
2219                  __( 'The "%s" options group has been removed. Use another settings group.' ),
2220                  'privacy'
2221              )
2222          );
2223          $option_group = 'reading';
2224      }
2225  
2226      $pos = array_search( $option_name, (array) $new_whitelist_options[ $option_group ] );
2227      if ( $pos !== false ) {
2228          unset( $new_whitelist_options[ $option_group ][ $pos ] );
2229      }
2230      if ( '' !== $deprecated ) {
2231          _deprecated_argument(
2232              __FUNCTION__,
2233              '4.7.0',
2234              sprintf(
2235                  /* translators: 1: $sanitize_callback, 2: register_setting() */
2236                  __( '%1$s is deprecated. The callback from %2$s is used instead.' ),
2237                  '<code>$sanitize_callback</code>',
2238                  '<code>register_setting()</code>'
2239              )
2240          );
2241          remove_filter( "sanitize_option_{$option_name}", $deprecated );
2242      }
2243  
2244      if ( isset( $wp_registered_settings[ $option_name ] ) ) {
2245          // Remove the sanitize callback if one was set during registration.
2246          if ( ! empty( $wp_registered_settings[ $option_name ]['sanitize_callback'] ) ) {
2247              remove_filter( "sanitize_option_{$option_name}", $wp_registered_settings[ $option_name ]['sanitize_callback'] );
2248          }
2249  
2250          // Remove the default filter if a default was provided during registration.
2251          if ( array_key_exists( 'default', $wp_registered_settings[ $option_name ] ) ) {
2252              remove_filter( "default_option_{$option_name}", 'filter_default_option', 10 );
2253          }
2254  
2255          unset( $wp_registered_settings[ $option_name ] );
2256      }
2257  }
2258  
2259  /**
2260   * Retrieves an array of registered settings.
2261   *
2262   * @since 4.7.0
2263   *
2264   * @global array $wp_registered_settings
2265   *
2266   * @return array List of registered settings, keyed by option name.
2267   */
2268  function get_registered_settings() {
2269      global $wp_registered_settings;
2270  
2271      if ( ! is_array( $wp_registered_settings ) ) {
2272          return array();
2273      }
2274  
2275      return $wp_registered_settings;
2276  }
2277  
2278  /**
2279   * Filter the default value for the option.
2280   *
2281   * For settings which register a default setting in `register_setting()`, this
2282   * function is added as a filter to `default_option_{$option}`.
2283   *
2284   * @since 4.7.0
2285   *
2286   * @param mixed $default Existing default value to return.
2287   * @param string $option Option name.
2288   * @param bool $passed_default Was `get_option()` passed a default value?
2289   * @return mixed Filtered default value.
2290   */
2291  function filter_default_option( $default, $option, $passed_default ) {
2292      if ( $passed_default ) {
2293          return $default;
2294      }
2295  
2296      $registered = get_registered_settings();
2297      if ( empty( $registered[ $option ] ) ) {
2298          return $default;
2299      }
2300  
2301      return $registered[ $option ]['default'];
2302  }


Generated: Sat Nov 23 20:47:33 2019 Cross-referenced by PHPXref 0.7