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