WordPress / WordPress/WordPress-Coding-Standards
Accept `null` as a valid version argument for wp_enqueue_style()
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 2.8k
- Forks
- 521
- Avg merge
- 5d 20h
- Merged PRs (30d)
- 1
Description
Bug Description
When a script is enqueued through Google Fonts that loads multiple font families, the family parameter is used in the URL more than once. When wp_enqueue_style() adds a ver parameter to this URL, all but one of those family parameters is removed. This is a bug in PHP and/or WP core, see: https://core.trac.wordpress.org/ticket/49742
To work around this, null can be passed to wp_enqueue_style() as the version number. This results in no version string added to the URL.
When null is used as the version argument for wp_enqueue_style(), two PHPCS errors apply:
WordPress.WP.EnqueuedResourceParameters.NoExplicitVersionis shown first, because it sees the argument as falsy.WordPress.WP.EnqueuedResourceParameters.MissingVersionis shown once that is ignored, because it seesnullas the same as no argument being passed.
I would argue that in this case, the use of null is an explicit decision that should be treated the same as adding a valid version number. (This is mostly because it felt strange to have two different ignore statements for 4 characters of code)
Minimal Code Snippet
wp_enqueue_style( // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NoExplicitVersion
'google-variable-fonts',
'https://fonts.googleapis.com/css2?family=Roboto+Slab:wght@400..800&family=Work+Sans:wght@300..800&family=Work+Sans:ital,wght@1,300..800',
array(),
null // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
);
Environment
| Question | Answer |
|---|---|
| PHP version | 7.2.3.1 |
| PHP_CodeSniffer version | 3.5.6 |
| WPCS version | 2.3.0 |
| WPCS install type | Composer Global |
Tested Against develop branch?
- I have verified the issue still exists in the
developbranch of WPCS.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the WordPress.WP.EnqueuedResourceParameters.NoExplicitVersion and MissingVersion sniffs that report on wp_enqueue_style() arguments. Trace how a null version is classified, then verify that it is treated as an explicit version without either diagnostic applying; the issue should remain compatible with the stated PHP_CodeSniffer and WPCS versions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100