wp/wp-includes/option.php
author ymh <ymh.work@gmail.com>
Wed, 06 Nov 2013 03:21:17 +0000
changeset 0 d970ebf37754
child 5 5e2f62d02dcd
permissions -rw-r--r--
first import
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
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 */
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
 * Retrieve option value based on name of option.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
 * 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
    12
 * 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
    13
 * and is commonly used during installation of plugin options and to test
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
 * whether upgrading is required.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
 * 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
    17
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
 * @since 1.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
 * @subpackage Option
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
 * @uses apply_filters() Calls 'pre_option_$option' before checking the option.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
 * 	Any value other than false will "short-circuit" the retrieval of the option
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
 *	and return the returned value. You should not try to override special options,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
 * 	but you will not be prevented from doing so.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
 * @uses apply_filters() Calls 'option_$option', after checking the option, with
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
 * 	the option value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
 * @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
    29
 * @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
    30
 * @return mixed Value set for the option.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
function get_option( $option, $default = false ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
	$option = trim( $option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
	if ( empty( $option ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
	// Allow plugins to short-circuit options.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
	$pre = apply_filters( 'pre_option_' . $option, false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
	if ( false !== $pre )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
		return $pre;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
	if ( defined( 'WP_SETUP_CONFIG' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
	if ( ! defined( 'WP_INSTALLING' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
		// prevent non-existent options from triggering multiple queries
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
		$notoptions = wp_cache_get( 'notoptions', 'options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
		if ( isset( $notoptions[$option] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
			return apply_filters( 'default_option_' . $option, $default );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
		$alloptions = wp_load_alloptions();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
		if ( isset( $alloptions[$option] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
			$value = $alloptions[$option];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
			$value = wp_cache_get( $option, 'options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
			if ( false === $value ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
				$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
    62
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
				// 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
    64
				if ( is_object( $row ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
					$value = $row->option_value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
					wp_cache_add( $option, $value, 'options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
				} else { // option does not exist, so we must cache its non-existence
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
					$notoptions[$option] = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
					wp_cache_set( 'notoptions', $notoptions, 'options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
					return apply_filters( 'default_option_' . $option, $default );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
		$suppress = $wpdb->suppress_errors();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
		$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
    77
		$wpdb->suppress_errors( $suppress );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
		if ( is_object( $row ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
			$value = $row->option_value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
			return apply_filters( 'default_option_' . $option, $default );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
	// If home is not set use siteurl.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
	if ( 'home' == $option && '' == $value )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
		return get_option( 'siteurl' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
	if ( in_array( $option, array('siteurl', 'home', 'category_base', 'tag_base') ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
		$value = untrailingslashit( $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
	return apply_filters( 'option_' . $option, maybe_unserialize( $value ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
 * Protect WordPress special option from being modified.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
 * Will die if $option is in protected list. Protected options are 'alloptions'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
 * and 'notoptions' options.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
 * @since 2.2.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
 * @subpackage Option
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
 * @param string $option Option name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
function wp_protect_special_option( $option ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
	if ( 'alloptions' === $option || 'notoptions' === $option )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
		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
   109
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
 * Print option value after sanitizing for forms.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
 * @uses attr Sanitizes value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
 * @since 1.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
 * @subpackage Option
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
 * @param string $option Option name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
function form_option( $option ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
	echo esc_attr( get_option( $option ) );
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
 * Loads and caches all autoloaded options, if available or all options.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
 * @since 2.2.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
 * @subpackage Option
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
 * @return array List of all options.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
function wp_load_alloptions() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
	if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
		$alloptions = wp_cache_get( 'alloptions', 'options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
	else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
		$alloptions = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
	if ( !$alloptions ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
		$suppress = $wpdb->suppress_errors();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
		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
   145
			$alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
		$wpdb->suppress_errors($suppress);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
		$alloptions = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
		foreach ( (array) $alloptions_db as $o ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
			$alloptions[$o->option_name] = $o->option_value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
		if ( !defined( 'WP_INSTALLING' ) || !is_multisite() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
			wp_cache_add( 'alloptions', $alloptions, '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
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
	return $alloptions;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
 * 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
   160
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
 * @subpackage Option
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
 * @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
   166
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
function wp_load_core_site_options( $site_id = null ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
	if ( !is_multisite() || wp_using_ext_object_cache() || defined( 'WP_INSTALLING' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
	if ( empty($site_id) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
		$site_id = $wpdb->siteid;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
	$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
   177
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
	$core_options_in = "'" . implode("', '", $core_options) . "'";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
	$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
   180
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
	foreach ( $options as $option ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
		$key = $option->meta_key;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
		$cache_key = "{$site_id}:$key";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
		$option->meta_value = maybe_unserialize( $option->meta_value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
		wp_cache_set( $cache_key, $option->meta_value, 'site-options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
 * Update the value of an option that was already added.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
 * 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
   194
 * it will be serialized before it is inserted into the database. Remember,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
 * resources can not be serialized or added as an option.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
 * 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
   198
 * 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
   199
 * 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
   200
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
 * @since 1.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
 * @subpackage Option
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
 * @uses apply_filters() Calls 'pre_update_option_$option' hook to allow overwriting the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
 * 	option value to be stored.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
 * @uses do_action() Calls 'update_option' hook before updating the option.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
 * @uses do_action() Calls 'update_option_$option' and 'updated_option' hooks on success.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
 * @param string $option Option name. Expected to not be SQL-escaped.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
 * @param mixed $value Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
 * @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
   213
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
function update_option( $option, $value ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
	$option = trim($option);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
	if ( empty($option) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
	wp_protect_special_option( $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
	if ( is_object( $value ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
		$value = clone $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
	$value = sanitize_option( $option, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
	$old_value = get_option( $option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
	$value = apply_filters( 'pre_update_option_' . $option, $value, $old_value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
	// If the new and old values are the same, no need to update.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
	if ( $value === $old_value )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
	if ( false === $old_value )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
		return add_option( $option, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
	$serialized_value = maybe_serialize( $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
	do_action( 'update_option', $option, $old_value, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
	$result = $wpdb->update( $wpdb->options, array( 'option_value' => $serialized_value ), array( 'option_name' => $option ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
	if ( ! $result )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
	$notoptions = wp_cache_get( 'notoptions', 'options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
	if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
		unset( $notoptions[$option] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
		wp_cache_set( 'notoptions', $notoptions, 'options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
	if ( ! defined( 'WP_INSTALLING' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
		$alloptions = wp_load_alloptions();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
		if ( isset( $alloptions[$option] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
			$alloptions[ $option ] = $serialized_value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
			wp_cache_set( 'alloptions', $alloptions, 'options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
			wp_cache_set( $option, $serialized_value, 'options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
	do_action( "update_option_{$option}", $old_value, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
	do_action( 'updated_option', $option, $old_value, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
	return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
 * Add a new option.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
 * 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
   269
 * it will be serialized before it is inserted into the database. Remember,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
 * resources can not be serialized or added as an option.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
 * You can create options without values and then update the values later.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
 * 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
   274
 * 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
   275
 * options the same as the ones which are protected.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
 * @subpackage Option
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
 * @since 1.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
 * @uses do_action() Calls 'add_option' hook before adding the option.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
 * @uses do_action() Calls 'add_option_$option' and 'added_option' hooks on success.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
 * @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
   285
 * @param mixed $value Optional. Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
 * @param mixed $deprecated Optional. Description. Not used anymore.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
 * @param bool $autoload Optional. Default is enabled. Whether to load the option when WordPress starts up.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
 * @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
   289
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
	if ( !empty( $deprecated ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
		_deprecated_argument( __FUNCTION__, '2.3' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
	$option = trim($option);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
	if ( empty($option) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
	wp_protect_special_option( $option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
	if ( is_object($value) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
		$value = clone $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
	$value = sanitize_option( $option, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
	// 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
   308
	$notoptions = wp_cache_get( 'notoptions', 'options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
	if ( !is_array( $notoptions ) || !isset( $notoptions[$option] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
		if ( false !== get_option( $option ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
	$serialized_value = maybe_serialize( $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
	$autoload = ( 'no' === $autoload ) ? 'no' : 'yes';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
	do_action( 'add_option', $option, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
	$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
   318
	if ( ! $result )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
	if ( ! defined( 'WP_INSTALLING' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
		if ( 'yes' == $autoload ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
			$alloptions = wp_load_alloptions();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
			$alloptions[ $option ] = $serialized_value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
			wp_cache_set( 'alloptions', $alloptions, 'options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
			wp_cache_set( $option, $serialized_value, 'options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
	// This option exists now
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
	$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
   333
	if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
		unset( $notoptions[$option] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
		wp_cache_set( 'notoptions', $notoptions, 'options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
	do_action( "add_option_{$option}", $option, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
	do_action( 'added_option', $option, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
	return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
 * Removes option by name. Prevents removal of protected WordPress options.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
 * @subpackage Option
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
 * @since 1.2.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
 * @uses do_action() Calls 'delete_option' hook before option is deleted.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
 * @uses do_action() Calls 'deleted_option' and 'delete_option_$option' hooks on success.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
 * @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
   354
 * @return bool True, if option is successfully deleted. False on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
function delete_option( $option ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
	$option = trim( $option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
	if ( empty( $option ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
	wp_protect_special_option( $option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
	// Get the ID, if no ID then return
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
	$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
   367
	if ( is_null( $row ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
	do_action( 'delete_option', $option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
	$result = $wpdb->delete( $wpdb->options, array( 'option_name' => $option ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
	if ( ! defined( 'WP_INSTALLING' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
		if ( 'yes' == $row->autoload ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
			$alloptions = wp_load_alloptions();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
			if ( is_array( $alloptions ) && isset( $alloptions[$option] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
				unset( $alloptions[$option] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
				wp_cache_set( 'alloptions', $alloptions, 'options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
			wp_cache_delete( $option, 'options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
	if ( $result ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
		do_action( "delete_option_$option", $option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
		do_action( 'deleted_option', $option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
 * Delete a transient.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
 * @subpackage Transient
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
 * @uses do_action() Calls 'delete_transient_$transient' hook before transient is deleted.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
 * @uses do_action() Calls 'deleted_transient' hook on success.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
 * @param string $transient Transient name. Expected to not be SQL-escaped.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
 * @return bool true if successful, false otherwise
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
function delete_transient( $transient ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
	do_action( 'delete_transient_' . $transient, $transient );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
	if ( wp_using_ext_object_cache() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
		$result = wp_cache_delete( $transient, 'transient' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
		$option_timeout = '_transient_timeout_' . $transient;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
		$option = '_transient_' . $transient;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
		$result = delete_option( $option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
		if ( $result )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
			delete_option( $option_timeout );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   416
	if ( $result )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   417
		do_action( 'deleted_transient', $transient );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
	return $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
 * Get the value of a transient.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   423
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
 * If the transient does not exist or does not have a value, then the return value
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   425
 * will be false.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   427
 * @uses apply_filters() Calls 'pre_transient_$transient' hook before checking the transient.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   428
 * 	Any value other than false will "short-circuit" the retrieval of the transient
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   429
 *	and return the returned value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   430
 * @uses apply_filters() Calls 'transient_$option' hook, after checking the transient, with
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   431
 * 	the transient value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   432
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   433
 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   434
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   435
 * @subpackage Transient
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   436
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   437
 * @param string $transient Transient name. Expected to not be SQL-escaped
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   438
 * @return mixed Value of transient
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   439
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
function get_transient( $transient ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   441
	$pre = apply_filters( 'pre_transient_' . $transient, false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
	if ( false !== $pre )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   443
		return $pre;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   444
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
	if ( wp_using_ext_object_cache() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
		$value = wp_cache_get( $transient, 'transient' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   448
		$transient_option = '_transient_' . $transient;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   449
		if ( ! defined( 'WP_INSTALLING' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   450
			// 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
   451
			$alloptions = wp_load_alloptions();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   452
			if ( !isset( $alloptions[$transient_option] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   453
				$transient_timeout = '_transient_timeout_' . $transient;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
				if ( get_option( $transient_timeout ) < time() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   455
					delete_option( $transient_option  );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   456
					delete_option( $transient_timeout );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   457
					$value = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   458
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   460
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   461
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   462
		if ( ! isset( $value ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   463
			$value = get_option( $transient_option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   465
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
	return apply_filters( 'transient_' . $transient, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   470
 * Set/update the value of a transient.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   472
 * 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
   473
 * it will be serialized before it is set.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   474
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   475
 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   476
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   477
 * @subpackage Transient
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   478
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   479
 * @uses apply_filters() Calls 'pre_set_transient_$transient' hook to allow overwriting the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   480
 * 	transient value to be stored.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   481
 * @uses do_action() Calls 'set_transient_$transient' and 'setted_transient' hooks on success.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   482
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   483
 * @param string $transient Transient name. Expected to not be SQL-escaped.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   484
 * @param mixed $value Transient value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   485
 * @param int $expiration Time until expiration in seconds, default 0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   486
 * @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
   487
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   488
function set_transient( $transient, $value, $expiration = 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
	$value = apply_filters( 'pre_set_transient_' . $transient, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   490
	$expiration = (int) $expiration;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   491
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   492
	if ( wp_using_ext_object_cache() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   493
		$result = wp_cache_set( $transient, $value, 'transient', $expiration );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   494
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   495
		$transient_timeout = '_transient_timeout_' . $transient;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   496
		$transient = '_transient_' . $transient;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   497
		if ( false === get_option( $transient ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   498
			$autoload = 'yes';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   499
			if ( $expiration ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   500
				$autoload = 'no';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   501
				add_option( $transient_timeout, time() + $expiration, '', 'no' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   502
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   503
			$result = add_option( $transient, $value, '', $autoload );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   504
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   505
			if ( $expiration )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   506
				update_option( $transient_timeout, time() + $expiration );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   507
			$result = update_option( $transient, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   508
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   509
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   510
	if ( $result ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   511
		do_action( 'set_transient_' . $transient, $value, $expiration );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   512
		do_action( 'setted_transient', $transient, $value, $expiration );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   513
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   514
	return $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   515
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   516
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   517
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   518
 * Saves and restores user interface settings stored in a cookie.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   519
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   520
 * 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
   521
 * cookie exists (different browser used), adds the last saved cookie restoring
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   522
 * the settings.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   523
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   524
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   525
 * @subpackage Option
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   526
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   527
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   528
function wp_user_settings() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   529
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   530
	if ( ! is_admin() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   531
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   532
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   533
	if ( defined('DOING_AJAX') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   534
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   535
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   536
	if ( ! $user_id = get_current_user_id() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   537
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   538
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   539
	if ( is_super_admin() && ! is_user_member_of_blog() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   540
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   541
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   542
	$settings = (string) get_user_option( 'user-settings', $user_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   543
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   544
	if ( isset( $_COOKIE['wp-settings-' . $user_id] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   545
		$cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user_id] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   546
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   547
		// No change or both empty
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   548
		if ( $cookie == $settings )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   549
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   550
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   551
		$last_saved = (int) get_user_option( 'user-settings-time', $user_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   552
		$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
   553
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   554
		// 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
   555
		if ( $current > $last_saved ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   556
			update_user_option( $user_id, 'user-settings', $cookie, false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   557
			update_user_option( $user_id, 'user-settings-time', time() - 5, false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   558
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   559
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   560
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   561
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   562
	// The cookie is not set in the current browser or the saved value is newer.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   563
	setcookie( 'wp-settings-' . $user_id, $settings, time() + YEAR_IN_SECONDS, SITECOOKIEPATH );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   564
	setcookie( 'wp-settings-time-' . $user_id, time(), time() + YEAR_IN_SECONDS, SITECOOKIEPATH );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   565
	$_COOKIE['wp-settings-' . $user_id] = $settings;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   566
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   567
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   568
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   569
 * Retrieve user interface setting value based on setting name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   570
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   571
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   572
 * @subpackage Option
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   573
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   574
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   575
 * @param string $name The name of the setting.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   576
 * @param string $default Optional default value to return when $name is not set.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   577
 * @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
   578
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   579
function get_user_setting( $name, $default = false ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   580
	$all_user_settings = get_all_user_settings();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   581
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   582
	return isset( $all_user_settings[$name] ) ? $all_user_settings[$name] : $default;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   583
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   584
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   585
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   586
 * Add or update user interface setting.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   587
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   588
 * Both $name and $value can contain only ASCII letters, numbers and underscores.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   589
 * 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
   590
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   591
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   592
 * @subpackage Option
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   593
 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   594
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   595
 * @param string $name The name of the setting.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   596
 * @param string $value The value for the setting.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   597
 * @return bool true if set successfully/false if not.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   598
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   599
function set_user_setting( $name, $value ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   600
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   601
	if ( headers_sent() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   602
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   603
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   604
	$all_user_settings = get_all_user_settings();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   605
	$all_user_settings[$name] = $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   606
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   607
	return wp_set_all_user_settings( $all_user_settings );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   608
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   609
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   610
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   611
 * Delete user interface settings.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   612
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   613
 * Deleting settings would reset them to the defaults.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   614
 * 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
   615
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   616
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   617
 * @subpackage Option
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   618
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   619
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   620
 * @param mixed $names The name or array of names of the setting to be deleted.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   621
 * @return bool true if deleted successfully/false if not.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   622
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   623
function delete_user_setting( $names ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   624
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   625
	if ( headers_sent() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   626
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   627
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   628
	$all_user_settings = get_all_user_settings();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   629
	$names = (array) $names;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   630
	$deleted = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   631
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   632
	foreach ( $names as $name ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   633
		if ( isset( $all_user_settings[$name] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   634
			unset( $all_user_settings[$name] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   635
			$deleted = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   636
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   637
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   638
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   639
	if ( $deleted )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   640
		return wp_set_all_user_settings( $all_user_settings );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   641
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   642
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   643
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   644
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   645
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   646
 * Retrieve all user interface settings.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   647
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   648
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   649
 * @subpackage Option
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   650
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   651
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   652
 * @return array the last saved user settings or empty array.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   653
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   654
function get_all_user_settings() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   655
	global $_updated_user_settings;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   656
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   657
	if ( ! $user_id = get_current_user_id() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   658
		return array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   659
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   660
	if ( isset( $_updated_user_settings ) && is_array( $_updated_user_settings ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   661
		return $_updated_user_settings;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   662
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   663
	$user_settings = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   664
	if ( isset( $_COOKIE['wp-settings-' . $user_id] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   665
		$cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user_id] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   666
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   667
		if ( $cookie && strpos( $cookie, '=' ) ) // '=' cannot be 1st char
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   668
			parse_str( $cookie, $user_settings );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   669
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   670
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   671
		$option = get_user_option( 'user-settings', $user_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   672
		if ( $option && is_string($option) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   673
			parse_str( $option, $user_settings );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   674
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   675
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   676
	$_updated_user_settings = $user_settings;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   677
	return $user_settings;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   678
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   679
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   680
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   681
 * Private. Set all user interface settings.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   682
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   683
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   684
 * @subpackage Option
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   685
 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   686
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   687
 * @param array $user_settings
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   688
 * @return bool
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   689
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   690
function wp_set_all_user_settings( $user_settings ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   691
	global $_updated_user_settings;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   692
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   693
	if ( ! $user_id = get_current_user_id() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   694
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   695
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   696
	if ( is_super_admin() && ! is_user_member_of_blog() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   697
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   698
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   699
	$settings = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   700
	foreach ( $user_settings as $name => $value ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   701
		$_name = preg_replace( '/[^A-Za-z0-9_]+/', '', $name );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   702
		$_value = preg_replace( '/[^A-Za-z0-9_]+/', '', $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   703
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   704
		if ( ! empty( $_name ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   705
			$settings .= $_name . '=' . $_value . '&';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   706
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   707
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   708
	$settings = rtrim($settings, '&');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   709
	parse_str( $settings, $_updated_user_settings );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   710
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   711
	update_user_option( $user_id, 'user-settings', $settings, false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   712
	update_user_option( $user_id, 'user-settings-time', time(), false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   713
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   714
	return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   715
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   716
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   717
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   718
 * Delete the user settings of the current user.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   719
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   720
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   721
 * @subpackage Option
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   722
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   723
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   724
function delete_all_user_settings() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   725
	if ( ! $user_id = get_current_user_id() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   726
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   727
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   728
	update_user_option( $user_id, 'user-settings', '', false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   729
	setcookie('wp-settings-' . $user_id, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   730
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   731
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   732
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   733
 * Retrieve site option value based on name of option.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   734
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   735
 * @see get_option()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   736
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   737
 * @subpackage Option
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   738
 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   739
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   740
 * @uses apply_filters() Calls 'pre_site_option_$option' before checking the option.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   741
 * 	Any value other than false will "short-circuit" the retrieval of the option
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   742
 *	and return the returned value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   743
 * @uses apply_filters() Calls 'site_option_$option', after checking the  option, with
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   744
 * 	the option value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   745
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   746
 * @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
   747
 * @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
   748
 * @param bool $use_cache Whether to use cache. Multisite only. Default true.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   749
 * @return mixed Value set for the option.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   750
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   751
function get_site_option( $option, $default = false, $use_cache = true ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   752
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   753
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   754
	// Allow plugins to short-circuit site options.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   755
 	$pre = apply_filters( 'pre_site_option_' . $option, false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   756
 	if ( false !== $pre )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   757
 		return $pre;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   758
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   759
	// prevent non-existent options from triggering multiple queries
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   760
	$notoptions = wp_cache_get( 'notoptions', 'site-options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   761
	if ( isset( $notoptions[$option] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   762
		return apply_filters( 'default_site_option_' . $option, $default );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   763
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   764
	if ( ! is_multisite() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   765
		$default = apply_filters( 'default_site_option_' . $option, $default );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   766
		$value = get_option($option, $default);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   767
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   768
		$cache_key = "{$wpdb->siteid}:$option";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   769
		if ( $use_cache )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   770
			$value = wp_cache_get($cache_key, 'site-options');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   771
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   772
		if ( !isset($value) || (false === $value) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   773
			$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
   774
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   775
			// 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
   776
			if ( is_object( $row ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   777
				$value = $row->meta_value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   778
				$value = maybe_unserialize( $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   779
				wp_cache_set( $cache_key, $value, 'site-options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   780
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   781
				$notoptions[$option] = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   782
				wp_cache_set( 'notoptions', $notoptions, 'site-options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   783
				$value = apply_filters( 'default_site_option_' . $option, $default );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   784
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   785
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   786
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   787
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   788
 	return apply_filters( 'site_option_' . $option, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   789
}
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
 * Add a new site option.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   793
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   794
 * 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
   795
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   796
 * @see add_option()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   797
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   798
 * @subpackage Option
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   799
 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   800
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   801
 * @uses apply_filters() Calls 'pre_add_site_option_$option' hook to allow overwriting the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   802
 * 	option value to be stored.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   803
 * @uses do_action() Calls 'add_site_option_$option' and 'add_site_option' hooks on success.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   804
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   805
 * @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
   806
 * @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
   807
 * @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
   808
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   809
function add_site_option( $option, $value ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   810
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   811
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   812
	wp_protect_special_option( $option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   813
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   814
	$value = apply_filters( 'pre_add_site_option_' . $option, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   815
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   816
	if ( !is_multisite() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   817
		$result = add_option( $option, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   818
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   819
		$cache_key = "{$wpdb->siteid}:$option";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   820
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   821
		// 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
   822
		$notoptions = wp_cache_get( 'notoptions', 'site-options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   823
		if ( ! is_array( $notoptions ) || ! isset( $notoptions[$option] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   824
			if ( false !== get_site_option( $option ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   825
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   826
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   827
		$value = sanitize_option( $option, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   828
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   829
		$serialized_value = maybe_serialize( $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   830
		$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
   831
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   832
		if ( ! $result )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   833
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   834
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   835
		wp_cache_set( $cache_key, $value, 'site-options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   836
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   837
		// This option exists now
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   838
		$notoptions = wp_cache_get( 'notoptions', 'site-options' ); // yes, again... we need it to be fresh
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   839
		if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   840
			unset( $notoptions[$option] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   841
			wp_cache_set( 'notoptions', $notoptions, 'site-options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   842
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   843
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   844
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   845
	if ( $result ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   846
		do_action( "add_site_option_{$option}", $option, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   847
		do_action( "add_site_option", $option, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   848
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   849
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   850
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   851
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   852
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   853
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   854
 * Removes site option by name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   855
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   856
 * @see delete_option()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   857
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   858
 * @subpackage Option
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   859
 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   860
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   861
 * @uses do_action() Calls 'pre_delete_site_option_$option' hook before option is deleted.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   862
 * @uses do_action() Calls 'delete_site_option' and 'delete_site_option_$option'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   863
 * 	hooks on success.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   864
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   865
 * @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
   866
 * @return bool True, if succeed. False, if failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   867
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   868
function delete_site_option( $option ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   869
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   870
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   871
	// ms_protect_special_option( $option ); @todo
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   872
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   873
	do_action( 'pre_delete_site_option_' . $option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   874
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   875
	if ( !is_multisite() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   876
		$result = delete_option( $option );
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
		$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
   879
		if ( is_null( $row ) || !$row->meta_id )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   880
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   881
		$cache_key = "{$wpdb->siteid}:$option";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   882
		wp_cache_delete( $cache_key, 'site-options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   883
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   884
		$result = $wpdb->delete( $wpdb->sitemeta, array( 'meta_key' => $option, 'site_id' => $wpdb->siteid ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   885
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   886
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   887
	if ( $result ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   888
		do_action( "delete_site_option_{$option}", $option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   889
		do_action( "delete_site_option", $option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   890
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   891
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   892
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   893
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   894
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   895
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   896
 * Update the value of a site option that was already added.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   897
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   898
 * @see update_option()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   899
 * @since 2.8.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   900
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   901
 * @subpackage Option
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   902
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   903
 * @uses apply_filters() Calls 'pre_update_site_option_$option' hook to allow overwriting the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   904
 * 	option value to be stored.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   905
 * @uses do_action() Calls 'update_site_option_$option' and 'update_site_option' hooks on success.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   906
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   907
 * @param string $option Name of option. Expected to not be SQL-escaped.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   908
 * @param mixed $value Option value. Expected to not be SQL-escaped.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   909
 * @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
   910
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   911
function update_site_option( $option, $value ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   912
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   913
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   914
	wp_protect_special_option( $option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   915
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   916
	$old_value = get_site_option( $option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   917
	$value = apply_filters( 'pre_update_site_option_' . $option, $value, $old_value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   918
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   919
	if ( $value === $old_value )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   920
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   921
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   922
	if ( false === $old_value )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   923
		return add_site_option( $option, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   924
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   925
	$notoptions = wp_cache_get( 'notoptions', 'site-options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   926
	if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   927
		unset( $notoptions[$option] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   928
		wp_cache_set( 'notoptions', $notoptions, 'site-options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   929
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   930
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   931
	if ( !is_multisite() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   932
		$result = update_option( $option, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   933
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   934
		$value = sanitize_option( $option, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   935
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   936
		$serialized_value = maybe_serialize( $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   937
		$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
   938
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   939
		if ( $result ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   940
			$cache_key = "{$wpdb->siteid}:$option";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   941
			wp_cache_set( $cache_key, $value, 'site-options' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   942
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   943
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   944
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   945
	if ( $result ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   946
		do_action( "update_site_option_{$option}", $option, $value, $old_value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   947
		do_action( "update_site_option", $option, $value, $old_value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   948
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   949
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   950
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   951
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   952
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   953
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   954
 * Delete a site transient.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   955
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   956
 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   957
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   958
 * @subpackage Transient
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   959
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   960
 * @uses do_action() Calls 'delete_site_transient_$transient' hook before transient is deleted.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   961
 * @uses do_action() Calls 'deleted_site_transient' hook on success.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   962
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   963
 * @param string $transient Transient name. Expected to not be SQL-escaped.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   964
 * @return bool True if successful, false otherwise
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   965
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   966
function delete_site_transient( $transient ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   967
	do_action( 'delete_site_transient_' . $transient, $transient );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   968
	if ( wp_using_ext_object_cache() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   969
		$result = wp_cache_delete( $transient, 'site-transient' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   970
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   971
		$option_timeout = '_site_transient_timeout_' . $transient;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   972
		$option = '_site_transient_' . $transient;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   973
		$result = delete_site_option( $option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   974
		if ( $result )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   975
			delete_site_option( $option_timeout );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   976
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   977
	if ( $result )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   978
		do_action( 'deleted_site_transient', $transient );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   979
	return $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   980
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   981
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   982
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   983
 * Get the value of a site transient.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   984
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   985
 * If the transient does not exist or does not have a value, then the return value
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   986
 * will be false.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   987
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   988
 * @see get_transient()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   989
 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   990
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   991
 * @subpackage Transient
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   992
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   993
 * @uses apply_filters() Calls 'pre_site_transient_$transient' hook before checking the transient.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   994
 * 	Any value other than false will "short-circuit" the retrieval of the transient
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   995
 *	and return the returned value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   996
 * @uses apply_filters() Calls 'site_transient_$option' hook, after checking the transient, with
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   997
 * 	the transient value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   998
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   999
 * @param string $transient Transient name. Expected to not be SQL-escaped.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1000
 * @return mixed Value of transient
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1001
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1002
function get_site_transient( $transient ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1003
	$pre = apply_filters( 'pre_site_transient_' . $transient, false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1004
	if ( false !== $pre )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1005
		return $pre;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1006
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1007
	if ( wp_using_ext_object_cache() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1008
		$value = wp_cache_get( $transient, 'site-transient' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1009
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1010
		// 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
  1011
		$no_timeout = array('update_core', 'update_plugins', 'update_themes');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1012
		$transient_option = '_site_transient_' . $transient;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1013
		if ( ! in_array( $transient, $no_timeout ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1014
			$transient_timeout = '_site_transient_timeout_' . $transient;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1015
			$timeout = get_site_option( $transient_timeout );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1016
			if ( false !== $timeout && $timeout < time() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1017
				delete_site_option( $transient_option  );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1018
				delete_site_option( $transient_timeout );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1019
				$value = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1020
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1021
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1022
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1023
		if ( ! isset( $value ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1024
			$value = get_site_option( $transient_option );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1025
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1026
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1027
	return apply_filters( 'site_transient_' . $transient, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1028
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1029
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1030
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1031
 * Set/update the value of a site transient.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1032
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1033
 * 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
  1034
 * it will be serialized before it is set.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1035
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1036
 * @see set_transient()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1037
 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1038
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1039
 * @subpackage Transient
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1040
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1041
 * @uses apply_filters() Calls 'pre_set_site_transient_$transient' hook to allow overwriting the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1042
 * 	transient value to be stored.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1043
 * @uses do_action() Calls 'set_site_transient_$transient' and 'setted_site_transient' hooks on success.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1044
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1045
 * @param string $transient Transient name. Expected to not be SQL-escaped.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1046
 * @param mixed $value Transient value. Expected to not be SQL-escaped.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1047
 * @param int $expiration Time until expiration in seconds, default 0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1048
 * @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
  1049
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1050
function set_site_transient( $transient, $value, $expiration = 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1051
	$value = apply_filters( 'pre_set_site_transient_' . $transient, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1052
	$expiration = (int) $expiration;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1053
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1054
	if ( wp_using_ext_object_cache() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1055
		$result = wp_cache_set( $transient, $value, 'site-transient', $expiration );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1056
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1057
		$transient_timeout = '_site_transient_timeout_' . $transient;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1058
		$option = '_site_transient_' . $transient;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1059
		if ( false === get_site_option( $option ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1060
			if ( $expiration )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1061
				add_site_option( $transient_timeout, time() + $expiration );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1062
			$result = add_site_option( $option, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1063
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1064
			if ( $expiration )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1065
				update_site_option( $transient_timeout, time() + $expiration );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1066
			$result = update_site_option( $option, $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1067
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1068
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1069
	if ( $result ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1070
		do_action( 'set_site_transient_' . $transient, $value, $expiration );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1071
		do_action( 'setted_site_transient', $transient, $value, $expiration );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1072
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1073
	return $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1074
}