option.php 68 KB

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