[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

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

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


Generated: Fri Oct 18 08:20:01 2019 Cross-referenced by PHPXref 0.7