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 8 commits into
base: develop
Choose a base branch
from
7 changes: 7 additions & 0 deletions WordPress-Extra/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,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
102 changes: 102 additions & 0 deletions WordPress/Docs/WP/OptionAutoloadStandard.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?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. This parameter determines
whether the option is automatically loaded on every page load, which can impact site performance.
]]>
</standard>
<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. Using 'auto', 'auto-on', 'auto-off',
'on', or 'off' in plugin/theme code is strongly discouraged as those values are meant for
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[
add_option(
'my_option',
'value',
'',
<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>
<standard>
<![CDATA[
Even though the autoload parameter is optional for `add_option()` and `update_option()`, it is
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>
</documentation>
Loading
Loading