Skip to content

✨ New WordPress.WP.OptionAutoload sniff #2520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 26 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
fb40a73
✨ New WordPress.WP.OptionAutoload sniff
rodrigoprimo Aug 26, 2024
f9f2b95
Apply suggestions from code review
rodrigoprimo Feb 28, 2025
f18b9ed
Attemp to improve the XML documentation by combining standards
rodrigoprimo Feb 28, 2025
18c8a31
Fix unintentional coding standard violation in a test case
rodrigoprimo Mar 11, 2025
1b5b8bf
Update some tests to have more function name case variations
rodrigoprimo Mar 12, 2025
5ac7e1c
Update some tests to have more whitespaces, comments and annotations
rodrigoprimo Mar 12, 2025
d5ec3a2
Use `false === isset()` instead of `! isset()` for consistency with t…
rodrigoprimo Mar 12, 2025
a777a97
Improve sniff performance by changing the format of some properties
rodrigoprimo Mar 12, 2025
39524bc
Apply suggestions from code review
rodrigoprimo Jul 4, 2025
62532db
Remove unused parameter
rodrigoprimo Jul 4, 2025
60a85c9
Rename test case file to be able to add more test case files with syn…
rodrigoprimo Jul 4, 2025
c8bee3d
Handle case when $array_token is false + add a test for it
rodrigoprimo Jul 4, 2025
f506c0b
Improve sniff XML doc following suggestions from code review
rodrigoprimo Jul 4, 2025
5b58aba
Add link to external blog post about when to use the autoload flag
rodrigoprimo Jul 4, 2025
e85d37f
Add backticks to all function calls in the docblocks
rodrigoprimo Jul 4, 2025
d6dbd92
Rename property `valid_values_other_functions` to `valid_values_wp_se…
rodrigoprimo Jul 4, 2025
c17ed01
Explain in the docblock the array properties where all values are `true`
rodrigoprimo Jul 4, 2025
21a5350
Ensure all `@param array` tags explain the structure of the array
rodrigoprimo Jul 4, 2025
325f3ba
Only record `param missing` metric when the autoload parameter is opt…
rodrigoprimo Jul 4, 2025
4944d88
Use array union instead of `array_merge()` to create the list of know…
rodrigoprimo Jul 15, 2025
2b48e41
Use one error code for fixable internal values and another for non-fi…
rodrigoprimo Jul 15, 2025
4c7b209
Update if condition to explicitly check what is expected and add a te…
rodrigoprimo Jul 15, 2025
2a5815a
Add another test without whitespaces surrounding the parameter to tes…
rodrigoprimo Jul 15, 2025
6c930ea
Add a comment explaining why `null` needs special treatment on line 361
rodrigoprimo Jul 15, 2025
4c7d770
Explicitly check if $param_second_token is an integer
rodrigoprimo Jul 15, 2025
e13b23b
Move the third block of the documentation up as suggestion during cod…
rodrigoprimo Jul 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions WordPress-Extra/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,13 @@
https://github.com/WordPress/WordPress-Coding-Standards/issues/2459 -->
<rule ref="WordPress.WP.GetMetaSingle"/>

<!-- Flags calls to add_option(), update_option(), wp_set_options_autoload(),
wp_set_option_autoload(), wp_set_option_autoload_values() functions when the
`$autoload` parameter is missing or contain an invalid, internal-only or
deprecated value.
https://github.com/WordPress/WordPress-Coding-Standards/issues/2473 -->
<rule ref="WordPress.WP.OptionAutoload"/>

<!--
#############################################################################
Code style sniffs for more recent PHP features and syntaxes.
Expand Down
100 changes: 100 additions & 0 deletions WordPress/Docs/WP/OptionAutoloadStandard.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?xml version="1.0"?>
<documentation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://phpcsstandards.github.io/PHPCSDevTools/phpcsdocs.xsd"
title="Option Autoload"
>
<standard>
<![CDATA[
When using `add_option()`, `update_option()`, `wp_set_options_autoload()`,
`wp_set_option_autoload()`, or `wp_set_option_autoload_values()`, it is recommended to
explicitly set the autoload parameter to ensure predictable behavior.
The autoload flag determines whether the option is automatically loaded on every page load,
which can impact site performance.
]]>
</standard>
<standard>
<![CDATA[
Even though the autoload parameter is optional for `add_option()` and `update_option()`, it is
strongly recommended to always explicitly set it.
]]>
</standard>
<code_comparison>
<code title="Valid: Explicitly setting the autoload parameter.">
<![CDATA[
add_option(
'my_option',
'value',
'',
<em>true</em>
);
]]>
</code>
<code title="Invalid: Autoload parameter missing.">
<![CDATA[
add_option(
'my_option',
'value',
''
);
]]>
</code>
</code_comparison>
<standard>
<![CDATA[
The following values should be used when setting the autoload parameter depending on the function:

- For `add_option()` and `update_option()`: `true`, `false`, or `null`.
- For `wp_set_option_autoload_values()`, `wp_set_option_autoload()`, and
`wp_set_options_autoload()`: `true` or `false`.

Using 'yes' or 'no' is deprecated since WordPress 6.6.0. Plugin/theme code should also not use
values for the parameter which are for WP core internal use only.

Any other values, including `null` for functions other than `add_option()` and `update_option()`,
are invalid.
]]>
</standard>
<code_comparison>
<code title="Valid: Using a boolean value or `null`">
<![CDATA[
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the valid example have an example from both function groups ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

wp_set_options_autoload(
array('option1', 'option2'),
<em>true</em>
);

update_option(
'my_option',
'value',
<em>null</em>
);
]]>
</code>
<code title="Invalid: Using deprecated, internal-only, or invalid values">
<![CDATA[
add_option(
'my_option',
'value',
'',
<em>'yes'</em>
);

wp_set_option_autoload(
'my_option',
<em>null</em>
);

wp_set_option_autoload_values(
array(
'option1' => <em>'auto-on'</em>,
'option2' => <em>'off'</em>
)
);

wp_set_options_autoload(
array('option1', 'option2'),
<em>1</em>
);
]]>
</code>
</code_comparison>
</documentation>
Loading
Loading