[ Index ]

WordPress 5.4.1

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

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   * Protects 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   * Prints 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.3.1 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   * Updates 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,
 283   * then it will be serialized before it is inserted into the database.
 284   * Remember, resources cannot be serialized or added as an option.
 285   *
 286   * If the option does not exist, it will be created.
 287  
 288   * This function is designed to work with or without a logged-in user. In terms of security,
 289   * plugin developers should check the current user's capabilities before updating any options.
 290   *
 291   * @since 1.0.0
 292   * @since 4.2.0 The `$autoload` parameter was added.
 293   *
 294   * @global wpdb $wpdb WordPress database abstraction object.
 295   *
 296   * @param string      $option   Option name. Expected to not be SQL-escaped.
 297   * @param mixed       $value    Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
 298   * @param string|bool $autoload Optional. Whether to load the option when WordPress starts up. For existing options,
 299   *                              `$autoload` can only be updated using `update_option()` if `$value` is also changed.
 300   *                              Accepts 'yes'|true to enable or 'no'|false to disable. For non-existent options,
 301   *                              the default value is 'yes'. Default null.
 302   * @return bool False if value was not updated and true if value was updated.
 303   */
 304  function update_option( $option, $value, $autoload = null ) {
 305      global $wpdb;
 306  
 307      $option = trim( $option );
 308      if ( empty( $option ) ) {
 309          return false;
 310      }
 311  
 312      wp_protect_special_option( $option );
 313  
 314      if ( is_object( $value ) ) {
 315          $value = clone $value;
 316      }
 317  
 318      $value     = sanitize_option( $option, $value );
 319      $old_value = get_option( $option );
 320  
 321      /**
 322       * Filters a specific option before its value is (maybe) serialized and updated.
 323       *
 324       * The dynamic portion of the hook name, `$option`, refers to the option name.
 325       *
 326       * @since 2.6.0
 327       * @since 4.4.0 The `$option` parameter was added.
 328       *
 329       * @param mixed  $value     The new, unserialized option value.
 330       * @param mixed  $old_value The old option value.
 331       * @param string $option    Option name.
 332       */
 333      $value = apply_filters( "pre_update_option_{$option}", $value, $old_value, $option );
 334  
 335      /**
 336       * Filters an option before its value is (maybe) serialized and updated.
 337       *
 338       * @since 3.9.0
 339       *
 340       * @param mixed  $value     The new, unserialized option value.
 341       * @param string $option    Name of the option.
 342       * @param mixed  $old_value The old option value.
 343       */
 344      $value = apply_filters( 'pre_update_option', $value, $option, $old_value );
 345  
 346      /*
 347       * If the new and old values are the same, no need to update.
 348       *
 349       * Unserialized values will be adequate in most cases. If the unserialized
 350       * data differs, the (maybe) serialized data is checked to avoid
 351       * unnecessary database calls for otherwise identical object instances.
 352       *
 353       * See https://core.trac.wordpress.org/ticket/38903
 354       */
 355      if ( $value === $old_value || maybe_serialize( $value ) === maybe_serialize( $old_value ) ) {
 356          return false;
 357      }
 358  
 359      /** This filter is documented in wp-includes/option.php */
 360      if ( apply_filters( "default_option_{$option}", false, $option, false ) === $old_value ) {
 361          // Default setting for new options is 'yes'.
 362          if ( null === $autoload ) {
 363              $autoload = 'yes';
 364          }
 365  
 366          return add_option( $option, $value, '', $autoload );
 367      }
 368  
 369      $serialized_value = maybe_serialize( $value );
 370  
 371      /**
 372       * Fires immediately before an option value is updated.
 373       *
 374       * @since 2.9.0
 375       *
 376       * @param string $option    Name of the option to update.
 377       * @param mixed  $old_value The old option value.
 378       * @param mixed  $value     The new option value.
 379       */
 380      do_action( 'update_option', $option, $old_value, $value );
 381  
 382      $update_args = array(
 383          'option_value' => $serialized_value,
 384      );
 385  
 386      if ( null !== $autoload ) {
 387          $update_args['autoload'] = ( 'no' === $autoload || false === $autoload ) ? 'no' : 'yes';
 388      }
 389  
 390      $result = $wpdb->update( $wpdb->options, $update_args, array( 'option_name' => $option ) );
 391      if ( ! $result ) {
 392          return false;
 393      }
 394  
 395      $notoptions = wp_cache_get( 'notoptions', 'options' );
 396      if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
 397          unset( $notoptions[ $option ] );
 398          wp_cache_set( 'notoptions', $notoptions, 'options' );
 399      }
 400  
 401      if ( ! wp_installing() ) {
 402          $alloptions = wp_load_alloptions( true );
 403          if ( isset( $alloptions[ $option ] ) ) {
 404              $alloptions[ $option ] = $serialized_value;
 405              wp_cache_set( 'alloptions', $alloptions, 'options' );
 406          } else {
 407              wp_cache_set( $option, $serialized_value, 'options' );
 408          }
 409      }
 410  
 411      /**
 412       * Fires after the value of a specific option has been successfully updated.
 413       *
 414       * The dynamic portion of the hook name, `$option`, refers to the option name.
 415       *
 416       * @since 2.0.1
 417       * @since 4.4.0 The `$option` parameter was added.
 418       *
 419       * @param mixed  $old_value The old option value.
 420       * @param mixed  $value     The new option value.
 421       * @param string $option    Option name.
 422       */
 423      do_action( "update_option_{$option}", $old_value, $value, $option );
 424  
 425      /**
 426       * Fires after the value of an option has been successfully updated.
 427       *
 428       * @since 2.9.0
 429       *
 430       * @param string $option    Name of the updated option.
 431       * @param mixed  $old_value The old option value.
 432       * @param mixed  $value     The new option value.
 433       */
 434      do_action( 'updated_option', $option, $old_value, $value );
 435      return true;
 436  }
 437  
 438  /**
 439   * Adds a new option.
 440   *
 441   * You do not need to serialize values. If the value needs to be serialized,
 442   * then it will be serialized before it is inserted into the database.
 443   * Remember, resources cannot be serialized or added as an option.
 444   *
 445   * You can create options without values and then update the values later.
 446   * Existing options will not be updated and checks are performed to ensure that you
 447   * aren't adding a protected WordPress option. Care should be taken to not name
 448   * options the same as the ones which are protected.
 449   *
 450   * @since 1.0.0
 451   *
 452   * @global wpdb $wpdb WordPress database abstraction object.
 453   *
 454   * @param string         $option      Name of option to add. Expected to not be SQL-escaped.
 455   * @param mixed          $value       Optional. Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
 456   * @param string         $deprecated  Optional. Description. Not used anymore.
 457   * @param string|bool    $autoload    Optional. Whether to load the option when WordPress starts up.
 458   *                                    Default is enabled. Accepts 'no' to disable for legacy reasons.
 459   * @return bool False if option was not added and true if option was added.
 460   */
 461  function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) {
 462      global $wpdb;
 463  
 464      if ( ! empty( $deprecated ) ) {
 465          _deprecated_argument( __FUNCTION__, '2.3.0' );
 466      }
 467  
 468      $option = trim( $option );
 469      if ( empty( $option ) ) {
 470          return false;
 471      }
 472  
 473      wp_protect_special_option( $option );
 474  
 475      if ( is_object( $value ) ) {
 476          $value = clone $value;
 477      }
 478  
 479      $value = sanitize_option( $option, $value );
 480  
 481      // Make sure the option doesn't already exist.
 482      // 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   * Deletes 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   * Retrieves 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   * Sets/updates the value of a transient.
 744   *
 745   * You do not need to serialize values. If the value needs to be serialized,
 746   * then 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          // Single site 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   * Retrieves 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   * Adds or updates 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   * Deletes 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   * Retrieves 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. Sets 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   * Deletes 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   * Adds 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   * Updates 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   * Retrieves 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   * Adds 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.
1393          // We can check the 'notoptions' cache before we ask for a DB query.
1394          $notoptions = wp_cache_get( $notoptions_key, 'site-options' );
1395          if ( ! is_array( $notoptions ) || ! isset( $notoptions[ $option ] ) ) {
1396              if ( false !== get_network_option( $network_id, $option, false ) ) {
1397                  return false;
1398              }
1399          }
1400  
1401          $value = sanitize_option( $option, $value );
1402  
1403          $serialized_value = maybe_serialize( $value );
1404          $result           = $wpdb->insert(
1405              $wpdb->sitemeta,
1406              array(
1407                  'site_id'    => $network_id,
1408                  'meta_key'   => $option,
1409                  'meta_value' => $serialized_value,
1410              )
1411          );
1412  
1413          if ( ! $result ) {
1414              return false;
1415          }
1416  
1417          wp_cache_set( $cache_key, $value, 'site-options' );
1418  
1419          // This option exists now.
1420          $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); // Yes, again... we need it to be fresh.
1421          if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
1422              unset( $notoptions[ $option ] );
1423              wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
1424          }
1425      }
1426  
1427      if ( $result ) {
1428  
1429          /**
1430           * Fires after a specific network option has been successfully added.
1431           *
1432           * The dynamic portion of the hook name, `$option`, refers to the option name.
1433           *
1434           * @since 2.9.0 As "add_site_option_{$key}"
1435           * @since 3.0.0
1436           * @since 4.7.0 The `$network_id` parameter was added.
1437           *
1438           * @param string $option     Name of the network option.
1439           * @param mixed  $value      Value of the network option.
1440           * @param int    $network_id ID of the network.
1441           */
1442          do_action( "add_site_option_{$option}", $option, $value, $network_id );
1443  
1444          /**
1445           * Fires after a network option has been successfully added.
1446           *
1447           * @since 3.0.0
1448           * @since 4.7.0 The `$network_id` parameter was added.
1449           *
1450           * @param string $option     Name of the network option.
1451           * @param mixed  $value      Value of the network option.
1452           * @param int    $network_id ID of the network.
1453           */
1454          do_action( 'add_site_option', $option, $value, $network_id );
1455  
1456          return true;
1457      }
1458  
1459      return false;
1460  }
1461  
1462  /**
1463   * Removes a network option by name.
1464   *
1465   * @since 4.4.0
1466   *
1467   * @see delete_option()
1468   *
1469   * @global wpdb $wpdb WordPress database abstraction object.
1470   *
1471   * @param int    $network_id ID of the network. Can be null to default to the current network ID.
1472   * @param string $option     Name of option to remove. Expected to not be SQL-escaped.
1473   * @return bool True, if succeed. False, if failure.
1474   */
1475  function delete_network_option( $network_id, $option ) {
1476      global $wpdb;
1477  
1478      if ( $network_id && ! is_numeric( $network_id ) ) {
1479          return false;
1480      }
1481  
1482      $network_id = (int) $network_id;
1483  
1484      // Fallback to the current network if a network ID is not specified.
1485      if ( ! $network_id ) {
1486          $network_id = get_current_network_id();
1487      }
1488  
1489      /**
1490       * Fires immediately before a specific network option is deleted.
1491       *
1492       * The dynamic portion of the hook name, `$option`, refers to the option name.
1493       *
1494       * @since 3.0.0
1495       * @since 4.4.0 The `$option` parameter was added.
1496       * @since 4.7.0 The `$network_id` parameter was added.
1497       *
1498       * @param string $option     Option name.
1499       * @param int    $network_id ID of the network.
1500       */
1501      do_action( "pre_delete_site_option_{$option}", $option, $network_id );
1502  
1503      if ( ! is_multisite() ) {
1504          $result = delete_option( $option );
1505      } else {
1506          $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $network_id ) );
1507          if ( is_null( $row ) || ! $row->meta_id ) {
1508              return false;
1509          }
1510          $cache_key = "$network_id:$option";
1511          wp_cache_delete( $cache_key, 'site-options' );
1512  
1513          $result = $wpdb->delete(
1514              $wpdb->sitemeta,
1515              array(
1516                  'meta_key' => $option,
1517                  'site_id'  => $network_id,
1518              )
1519          );
1520      }
1521  
1522      if ( $result ) {
1523  
1524          /**
1525           * Fires after a specific network option has been deleted.
1526           *
1527           * The dynamic portion of the hook name, `$option`, refers to the option name.
1528           *
1529           * @since 2.9.0 As "delete_site_option_{$key}"
1530           * @since 3.0.0
1531           * @since 4.7.0 The `$network_id` parameter was added.
1532           *
1533           * @param string $option     Name of the network option.
1534           * @param int    $network_id ID of the network.
1535           */
1536          do_action( "delete_site_option_{$option}", $option, $network_id );
1537  
1538          /**
1539           * Fires after a network option has been deleted.
1540           *
1541           * @since 3.0.0
1542           * @since 4.7.0 The `$network_id` parameter was added.
1543           *
1544           * @param string $option     Name of the network option.
1545           * @param int    $network_id ID of the network.
1546           */
1547          do_action( 'delete_site_option', $option, $network_id );
1548  
1549          return true;
1550      }
1551  
1552      return false;
1553  }
1554  
1555  /**
1556   * Updates the value of a network option that was already added.
1557   *
1558   * @since 4.4.0
1559   *
1560   * @see update_option()
1561   *
1562   * @global wpdb $wpdb WordPress database abstraction object.
1563   *
1564   * @param int      $network_id ID of the network. Can be null to default to the current network ID.
1565   * @param string   $option     Name of option. Expected to not be SQL-escaped.
1566   * @param mixed    $value      Option value. Expected to not be SQL-escaped.
1567   * @return bool False if value was not updated and true if value was updated.
1568   */
1569  function update_network_option( $network_id, $option, $value ) {
1570      global $wpdb;
1571  
1572      if ( $network_id && ! is_numeric( $network_id ) ) {
1573          return false;
1574      }
1575  
1576      $network_id = (int) $network_id;
1577  
1578      // Fallback to the current network if a network ID is not specified.
1579      if ( ! $network_id ) {
1580          $network_id = get_current_network_id();
1581      }
1582  
1583      wp_protect_special_option( $option );
1584  
1585      $old_value = get_network_option( $network_id, $option, false );
1586  
1587      /**
1588       * Filters a specific network option before its value is updated.
1589       *
1590       * The dynamic portion of the hook name, `$option`, refers to the option name.
1591       *
1592       * @since 2.9.0 As 'pre_update_site_option_' . $key
1593       * @since 3.0.0
1594       * @since 4.4.0 The `$option` parameter was added.
1595       * @since 4.7.0 The `$network_id` parameter was added.
1596       *
1597       * @param mixed  $value      New value of the network option.
1598       * @param mixed  $old_value  Old value of the network option.
1599       * @param string $option     Option name.
1600       * @param int    $network_id ID of the network.
1601       */
1602      $value = apply_filters( "pre_update_site_option_{$option}", $value, $old_value, $option, $network_id );
1603  
1604      /*
1605       * If the new and old values are the same, no need to update.
1606       *
1607       * Unserialized values will be adequate in most cases. If the unserialized
1608       * data differs, the (maybe) serialized data is checked to avoid
1609       * unnecessary database calls for otherwise identical object instances.
1610       *
1611       * See https://core.trac.wordpress.org/ticket/44956
1612       */
1613      if ( $value === $old_value || maybe_serialize( $value ) === maybe_serialize( $old_value ) ) {
1614          return false;
1615      }
1616  
1617      if ( false === $old_value ) {
1618          return add_network_option( $network_id, $option, $value );
1619      }
1620  
1621      $notoptions_key = "$network_id:notoptions";
1622      $notoptions     = wp_cache_get( $notoptions_key, 'site-options' );
1623      if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
1624          unset( $notoptions[ $option ] );
1625          wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
1626      }
1627  
1628      if ( ! is_multisite() ) {
1629          $result = update_option( $option, $value, 'no' );
1630      } else {
1631          $value = sanitize_option( $option, $value );
1632  
1633          $serialized_value = maybe_serialize( $value );
1634          $result           = $wpdb->update(
1635              $wpdb->sitemeta,
1636              array( 'meta_value' => $serialized_value ),
1637              array(
1638                  'site_id'  => $network_id,
1639                  'meta_key' => $option,
1640              )
1641          );
1642  
1643          if ( $result ) {
1644              $cache_key = "$network_id:$option";
1645              wp_cache_set( $cache_key, $value, 'site-options' );
1646          }
1647      }
1648  
1649      if ( $result ) {
1650  
1651          /**
1652           * Fires after the value of a specific network option has been successfully updated.
1653           *
1654           * The dynamic portion of the hook name, `$option`, refers to the option name.
1655           *
1656           * @since 2.9.0 As "update_site_option_{$key}"
1657           * @since 3.0.0
1658           * @since 4.7.0 The `$network_id` parameter was added.
1659           *
1660           * @param string $option     Name of the network option.
1661           * @param mixed  $value      Current value of the network option.
1662           * @param mixed  $old_value  Old value of the network option.
1663           * @param int    $network_id ID of the network.
1664           */
1665          do_action( "update_site_option_{$option}", $option, $value, $old_value, $network_id );
1666  
1667          /**
1668           * Fires after the value of a network option has been successfully updated.
1669           *
1670           * @since 3.0.0
1671           * @since 4.7.0 The `$network_id` parameter was added.
1672           *
1673           * @param string $option     Name of the network option.
1674           * @param mixed  $value      Current value of the network option.
1675           * @param mixed  $old_value  Old value of the network option.
1676           * @param int    $network_id ID of the network.
1677           */
1678          do_action( 'update_site_option', $option, $value, $old_value, $network_id );
1679  
1680          return true;
1681      }
1682  
1683      return false;
1684  }
1685  
1686  /**
1687   * Deletes a site transient.
1688   *
1689   * @since 2.9.0
1690   *
1691   * @param string $transient Transient name. Expected to not be SQL-escaped.
1692   * @return bool True if successful, false otherwise
1693   */
1694  function delete_site_transient( $transient ) {
1695  
1696      /**
1697       * Fires immediately before a specific site transient is deleted.
1698       *
1699       * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1700       *
1701       * @since 3.0.0
1702       *
1703       * @param string $transient Transient name.
1704       */
1705      do_action( "delete_site_transient_{$transient}", $transient );
1706  
1707      if ( wp_using_ext_object_cache() ) {
1708          $result = wp_cache_delete( $transient, 'site-transient' );
1709      } else {
1710          $option_timeout = '_site_transient_timeout_' . $transient;
1711          $option         = '_site_transient_' . $transient;
1712          $result         = delete_site_option( $option );
1713          if ( $result ) {
1714              delete_site_option( $option_timeout );
1715          }
1716      }
1717      if ( $result ) {
1718  
1719          /**
1720           * Fires after a transient is deleted.
1721           *
1722           * @since 3.0.0
1723           *
1724           * @param string $transient Deleted transient name.
1725           */
1726          do_action( 'deleted_site_transient', $transient );
1727      }
1728  
1729      return $result;
1730  }
1731  
1732  /**
1733   * Retrieves the value of a site transient.
1734   *
1735   * If the transient does not exist, does not have a value, or has expired,
1736   * then the return value will be false.
1737   *
1738   * @since 2.9.0
1739   *
1740   * @see get_transient()
1741   *
1742   * @param string $transient Transient name. Expected to not be SQL-escaped.
1743   * @return mixed Value of transient.
1744   */
1745  function get_site_transient( $transient ) {
1746  
1747      /**
1748       * Filters the value of an existing site transient.
1749       *
1750       * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1751       *
1752       * Passing a truthy value to the filter will effectively short-circuit retrieval,
1753       * returning the passed value instead.
1754       *
1755       * @since 2.9.0
1756       * @since 4.4.0 The `$transient` parameter was added.
1757       *
1758       * @param mixed  $pre_site_transient The default value to return if the site transient does not exist.
1759       *                                   Any value other than false will short-circuit the retrieval
1760       *                                   of the transient, and return the returned value.
1761       * @param string $transient          Transient name.
1762       */
1763      $pre = apply_filters( "pre_site_transient_{$transient}", false, $transient );
1764  
1765      if ( false !== $pre ) {
1766          return $pre;
1767      }
1768  
1769      if ( wp_using_ext_object_cache() ) {
1770          $value = wp_cache_get( $transient, 'site-transient' );
1771      } else {
1772          // Core transients that do not have a timeout. Listed here so querying timeouts can be avoided.
1773          $no_timeout       = array( 'update_core', 'update_plugins', 'update_themes' );
1774          $transient_option = '_site_transient_' . $transient;
1775          if ( ! in_array( $transient, $no_timeout ) ) {
1776              $transient_timeout = '_site_transient_timeout_' . $transient;
1777              $timeout           = get_site_option( $transient_timeout );
1778              if ( false !== $timeout && $timeout < time() ) {
1779                  delete_site_option( $transient_option );
1780                  delete_site_option( $transient_timeout );
1781                  $value = false;
1782              }
1783          }
1784  
1785          if ( ! isset( $value ) ) {
1786              $value = get_site_option( $transient_option );
1787          }
1788      }
1789  
1790      /**
1791       * Filters the value of an existing site transient.
1792       *
1793       * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1794       *
1795       * @since 2.9.0
1796       * @since 4.4.0 The `$transient` parameter was added.
1797       *
1798       * @param mixed  $value     Value of site transient.
1799       * @param string $transient Transient name.
1800       */
1801      return apply_filters( "site_transient_{$transient}", $value, $transient );
1802  }
1803  
1804  /**
1805   * Sets/updates the value of a site transient.
1806   *
1807   * You do not need to serialize values. If the value needs to be serialized,
1808   * then it will be serialized before it is set.
1809   *
1810   * @since 2.9.0
1811   *
1812   * @see set_transient()
1813   *
1814   * @param string $transient  Transient name. Expected to not be SQL-escaped. Must be
1815   *                           167 characters or fewer in length.
1816   * @param mixed  $value      Transient value. Expected to not be SQL-escaped.
1817   * @param int    $expiration Optional. Time until expiration in seconds. Default 0 (no expiration).
1818   * @return bool False if value was not set and true if value was set.
1819   */
1820  function set_site_transient( $transient, $value, $expiration = 0 ) {
1821  
1822      /**
1823       * Filters the value of a specific site transient before it is set.
1824       *
1825       * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1826       *
1827       * @since 3.0.0
1828       * @since 4.4.0 The `$transient` parameter was added.
1829       *
1830       * @param mixed  $value     New value of site transient.
1831       * @param string $transient Transient name.
1832       */
1833      $value = apply_filters( "pre_set_site_transient_{$transient}", $value, $transient );
1834  
1835      $expiration = (int) $expiration;
1836  
1837      /**
1838       * Filters the expiration for a site transient before its value is set.
1839       *
1840       * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1841       *
1842       * @since 4.4.0
1843       *
1844       * @param int    $expiration Time until expiration in seconds. Use 0 for no expiration.
1845       * @param mixed  $value      New value of site transient.
1846       * @param string $transient  Transient name.
1847       */
1848      $expiration = apply_filters( "expiration_of_site_transient_{$transient}", $expiration, $value, $transient );
1849  
1850      if ( wp_using_ext_object_cache() ) {
1851          $result = wp_cache_set( $transient, $value, 'site-transient', $expiration );
1852      } else {
1853          $transient_timeout = '_site_transient_timeout_' . $transient;
1854          $option            = '_site_transient_' . $transient;
1855          if ( false === get_site_option( $option ) ) {
1856              if ( $expiration ) {
1857                  add_site_option( $transient_timeout, time() + $expiration );
1858              }
1859              $result = add_site_option( $option, $value );
1860          } else {
1861              if ( $expiration ) {
1862                  update_site_option( $transient_timeout, time() + $expiration );
1863              }
1864              $result = update_site_option( $option, $value );
1865          }
1866      }
1867      if ( $result ) {
1868  
1869          /**
1870           * Fires after the value for a specific site transient has been set.
1871           *
1872           * The dynamic portion of the hook name, `$transient`, refers to the transient name.
1873           *
1874           * @since 3.0.0
1875           * @since 4.4.0 The `$transient` parameter was added
1876           *
1877           * @param mixed  $value      Site transient value.
1878           * @param int    $expiration Time until expiration in seconds.
1879           * @param string $transient  Transient name.
1880           */
1881          do_action( "set_site_transient_{$transient}", $value, $expiration, $transient );
1882  
1883          /**
1884           * Fires after the value for a site transient has been set.
1885           *
1886           * @since 3.0.0
1887           *
1888           * @param string $transient  The name of the site transient.
1889           * @param mixed  $value      Site transient value.
1890           * @param int    $expiration Time until expiration in seconds.
1891           */
1892          do_action( 'setted_site_transient', $transient, $value, $expiration );
1893      }
1894      return $result;
1895  }
1896  
1897  /**
1898   * Registers default settings available in WordPress.
1899   *
1900   * The settings registered here are primarily useful for the REST API, so this
1901   * does not encompass all settings available in WordPress.
1902   *
1903   * @since 4.7.0
1904   */
1905  function register_initial_settings() {
1906      register_setting(
1907          'general',
1908          'blogname',
1909          array(
1910              'show_in_rest' => array(
1911                  'name' => 'title',
1912              ),
1913              'type'         => 'string',
1914              'description'  => __( 'Site title.' ),
1915          )
1916      );
1917  
1918      register_setting(
1919          'general',
1920          'blogdescription',
1921          array(
1922              'show_in_rest' => array(
1923                  'name' => 'description',
1924              ),
1925              'type'         => 'string',
1926              'description'  => __( 'Site tagline.' ),
1927          )
1928      );
1929  
1930      if ( ! is_multisite() ) {
1931          register_setting(
1932              'general',
1933              'siteurl',
1934              array(
1935                  'show_in_rest' => array(
1936                      'name'   => 'url',
1937                      'schema' => array(
1938                          'format' => 'uri',
1939                      ),
1940                  ),
1941                  'type'         => 'string',
1942                  'description'  => __( 'Site URL.' ),
1943              )
1944          );
1945      }
1946  
1947      if ( ! is_multisite() ) {
1948          register_setting(
1949              'general',
1950              'admin_email',
1951              array(
1952                  'show_in_rest' => array(
1953                      'name'   => 'email',
1954                      'schema' => array(
1955                          'format' => 'email',
1956                      ),
1957                  ),
1958                  'type'         => 'string',
1959                  'description'  => __( 'This address is used for admin purposes, like new user notification.' ),
1960              )
1961          );
1962      }
1963  
1964      register_setting(
1965          'general',
1966          'timezone_string',
1967          array(
1968              'show_in_rest' => array(
1969                  'name' => 'timezone',
1970              ),
1971              'type'         => 'string',
1972              'description'  => __( 'A city in the same timezone as you.' ),
1973          )
1974      );
1975  
1976      register_setting(
1977          'general',
1978          'date_format',
1979          array(
1980              'show_in_rest' => true,
1981              'type'         => 'string',
1982              'description'  => __( 'A date format for all date strings.' ),
1983          )
1984      );
1985  
1986      register_setting(
1987          'general',
1988          'time_format',
1989          array(
1990              'show_in_rest' => true,
1991              'type'         => 'string',
1992              'description'  => __( 'A time format for all time strings.' ),
1993          )
1994      );
1995  
1996      register_setting(
1997          'general',
1998          'start_of_week',
1999          array(
2000              'show_in_rest' => true,
2001              'type'         => 'integer',
2002              'description'  => __( 'A day number of the week that the week should start on.' ),
2003          )
2004      );
2005  
2006      register_setting(
2007          'general',
2008          'WPLANG',
2009          array(
2010              'show_in_rest' => array(
2011                  'name' => 'language',
2012              ),
2013              'type'         => 'string',
2014              'description'  => __( 'WordPress locale code.' ),
2015              'default'      => 'en_US',
2016          )
2017      );
2018  
2019      register_setting(
2020          'writing',
2021          'use_smilies',
2022          array(
2023              'show_in_rest' => true,
2024              'type'         => 'boolean',
2025              'description'  => __( 'Convert emoticons like :-) and :-P to graphics on display.' ),
2026              'default'      => true,
2027          )
2028      );
2029  
2030      register_setting(
2031          'writing',
2032          'default_category',
2033          array(
2034              'show_in_rest' => true,
2035              'type'         => 'integer',
2036              'description'  => __( 'Default post category.' ),
2037          )
2038      );
2039  
2040      register_setting(
2041          'writing',
2042          'default_post_format',
2043          array(
2044              'show_in_rest' => true,
2045              'type'         => 'string',
2046              'description'  => __( 'Default post format.' ),
2047          )
2048      );
2049  
2050      register_setting(
2051          'reading',
2052          'posts_per_page',
2053          array(
2054              'show_in_rest' => true,
2055              'type'         => 'integer',
2056              'description'  => __( 'Blog pages show at most.' ),
2057              'default'      => 10,
2058          )
2059      );
2060  
2061      register_setting(
2062          'discussion',
2063          'default_ping_status',
2064          array(
2065              'show_in_rest' => array(
2066                  'schema' => array(
2067                      'enum' => array( 'open', 'closed' ),
2068                  ),
2069              ),
2070              'type'         => 'string',
2071              'description'  => __( 'Allow link notifications from other blogs (pingbacks and trackbacks) on new articles.' ),
2072          )
2073      );
2074  
2075      register_setting(
2076          'discussion',
2077          'default_comment_status',
2078          array(
2079              'show_in_rest' => array(
2080                  'schema' => array(
2081                      'enum' => array( 'open', 'closed' ),
2082                  ),
2083              ),
2084              'type'         => 'string',
2085              'description'  => __( 'Allow people to submit comments on new posts.' ),
2086          )
2087      );
2088  }
2089  
2090  /**
2091   * Registers a setting and its data.
2092   *
2093   * @since 2.7.0
2094   * @since 4.7.0 `$args` can be passed to set flags on the setting, similar to `register_meta()`.
2095   *
2096   * @global array $new_whitelist_options
2097   * @global array $wp_registered_settings
2098   *
2099   * @param string $option_group A settings group name. Should correspond to a whitelisted option key name.
2100   *                             Default whitelisted option key names include 'general', 'discussion', 'media',
2101   *                             'reading', 'writing', 'misc', 'options', and 'privacy'.
2102   * @param string $option_name The name of an option to sanitize and save.
2103   * @param array  $args {
2104   *     Data used to describe the setting when registered.
2105   *
2106   *     @type string     $type              The type of data associated with this setting.
2107   *                                         Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'.
2108   *     @type string     $description       A description of the data attached to this setting.
2109   *     @type callable   $sanitize_callback A callback function that sanitizes the option's value.
2110   *     @type bool|array $show_in_rest      Whether data associated with this setting should be included in the REST API.
2111   *                                         When registering complex settings, this argument may optionally be an
2112   *                                         array with a 'schema' key.
2113   *     @type mixed      $default           Default value when calling `get_option()`.
2114   * }
2115   */
2116  function register_setting( $option_group, $option_name, $args = array() ) {
2117      global $new_whitelist_options, $wp_registered_settings;
2118  
2119      $defaults = array(
2120          'type'              => 'string',
2121          'group'             => $option_group,
2122          'description'       => '',
2123          'sanitize_callback' => null,
2124          'show_in_rest'      => false,
2125      );
2126  
2127      // Back-compat: old sanitize callback is added.
2128      if ( is_callable( $args ) ) {
2129          $args = array(
2130              'sanitize_callback' => $args,
2131          );
2132      }
2133  
2134      /**
2135       * Filters the registration arguments when registering a setting.
2136       *
2137       * @since 4.7.0
2138       *
2139       * @param array  $args         Array of setting registration arguments.
2140       * @param array  $defaults     Array of default arguments.
2141       * @param string $option_group Setting group.
2142       * @param string $option_name  Setting name.
2143       */
2144      $args = apply_filters( 'register_setting_args', $args, $defaults, $option_group, $option_name );
2145      $args = wp_parse_args( $args, $defaults );
2146  
2147      // Require an item schema when registering settings with an array type.
2148      if ( false !== $args['show_in_rest'] && 'array' === $args['type'] && ( ! is_array( $args['show_in_rest'] ) || ! isset( $args['show_in_rest']['schema']['items'] ) ) ) {
2149          _doing_it_wrong( __FUNCTION__, __( 'When registering an "array" setting to show in the REST API, you must specify the schema for each array item in "show_in_rest.schema.items".' ), '5.4.0' );
2150      }
2151  
2152      if ( ! is_array( $wp_registered_settings ) ) {
2153          $wp_registered_settings = array();
2154      }
2155  
2156      if ( 'misc' == $option_group ) {
2157          _deprecated_argument(
2158              __FUNCTION__,
2159              '3.0.0',
2160              sprintf(
2161                  /* translators: %s: misc */
2162                  __( 'The "%s" options group has been removed. Use another settings group.' ),
2163                  'misc'
2164              )
2165          );
2166          $option_group = 'general';
2167      }
2168  
2169      if ( 'privacy' == $option_group ) {
2170          _deprecated_argument(
2171              __FUNCTION__,
2172              '3.5.0',
2173              sprintf(
2174                  /* translators: %s: privacy */
2175                  __( 'The "%s" options group has been removed. Use another settings group.' ),
2176                  'privacy'
2177              )
2178          );
2179          $option_group = 'reading';
2180      }
2181  
2182      $new_whitelist_options[ $option_group ][] = $option_name;
2183      if ( ! empty( $args['sanitize_callback'] ) ) {
2184          add_filter( "sanitize_option_{$option_name}", $args['sanitize_callback'] );
2185      }
2186      if ( array_key_exists( 'default', $args ) ) {
2187          add_filter( "default_option_{$option_name}", 'filter_default_option', 10, 3 );
2188      }
2189  
2190      $wp_registered_settings[ $option_name ] = $args;
2191  }
2192  
2193  /**
2194   * Unregisters a setting.
2195   *
2196   * @since 2.7.0
2197   * @since 4.7.0 `$sanitize_callback` was deprecated. The callback from `register_setting()` is now used instead.
2198   *
2199   * @global array $new_whitelist_options
2200   * @global array $wp_registered_settings
2201   *
2202   * @param string   $option_group      The settings group name used during registration.
2203   * @param string   $option_name       The name of the option to unregister.
2204   * @param callable $deprecated        Deprecated.
2205   */
2206  function unregister_setting( $option_group, $option_name, $deprecated = '' ) {
2207      global $new_whitelist_options, $wp_registered_settings;
2208  
2209      if ( 'misc' == $option_group ) {
2210          _deprecated_argument(
2211              __FUNCTION__,
2212              '3.0.0',
2213              sprintf(
2214                  /* translators: %s: misc */
2215                  __( 'The "%s" options group has been removed. Use another settings group.' ),
2216                  'misc'
2217              )
2218          );
2219          $option_group = 'general';
2220      }
2221  
2222      if ( 'privacy' == $option_group ) {
2223          _deprecated_argument(
2224              __FUNCTION__,
2225              '3.5.0',
2226              sprintf(
2227                  /* translators: %s: privacy */
2228                  __( 'The "%s" options group has been removed. Use another settings group.' ),
2229                  'privacy'
2230              )
2231          );
2232          $option_group = 'reading';
2233      }
2234  
2235      $pos = array_search( $option_name, (array) $new_whitelist_options[ $option_group ] );
2236      if ( false !== $pos ) {
2237          unset( $new_whitelist_options[ $option_group ][ $pos ] );
2238      }
2239      if ( '' !== $deprecated ) {
2240          _deprecated_argument(
2241              __FUNCTION__,
2242              '4.7.0',
2243              sprintf(
2244                  /* translators: 1: $sanitize_callback, 2: register_setting() */
2245                  __( '%1$s is deprecated. The callback from %2$s is used instead.' ),
2246                  '<code>$sanitize_callback</code>',
2247                  '<code>register_setting()</code>'
2248              )
2249          );
2250          remove_filter( "sanitize_option_{$option_name}", $deprecated );
2251      }
2252  
2253      if ( isset( $wp_registered_settings[ $option_name ] ) ) {
2254          // Remove the sanitize callback if one was set during registration.
2255          if ( ! empty( $wp_registered_settings[ $option_name ]['sanitize_callback'] ) ) {
2256              remove_filter( "sanitize_option_{$option_name}", $wp_registered_settings[ $option_name ]['sanitize_callback'] );
2257          }
2258  
2259          // Remove the default filter if a default was provided during registration.
2260          if ( array_key_exists( 'default', $wp_registered_settings[ $option_name ] ) ) {
2261              remove_filter( "default_option_{$option_name}", 'filter_default_option', 10 );
2262          }
2263  
2264          unset( $wp_registered_settings[ $option_name ] );
2265      }
2266  }
2267  
2268  /**
2269   * Retrieves an array of registered settings.
2270   *
2271   * @since 4.7.0
2272   *
2273   * @global array $wp_registered_settings
2274   *
2275   * @return array List of registered settings, keyed by option name.
2276   */
2277  function get_registered_settings() {
2278      global $wp_registered_settings;
2279  
2280      if ( ! is_array( $wp_registered_settings ) ) {
2281          return array();
2282      }
2283  
2284      return $wp_registered_settings;
2285  }
2286  
2287  /**
2288   * Filters the default value for the option.
2289   *
2290   * For settings which register a default setting in `register_setting()`, this
2291   * function is added as a filter to `default_option_{$option}`.
2292   *
2293   * @since 4.7.0
2294   *
2295   * @param mixed $default Existing default value to return.
2296   * @param string $option Option name.
2297   * @param bool $passed_default Was `get_option()` passed a default value?
2298   * @return mixed Filtered default value.
2299   */
2300  function filter_default_option( $default, $option, $passed_default ) {
2301      if ( $passed_default ) {
2302          return $default;
2303      }
2304  
2305      $registered = get_registered_settings();
2306      if ( empty( $registered[ $option ] ) ) {
2307          return $default;
2308      }
2309  
2310      return $registered[ $option ]['default'];
2311  }


Generated: Tue May 19 15:51:04 2020 Cross-referenced by PHPXref 0.7.1