Нет описания

option.php 75KB

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