You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

2013 lines
62 KiB

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