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