Reduce the number of queries getting and setting transients on sites without a persistent cache.
Transients are stored in two options: one each for the transient value and timeout. Priming the cache reduces the database queries for getting a transient from two to one.
Props peterwilsoncc, swissspidy.
Fixes#61193.
Built from https://develop.svn.wordpress.org/trunk@58134
git-svn-id: http://core.svn.wordpress.org/trunk@57599 1a063a9b-81f0-0310-95a4-ce76da25c4cd
An excessive amount of autoloaded options is a common cause for slow database responses, sometimes caused by very large individual autoloaded options. As it is not mandatory to provide an autoload value when adding an option to the database, it tends to be ignored, which in combination with a default value of "yes" and lack of documentation can lead to the aforementioned problem.
This changeset enhances the option autoloading behavior in several ways:
* Update the function documentation to encourage the use of boolean `true` or `false` to explicitly provide an autoload value for an option.
* Use new string values `on` and `off` for explicitly provided values stored in the database, to distinguish them from `yes` and `no`, since `yes` does not allow determining whether it was set intentionally by the developer or only as a default.
* Effectively deprecate the values `yes` and `no`. They are still supported for backward compatibility, but now discouraged.
* Use `null` as new default autoload value for `add_option()`. If the developer does not provide an explicit value, this will now trigger WordPress logic to determine an autoload value to use:
* If WordPress determines that the option should not be autoloaded, it is stored in the database as `auto-off`. As part of this changeset, the single heuristic introduced for that is to check whether the option size is larger than a threshold of 150k bytes. This threshold is filterable via a new `wp_max_autoloaded_option_size` filter.
* If WordPress determines that the option should be autoloaded, it is stored in the database as `auto-on`. No logic to make such a decision is introduced as part of this changeset, but a new filter `wp_default_autoload_value` can be used to define such heuristics, e.g. by optimization plugins.
* If WordPress cannot determine whether or not to autoload the option, it is stored in the database as `auto`.
* This effectively means that any option without an explicit autoload value provided by the developer will be stored with an autoload value of `auto`, unless the option's size exceeds the aforementioned threshold. Options with a value of `auto` are still autoloaded as of today, most importantly for backward compatibility. A new function `wp_autoload_values_to_autoload()` returns the list of autolaod values that dictate for an option to be autoloaded, and a new filter `wp_autoload_values_to_autoload` can be used to alter that list.
These behavioral changes encourage developers to be more mindful of autoloading, while providing WordPress core and optimization plugins with additional control over heuristics for autoloading options where no explicit autoload value was provided.
At the same time, the changes are fully backward compatible from a functionality perspective, with the only exception being that very large options will now no longer be autoloaded if the developer did not explicitly request for them to be autoloaded. Neither WordPress core nor plugins are able to override an explicitly provided value, which is intentional to continue giving developers full control over their own options.
Props pbearne, flixos90, joemcgill, azaozz, spacedmonkey, swissspidy, mukesh27, markjaquith.
Fixes#42441.
Built from https://develop.svn.wordpress.org/trunk@57920
git-svn-id: http://core.svn.wordpress.org/trunk@57421 1a063a9b-81f0-0310-95a4-ce76da25c4cd
A collection of fixes for `wp_prime_option_caches()`:
* cache arrays and objects in their serialized form for consistency with `get_option()` and `wp_load_alloptions()`
* prevent repeat database queries for falsey and known non-existent options (notoptions)
Additional tests for `wp_prime_option_caches()` to ensure:
* additional database queries are not made repriming options (known, known-unknown and alloptions)
* cache is primed consistently
* `get_option()` returns a consistent value regardless of how it is primed
* database queries do not contain earlier primed options
* `get_option` does not prime the cache when testing the cache has been successfully primed
Fixes a test for `wp_prime_option_caches_by_group()` to ensure `get_option` does not prime the cache when testing the cache has been successfully primed.
Follow up to [56445],[56990],[57013].
Props peterwilsoncc, costdev, flixos90, hellofromTonya, mikeschroder, joemcgill.
Fixes#59738. See #58962.
Built from https://develop.svn.wordpress.org/trunk@57029
git-svn-id: http://core.svn.wordpress.org/trunk@56540 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Rename the option cache priming functions to more closely follow the naming convention used by other cache priming functions.
* `wp_load_options()` becomes `wp_prime_option_caches()`
* `wp_load_options_by_group()` becomes `wp_prime_option_caches_by_group()`
The unit test files and classes are renamed accordingly.
Unlike the existing cache priming functions, these functions were introduced with the intention of being public so use the `wp_` prefix rather than the `_` prefix used by the functions initially introduced as private functions but since made public.
Follow up to [56445],[56990].
Props flixos90, peterwilsoncc, joemcgill, SergeyBiryukov, desrosj.
Fixes#58962.
Built from https://develop.svn.wordpress.org/trunk@57013
git-svn-id: http://core.svn.wordpress.org/trunk@56524 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This clearly separates these functions which are intended to be used by external developers from the existing `_prime_*_caches()` functions which are primarily intended for internal usage. The term "load" is additionally more accessible than "prime".
This changeset renames the above function, as well as the wrapper function `prime_options_by_group()` to `wp_load_options_by_group()`.
Props peterwilsoncc, joemcgill, hellofromTonya, poran766, flixos90.
Fixes#58962.
Built from https://develop.svn.wordpress.org/trunk@56990
git-svn-id: http://core.svn.wordpress.org/trunk@56501 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This reverts changes from [56648], [56681], [56717], [56762], [56788], [56797], and [56814] to restore the behavior for `update_option()` and `update_network_option()` to their prior state as of 6.3.X due to the discovery of various backwards compatibility issues found late in the 6.4 release cycle.
Props mukesh27, costdev, flixos90, spacedmonkey, snicco, jrf, joemcgill.
See #22192, #59360.
Built from https://develop.svn.wordpress.org/trunk@56946
git-svn-id: http://core.svn.wordpress.org/trunk@56457 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Checks if the given `$option_group` exists before searching for the `$option_name`. Sets the `$pos` to `false`, as `array_search()` returns `false` if the option name (needle) does not exist.
This changeset fixes 2 different PHP Warning|Notice scenarios:
1. When the global `$new_allowed_options` is `null`, fixes raising `Trying to access array offset on value of type null` PHP Notice (PHP 7.4) | Warning (on PHP 8).
2. When the global `$new_allowed_options` is an `array` and the setting group key does not exist, fixes raising "Undefined index: unknown_setting_group" PHP Notice (PHP 7) | Warning (on PHP 8).
For both scenarios, the `array_search()` is skipped and the `$pos` is set to a default of `false`, i.e. which is the value returned when `array_search()` is unsuccessful.
Props xknown, hellofromTonya, nicolefurlan, oglekler, SergeyBiryukov, shailu25.
Fixes#57674.
Built from https://develop.svn.wordpress.org/trunk@56817
git-svn-id: http://core.svn.wordpress.org/trunk@56329 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Previously, in the update_network_option function, strict comparisons between old and new values could erroneously trigger updates in cases where there was no actual change in values. Building upon the groundwork laid in #22192, this commit introduces the use of the _is_equal_database_value function to accurately compare values before initiating any database updates. This change ensures consistency between the behaviors of `update_option` and `update_network_option`.
Furthermore, we have incorporated similar workarounds that were previously implemented in [56717]. These changes ensure that the raw version of network option values is considered in the comparisons, enhancing the overall reliability of the update process.
Props mukesh27, flixos90, joemcgill, costdev, spacedmonkey.
Fixes#59360.
Built from https://develop.svn.wordpress.org/trunk@56814
git-svn-id: http://core.svn.wordpress.org/trunk@56326 1a063a9b-81f0-0310-95a4-ce76da25c4cd
When using the `$autoload` parameter of `update_option()` alongside an option value update, prior to this changeset the function would update the incorrect cache, not respecting the new autoload value. This could have severe implications such as returning a stale option value when the option in fact had already been deleted.
This changeset fixes the bug alongside test coverage that failed with `trunk` but now passes.
Props kkmuffme, pentatonicfunk, SergeyBiryukov, oglekler, azaozz, spacedmonkey, nicolefurlan, joemcgill, flixos90.
Fixes#51352.
Built from https://develop.svn.wordpress.org/trunk@56796
git-svn-id: http://core.svn.wordpress.org/trunk@56308 1a063a9b-81f0-0310-95a4-ce76da25c4cd
When calling `update_option()` with value `false` on a non-existent option, prior to [56681] the function would have returned `false` and not stored the value in the database, since the given value was the same as the default.
The aforementioned changeset broke that promise with good intention, however this particular change was a backward compatibility break and therefore is resolved here.
Props mukesh27, costdev.
Fixes#22192.
Built from https://develop.svn.wordpress.org/trunk@56788
git-svn-id: http://core.svn.wordpress.org/trunk@56300 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Prior to this change, a strict comparison between the old and new database value could lead to a false negative, since database values are generally stored as strings. For example, passing an integer to `update_option()` would almost always result in an update given any existing database value for that option would be that number cast to a string.
This changeset adjusts the logic to perform an intentional "loose-y" comparison by casting the values to strings. Extensive coverage previously added in [56648] provides additional confidence that this does not introduce any backward compatibility issues.
Props mukesh27, costdev, spacedmonkey, joemcgill, flixos90, nacin, atimmer, duck_, boonebgorges.
Fixes#22192.
Built from https://develop.svn.wordpress.org/trunk@56681
git-svn-id: http://core.svn.wordpress.org/trunk@56193 1a063a9b-81f0-0310-95a4-ce76da25c4cd
In the get_option function, a cache lookup for the notoptions key is performed, which stores an array of keys for options known not to exist. This optimization prevents repeated database queries when certain options are requested. However, the cache lookup for notoptions was conducted before checking if the requested option exists in the cache. Given that it's more likely that the option does exist, this commit reorders the checks to first verify the option's existence in the cache before confirming its absence. This adjustment reduces redundant queries and also eliminates an unnecessary cache lookup, improving overall performance.
Props spacedmonkey, costdev, flixos90, azaozz.
Fixes#58277.
Built from https://develop.svn.wordpress.org/trunk@56595
git-svn-id: http://core.svn.wordpress.org/trunk@56107 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This function accepts an associative array of option names and their autoload values to set, and it will update those values in the database in bulk, only for those options where the autoload field is not already set to the given value.
Two wrapper functions for ease of use accompany the new main function:
* `wp_set_options_autoload( $options, $autoload )` can be used to set multiple options to the same autoload value.
* `wp_set_option_autoload( $option, $autoload )` can be used to set the autoload value for a single option.
All of these functions allow changing the autoload value of an option, which previously has only been possible in combination with updating the value. This limitation prevented some relevant use-cases: For example, a plugin deactivation hook could set all of its options to not autoload, as a cleanup routine, while not actually deleting any data. The plugin's activation hook could then implement the reverse, resetting those options' autoload values to the originally intended ones for when using the plugin.
Props boonebgorges, joemcgill, costdev, mukesh27, SergeyBiryukov, tabrisrp, flixos90.
Fixes#58964.
Built from https://develop.svn.wordpress.org/trunk@56508
git-svn-id: http://core.svn.wordpress.org/trunk@56020 1a063a9b-81f0-0310-95a4-ce76da25c4cd
WordPress's `get_option()` function generally relies on making individual database requests for each option, however with the majority of options (in most cases) being autoloaded, i.e. fetched once with a single database request and then stored in (memory) cache.
As part of a greater effort to reduce the amount of options that are unnecessarily autoloaded, this changeset introduces an alternative way to retrieve multiple options in a performant manner, with a single database request. This provides a reasonable alternative for e.g. plugins that use several options which only need to be loaded in a few specific screens.
Specifically, this changeset introduces the following functions:
* `prime_options( $options )` is the foundation to load multiple specific options with a single database request. Only options that aren't already cached (in `alloptions` or an individual cache) are retrieved from the database.
* `prime_options_by_group( $option_group )` is a convenience wrapper function for the above which allows to prime all options of a specific option group (as configured via `register_setting()`).
* `get_options( $options )` is another wrapper function which first primes the requested options and then returns them in an associative array, calling `get_option()` for each of them.
Props mukesh27, joemcgill, costdev, olliejones.
Fixes#58962.
Built from https://develop.svn.wordpress.org/trunk@56445
git-svn-id: http://core.svn.wordpress.org/trunk@55957 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This changeset expands documentation of the `$autoload` parameter of the `add_option()` and `update_option()` functions, in order to provide more context on what autoloading is and which considerations should go into the decision whether to autoload an option.
Excessive autoloading of options can lead to severe performance problems on some sites, and lack of documentation is a partial cause for the issue.
Props rajinsharwar.
Fixes#58963.
Built from https://develop.svn.wordpress.org/trunk@56370
git-svn-id: http://core.svn.wordpress.org/trunk@55882 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Tweak the `wp_load_core_site_options` function, to also prime network options using `wp_cache_get_multiple` if persistent object cache is present.
Props mukesh27, oglekler, peterwilsoncc, costdev, jeremyfelt, spacedmonkey.
Fixes#56913.
Built from https://develop.svn.wordpress.org/trunk@56061
git-svn-id: http://core.svn.wordpress.org/trunk@55573 1a063a9b-81f0-0310-95a4-ce76da25c4cd
While using reserved PHP keywords as parameter name labels is allowed, in the context of function calls using named parameters in PHP 8.0+, this will easily lead to confusion. To avoid that, it is recommended not to use reserved keywords as function parameter names.
This commit renames the `$default` parameter to `$default_value` in:
* `get_option()`
* `get_user_setting()`
* `get_site_option()`
* `get_network_option()`
* `filter_default_option()`
Follow-up to [52946], [52996], [52997], [52998], [53003], [53014], [53029], [53039], [53116], [53117], [53137], [53174], [53184], [53185], [53192], [53193], [53198], [53203], [53207], [53215], [53216], [53220], [53230], [53232], [53236], [53239], [53240], [53242], [53243], [53245], [53246], [53257], [53269], [53270], [53271], [53272], [53273], [53274], [53275], [53276], [53277], [53281], [53283], [53284], [53285], [53287], [53364], [53365], [54927], [54929], [54930], [54931], [54932], [54933], [54938], [54943], [54944], [54945], [54946], [54947].
Props jrf, aristath, poena, justinahinon, SergeyBiryukov.
See #56788.
Built from https://develop.svn.wordpress.org/trunk@54948
git-svn-id: http://core.svn.wordpress.org/trunk@54500 1a063a9b-81f0-0310-95a4-ce76da25c4cd
[54080] refactored the logic in `get_network_option()`, `update_network_option()` and `delete_network_option()` to use the metadata API. However, this change resulted in issues with large multisite installs that utilize memcached having network options > 1MB in size.
This change reverts [54080] and all related follow-up changes.
Reverts [54080], [54081], and [54082]. Partially reverts [54267] and [54402].
Props pavelschoffer, rebasaurus, johnbillion, spacedmonkey, desrosj, rinatkhaziev.
Fixes#56845.
See #37181.
Built from https://develop.svn.wordpress.org/trunk@54637
git-svn-id: http://core.svn.wordpress.org/trunk@54189 1a063a9b-81f0-0310-95a4-ce76da25c4cd
When the `notoptions` key does not exist in a persistent object cache, it was requested hundreds of times until the first not-option is written.
This commit improves performance by setting the value to an empty array to prevent non-existent `notoptions` key from triggering multiple key lookups.
Follow-up to [4855], [14515].
Props tillkruess, dd32, spacedmonkey.
Fixes#56639.
Built from https://develop.svn.wordpress.org/trunk@54345
git-svn-id: http://core.svn.wordpress.org/trunk@53904 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Although a `pre_option_{$option}` filter already exists, this change adds a more general `pre_option` filter that will run on every `get_option` call. This brings the control flow into similar flow as `update_option`.
Props flixos90, NathanAtmoz, desrosj, spacedmonkey, pbearne.
Fixes#37930.
Built from https://develop.svn.wordpress.org/trunk@54145
git-svn-id: http://core.svn.wordpress.org/trunk@53704 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Replace logic found in `get_network_option`, `update_network_option` and `delete_network_option` to use the metadata api. Using the metadata api has a number of benefits, such as consistency, default values and useful filters. This change also improves performance by priming the caches of all network options in a single database request.
Props spacedmonkey, swissspidy, sc0ttkclark, johnjamesjacoby, flixos90, jeremyfelt, pento, peterwilsoncc, mukesh27, desrosj.
Fixes#37181
Built from https://develop.svn.wordpress.org/trunk@54080
git-svn-id: http://core.svn.wordpress.org/trunk@53639 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This is a micro-optimization that makes the conditional several times faster for non-deprecated options by avoiding an unnecessary function call.
Functions affected:
* `get_option()`
* `update_option()`
* `add_option()`
Follow-up to [48575].
See #55647.
Built from https://develop.svn.wordpress.org/trunk@53914
git-svn-id: http://core.svn.wordpress.org/trunk@53473 1a063a9b-81f0-0310-95a4-ce76da25c4cd
The associated changes were backported to the 6.0 branch, so the version should be 6.0.1 instead of 6.1.0.
This affects:
* `@since` tag for the `block-templates` feature in `create_initial_theme_features()`.
* `@since` tag for the `show_on_front`, `page_on_front`, and `page_for_posts` options on the REST API settings endpoint.
Follow-up to [53590], [53661].
Props tommusrhodus, johnbillion.
Fixes#56225.
Built from https://develop.svn.wordpress.org/trunk@53704
git-svn-id: http://core.svn.wordpress.org/trunk@53263 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This adjusts the newly added options in the settings endpoint to use the `integer` type instead of `number`. Since these are page IDs and are not supposed to be floats, `integer` is the correct type.
Follow-up to [53588].
See #56058.
Built from https://develop.svn.wordpress.org/trunk@53589
git-svn-id: http://core.svn.wordpress.org/trunk@53177 1a063a9b-81f0-0310-95a4-ce76da25c4cd
The `wp_user_settings()` function calls the PHP native `setcookie()` function, the fifth parameter of which is the ''optional'' `$domain` parameter which expects a `string`.
A parameter being optional, however, does not automatically make it nullable.
As of PHP 8.1, passing `null` to a non-nullable PHP native function will generate a deprecation notice.
In this case, this function call yielded a `setcookie(): Passing null to parameter #5 ($domain) of type string is deprecated` notice.
Changing the `null` to an empty string fixes this without a backward compatibility break.
References:
* [https://www.php.net/manual/en/function.setcookie.php PHP Manual: setcookie()]
* [https://wiki.php.net/rfc/deprecate_null_to_scalar_internal_arg PHP RFC: Deprecate passing null to non-nullable arguments of internal functions]
Follow-up to [29478].
Props ocean90, shenyanzhi, meysamnorouzi, jrf.
Fixes#54914.
Built from https://develop.svn.wordpress.org/trunk@53490
git-svn-id: http://core.svn.wordpress.org/trunk@53079 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Implement the `wp_cache_add_multiple`, `wp_cache_set_multiple` and `wp_cache_delete_multiple` in a number of core functions after they were introduced in [52700]
Props: spacedmonkey, adamsilverstein, flixos90, mitogh.
Fixes: #55029.
Built from https://develop.svn.wordpress.org/trunk@52707
git-svn-id: http://core.svn.wordpress.org/trunk@52296 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Prevent the transient setters and getters from attempting to use the database table before they exist during the installation process.
During installation transients now use the `wp_cache_*()` functions on all sites, including those without a persistent cache, to prevent database errors. The use of the caching functions stores the data in memory for the duration of the request to account for transient data that is used multiple times during installation.
Props dd32, audrasjb, tnolte, antonvlasenko, noisysocks, peterwilsoncc.
Fixes#54849.
Built from https://develop.svn.wordpress.org/trunk@52694
git-svn-id: http://core.svn.wordpress.org/trunk@52283 1a063a9b-81f0-0310-95a4-ce76da25c4cd
In all four of the `get_option()`, `add_option()`, `update_option()` and `delete_option()` functions, the `$option` parameter (i.e. the option name) is passed to the PHP native `trim()` function without prior input validation.
In PHP 8.1, this could lead to a `trim(): Passing null to parameter #1 ($string) of type string is deprecated` for each of these functions.
`trim()`:
- expects a text string and is only useful when ''passed'' a text string as no other variable type can contain whitespace.
- will always return a `string`, which means that in practice for any non-string values passed, it would effectively function as a type cast to string.
This commit:
- Adds a check to verify the `$option` name is a scalar before processing it with `trim()`.
- The "type cast" behavior is maintained.
- If the given `$option` name is not a scalar, such as `null`, the fix prevents the PHP 8.1 deprecation notice.
- Tests are added for valid but undesired option names to safeguard against regressions.
This issue is already covered by:
- the existing `Tests_Option_Option::test_bad_option_names()` test group.
- the new `test_valid_but_undesired_option_names()` tests.
Follow-up to [13858], [22633], [23510], [25002], [51817].
Props jrf, hellofromTonya, pbearne.
See #53635.
Built from https://develop.svn.wordpress.org/trunk@51818
git-svn-id: http://core.svn.wordpress.org/trunk@51425 1a063a9b-81f0-0310-95a4-ce76da25c4cd