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_Check
should be created and exists atincludes/Checker/Checks
- Class
Enqueued_Scripts_Size_Check
should extend theAbstract_Runtime_Check
class. - Class
Enqueued_Scripts_Size_Check
will use theURL_Aware
trait. - Class
Enqueued_Scripts_Size_Check
will implement theWith_Shared_Preparations
interface. - Class has a protected
$threshold_size
property 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_Preparation
class 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_Check
abstract 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_Results
instance. - The
Check_Result $check_result
will be returned by therun
method.
- The
- 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
andsize
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.
- Each script in the
- The method will accept an instance of the
URL Aware Trait
- A new
URL_Aware
trait 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_Query
to 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
$callback
function passed for each of the URLs passing an instance of theCheck_Result
class 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_Check
class is expected to extendRuntime_Check
classEnqueued_Scripts_Size_Check
class is expected to be instance ofRuntime_Check
classrun
method expected to append the audit results to$check_result