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