Skip to content

Create Enqueued_Scripts_Size_Check #17

Closed
@mehulkaklotar

Description

@mehulkaklotar

Description

Checks the number of scripts and the total size of all scripts registered in the plugin. This check will run the wp_enqueue_scripts action to register all plugin scripts. Using wp_scripts(), all scripts registered by the plugin will be looped over to get the size of the script along with any additional inline scripts. The number and total file size of all scripts will be calculated and then tested against the threshold, warning if either exceeds a threshold. Check should run relevant actions to make the data as accurate as possible as scripts can be registered and enqueued with any actions provided by WP like in the header or in the document body. Check should run through multiple contexts to make sure scripts enqueued by the plugin are covered in the analysis. Contexts could be any URLs from the site like the homepage, post types, templates, etc.

This issue is dependant on:

Acceptance Criteria

Enqueued_Scripts_Size_Check class

  • Class Enqueued_Scripts_Size_Check should be created and exists at includes/Checker/Checks
  • Class Enqueued_Scripts_Size_Check should extend the Abstract_Runtime_Check class.
  • Class Enqueued_Scripts_Size_Check will use the URL_Aware trait.
  • Class Enqueued_Scripts_Size_Check will implement theWith_Shared_Preparations interface.
  • Class has a protected $threshold_size property with a value of 300000.
  • A prepare() method will be implemented in the class.
    • This method will save the $GLOBALS['wp_scripts']; to a temporary variable.
    • Then set the $GLOBALS['wp_scripts']; to null if set.
    • A clean up function is returned which uses the temporary variable to reinstate the original $GLOBALS['wp_scripts']; value.
  • A get_shared_preparations() method will be implemented.
    • The get_shared_preparations() method will be used to return a preparation that creates a demo post for all publicly viewable post types using Create Demo_Posts_Creation_Preparation #40
    • The method will return an array where the Demo_Post_Creation_Preparation class name is an array key and it's value is an array of posts data to create, compatible with wp_insert_post.
  • A protected get_urls() method will be created.
    • The method will return an array of URLs containing the home URL and URLs for all publicly viewable post types.
  • Method run( Check_Result $results ) should be implemented from the Runtime_Check abstract class.
    • The run() method will call the run_for_urls() method passing an array of URLs returned by the get_urls() method, a callable which will be the classes check_url() method, and the Check_Results instance.
    • The Check_Result $check_result will be returned by the run method.
  • A protected check_url() method will be created.
    • The method will accept an instance of the Check_Result class and a URL string as parameters.
    • The method will reset the $GLOBALS['wp_scripts'];
    • Then will call the wp_enqueue_scripts() to enqueue all of the checked plugins scripts.
    • Using the object returned by wp_scripts() all enqueued scripts for the URL will be looped over:
      • Check the script is part of the plugin being checked by matching the plugin URL with the script URL
      • If the script is part of the plugin, calculate the size of the script including any additional inline scripts.
      • Add the script path and size to a $plugin_scripts array to be used as part of the results.
      • Record the total plugin scripts size in a $plugin_scripts_size variable to be used as part of the results.
    • After looping over all plugin scripts, the total $plugin_scripts_size is checked agains the $threshold_size
    • If the total script size is larger than the threshold, warnings are added to the $check_results
      • Each script in the $plugin_scripts is looped over an a warning message added for each.
      • Each warning includes the current URL, the threshold, the script path and size.

URL Aware Trait

  • A new URL_Aware trait exists in includes\Traits\URL_Aware
  • Trait contains a public go_to( $url ) method that will simulate in WordPress the passed URL.
    • The go_to() method will simulate the URL, similar to the WordPress Core test suite, by modifying the global WP_Query to match the URL passed.
  • Trait contains a public run_for_urls( array $urls, callable $callback )
    • The method will loop over each of the URLs passed and simulate the URL using the go_to() method.
    • Then it will call the $callback function passed for each of the URLs passing an instance of the Check_Result class and the URL as parameters.

Tests Coverage

  • Enqueued_Scripts_Size_Check class is expected to extend Runtime_Check class
  • Enqueued_Scripts_Size_Check class is expected to be instance of Runtime_Check class
  • run method expected to append the audit results to $check_result

Metadata

Metadata

Assignees

Labels

ChecksAudit/test of the particular part of the plugin[Type] EnhancementA suggestion for improvement of an existing feature

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions