-
-
Notifications
You must be signed in to change notification settings - Fork 490
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
Add sniff to warn about missing $autoload
parameter when calling relevant WordPress option functions
#2473
Comments
@felixarntz Thanks for opening this issue. I think this is a valid feature request and I support this proposal. I do have some questions though which will need to be answered to fully understand the specs for the new sniff.
I think we'll need to have a careful think about the warning message text, but more than anything, I think this could be explained in more detail in the sniff XML documentation. @felixarntz Would you be up to contributing these docs, or collaborating on them, once the sniff has been written ?
Looking at commit WordPress/wordpress-develop@7d606a3, I see some more potential values for the option - And yes, it would be trivial for the sniff to flag anything which is non- Along the same lines, This would mean the sniff logic would need to be along the following lines:
Does this sound correct to you ? |
@jrfnl Replying to your questions:
It's only
Not planned, no. They are predominantly for multisite network options, where there's no autoloading functionality like for regular options. On regular sites they pass through to regular options, so there's potential value here, but it's tricky from an API perspective and overall probably low impact, given how less common it is for those functions to be used.
Yes, that won't be a problem at all from a functionality perspective. $autoload = ( 'no' === $autoload || false === $autoload ) ? 'no' : 'yes';
Definitely happy to help with that!
All those values are internal values stored in the database, nothing to be concerned with for someone calling
I like that idea. Yeah, none of the additional above Your summary sounds great, plus taking the above into account on the TBD sections:
Let me know if you have any follow up questions. |
Thanks for your response @felixarntz. That clarifies all questions I had. @rodrigoprimo Is this something you may like to work on ? And if so, have you got time to do so ? |
@jrfnl, yes, I would like to work on this new sniff. I can start next week. |
Updating the sniff summary that @jrfnl created with the latest information that @felixarntz provided:
|
Thanks @rodrigoprimo for this summary, it looks accurate to me. I just remembered the additional option autoloading related core functions introduced in WordPress 6.4:
I just opened a WordPress Core ticket to update the parameter documentation of those functions to as well no longer recommend While they probably aren't used too much in the ecosystem compared to cc @jrfnl |
I concur and I think it would be good to include this in the sniff. I don't think the Caveat for all detection: if the values aren't being passed directly in the function call/hard-coded, we're out of luck as tracing variable definitions/function calls throughout the codebase is not something PHPCS is suitable for. In other words: the below can be flagged and fixed: add_option( $option, $value, '', 'yes' );
wp_set_option_autoload_values(
array(
'optionA' => 'no',
'optionB' => 'yes'
)
); ... but these can't be: add_option( $option, $value, '' AUTOLOAD_VALUE );
wp_set_option_autoload_values($options_array); |
Including those three functions sounds like a good idea to me as well. We discussed generating a warning when the The same parameter is mandatory for the |
I personally think for those functions we don't necessarily have to sniff for the parameter missing on those functions. In those cases it would already cause a PHP error anyway because it is technically required. So for |
I think that is a fair point and a good option as well. I prefer slightly the error as it seems to me as a more consistent behavior from the sniff perspective. The sniff will always generate something (a warning or a error) when the What is your take on this, @jrfnl? |
I agree with @felixarntz on this. It is not the job of the sniff to flag PHP errors. That's a case of scope creep for this sniff, but also for sniffs in general. WordPress-Coding-Standards/WordPress/Sniffs/PHP/IniSetSniff.php Lines 148 to 151 in 7f76630
WordPress-Coding-Standards/WordPress/Sniffs/DateTime/CurrentTimeTimestampSniff.php Lines 77 to 81 in 7f76630
|
Ok, and thanks for the examples of prior art. FYI, I already have a functional draft of the sniff, but I won't be able to work on it for the next three weeks. I will resume this work in the last week of September. Below I'm sharing some metrics gathered running the sniff against a local and up to date copy of the WP.org plugins repository. This gives us an idea of how the
I can run it again once the sniff is finalized and then once more after a few months that it is released. This should give us an idea of how the community is responding to this change in WP. |
Enjoy your time off @rodrigoprimo! Regarding the metrics, here's some feedback:
Edit: if the "known" values were already in a separate bucket and just didn't show up, that would be a good thing, of course ;-) |
Sorry for the long delay in responding. I have resumed work on this sniff today. Thanks for your feedback regarding the metrics, @jrfnl. I have implemented the changes that you suggested. Here are the updated metrics. The numbers are slightly different because now the sniff also checks for the functions
|
Updating once again the sniff summary to include what was discussed in the previous comments:
|
@rodrigoprimo Thank you for the update and for continuing to work on this! Your summary looks correct to me, only a few very minor clarifications (potentially they were already clear to you, but I want to make sure): The three newer functions have slightly different implication than
|
Is your feature request related to a problem?
Several option functions (e.g.
add_option()
,update_option()
) have been supporting an$autoload
parameter that is optional and has had a default value of'yes'
. This has led to notable problems where far too many options that plugins or themes add are being autoloaded, even if they're only needed on a few specific pages of the application.WordPress 6.6 introduced changes in the API and how it should be used. While we can't require a parameter that used to be optional, it is now more than ever encouraged to always pass it. Going forward, not passing the parameter will be interpreted by core as implicit, i.e. core is able to determine whether or not to autoload the option by itself. So far this effectively still means an option without any provided value will be autoloaded, unless it is a particularly large option (the only condition implemented in WordPress core so far). But this current behavior to still mostly autoload by default is only for backward compatibility and could change in the future.
See https://make.wordpress.org/core/2024/06/18/options-api-disabling-autoload-for-large-options/ for additional context. And somewhat related: https://make.wordpress.org/core/2023/10/17/new-option-functions-in-6-4/
Describe the solution you'd like
WordPress-Extra
should include a sniff that warns whenever the$autoload
parameter is missing onadd_option()
orupdate_option()
calls.While they should typically use either
true
orfalse
, passingnull
is fine too as explicit acknowledgement that core will be able to make the decision.Nice to have
It used to be formally documented that the strings
'yes'
and'no'
could be passed alternatively totrue
andfalse
respectively. This is still supported and not formally deprecated, but it's also no longer encouraged, so it would be nice to clean that up over time. There are however no functional differences between passing one or the other variant, so it's certainly not as important as the main solution described above. But if it's trivial to include in the new sniff, it's worth considering.Additional context (optional)
Just some additional context how to decide whether an option should be autoloaded or not:
true
if the option is retrieved in the majority of page loads, including frontend requests.false
if the option is only retrieved in particular page loads, e.g. a specific page in the frontend or a specific admin screen.null
to leave the decision whether or not to autoload the option to WordPress core.One additional explanation for a somewhat common situation:
wp_prime_option_caches()
orwp_prime_option_caches_by_group()
, which were introduced in WordPress 6.4. With these functions, multiple options can be retrieved ("pre-loaded") from the database with a single request, without "polluting" the list of autoloaded options, which should remain reserved for options that are loaded across most of the WordPress site.The text was updated successfully, but these errors were encountered: