/wp-includes/option.php

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