Skip to content
Draft
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
f8fdd6f
✨ New WordPress.WP.OptionAutoload sniff
rodrigoprimo Aug 26, 2024
b47e08a
Apply suggestions from code review
rodrigoprimo Feb 28, 2025
715b43a
Attemp to improve the XML documentation by combining standards
rodrigoprimo Feb 28, 2025
4cf89cf
Fix unintentional coding standard violation in a test case
rodrigoprimo Mar 11, 2025
3ca5d1f
Update some tests to have more function name case variations
rodrigoprimo Mar 12, 2025
5571c8e
Update some tests to have more whitespaces, comments and annotations
rodrigoprimo Mar 12, 2025
b11700f
Use `false === isset()` instead of `! isset()` for consistency with t…
rodrigoprimo Mar 12, 2025
d86b341
Improve sniff performance by changing the format of some properties
rodrigoprimo Mar 12, 2025
5ca8075
Apply suggestions from code review
rodrigoprimo Jul 4, 2025
86740fb
Remove unused parameter
rodrigoprimo Jul 4, 2025
a9a3e8a
Rename test case file to be able to add more test case files with syn…
rodrigoprimo Jul 4, 2025
69cf066
Handle case when $array_token is false + add a test for it
rodrigoprimo Jul 4, 2025
a07dbf3
Improve sniff XML doc following suggestions from code review
rodrigoprimo Jul 4, 2025
06b2551
Add link to external blog post about when to use the autoload flag
rodrigoprimo Jul 4, 2025
86c88b8
Add backticks to all function calls in the docblocks
rodrigoprimo Jul 4, 2025
b882b8e
Rename property `valid_values_other_functions` to `valid_values_wp_se…
rodrigoprimo Jul 4, 2025
088a645
Explain in the docblock the array properties where all values are `true`
rodrigoprimo Jul 4, 2025
198677d
Ensure all `@param array` tags explain the structure of the array
rodrigoprimo Jul 4, 2025
6e18b20
Only record `param missing` metric when the autoload parameter is opt…
rodrigoprimo Jul 4, 2025
2752fff
Use array union instead of `array_merge()` to create the list of know…
rodrigoprimo Jul 15, 2025
1465df0
Use one error code for fixable internal values and another for non-fi…
rodrigoprimo Jul 15, 2025
1975fcc
Update if condition to explicitly check what is expected and add a te…
rodrigoprimo Jul 15, 2025
3f728aa
Add another test without whitespaces surrounding the parameter to tes…
rodrigoprimo Jul 15, 2025
57237b0
Add a comment explaining why `null` needs special treatment on line 361
rodrigoprimo Jul 15, 2025
c7bd613
Explicitly check if $param_second_token is an integer
rodrigoprimo Jul 15, 2025
b8f7cf6
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