Skip to content
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

Implement Enqueued_Scripts_Scope_Check #485

Merged
merged 3 commits into from
Jul 2, 2024
Merged

Conversation

swissspidy
Copy link
Member

@swissspidy swissspidy commented Jul 1, 2024

Note: this is basically an exact copy of Enqueued_Styles_Scope_Check right now. And the other script/style-related checks are very similar too.

I suggest creating some abstraction in a new, separate PR to remove repeated code.

Fixes #23

@swissspidy swissspidy added [Type] Enhancement A suggestion for improvement of an existing feature Checks Audit/test of the particular part of the plugin labels Jul 1, 2024
@swissspidy swissspidy marked this pull request as ready for review July 1, 2024 18:20
* @throws Exception Thrown when preparation fails.
*/
public function prepare() {
$orig_scripts = isset( $GLOBALS['wp_scripts'] ) ? $GLOBALS['wp_scripts'] : null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified to:

Suggested change
$orig_scripts = isset( $GLOBALS['wp_scripts'] ) ? $GLOBALS['wp_scripts'] : null;
$orig_scripts = $GLOBALS['wp_scripts'] ?? null;

But not required.

$demo_posts = array_map(
static function ( $post_type ) {
return array(
'post_title' => "Demo {$post_type} post",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Advised by PhpStorm:

Suggested change
'post_title' => "Demo {$post_type} post",
'post_title' => "Demo $post_type post",
'update_post_term_cache' => false,
);

$the_query = new \WP_Query( $args );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If use WP_Query is added to the top:

Suggested change
$the_query = new \WP_Query( $args );
$the_query = new WP_Query( $args );
Comment on lines +177 to +181
$the_query->the_post();

$urls[] = get_permalink();
$post = get_post();
$taxonomy_names = get_post_taxonomies( $post );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$the_query->the_post();
$urls[] = get_permalink();
$post = get_post();
$taxonomy_names = get_post_taxonomies( $post );
$the_query->next_post();
$urls[] = get_permalink( $the_query->post );
$taxonomy_names = get_post_taxonomies( $the_query->post );
Comment on lines +208 to +210

/* Restore original Post Data */
wp_reset_postdata();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now unneeded if we don't call the_post() above.

Suggested change
/* Restore original Post Data */
wp_reset_postdata();
if ( isset( $plugin_script['count'] ) && ( $url_count === $plugin_script['count'] ) ) {
$this->add_result_warning_for_file(
$result,
__( 'This script is being loaded in all contexts.', 'plugin-check' ),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it a problem if a script is loaded in all contexts?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not always a problem, but often times this is done in error and not actually on purpose. This can also be often observed in the wp-admin, when a plugin loads JS on every page instead of only its own settings page.

This check is aimed to detect the former, but of course it can't be bulletproof. Hence this is a warning and not an error.

@swissspidy
Copy link
Member Author

@westonruter Just noting that I agree with the suggestions, but since they would need to be done in Enqueued_Styles_Scope_Check too, might be best to do that in a follow-up PR converging the two classes.

@swissspidy swissspidy added the [Team] Performance Issues owned by Performance Team label Jul 1, 2024
Copy link

github-actions bot commented Jul 2, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: swissspidy <swissspidy@git.wordpress.org>
Co-authored-by: mukeshpanchal27 <mukesh27@git.wordpress.org>
Co-authored-by: westonruter <westonruter@git.wordpress.org>
Co-authored-by: mehulkaklotar <mehulkaklotar@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@mukeshpanchal27 mukeshpanchal27 merged commit 46f2635 into trunk Jul 2, 2024
23 checks passed
@mukeshpanchal27 mukeshpanchal27 deleted the fix/23-scripts-scope branch July 2, 2024 10:58
@swissspidy swissspidy added this to the 1.1.0 milestone Jul 3, 2024
swissspidy added a commit that referenced this pull request Jul 8, 2024
@swissspidy swissspidy modified the milestones: 1.1.0, 1.0.2 Jul 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Checks Audit/test of the particular part of the plugin [Team] Performance Issues owned by Performance Team [Type] Enhancement A suggestion for improvement of an existing feature
3 participants