-
Notifications
You must be signed in to change notification settings - Fork 83
Description
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_Checkshould be created and exists atincludes/Checker/Checks - Class
Enqueued_Scripts_Size_Checkshould extend theAbstract_Runtime_Checkclass. - Class
Enqueued_Scripts_Size_Checkwill use theURL_Awaretrait. - Class
Enqueued_Scripts_Size_Checkwill implement theWith_Shared_Preparationsinterface. - Class has a protected
$threshold_sizeproperty with a value of300000. - 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.
- This method will save the
- 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_Preparationclass name is an array key and it's value is an array of posts data to create, compatible withwp_insert_post.
- The
- 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 theRuntime_Checkabstract class.- The
run()method will call therun_for_urls()method passing an array of URLs returned by theget_urls()method, a callable which will be the classescheck_url()method, and theCheck_Resultsinstance. - The
Check_Result $check_resultwill be returned by therunmethod.
- The
- A protected
check_url()method will be created.- The method will accept an instance of the
Check_Resultclass 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
pathandsizeto a$plugin_scriptsarray to be used as part of the results. - Record the total plugin scripts size in a
$plugin_scripts_sizevariable to be used as part of the results.
- After looping over all plugin scripts, the total
$plugin_scripts_sizeis 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_scriptsis looped over an a warning message added for each. - Each warning includes the current URL, the threshold, the script path and size.
- Each script in the
- The method will accept an instance of the
URL Aware Trait
- A new
URL_Awaretrait exists inincludes\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 globalWP_Queryto match the URL passed.
- The
- 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
$callbackfunction passed for each of the URLs passing an instance of theCheck_Resultclass and the URL as parameters.
- The method will loop over each of the URLs passed and simulate the URL using the
Tests Coverage
Enqueued_Scripts_Size_Checkclass is expected to extendRuntime_CheckclassEnqueued_Scripts_Size_Checkclass is expected to be instance ofRuntime_Checkclassrunmethod expected to append the audit results to$check_result