wp/wp-includes/option.php
author ymh <ymh.work@gmail.com>
Tue, 09 Jun 2015 03:35:32 +0200
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
permissions -rw-r--r--
upgrade wordpress + plugins
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * Option API
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * @package WordPress
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     6
 * @subpackage Option
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
 * Retrieve option value based on name of option.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
 * If the option does not exist or does not have a value, then the return value
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
 * will be false. This is useful to check whether you need to install an option
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
 * and is commonly used during installation of plugin options and to test
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
 * whether upgrading is required.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
 * If the option was serialized then it will be unserialized when it is returned.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
 * @since 1.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
 * @param mixed $default Optional. Default value to return if the option does not exist.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
 * @return mixed Value set for the option.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
function get_option( $option, $default = false ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
	$option = trim( $option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
	if ( empty( $option ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    32
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    33
	 * Filter the value of an existing option before it is retrieved.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    34
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    35
	 * The dynamic portion of the hook name, `$option`, refers to the option name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    36
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    37
	 * Passing a truthy value to the filter will short-circuit retrieving
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    38
	 * the option value, returning the passed value instead.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    39
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    40
	 * @since 1.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    41
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    42
	 * @param bool|mixed $pre_option Value to return instead of the option value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    43
	 *                               Default false to skip it.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    44
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
	$pre = apply_filters( 'pre_option_' . $option, false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
	if ( false !== $pre )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
		return $pre;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
	if ( defined( 'WP_SETUP_CONFIG' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
	if ( ! defined( 'WP_INSTALLING' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
		// prevent non-existent options from triggering multiple queries
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
		$notoptions = wp_cache_get( 'notoptions', 'options' );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    55
		if ( isset( $notoptions[ $option ] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    56
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    57
			 * Filter the default value for an option.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    58
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    59
			 * The dynamic portion of the hook name, `$option`, refers to the option name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    60
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    61
			 * @since 3.4.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    62
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    63
			 * @param mixed $default The default value to return if the option does not exist
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    64
			 *                       in the database.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    65
			 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
			return apply_filters( 'default_option_' . $option, $default );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    67
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
		$alloptions = wp_load_alloptions();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
		if ( isset( $alloptions[$option] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
			$value = $alloptions[$option];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
			$value = wp_cache_get( $option, 'options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
			if ( false === $value ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
				$row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
				// Has to be get_row instead of get_var because of funkiness with 0, false, null values
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
				if ( is_object( $row ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
					$value = $row->option_value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
					wp_cache_add( $option, $value, 'options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
				} else { // option does not exist, so we must cache its non-existence
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
					$notoptions[$option] = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
					wp_cache_set( 'notoptions', $notoptions, 'options' );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    86
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    87
					/** This filter is documented in wp-includes/option.php */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
					return apply_filters( 'default_option_' . $option, $default );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
		$suppress = $wpdb->suppress_errors();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
		$row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
		$wpdb->suppress_errors( $suppress );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    96
		if ( is_object( $row ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
			$value = $row->option_value;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    98
		} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    99
			/** This filter is documented in wp-includes/option.php */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
			return apply_filters( 'default_option_' . $option, $default );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   101
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
	// If home is not set use siteurl.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
	if ( 'home' == $option && '' == $value )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
		return get_option( 'siteurl' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
	if ( in_array( $option, array('siteurl', 'home', 'category_base', 'tag_base') ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
		$value = untrailingslashit( $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   111
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   112
	 * Filter the value of an existing option.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   113
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   114
	 * The dynamic portion of the hook name, `$option`, refers to the option name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   115
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   116
	 * @since 1.5.0 As 'option_' . $setting
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   117
	 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   118
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   119
	 * @param mixed $value Value of the option. If stored serialized, it will be
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   120
	 *                     unserialized prior to being returned.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   121
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
	return apply_filters( 'option_' . $option, maybe_unserialize( $value ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
 * Protect WordPress special option from being modified.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
 * Will die if $option is in protected list. Protected options are 'alloptions'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
 * and 'notoptions' options.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
 * @since 2.2.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
 * @param string $option Option name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
function wp_protect_special_option( $option ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
	if ( 'alloptions' === $option || 'notoptions' === $option )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
		wp_die( sprintf( __( '%s is a protected WP option and may not be modified' ), esc_html( $option ) ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
 * Print option value after sanitizing for forms.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
 * @since 1.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
 * @param string $option Option name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
function form_option( $option ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
	echo esc_attr( get_option( $option ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
 * Loads and caches all autoloaded options, if available or all options.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
 * @since 2.2.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
 * @return array List of all options.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
function wp_load_alloptions() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
	if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
		$alloptions = wp_cache_get( 'alloptions', 'options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
	else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
		$alloptions = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
	if ( !$alloptions ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
		$suppress = $wpdb->suppress_errors();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
		if ( !$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
			$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
		$wpdb->suppress_errors($suppress);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
		$alloptions = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
		foreach ( (array) $alloptions_db as $o ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
			$alloptions[$o->option_name] = $o->option_value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
		if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
			wp_cache_add( 'alloptions', $alloptions, 'options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
	return $alloptions;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
 * Loads and caches certain often requested site options if is_multisite() and a persistent cache is not being used.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
 * @param int $site_id Optional site ID for which to query the options. Defaults to the current site.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
function wp_load_core_site_options( $site_id = null ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
	if ( !is_multisite() || wp_using_ext_object_cache() || defined( 'WP_INSTALLING' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
	if ( empty($site_id) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
		$site_id = $wpdb->siteid;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
	$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' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
	$core_options_in = "'" . implode("', '", $core_options) . "'";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
	$options = $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value FROM $wpdb->sitemeta WHERE meta_key IN ($core_options_in) AND site_id = %d", $site_id) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
	foreach ( $options as $option ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
		$key = $option->meta_key;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
		$cache_key = "{$site_id}:$key";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
		$option->meta_value = maybe_unserialize( $option->meta_value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
		wp_cache_set( $cache_key, $option->meta_value, 'site-options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
 * Update the value of an option that was already added.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
 * You do not need to serialize values. If the value needs to be serialized, then
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
 * it will be serialized before it is inserted into the database. Remember,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
 * resources can not be serialized or added as an option.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
 * If the option does not exist, then the option will be added with the option
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
 * value, but you will not be able to set whether it is autoloaded. If you want
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
 * to set whether an option is autoloaded, then you need to use the add_option().
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
 * @since 1.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   224
 * @since 4.2.0 The `$autoload` parameter was added.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   226
 * @param string      $option   Option name. Expected to not be SQL-escaped.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   227
 * @param mixed       $value    Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   228
 * @param string|bool $autoload Optional. Whether to load the option when WordPress starts up. For existing options,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   229
 *                              `$autoload` can only be updated using `update_option()` if `$value` is also changed.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   230
 *                              Accepts 'yes' or true to enable, 'no' or false to disable. For non-existent options,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   231
 *                              the default value is 'yes'.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
 * @return bool False if value was not updated and true if value was updated.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   234
function update_option( $option, $value, $autoload = null ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
	$option = trim($option);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
	if ( empty($option) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
	wp_protect_special_option( $option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
	if ( is_object( $value ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
		$value = clone $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
	$value = sanitize_option( $option, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
	$old_value = get_option( $option );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   248
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   249
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   250
	 * Filter a specific option before its value is (maybe) serialized and updated.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   251
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   252
	 * The dynamic portion of the hook name, `$option`, refers to the option name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   253
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   254
	 * @since 2.6.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   255
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   256
	 * @param mixed $value     The new, unserialized option value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   257
	 * @param mixed $old_value The old option value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   258
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
	$value = apply_filters( 'pre_update_option_' . $option, $value, $old_value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   261
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   262
	 * Filter an option before its value is (maybe) serialized and updated.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   263
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   264
	 * @since 3.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   265
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   266
	 * @param mixed  $value     The new, unserialized option value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   267
	 * @param string $option    Name of the option.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   268
	 * @param mixed  $old_value The old option value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   269
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   270
	$value = apply_filters( 'pre_update_option', $value, $option, $old_value );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   271
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
	// If the new and old values are the same, no need to update.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
	if ( $value === $old_value )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   276
	/** This filter is documented in wp-includes/option.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   277
	if ( apply_filters( 'default_option_' . $option, false ) === $old_value ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   278
		// Default setting for new options is 'yes'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   279
		if ( null === $autoload ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   280
			$autoload = 'yes';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   281
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   282
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   283
		return add_option( $option, $value, '', $autoload );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   284
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
	$serialized_value = maybe_serialize( $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   288
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   289
	 * Fires immediately before an option value is updated.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   290
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   291
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   292
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   293
	 * @param string $option    Name of the option to update.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   294
	 * @param mixed  $old_value The old option value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   295
	 * @param mixed  $value     The new option value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   296
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
	do_action( 'update_option', $option, $old_value, $value );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   298
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   299
	$update_args = array(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   300
		'option_value' => $serialized_value,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   301
	);
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   302
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   303
	if ( null !== $autoload ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   304
		$update_args['autoload'] = ( 'no' === $autoload || false === $autoload ) ? 'no' : 'yes';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   305
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   306
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   307
	$result = $wpdb->update( $wpdb->options, $update_args, array( 'option_name' => $option ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
	if ( ! $result )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
	$notoptions = wp_cache_get( 'notoptions', 'options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
	if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
		unset( $notoptions[$option] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
		wp_cache_set( 'notoptions', $notoptions, 'options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
	if ( ! defined( 'WP_INSTALLING' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
		$alloptions = wp_load_alloptions();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
		if ( isset( $alloptions[$option] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
			$alloptions[ $option ] = $serialized_value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
			wp_cache_set( 'alloptions', $alloptions, 'options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
			wp_cache_set( $option, $serialized_value, 'options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   327
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   328
	 * Fires after the value of a specific option has been successfully updated.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   329
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   330
	 * The dynamic portion of the hook name, `$option`, refers to the option name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   331
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   332
	 * @since 2.0.1
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   333
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   334
	 * @param mixed $old_value The old option value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   335
	 * @param mixed $value     The new option value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   336
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
	do_action( "update_option_{$option}", $old_value, $value );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   338
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   339
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   340
	 * Fires after the value of an option has been successfully updated.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   341
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   342
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   343
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   344
	 * @param string $option    Name of the updated option.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   345
	 * @param mixed  $old_value The old option value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   346
	 * @param mixed  $value     The new option value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   347
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
	do_action( 'updated_option', $option, $old_value, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
	return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
 * Add a new option.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
 * You do not need to serialize values. If the value needs to be serialized, then
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
 * it will be serialized before it is inserted into the database. Remember,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
 * resources can not be serialized or added as an option.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
 * You can create options without values and then update the values later.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
 * Existing options will not be updated and checks are performed to ensure that you
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
 * aren't adding a protected WordPress option. Care should be taken to not name
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
 * options the same as the ones which are protected.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
 * @since 1.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   366
 * @param string         $option      Name of option to add. Expected to not be SQL-escaped.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   367
 * @param mixed          $value       Optional. Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   368
 * @param string         $deprecated  Optional. Description. Not used anymore.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   369
 * @param string|bool    $autoload    Optional. Whether to load the option when WordPress starts up.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   370
 *                                    Default is enabled. Accepts 'no' to disable for legacy reasons.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
 * @return bool False if option was not added and true if option was added.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
	if ( !empty( $deprecated ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
		_deprecated_argument( __FUNCTION__, '2.3' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
	$option = trim($option);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
	if ( empty($option) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
	wp_protect_special_option( $option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
	if ( is_object($value) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
		$value = clone $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
	$value = sanitize_option( $option, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
	// Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
	$notoptions = wp_cache_get( 'notoptions', 'options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
	if ( !is_array( $notoptions ) || !isset( $notoptions[$option] ) )
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   393
		/** This filter is documented in wp-includes/option.php */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   394
		if ( apply_filters( 'default_option_' . $option, false ) !== get_option( $option ) )
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
	$serialized_value = maybe_serialize( $value );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   398
	$autoload = ( 'no' === $autoload || false === $autoload ) ? 'no' : 'yes';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   399
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   400
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   401
	 * Fires before an option is added.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   402
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   403
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   404
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   405
	 * @param string $option Name of the option to add.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   406
	 * @param mixed  $value  Value of the option.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   407
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
	do_action( 'add_option', $option, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
	$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 ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
	if ( ! $result )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
	if ( ! defined( 'WP_INSTALLING' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
		if ( 'yes' == $autoload ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   416
			$alloptions = wp_load_alloptions();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   417
			$alloptions[ $option ] = $serialized_value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
			wp_cache_set( 'alloptions', $alloptions, 'options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
			wp_cache_set( $option, $serialized_value, 'options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   423
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
	// This option exists now
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   425
	$notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
	if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   427
		unset( $notoptions[$option] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   428
		wp_cache_set( 'notoptions', $notoptions, 'options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   429
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   430
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   431
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   432
	 * Fires after a specific option has been added.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   433
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   434
	 * The dynamic portion of the hook name, `$option`, refers to the option name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   435
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   436
	 * @since 2.5.0 As "add_option_{$name}"
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   437
	 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   438
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   439
	 * @param string $option Name of the option to add.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   440
	 * @param mixed  $value  Value of the option.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   441
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
	do_action( "add_option_{$option}", $option, $value );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   443
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   444
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   445
	 * Fires after an option has been added.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   446
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   447
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   448
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   449
	 * @param string $option Name of the added option.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   450
	 * @param mixed  $value  Value of the option.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   451
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   452
	do_action( 'added_option', $option, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   453
	return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   455
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   456
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   457
 * Removes option by name. Prevents removal of protected WordPress options.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   458
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
 * @since 1.2.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   460
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   461
 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   462
 * @return bool True, if option is successfully deleted. False on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   463
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
function delete_option( $option ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   465
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
	$option = trim( $option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
	if ( empty( $option ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   470
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
	wp_protect_special_option( $option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   472
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   473
	// Get the ID, if no ID then return
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   474
	$row = $wpdb->get_row( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   475
	if ( is_null( $row ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   476
		return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   477
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   478
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   479
	 * Fires immediately before an option is deleted.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   480
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   481
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   482
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   483
	 * @param string $option Name of the option to delete.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   484
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   485
	do_action( 'delete_option', $option );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   486
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   487
	$result = $wpdb->delete( $wpdb->options, array( 'option_name' => $option ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   488
	if ( ! defined( 'WP_INSTALLING' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
		if ( 'yes' == $row->autoload ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   490
			$alloptions = wp_load_alloptions();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   491
			if ( is_array( $alloptions ) && isset( $alloptions[$option] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   492
				unset( $alloptions[$option] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   493
				wp_cache_set( 'alloptions', $alloptions, 'options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   494
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   495
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   496
			wp_cache_delete( $option, 'options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   497
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   498
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   499
	if ( $result ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   500
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   501
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   502
		 * Fires after a specific option has been deleted.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   503
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   504
		 * The dynamic portion of the hook name, `$option`, refers to the option name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   505
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   506
		 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   507
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   508
		 * @param string $option Name of the deleted option.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   509
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   510
		do_action( "delete_option_$option", $option );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   511
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   512
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   513
		 * Fires after an option has been deleted.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   514
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   515
		 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   516
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   517
		 * @param string $option Name of the deleted option.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   518
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   519
		do_action( 'deleted_option', $option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   520
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   521
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   522
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   523
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   524
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   525
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   526
 * Delete a transient.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   527
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   528
 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   529
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   530
 * @param string $transient Transient name. Expected to not be SQL-escaped.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   531
 * @return bool true if successful, false otherwise
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   532
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   533
function delete_transient( $transient ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   534
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   535
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   536
	 * Fires immediately before a specific transient is deleted.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   537
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   538
	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   539
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   540
	 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   541
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   542
	 * @param string $transient Transient name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   543
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   544
	do_action( 'delete_transient_' . $transient, $transient );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   545
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   546
	if ( wp_using_ext_object_cache() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   547
		$result = wp_cache_delete( $transient, 'transient' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   548
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   549
		$option_timeout = '_transient_timeout_' . $transient;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   550
		$option = '_transient_' . $transient;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   551
		$result = delete_option( $option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   552
		if ( $result )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   553
			delete_option( $option_timeout );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   554
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   555
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   556
	if ( $result ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   557
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   558
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   559
		 * Fires after a transient is deleted.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   560
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   561
		 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   562
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   563
		 * @param string $transient Deleted transient name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   564
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   565
		do_action( 'deleted_transient', $transient );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   566
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   567
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   568
	return $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   569
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   570
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   571
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   572
 * Get the value of a transient.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   573
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   574
 * If the transient does not exist, does not have a value, or has expired,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   575
 * then the return value will be false.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   576
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   577
 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   578
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   579
 * @param string $transient Transient name. Expected to not be SQL-escaped.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   580
 * @return mixed Value of transient.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   581
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   582
function get_transient( $transient ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   583
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   584
 	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   585
	 * Filter the value of an existing transient.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   586
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   587
	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   588
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   589
	 * Passing a truthy value to the filter will effectively short-circuit retrieval
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   590
	 * of the transient, returning the passed value instead.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   591
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   592
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   593
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   594
	 * @param mixed $pre_transient The default value to return if the transient does not exist.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   595
	 *                             Any value other than false will short-circuit the retrieval
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   596
	 *                             of the transient, and return the returned value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   597
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   598
	$pre = apply_filters( 'pre_transient_' . $transient, false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   599
	if ( false !== $pre )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   600
		return $pre;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   601
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   602
	if ( wp_using_ext_object_cache() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   603
		$value = wp_cache_get( $transient, 'transient' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   604
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   605
		$transient_option = '_transient_' . $transient;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   606
		if ( ! defined( 'WP_INSTALLING' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   607
			// If option is not in alloptions, it is not autoloaded and thus has a timeout
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   608
			$alloptions = wp_load_alloptions();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   609
			if ( !isset( $alloptions[$transient_option] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   610
				$transient_timeout = '_transient_timeout_' . $transient;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   611
				if ( get_option( $transient_timeout ) < time() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   612
					delete_option( $transient_option  );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   613
					delete_option( $transient_timeout );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   614
					$value = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   615
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   616
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   617
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   618
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   619
		if ( ! isset( $value ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   620
			$value = get_option( $transient_option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   621
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   622
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   623
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   624
	 * Filter an existing transient's value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   625
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   626
	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   627
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   628
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   629
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   630
	 * @param mixed $value Value of transient.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   631
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   632
	return apply_filters( 'transient_' . $transient, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   633
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   634
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   635
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   636
 * Set/update the value of a transient.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   637
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   638
 * You do not need to serialize values. If the value needs to be serialized, then
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   639
 * it will be serialized before it is set.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   640
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   641
 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   642
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   643
 * @param string $transient  Transient name. Expected to not be SQL-escaped. Must be
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   644
 *                           45 characters or fewer in length.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   645
 * @param mixed  $value      Transient value. Must be serializable if non-scalar.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   646
 *                           Expected to not be SQL-escaped.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   647
 * @param int    $expiration Optional. Time until expiration in seconds. Default 0.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   648
 * @return bool False if value was not set and true if value was set.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   649
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   650
function set_transient( $transient, $value, $expiration = 0 ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   651
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   652
	$expiration = (int) $expiration;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   653
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   654
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   655
	 * Filter a specific transient before its value is set.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   656
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   657
	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   658
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   659
	 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   660
	 * @since 4.2.0 Added `$expiration` parameter.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   661
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   662
	 * @param mixed $value      New value of transient.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   663
	 * @param int   $expiration Time until expiration in seconds.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   664
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   665
	$value = apply_filters( 'pre_set_transient_' . $transient, $value, $expiration );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   666
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   667
	if ( wp_using_ext_object_cache() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   668
		$result = wp_cache_set( $transient, $value, 'transient', $expiration );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   669
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   670
		$transient_timeout = '_transient_timeout_' . $transient;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   671
		$transient = '_transient_' . $transient;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   672
		if ( false === get_option( $transient ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   673
			$autoload = 'yes';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   674
			if ( $expiration ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   675
				$autoload = 'no';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   676
				add_option( $transient_timeout, time() + $expiration, '', 'no' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   677
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   678
			$result = add_option( $transient, $value, '', $autoload );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   679
		} else {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   680
			// If expiration is requested, but the transient has no timeout option,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   681
			// delete, then re-create transient rather than update.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   682
			$update = true;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   683
			if ( $expiration ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   684
				if ( false === get_option( $transient_timeout ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   685
					delete_option( $transient );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   686
					add_option( $transient_timeout, time() + $expiration, '', 'no' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   687
					$result = add_option( $transient, $value, '', 'no' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   688
					$update = false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   689
				} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   690
					update_option( $transient_timeout, time() + $expiration );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   691
				}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   692
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   693
			if ( $update ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   694
				$result = update_option( $transient, $value );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   695
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   696
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   697
	}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   698
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   699
	if ( $result ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   700
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   701
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   702
		 * Fires after the value for a specific transient has been set.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   703
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   704
		 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   705
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   706
		 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   707
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   708
		 * @param mixed $value      Transient value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   709
		 * @param int   $expiration Time until expiration in seconds. Default 0.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   710
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   711
		do_action( 'set_transient_' . $transient, $value, $expiration );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   712
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   713
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   714
		 * Fires after the value for a transient has been set.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   715
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   716
		 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   717
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   718
		 * @param string $transient  The name of the transient.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   719
		 * @param mixed  $value      Transient value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   720
		 * @param int    $expiration Time until expiration in seconds. Default 0.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   721
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   722
		do_action( 'setted_transient', $transient, $value, $expiration );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   723
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   724
	return $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   725
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   726
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   727
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   728
 * Saves and restores user interface settings stored in a cookie.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   729
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   730
 * Checks if the current user-settings cookie is updated and stores it. When no
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   731
 * cookie exists (different browser used), adds the last saved cookie restoring
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   732
 * the settings.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   733
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   734
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   735
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   736
function wp_user_settings() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   737
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   738
	if ( ! is_admin() || defined( 'DOING_AJAX' ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   739
		return;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   740
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   741
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   742
	if ( ! $user_id = get_current_user_id() ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   743
		return;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   744
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   745
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   746
	if ( is_super_admin() && ! is_user_member_of_blog() ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   747
		return;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   748
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   749
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   750
	$settings = (string) get_user_option( 'user-settings', $user_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   751
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   752
	if ( isset( $_COOKIE['wp-settings-' . $user_id] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   753
		$cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user_id] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   754
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   755
		// No change or both empty
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   756
		if ( $cookie == $settings )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   757
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   758
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   759
		$last_saved = (int) get_user_option( 'user-settings-time', $user_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   760
		$current = isset( $_COOKIE['wp-settings-time-' . $user_id]) ? preg_replace( '/[^0-9]/', '', $_COOKIE['wp-settings-time-' . $user_id] ) : 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   761
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   762
		// The cookie is newer than the saved value. Update the user_option and leave the cookie as-is
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   763
		if ( $current > $last_saved ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   764
			update_user_option( $user_id, 'user-settings', $cookie, false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   765
			update_user_option( $user_id, 'user-settings-time', time() - 5, false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   766
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   767
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   768
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   769
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   770
	// The cookie is not set in the current browser or the saved value is newer.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   771
	$secure = ( 'https' === parse_url( site_url(), PHP_URL_SCHEME ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   772
	setcookie( 'wp-settings-' . $user_id, $settings, time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   773
	setcookie( 'wp-settings-time-' . $user_id, time(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH, null, $secure );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   774
	$_COOKIE['wp-settings-' . $user_id] = $settings;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   775
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   776
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   777
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   778
 * Retrieve user interface setting value based on setting name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   779
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   780
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   781
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   782
 * @param string $name The name of the setting.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   783
 * @param string $default Optional default value to return when $name is not set.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   784
 * @return mixed the last saved user setting or the default value/false if it doesn't exist.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   785
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   786
function get_user_setting( $name, $default = false ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   787
	$all_user_settings = get_all_user_settings();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   788
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   789
	return isset( $all_user_settings[$name] ) ? $all_user_settings[$name] : $default;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   790
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   791
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   792
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   793
 * Add or update user interface setting.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   794
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   795
 * Both $name and $value can contain only ASCII letters, numbers and underscores.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   796
 * This function has to be used before any output has started as it calls setcookie().
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   797
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   798
 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   799
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   800
 * @param string $name The name of the setting.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   801
 * @param string $value The value for the setting.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   802
 * @return null|bool true if set successfully/false if not.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   803
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   804
function set_user_setting( $name, $value ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   805
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   806
	if ( headers_sent() ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   807
		return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   808
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   809
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   810
	$all_user_settings = get_all_user_settings();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   811
	$all_user_settings[$name] = $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   812
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   813
	return wp_set_all_user_settings( $all_user_settings );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   814
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   815
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   816
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   817
 * Delete user interface settings.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   818
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   819
 * Deleting settings would reset them to the defaults.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   820
 * This function has to be used before any output has started as it calls setcookie().
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   821
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   822
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   823
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   824
 * @param string $names The name or array of names of the setting to be deleted.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   825
 * @return null|bool true if deleted successfully/false if not.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   826
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   827
function delete_user_setting( $names ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   828
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   829
	if ( headers_sent() ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   830
		return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   831
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   832
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   833
	$all_user_settings = get_all_user_settings();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   834
	$names = (array) $names;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   835
	$deleted = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   836
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   837
	foreach ( $names as $name ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   838
		if ( isset( $all_user_settings[$name] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   839
			unset( $all_user_settings[$name] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   840
			$deleted = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   841
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   842
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   843
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   844
	if ( $deleted ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   845
		return wp_set_all_user_settings( $all_user_settings );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   846
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   847
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   848
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   849
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   850
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   851
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   852
 * Retrieve all user interface settings.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   853
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   854
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   855
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   856
 * @return array the last saved user settings or empty array.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   857
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   858
function get_all_user_settings() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   859
	global $_updated_user_settings;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   860
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   861
	if ( ! $user_id = get_current_user_id() ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   862
		return array();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   863
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   864
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   865
	if ( isset( $_updated_user_settings ) && is_array( $_updated_user_settings ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   866
		return $_updated_user_settings;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   867
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   868
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   869
	$user_settings = array();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   870
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   871
	if ( isset( $_COOKIE['wp-settings-' . $user_id] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   872
		$cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user_id] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   873
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   874
		if ( strpos( $cookie, '=' ) ) { // '=' cannot be 1st char
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   875
			parse_str( $cookie, $user_settings );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   876
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   877
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   878
		$option = get_user_option( 'user-settings', $user_id );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   879
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   880
		if ( $option && is_string( $option ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   881
			parse_str( $option, $user_settings );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   882
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   883
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   884
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   885
	$_updated_user_settings = $user_settings;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   886
	return $user_settings;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   887
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   888
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   889
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   890
 * Private. Set all user interface settings.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   891
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   892
 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   893
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   894
 * @param array $user_settings
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   895
 * @return null|bool
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   896
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   897
function wp_set_all_user_settings( $user_settings ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   898
	global $_updated_user_settings;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   899
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   900
	if ( ! $user_id = get_current_user_id() ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   901
		return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   902
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   903
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   904
	if ( is_super_admin() && ! is_user_member_of_blog() ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   905
		return;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   906
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   907
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   908
	$settings = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   909
	foreach ( $user_settings as $name => $value ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   910
		$_name = preg_replace( '/[^A-Za-z0-9_]+/', '', $name );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   911
		$_value = preg_replace( '/[^A-Za-z0-9_]+/', '', $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   912
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   913
		if ( ! empty( $_name ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   914
			$settings .= $_name . '=' . $_value . '&';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   915
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   916
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   917
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   918
	$settings = rtrim( $settings, '&' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   919
	parse_str( $settings, $_updated_user_settings );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   920
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   921
	update_user_option( $user_id, 'user-settings', $settings, false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   922
	update_user_option( $user_id, 'user-settings-time', time(), false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   923
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   924
	return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   925
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   926
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   927
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   928
 * Delete the user settings of the current user.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   929
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   930
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   931
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   932
function delete_all_user_settings() {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   933
	if ( ! $user_id = get_current_user_id() ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   934
		return;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   935
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   936
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   937
	update_user_option( $user_id, 'user-settings', '', false );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   938
	setcookie( 'wp-settings-' . $user_id, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   939
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   940
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   941
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   942
 * Retrieve site option value based on name of option.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   943
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   944
 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   945
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   946
 * @see get_option()
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   947
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   948
 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   949
 * @param mixed $default Optional value to return if option doesn't exist. Default false.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   950
 * @param bool $use_cache Whether to use cache. Multisite only. Default true.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   951
 * @return mixed Value set for the option.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   952
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   953
function get_site_option( $option, $default = false, $use_cache = true ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   954
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   955
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   956
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   957
	 * Filter an existing site option before it is retrieved.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   958
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   959
	 * The dynamic portion of the hook name, `$option`, refers to the option name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   960
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   961
	 * Passing a truthy value to the filter will effectively short-circuit retrieval,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   962
	 * returning the passed value instead.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   963
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   964
	 * @since 2.9.0 As 'pre_site_option_' . $key
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   965
	 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   966
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   967
	 * @param mixed $pre_option The default value to return if the option does not exist.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   968
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   969
 	$pre = apply_filters( 'pre_site_option_' . $option, false );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   970
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   971
 	if ( false !== $pre )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   972
 		return $pre;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   973
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   974
	// prevent non-existent options from triggering multiple queries
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   975
	$notoptions_key = "{$wpdb->siteid}:notoptions";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   976
	$notoptions = wp_cache_get( $notoptions_key, 'site-options' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   977
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   978
	if ( isset( $notoptions[$option] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   979
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   980
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   981
		 * Filter a specific default site option.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   982
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   983
		 * The dynamic portion of the hook name, `$option`, refers to the option name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   984
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   985
		 * @since 3.4.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   986
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   987
		 * @param mixed $default The value to return if the site option does not exist
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   988
		 *                       in the database.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   989
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   990
		return apply_filters( 'default_site_option_' . $option, $default );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   991
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   992
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   993
	if ( ! is_multisite() ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   994
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   995
		/** This filter is documented in wp-includes/option.php */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   996
		$default = apply_filters( 'default_site_option_' . $option, $default );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   997
		$value = get_option($option, $default);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   998
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   999
		$cache_key = "{$wpdb->siteid}:$option";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1000
		if ( $use_cache )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1001
			$value = wp_cache_get($cache_key, 'site-options');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1002
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1003
		if ( !isset($value) || (false === $value) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1004
			$row = $wpdb->get_row( $wpdb->prepare("SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1005
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1006
			// Has to be get_row instead of get_var because of funkiness with 0, false, null values
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1007
			if ( is_object( $row ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1008
				$value = $row->meta_value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1009
				$value = maybe_unserialize( $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1010
				wp_cache_set( $cache_key, $value, 'site-options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1011
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1012
				$notoptions[$option] = true;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1013
				wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1014
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1015
				/** This filter is documented in wp-includes/option.php */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1016
				$value = apply_filters( 'default_site_option_' . $option, $default );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1017
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1018
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1019
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1020
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1021
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1022
	 * Filter the value of an existing site option.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1023
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1024
	 * The dynamic portion of the hook name, `$option`, refers to the option name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1025
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1026
	 * @since 2.9.0 As 'site_option_' . $key
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1027
	 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1028
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1029
	 * @param mixed $value Value of site option.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1030
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1031
 	return apply_filters( 'site_option_' . $option, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1032
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1033
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1034
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1035
 * Add a new site option.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1036
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1037
 * Existing options will not be updated. Note that prior to 3.3 this wasn't the case.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1038
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1039
 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1040
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1041
 * @see add_option()
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1042
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1043
 * @param string $option Name of option to add. Expected to not be SQL-escaped.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1044
 * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1045
 * @return bool False if option was not added and true if option was added.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1046
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1047
function add_site_option( $option, $value ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1048
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1049
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1050
	wp_protect_special_option( $option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1051
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1052
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1053
	 * Filter the value of a specific site option before it is added.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1054
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1055
	 * The dynamic portion of the hook name, `$option`, refers to the option name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1056
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1057
	 * @since 2.9.0 As 'pre_add_site_option_' . $key
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1058
	 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1059
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1060
	 * @param mixed $value Value of site option.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1061
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1062
	$value = apply_filters( 'pre_add_site_option_' . $option, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1063
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1064
	$notoptions_key = "{$wpdb->siteid}:notoptions";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1065
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1066
	if ( !is_multisite() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1067
		$result = add_option( $option, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1068
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1069
		$cache_key = "{$wpdb->siteid}:$option";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1070
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1071
		// Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1072
		$notoptions = wp_cache_get( $notoptions_key, 'site-options' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1073
		if ( ! is_array( $notoptions ) || ! isset( $notoptions[$option] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1074
			if ( false !== get_site_option( $option ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1075
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1076
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1077
		$value = sanitize_option( $option, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1078
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1079
		$serialized_value = maybe_serialize( $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1080
		$result = $wpdb->insert( $wpdb->sitemeta, array('site_id' => $wpdb->siteid, 'meta_key' => $option, 'meta_value' => $serialized_value ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1081
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1082
		if ( ! $result )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1083
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1084
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1085
		wp_cache_set( $cache_key, $value, 'site-options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1086
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1087
		// This option exists now
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1088
		$notoptions = wp_cache_get( $notoptions_key, 'site-options' ); // yes, again... we need it to be fresh
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1089
		if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1090
			unset( $notoptions[$option] );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1091
			wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1092
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1093
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1094
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1095
	if ( $result ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1096
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1097
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1098
		 * Fires after a specific site option has been successfully added.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1099
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1100
		 * The dynamic portion of the hook name, `$option`, refers to the option name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1101
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1102
		 * @since 2.9.0 As "add_site_option_{$key}"
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1103
		 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1104
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1105
		 * @param string $option Name of site option.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1106
		 * @param mixed  $value  Value of site option.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1107
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1108
		do_action( "add_site_option_{$option}", $option, $value );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1109
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1110
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1111
		 * Fires after a site option has been successfully added.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1112
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1113
		 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1114
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1115
		 * @param string $option Name of site option.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1116
		 * @param mixed  $value  Value of site option.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1117
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1118
		do_action( "add_site_option", $option, $value );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1119
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1120
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1121
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1122
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1123
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1124
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1125
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1126
 * Removes site option by name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1127
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1128
 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1129
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1130
 * @see delete_option()
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1131
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1132
 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1133
 * @return bool True, if succeed. False, if failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1134
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1135
function delete_site_option( $option ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1136
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1137
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1138
	// ms_protect_special_option( $option ); @todo
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1139
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1140
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1141
	 * Fires immediately before a specific site option is deleted.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1142
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1143
	 * The dynamic portion of the hook name, `$option`, refers to the option name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1144
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1145
	 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1146
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1147
	do_action( 'pre_delete_site_option_' . $option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1148
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1149
	if ( !is_multisite() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1150
		$result = delete_option( $option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1151
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1152
		$row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1153
		if ( is_null( $row ) || !$row->meta_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1154
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1155
		$cache_key = "{$wpdb->siteid}:$option";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1156
		wp_cache_delete( $cache_key, 'site-options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1157
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1158
		$result = $wpdb->delete( $wpdb->sitemeta, array( 'meta_key' => $option, 'site_id' => $wpdb->siteid ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1159
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1160
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1161
	if ( $result ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1162
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1163
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1164
		 * Fires after a specific site option has been deleted.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1165
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1166
		 * The dynamic portion of the hook name, `$option`, refers to the option name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1167
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1168
		 * @since 2.9.0 As "delete_site_option_{$key}"
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1169
		 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1170
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1171
		 * @param string $option Name of the site option.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1172
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1173
		do_action( "delete_site_option_{$option}", $option );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1174
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1175
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1176
		 * Fires after a site option has been deleted.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1177
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1178
		 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1179
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1180
		 * @param string $option Name of the site option.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1181
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1182
		do_action( "delete_site_option", $option );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1183
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1184
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1185
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1186
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1187
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1188
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1189
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1190
 * Update the value of a site option that was already added.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1191
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1192
 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1193
 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1194
 * @see update_option()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1195
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1196
 * @param string $option Name of option. Expected to not be SQL-escaped.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1197
 * @param mixed $value Option value. Expected to not be SQL-escaped.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1198
 * @return bool False if value was not updated and true if value was updated.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1199
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1200
function update_site_option( $option, $value ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1201
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1202
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1203
	wp_protect_special_option( $option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1204
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1205
	$old_value = get_site_option( $option );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1206
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1207
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1208
	 * Filter a specific site option before its value is updated.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1209
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1210
	 * The dynamic portion of the hook name, `$option`, refers to the option name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1211
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1212
	 * @since 2.9.0 As 'pre_update_site_option_' . $key
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1213
	 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1214
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1215
	 * @param mixed $value     New value of site option.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1216
	 * @param mixed $old_value Old value of site option.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1217
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1218
	$value = apply_filters( 'pre_update_site_option_' . $option, $value, $old_value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1219
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1220
	if ( $value === $old_value )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1221
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1222
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1223
	if ( false === $old_value )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1224
		return add_site_option( $option, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1225
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1226
	$notoptions_key = "{$wpdb->siteid}:notoptions";
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1227
	$notoptions = wp_cache_get( $notoptions_key, 'site-options' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1228
	if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1229
		unset( $notoptions[$option] );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1230
		wp_cache_set( $notoptions_key, $notoptions, 'site-options' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1231
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1232
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1233
	if ( !is_multisite() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1234
		$result = update_option( $option, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1235
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1236
		$value = sanitize_option( $option, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1237
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1238
		$serialized_value = maybe_serialize( $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1239
		$result = $wpdb->update( $wpdb->sitemeta, array( 'meta_value' => $serialized_value ), array( 'site_id' => $wpdb->siteid, 'meta_key' => $option ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1240
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1241
		if ( $result ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1242
			$cache_key = "{$wpdb->siteid}:$option";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1243
			wp_cache_set( $cache_key, $value, 'site-options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1244
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1245
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1246
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1247
	if ( $result ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1248
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1249
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1250
		 * Fires after the value of a specific site option has been successfully updated.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1251
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1252
		 * The dynamic portion of the hook name, `$option`, refers to the option name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1253
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1254
		 * @since 2.9.0 As "update_site_option_{$key}"
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1255
		 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1256
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1257
		 * @param string $option    Name of site option.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1258
		 * @param mixed  $value     Current value of site option.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1259
		 * @param mixed  $old_value Old value of site option.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1260
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1261
		do_action( "update_site_option_{$option}", $option, $value, $old_value );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1262
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1263
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1264
		 * Fires after the value of a site option has been successfully updated.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1265
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1266
		 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1267
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1268
		 * @param string $option    Name of site option.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1269
		 * @param mixed  $value     Current value of site option.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1270
		 * @param mixed  $old_value Old value of site option.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1271
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1272
		do_action( "update_site_option", $option, $value, $old_value );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1273
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1274
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1275
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1276
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1277
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1278
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1279
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1280
 * Delete a site transient.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1281
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1282
 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1283
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1284
 * @param string $transient Transient name. Expected to not be SQL-escaped.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1285
 * @return bool True if successful, false otherwise
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1286
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1287
function delete_site_transient( $transient ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1288
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1289
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1290
	 * Fires immediately before a specific site transient is deleted.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1291
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1292
	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1293
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1294
	 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1295
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1296
	 * @param string $transient Transient name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1297
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1298
	do_action( 'delete_site_transient_' . $transient, $transient );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1299
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1300
	if ( wp_using_ext_object_cache() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1301
		$result = wp_cache_delete( $transient, 'site-transient' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1302
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1303
		$option_timeout = '_site_transient_timeout_' . $transient;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1304
		$option = '_site_transient_' . $transient;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1305
		$result = delete_site_option( $option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1306
		if ( $result )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1307
			delete_site_option( $option_timeout );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1308
	}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1309
	if ( $result ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1310
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1311
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1312
		 * Fires after a transient is deleted.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1313
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1314
		 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1315
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1316
		 * @param string $transient Deleted transient name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1317
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1318
		do_action( 'deleted_site_transient', $transient );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1319
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1320
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1321
	return $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1322
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1323
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1324
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1325
 * Get the value of a site transient.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1326
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1327
 * If the transient does not exist, does not have a value, or has expired,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1328
 * then the return value will be false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1329
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1330
 * @since 2.9.0
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1331
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1332
 * @see get_transient()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1333
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1334
 * @param string $transient Transient name. Expected to not be SQL-escaped.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1335
 * @return mixed Value of transient.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1336
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1337
function get_site_transient( $transient ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1338
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1339
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1340
	 * Filter the value of an existing site transient.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1341
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1342
	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1343
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1344
	 * Passing a truthy value to the filter will effectively short-circuit retrieval,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1345
	 * returning the passed value instead.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1346
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1347
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1348
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1349
	 * @param mixed $pre_site_transient The default value to return if the site transient does not exist.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1350
	 *                                  Any value other than false will short-circuit the retrieval
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1351
	 *                                  of the transient, and return the returned value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1352
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1353
	$pre = apply_filters( 'pre_site_transient_' . $transient, false );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1354
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1355
	if ( false !== $pre )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1356
		return $pre;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1357
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1358
	if ( wp_using_ext_object_cache() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1359
		$value = wp_cache_get( $transient, 'site-transient' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1360
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1361
		// Core transients that do not have a timeout. Listed here so querying timeouts can be avoided.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1362
		$no_timeout = array('update_core', 'update_plugins', 'update_themes');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1363
		$transient_option = '_site_transient_' . $transient;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1364
		if ( ! in_array( $transient, $no_timeout ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1365
			$transient_timeout = '_site_transient_timeout_' . $transient;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1366
			$timeout = get_site_option( $transient_timeout );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1367
			if ( false !== $timeout && $timeout < time() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1368
				delete_site_option( $transient_option  );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1369
				delete_site_option( $transient_timeout );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1370
				$value = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1371
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1372
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1373
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1374
		if ( ! isset( $value ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1375
			$value = get_site_option( $transient_option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1376
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1377
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1378
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1379
	 * Filter the value of an existing site transient.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1380
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1381
	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1382
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1383
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1384
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1385
	 * @param mixed $value Value of site transient.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1386
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1387
	return apply_filters( 'site_transient_' . $transient, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1388
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1389
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1390
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1391
 * Set/update the value of a site transient.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1392
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1393
 * You do not need to serialize values, if the value needs to be serialize, then
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1394
 * it will be serialized before it is set.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1395
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1396
 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1397
 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1398
 * @see set_transient()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1399
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1400
 * @param string $transient  Transient name. Expected to not be SQL-escaped. Must be
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1401
 *                           40 characters or fewer in length.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1402
 * @param mixed  $value      Transient value. Expected to not be SQL-escaped.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1403
 * @param int    $expiration Optional. Time until expiration in seconds. Default 0.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1404
 * @return bool False if value was not set and true if value was set.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1405
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1406
function set_site_transient( $transient, $value, $expiration = 0 ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1407
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1408
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1409
	 * Filter the value of a specific site transient before it is set.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1410
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1411
	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1412
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1413
	 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1414
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1415
	 * @param mixed $value Value of site transient.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1416
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1417
	$value = apply_filters( 'pre_set_site_transient_' . $transient, $value );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1418
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1419
	$expiration = (int) $expiration;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1420
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1421
	if ( wp_using_ext_object_cache() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1422
		$result = wp_cache_set( $transient, $value, 'site-transient', $expiration );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1423
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1424
		$transient_timeout = '_site_transient_timeout_' . $transient;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1425
		$option = '_site_transient_' . $transient;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1426
		if ( false === get_site_option( $option ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1427
			if ( $expiration )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1428
				add_site_option( $transient_timeout, time() + $expiration );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1429
			$result = add_site_option( $option, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1430
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1431
			if ( $expiration )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1432
				update_site_option( $transient_timeout, time() + $expiration );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1433
			$result = update_site_option( $option, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1434
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1435
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1436
	if ( $result ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1437
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1438
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1439
		 * Fires after the value for a specific site transient has been set.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1440
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1441
		 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1442
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1443
		 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1444
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1445
		 * @param mixed $value      Site transient value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1446
		 * @param int   $expiration Time until expiration in seconds. Default 0.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1447
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1448
		do_action( 'set_site_transient_' . $transient, $value, $expiration );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1449
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1450
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1451
		 * Fires after the value for a site transient has been set.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1452
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1453
		 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1454
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1455
		 * @param string $transient  The name of the site transient.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1456
		 * @param mixed  $value      Site transient value.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1457
		 * @param int    $expiration Time until expiration in seconds. Default 0.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1458
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1459
		do_action( 'setted_site_transient', $transient, $value, $expiration );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1460
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1461
	return $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1462
}