]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-includes/option.php
WordPress 4.3
[autoinstalls/wordpress.git] / wp-includes / option.php
index 6134606471347db74f641f73250885c9de53caa3..1b5dde235cfbc23b47bc152eba6e53ac526cf06f 100644 (file)
  *
  * @since 1.5.0
  *
- * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
- * @param mixed $default Optional. Default value to return if the option does not exist.
+ * @global wpdb $wpdb
+ *
+ * @param string $option  Name of option to retrieve. Expected to not be SQL-escaped.
+ * @param mixed  $default Optional. Default value to return if the option does not exist.
  * @return mixed Value set for the option.
  */
 function get_option( $option, $default = false ) {
@@ -81,6 +83,9 @@ function get_option( $option, $default = false ) {
                                        $value = $row->option_value;
                                        wp_cache_add( $option, $value, 'options' );
                                } else { // option does not exist, so we must cache its non-existence
+                                       if ( ! is_array( $notoptions ) ) {
+                                                $notoptions = array();
+                                       }
                                        $notoptions[$option] = true;
                                        wp_cache_set( 'notoptions', $notoptions, 'options' );
 
@@ -153,6 +158,8 @@ function form_option( $option ) {
  *
  * @since 2.2.0
  *
+ * @global wpdb $wpdb
+ *
  * @return array List of all options.
  */
 function wp_load_alloptions() {
@@ -184,6 +191,8 @@ function wp_load_alloptions() {
  *
  * @since 3.0.0
  *
+ * @global wpdb $wpdb
+ *
  * @param int $site_id Optional site ID for which to query the options. Defaults to the current site.
  */
 function wp_load_core_site_options( $site_id = null ) {
@@ -216,17 +225,23 @@ function wp_load_core_site_options( $site_id = null ) {
  * it will be serialized before it is inserted into the database. Remember,
  * resources can not be serialized or added as an option.
  *
- * If the option does not exist, then the option will be added with the option
- * value, but you will not be able to set whether it is autoloaded. If you want
- * to set whether an option is autoloaded, then you need to use the add_option().
+ * If the option does not exist, then the option will be added with the option value,
+ * with an `$autoload` value of 'yes'.
  *
  * @since 1.0.0
+ * @since 4.2.0 The `$autoload` parameter was added.
+ *
+ * @global wpdb $wpdb
  *
- * @param string $option Option name. Expected to not be SQL-escaped.
- * @param mixed $value Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
+ * @param string      $option   Option name. Expected to not be SQL-escaped.
+ * @param mixed       $value    Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
+ * @param string|bool $autoload Optional. Whether to load the option when WordPress starts up. For existing options,
+ *                              `$autoload` can only be updated using `update_option()` if `$value` is also changed.
+ *                              Accepts 'yes'|true to enable or 'no'|false to disable. For non-existent options,
+ *                              the default value is 'yes'. Default null.
  * @return bool False if value was not updated and true if value was updated.
  */
-function update_option( $option, $value ) {
+function update_option( $option, $value, $autoload = null ) {
        global $wpdb;
 
        $option = trim($option);
@@ -268,8 +283,15 @@ function update_option( $option, $value ) {
        if ( $value === $old_value )
                return false;
 
-       if ( false === $old_value )
-               return add_option( $option, $value );
+       /** This filter is documented in wp-includes/option.php */
+       if ( apply_filters( 'default_option_' . $option, false ) === $old_value ) {
+               // Default setting for new options is 'yes'.
+               if ( null === $autoload ) {
+                       $autoload = 'yes';
+               }
+
+               return add_option( $option, $value, '', $autoload );
+       }
 
        $serialized_value = maybe_serialize( $value );
 
@@ -284,7 +306,15 @@ function update_option( $option, $value ) {
         */
        do_action( 'update_option', $option, $old_value, $value );
 
-       $result = $wpdb->update( $wpdb->options, array( 'option_value' => $serialized_value ), array( 'option_name' => $option ) );
+       $update_args = array(
+               'option_value' => $serialized_value,
+       );
+
+       if ( null !== $autoload ) {
+               $update_args['autoload'] = ( 'no' === $autoload || false === $autoload ) ? 'no' : 'yes';
+       }
+
+       $result = $wpdb->update( $wpdb->options, $update_args, array( 'option_name' => $option ) );
        if ( ! $result )
                return false;
 
@@ -343,10 +373,13 @@ function update_option( $option, $value ) {
  *
  * @since 1.0.0
  *
+ * @global wpdb $wpdb
+ *
  * @param string         $option      Name of option to add. Expected to not be SQL-escaped.
  * @param mixed          $value       Optional. Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
  * @param string         $deprecated  Optional. Description. Not used anymore.
- * @param string|bool    $autoload    Optional. Default is enabled. Whether to load the option when WordPress starts up.
+ * @param string|bool    $autoload    Optional. Whether to load the option when WordPress starts up.
+ *                                    Default is enabled. Accepts 'no' to disable for legacy reasons.
  * @return bool False if option was not added and true if option was added.
  */
 function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) {
@@ -369,11 +402,12 @@ function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' )
        // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
        $notoptions = wp_cache_get( 'notoptions', 'options' );
        if ( !is_array( $notoptions ) || !isset( $notoptions[$option] ) )
-               if ( false !== get_option( $option ) )
+               /** This filter is documented in wp-includes/option.php */
+               if ( apply_filters( 'default_option_' . $option, false ) !== get_option( $option ) )
                        return false;
 
        $serialized_value = maybe_serialize( $value );
-       $autoload = ( 'no' === $autoload ) ? 'no' : 'yes';
+       $autoload = ( 'no' === $autoload || false === $autoload ) ? 'no' : 'yes';
 
        /**
         * Fires before an option is added.
@@ -436,6 +470,8 @@ function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' )
  *
  * @since 1.2.0
  *
+ * @global wpdb $wpdb
+ *
  * @param string $option Name of option to remove. Expected to not be SQL-escaped.
  * @return bool True, if option is successfully deleted. False on failure.
  */
@@ -586,7 +622,8 @@ function get_transient( $transient ) {
                        $alloptions = wp_load_alloptions();
                        if ( !isset( $alloptions[$transient_option] ) ) {
                                $transient_timeout = '_transient_timeout_' . $transient;
-                               if ( get_option( $transient_timeout ) < time() ) {
+                               $timeout = get_option( $transient_timeout );
+                               if ( false !== $timeout && $timeout < time() ) {
                                        delete_option( $transient_option  );
                                        delete_option( $transient_timeout );
                                        $value = false;
@@ -627,18 +664,20 @@ function get_transient( $transient ) {
  */
 function set_transient( $transient, $value, $expiration = 0 ) {
 
+       $expiration = (int) $expiration;
+
        /**
         * Filter a specific transient before its value is set.
         *
         * The dynamic portion of the hook name, `$transient`, refers to the transient name.
         *
         * @since 3.0.0
+        * @since 4.2.0 Added `$expiration` parameter.
         *
-        * @param mixed $value New value of transient.
+        * @param mixed $value      New value of transient.
+        * @param int   $expiration Time until expiration in seconds.
         */
-       $value = apply_filters( 'pre_set_transient_' . $transient, $value );
-
-       $expiration = (int) $expiration;
+       $value = apply_filters( 'pre_set_transient_' . $transient, $value, $expiration );
 
        if ( wp_using_ext_object_cache() ) {
                $result = wp_cache_set( $transient, $value, 'transient', $expiration );
@@ -755,7 +794,7 @@ function wp_user_settings() {
  *
  * @since 2.7.0
  *
- * @param string $name The name of the setting.
+ * @param string $name    The name of the setting.
  * @param string $default Optional default value to return when $name is not set.
  * @return mixed the last saved user setting or the default value/false if it doesn't exist.
  */
@@ -773,12 +812,11 @@ function get_user_setting( $name, $default = false ) {
  *
  * @since 2.8.0
  *
- * @param string $name The name of the setting.
+ * @param string $name  The name of the setting.
  * @param string $value The value for the setting.
- * @return null|bool true if set successfully/false if not.
+ * @return bool|void true if set successfully/false if not.
  */
 function set_user_setting( $name, $value ) {
-
        if ( headers_sent() ) {
                return false;
        }
@@ -798,10 +836,9 @@ function set_user_setting( $name, $value ) {
  * @since 2.7.0
  *
  * @param string $names The name or array of names of the setting to be deleted.
- * @return null|bool true if deleted successfully/false if not.
+ * @return bool|void true if deleted successfully/false if not.
  */
 function delete_user_setting( $names ) {
-
        if ( headers_sent() ) {
                return false;
        }
@@ -829,6 +866,8 @@ function delete_user_setting( $names ) {
  *
  * @since 2.7.0
  *
+ * @global array $_updated_user_settings
+ *
  * @return array the last saved user settings or empty array.
  */
 function get_all_user_settings() {
@@ -867,8 +906,10 @@ function get_all_user_settings() {
  *
  * @since 2.8.0
  *
+ * @global array $_updated_user_settings
+ *
  * @param array $user_settings
- * @return null|bool
+ * @return bool|void
  */
 function wp_set_all_user_settings( $user_settings ) {
        global $_updated_user_settings;
@@ -921,9 +962,11 @@ function delete_all_user_settings() {
  *
  * @see get_option()
  *
- * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
- * @param mixed $default Optional value to return if option doesn't exist. Default false.
- * @param bool $use_cache Whether to use cache. Multisite only. Default true.
+ * @global wpdb $wpdb
+ *
+ * @param string $option    Name of option to retrieve. Expected to not be SQL-escaped.
+ * @param mixed  $default   Optional value to return if option doesn't exist. Default false.
+ * @param bool   $use_cache Whether to use cache. Multisite only. Default true.
  * @return mixed Value set for the option.
  */
 function get_site_option( $option, $default = false, $use_cache = true ) {
@@ -985,6 +1028,9 @@ function get_site_option( $option, $default = false, $use_cache = true ) {
                                $value = maybe_unserialize( $value );
                                wp_cache_set( $cache_key, $value, 'site-options' );
                        } else {
+                               if ( ! is_array( $notoptions ) ) {
+                                        $notoptions = array();
+                               }
                                $notoptions[$option] = true;
                                wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
 
@@ -1016,8 +1062,10 @@ function get_site_option( $option, $default = false, $use_cache = true ) {
  *
  * @see add_option()
  *
+ * @global wpdb $wpdb
+ *
  * @param string $option Name of option to add. Expected to not be SQL-escaped.
- * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
+ * @param mixed  $value  Optional. Option value, can be anything. Expected to not be SQL-escaped.
  * @return bool False if option was not added and true if option was added.
  */
 function add_site_option( $option, $value ) {
@@ -1105,6 +1153,8 @@ function add_site_option( $option, $value ) {
  *
  * @see delete_option()
  *
+ * @global wpdb $wpdb
+ *
  * @param string $option Name of option to remove. Expected to not be SQL-escaped.
  * @return bool True, if succeed. False, if failure.
  */
@@ -1169,8 +1219,10 @@ function delete_site_option( $option ) {
  *
  * @see update_option()
  *
+ * @global wpdb $wpdb
+ *
  * @param string $option Name of option. Expected to not be SQL-escaped.
- * @param mixed $value Option value. Expected to not be SQL-escaped.
+ * @param mixed  $value  Option value. Expected to not be SQL-escaped.
  * @return bool False if value was not updated and true if value was updated.
  */
 function update_site_option( $option, $value ) {