-
-
Notifications
You must be signed in to change notification settings - Fork 493
✨ 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
base: develop
Are you sure you want to change the base?
Changes from 1 commit
f384fb0
95afb67
a3da583
3aad7d3
571b0ed
1575735
4dbe72a
fc98b8e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,135 @@ | ||||||||||||||||
<?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 value to ensure predictable behavior. This value determines whether | ||||||||||||||||
the option is automatically loaded on every page load, which can impact site performance. | ||||||||||||||||
|
||||||||||||||||
Check https://felix-arntz.me/blog/autoloading-wordpress-options-efficiently-and-responsibly/ for | ||||||||||||||||
more information on when to set autoload to `true` or `false`. | ||||||||||||||||
jrfnl marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
]]> | ||||||||||||||||
</standard> | ||||||||||||||||
<standard> | ||||||||||||||||
<![CDATA[ | ||||||||||||||||
Even though the autoload parameter is optional for `add_option()` and `update_option()`, it is | ||||||||||||||||
recommended to always explicitly set it. | ||||||||||||||||
]]> | ||||||||||||||||
</standard> | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO, this block is redundant. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #2520 (comment) |
||||||||||||||||
<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[ | ||||||||||||||||
Using 'yes' or 'no' is not recommended. Those values are deprecated. Instead, use | ||||||||||||||||
`true`|`false`|`null` if using `add_option()` or `update_option()` or `true`|`false` if using | ||||||||||||||||
`wp_set_option_autoload_values()`, `wp_set_option_autoload()`, or `wp_set_options_autoload()`. | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please rephrase this from a "negative" paragraph to a "positive" paragraph. Maybe something along the lines of:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #2520 (comment) |
||||||||||||||||
]]> | ||||||||||||||||
</standard> | ||||||||||||||||
<code_comparison> | ||||||||||||||||
<code title="Valid: Using boolean values."> | ||||||||||||||||
rodrigoprimo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
<![CDATA[ | ||||||||||||||||
update_option( | ||||||||||||||||
'my_option', | ||||||||||||||||
'value', | ||||||||||||||||
<em>true</em> | ||||||||||||||||
); | ||||||||||||||||
]]> | ||||||||||||||||
</code> | ||||||||||||||||
<code title="Invalid: Using deprecated values."> | ||||||||||||||||
rodrigoprimo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
<![CDATA[ | ||||||||||||||||
update_option( | ||||||||||||||||
'my_option', | ||||||||||||||||
'value', | ||||||||||||||||
<em>'yes'</em> | ||||||||||||||||
); | ||||||||||||||||
]]> | ||||||||||||||||
</code> | ||||||||||||||||
</code_comparison> | ||||||||||||||||
<standard> | ||||||||||||||||
<![CDATA[ | ||||||||||||||||
It is not recommended to use internal-only values ('auto', 'auto-on', 'auto-off', 'on', 'off') | ||||||||||||||||
in plugin or theme code. Instead, use `true`|`false`|`null` if using `add_option()` or | ||||||||||||||||
`update_option()` or `true`|`false` if using `wp_set_option_autoload_values()`, | ||||||||||||||||
`wp_set_option_autoload()`, or `wp_set_options_autoload()`. | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above, please rephrase. "It is not recommended..." => "It is strongly discouraged..." Might even be better to combine this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #2520 (comment) |
||||||||||||||||
]]> | ||||||||||||||||
</standard> | ||||||||||||||||
<code_comparison> | ||||||||||||||||
<code title="Valid: Using boolean values."> | ||||||||||||||||
<![CDATA[ | ||||||||||||||||
wp_set_option_autoload_values( | ||||||||||||||||
array( | ||||||||||||||||
'my_option_1' => <em>true</em>, | ||||||||||||||||
'my_option_2' => <em>false</em> | ||||||||||||||||
) | ||||||||||||||||
); | ||||||||||||||||
]]> | ||||||||||||||||
</code> | ||||||||||||||||
<code title="Invalid: Using internal-only values."> | ||||||||||||||||
<![CDATA[ | ||||||||||||||||
wp_set_option_autoload_values( | ||||||||||||||||
array( | ||||||||||||||||
'my_option_1' => <em>'auto-on'</em>, | ||||||||||||||||
'my_option_2' => <em>'auto-off'</em> | ||||||||||||||||
) | ||||||||||||||||
); | ||||||||||||||||
]]> | ||||||||||||||||
</code> | ||||||||||||||||
</code_comparison> | ||||||||||||||||
<standard> | ||||||||||||||||
<![CDATA[ | ||||||||||||||||
Only `true` and `false` are valid values for the autoload parameter, with one exception: `null` | ||||||||||||||||
is also valid for `add_option()` and `update_option()`. | ||||||||||||||||
]]> | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, the rule is the same as the previous two Also not so sure if the below code comparison adds value. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In a separate commit, I tried again to structure the XML documentation. I kept the "introductory" standard block and the block for the presence of the autoload parameter. The other three blocks were combined into a single one that describes the autoload parameter's valid and invalid values. Any input on this new structure is welcome. I'm unsure if I should combine everything into a single There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do think the "missing parameter" and the "invalid parameter value" issues are two separate issues, but I'm not convinced the changed structure of the docs makes this clear enough. If I look at the latest version, my recommendation would be:
|
||||||||||||||||
</standard> | ||||||||||||||||
<code_comparison> | ||||||||||||||||
<code title="Valid: Using `true`, `false`, or `null`."> | ||||||||||||||||
<![CDATA[ | ||||||||||||||||
wp_set_options_autoload( | ||||||||||||||||
array( 'my_option_1', 'my_option_2' ), | ||||||||||||||||
<em>false</em> | ||||||||||||||||
); | ||||||||||||||||
|
||||||||||||||||
wp_set_option_autoload( | ||||||||||||||||
'my_option', | ||||||||||||||||
<em>false</em> | ||||||||||||||||
rodrigoprimo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
); | ||||||||||||||||
]]> | ||||||||||||||||
</code> | ||||||||||||||||
<code title="Invalid: Using invalid values."> | ||||||||||||||||
<![CDATA[ | ||||||||||||||||
wp_set_options_autoload( | ||||||||||||||||
array( 'my_option_1', 'my_option_2' ), | ||||||||||||||||
<em>1</em> | ||||||||||||||||
); | ||||||||||||||||
|
||||||||||||||||
wp_set_option_autoload( | ||||||||||||||||
'my_option', | ||||||||||||||||
<em>null</em> // `null` is invalid for this function. | ||||||||||||||||
); | ||||||||||||||||
]]> | ||||||||||||||||
</code> | ||||||||||||||||
</code_comparison> | ||||||||||||||||
</documentation> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried not to use
parameter
as forwp_set_option_autoload_values()
autoload is not a parameter. But I agree that value is probably worst. I will accept your suggestion, and I will let you know if I can think of a better approach.