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