-
Notifications
You must be signed in to change notification settings - Fork 113
Add a front end JQuery enqueue check #423
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
adamsilverstein
wants to merge
5
commits into
WordPress:trunk
Choose a base branch
from
adamsilverstein:add/jquery-check
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8650391
Add a front end JQuery enqueue check
adamsilverstein 21f0a91
Update link, point to make post
adamsilverstein 9e681ae
Merge trunk into add/jquery-check
adamsilverstein 98f6208
Fix inverted jQuery enqueue condition
adamsilverstein a8ce107
Detect jQuery loaded as a script dependency
adamsilverstein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| <?php | ||
| /** | ||
| * Checks if jQuery is loaded by the theme. | ||
| * | ||
| * @package Theme Check | ||
| */ | ||
|
|
||
| /** | ||
| * Checks if jQuery is loaded by the theme. | ||
| * | ||
| * Themes load jQuery both by enqueueing it directly and by listing it as a dependency | ||
| * of one of their own scripts, so both cases are reported. | ||
| */ | ||
| class JQuery_Check implements themecheck { | ||
| /** | ||
| * Error messages, warnings and info notices. | ||
| * | ||
| * @var array $error | ||
| */ | ||
| protected $error = array(); | ||
|
|
||
| /** | ||
| * Check that return true for good/okay/acceptable, false for bad/not-okay/unacceptable. | ||
| * | ||
| * @param array $php_files File paths and content for PHP files. | ||
| * @param array $css_files File paths and content for CSS files. | ||
| * @param array $other_files Folder names, file paths and content for other files. | ||
| */ | ||
| public function check( $php_files, $css_files, $other_files ) { | ||
| /* | ||
| * The jQuery script handles registered by WordPress: `jquery` itself, the | ||
| * `jquery-core` and `jquery-migrate` scripts it is built from, and the | ||
| * `jquery-ui-*` family, every member of which depends on `jquery`. Enqueueing | ||
| * any of them - or depending on any of them - results in jQuery being loaded. | ||
| */ | ||
| $handle = 'jquery(?:-core|-migrate|-ui-[a-z0-9_-]+)?'; | ||
|
|
||
| // jQuery enqueued directly, for example wp_enqueue_script( 'jquery' ). | ||
| $direct_regex = '/\bwp_enqueue_script\s*\(\s*([\'"])' . $handle . '\1/i'; | ||
|
|
||
| /* | ||
| * jQuery pulled in as a dependency of another script, for example | ||
| * wp_enqueue_script( 'theme-script', $src, array( 'jquery' ), $ver, true ) or the | ||
| * short array syntax [ 'jquery' ]. `[^;]` keeps the match inside a single | ||
| * statement, so the dependency list has to belong to the call that opened it. | ||
| */ | ||
| $dependency_regex = '/\bwp_(?:enqueue|register)_script\s*\([^;]{0,300}?(?:array\s*\(|\[)[^)\]]*?([\'"])' . $handle . '\1/i'; | ||
|
|
||
| checkcount(); | ||
|
|
||
| foreach ( $php_files as $file_path => $file_content ) { | ||
| if ( ! preg_match( $direct_regex, $file_content ) && ! preg_match( $dependency_regex, $file_content ) ) { | ||
| continue; | ||
| } | ||
|
|
||
| $grep = tc_preg( '/jquery/i', $file_path ); | ||
|
|
||
| $this->error[] = sprintf( | ||
| '<span class="tc-lead tc-recommended">%s</span>: %s %s', | ||
| __( 'RECOMMENDED', 'theme-check' ), | ||
| sprintf( | ||
| /* translators: 1: file name, 2: URL of an article about the performance impact of jQuery. */ | ||
| __( 'jQuery is loaded by %1$s, either enqueued directly or listed as a dependency of another script. You may not need it, see <a href="%2$s">this article</a> for tips on switching to vanilla JavaScript.', 'theme-check' ), | ||
| '<strong>' . tc_filename( $file_path ) . '</strong>', | ||
| 'https://make.wordpress.org/themes/2021/10/04/the-performance-impact-of-using-jquery-in-wordpress-themes/' | ||
| ), | ||
| $grep | ||
| ); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Get error messages from the checks. | ||
| * | ||
| * @return array Error message. | ||
| */ | ||
| public function getError() { | ||
| return $this->error; | ||
| } | ||
| } | ||
|
|
||
| $themechecks[] = new JQuery_Check(); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.