option.php 66 KB

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