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