-
Notifications
You must be signed in to change notification settings - Fork 165
Add module to alert about excessive JS and CSS assets #54
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
Merged
felixarntz
merged 21 commits into
WordPress:trunk
from
manuelRod:add/sitehealth-enqueued-assets
Jan 28, 2022
Merged
Changes from 11 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
a7883dc
Plugin https://github.com/audrasjb/site-health-audit-enqueued-assets/…
manuelRod 83fd785
partial unit testing added.
manuelRod 69d1208
Adding more unit testing.
manuelRod bda604d
adding missing tests.
manuelRod 663fe3a
removing duplicated get_transient
manuelRod 3a5cc54
Adding filter for transient, and invalidate cache on switch_theme, ac…
manuelRod 2e555a1
Adding user action to null transient cache.
manuelRod c668003
redirects after cleaning cache to site-health.php without query args
manuelRod 2baf8fe
All prefix needs to be updated to perflab_
manuelRod 327bed3
adding functionality to get resources sizes
manuelRod f7c2a60
Adding filesize to the transient cache, and functionality related to it.
manuelRod 5333d7d
Merge branch 'trunk' into add/sitehealth-enqueued-assets
manuelRod b0ccda4
migrating module to new module structure
manuelRod 2757e8c
migration to new module structure
manuelRod 07a6a70
split code refactor, creation of a new helper file with helper method…
manuelRod 47f4f9f
prefix fix
manuelRod 84af518
refactor adding suggestions:
manuelRod a528fb4
changing wording and adding information about bellow threshold limits.
manuelRod e42b9c2
implementing new threshold limits
manuelRod 90c17f8
Adding filters for limits. Fixing _n translatable strings.
manuelRod 35edbca
nit-pick fixes
manuelRod 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,312 @@ | ||
| <?php | ||
| /** | ||
| * Module Name: Audit Enqueued Assets | ||
| * Description: Adds a CSS and JS resource checker in Site Health checks. | ||
| * Focus: site-health | ||
| * Experimental: No | ||
| * | ||
| * @package performance-lab | ||
| * @since 1.0.0 | ||
| */ | ||
|
|
||
| /** | ||
| * Audit enqueued scripts in the frontend. Ignore /wp-includes scripts. | ||
| * | ||
| * It will save information in a transient for 12 hours. | ||
| * | ||
| * @since 1.0.0 | ||
| */ | ||
| function perflab_aea_audit_enqueued_scripts() { | ||
| if ( ! is_admin() && ! get_transient( 'aea_enqueued_scripts' ) ) { | ||
| global $wp_scripts; | ||
| $enqueued_scripts = array(); | ||
|
|
||
| foreach ( $wp_scripts->queue as $handle ) { | ||
| $src = $wp_scripts->registered[ $handle ]->src; | ||
| if ( $src && ! strpos( $src, 'wp-includes' ) ) { | ||
| $enqueued_scripts[] = array( | ||
| 'src' => $src, | ||
| 'size' => perflab_get_resource_file_size( perflab_get_path_from_resource_url( $src ) ), | ||
| ); | ||
| } | ||
| } | ||
| $expiration = apply_filters( 'perflab_aea_audit_enqueued_scripts_expiration_in_seconds', 12 * HOUR_IN_SECONDS ); | ||
|
felixarntz marked this conversation as resolved.
Outdated
|
||
| set_transient( 'aea_enqueued_scripts', $enqueued_scripts, $expiration ); | ||
|
manuelRod marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
| add_action( 'wp_print_scripts', 'perflab_aea_audit_enqueued_scripts' ); | ||
|
|
||
| /** | ||
| * Audit enqueued styles in the frontend. Ignore /wp-includes styles. | ||
| * | ||
| * It will save information in a transient for 12 hours. | ||
| * | ||
| * @since 1.0.0 | ||
| */ | ||
| function perflab_aea_audit_enqueued_styles() { | ||
| if ( ! is_admin() && ! get_transient( 'aea_enqueued_styles' ) ) { | ||
|
manuelRod marked this conversation as resolved.
Outdated
|
||
| global $wp_styles; | ||
| $enqueued_styles = array(); | ||
| foreach ( $wp_styles->queue as $handle ) { | ||
| $src = $wp_styles->registered[ $handle ]->src; | ||
| if ( $src && ! strpos( $src, 'wp-includes' ) ) { | ||
| $enqueued_styles[] = array( | ||
| 'src' => $src, | ||
| 'size' => perflab_get_resource_file_size( perflab_get_path_from_resource_url( $src ) ), | ||
| ); | ||
| } | ||
| } | ||
| $expiration = apply_filters( 'perflab_aea_audit_enqueued_styles_expiration_in_seconds', 12 * HOUR_IN_SECONDS ); | ||
|
felixarntz marked this conversation as resolved.
Outdated
|
||
| set_transient( 'aea_enqueued_styles', $enqueued_styles, $expiration ); | ||
|
manuelRod marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
| add_action( 'wp_print_styles', 'perflab_aea_audit_enqueued_styles' ); | ||
|
|
||
| /** | ||
| * Gets total of enqueued scripts. | ||
| * | ||
| * @since 1.0.0 | ||
| * | ||
| * @return int|false Number of total scripts or false if transient hasn't been set. | ||
| */ | ||
| function perflab_aea_get_total_enqueued_scripts() { | ||
| $enqueued_scripts = false; | ||
| $list_enqueued_scripts = get_transient( 'aea_enqueued_scripts' ); | ||
| if ( $list_enqueued_scripts ) { | ||
| $enqueued_scripts = count( $list_enqueued_scripts ); | ||
| } | ||
| return $enqueued_scripts; | ||
| } | ||
|
|
||
| /** | ||
| * Gets total size in bytes of Enqueued Scripts. | ||
| * | ||
| * @since 1.0.0 | ||
| * | ||
| * @return int Byte Total size. | ||
| */ | ||
| function perflab_aea_get_total_size_bytes_enqueued_scripts() { | ||
| $total_size = 0; | ||
|
manuelRod marked this conversation as resolved.
Outdated
|
||
| $list_enqueued_scripts = get_transient( 'aea_enqueued_scripts' ); | ||
| if ( $list_enqueued_scripts ) { | ||
| foreach ( $list_enqueued_scripts as $enqueued_script ) { | ||
| $total_size += $enqueued_script['size']; | ||
| } | ||
| } | ||
| return $total_size; | ||
| } | ||
|
|
||
| /** | ||
| * Gets total of enqueued styles. | ||
| * | ||
| * @since 1.0.0 | ||
| * | ||
| * @return int|false Number of total styles or false if transient hasn't been set. | ||
| */ | ||
| function perflab_aea_get_total_enqueued_styles() { | ||
| $enqueued_styles = false; | ||
| $list_enqueued_styles = get_transient( 'aea_enqueued_styles' ); | ||
| if ( $list_enqueued_styles ) { | ||
| $enqueued_styles = count( $list_enqueued_styles ); | ||
| } | ||
| return $enqueued_styles; | ||
| } | ||
|
|
||
| /** | ||
| * Gets total size in bytes of Enqueued Styles. | ||
| * | ||
| * @since 1.0.0 | ||
| * | ||
| * @return int Byte Total size. | ||
| */ | ||
| function perflab_aea_get_total_size_bytes_enqueued_styles() { | ||
| $total_size = 0; | ||
|
manuelRod marked this conversation as resolved.
Outdated
|
||
| $list_enqueued_styles = get_transient( 'aea_enqueued_styles' ); | ||
| if ( $list_enqueued_styles ) { | ||
| foreach ( $list_enqueued_styles as $enqueued_style ) { | ||
| $total_size += $enqueued_style['size']; | ||
| } | ||
| } | ||
| return $total_size; | ||
| } | ||
|
|
||
| /** | ||
| * Adds tests to site health. | ||
| * | ||
| * @since 1.0.0 | ||
| * | ||
| * @param array $tests Site Health Tests. | ||
| * @return array | ||
| */ | ||
| function perflab_aea_add_enqueued_assets_test( $tests ) { | ||
| $tests['direct']['enqueued_js_assets'] = array( | ||
| 'label' => esc_html__( 'JS assets', 'performance-lab' ), | ||
| 'test' => 'perflab_aea_enqueued_js_assets_test', | ||
| ); | ||
| $tests['direct']['enqueued_css_assets'] = array( | ||
| 'label' => esc_html__( 'CSS assets', 'performance-lab' ), | ||
| 'test' => 'perflab_aea_enqueued_css_assets_test', | ||
| ); | ||
|
|
||
| return $tests; | ||
| } | ||
| add_filter( 'site_status_tests', 'perflab_aea_add_enqueued_assets_test' ); | ||
|
|
||
| /** | ||
| * Callback for enqueued_js_assets test. | ||
| * | ||
| * @since 1.0.0 | ||
| * | ||
| * @return array | ||
| */ | ||
| function perflab_aea_enqueued_js_assets_test() { | ||
| /** | ||
| * If the test didn't run yet, deactivate. | ||
| */ | ||
| $enqueued_scripts = perflab_aea_get_total_enqueued_scripts(); | ||
| if ( false === $enqueued_scripts ) { | ||
| return array(); | ||
| } | ||
|
|
||
| $result = array( | ||
| 'label' => esc_html__( 'Enqueued JS assets', 'performance-lab' ), | ||
| 'status' => 'good', | ||
| 'badge' => array( | ||
| 'label' => esc_html__( 'Performance', 'performance-lab' ), | ||
| 'color' => 'blue', | ||
| ), | ||
| 'description' => sprintf( | ||
| '<p>%s</p>', | ||
| esc_html__( 'The amount of enqueued JS assets is acceptable.', 'performance-lab' ) | ||
|
manuelRod marked this conversation as resolved.
Outdated
|
||
| ), | ||
| 'actions' => '', | ||
| 'test' => 'enqueued_js_assets', | ||
| ); | ||
|
|
||
| if ( $enqueued_scripts > 10 ) { | ||
|
manuelRod marked this conversation as resolved.
Outdated
|
||
| $result['status'] = 'recommended'; | ||
| $result['badge']['color'] = 'orange'; | ||
| $result['description'] = sprintf( | ||
| /* translators: %s: Number of enqueued scripts */ | ||
| esc_html__( 'Your website enqueues %s scripts. Try to reduce the number of JS assets, or to concatenate them.', 'performance-lab' ), | ||
|
felixarntz marked this conversation as resolved.
Outdated
|
||
| $enqueued_scripts | ||
| ); | ||
| $result['actions'] .= sprintf( | ||
| /* translators: 1: HelpHub URL. 2: Link description. 3.URL to clean cache. 4. Clean Cache text. */ | ||
| '<p><a target="_blank" href="%1$s">%2$s</a></p><p><a href="%3$s">%4$s</a></p>', | ||
| esc_url( __( 'https://wordpress.org/support/article/optimization/', 'performance-lab' ) ), | ||
| esc_html__( 'More info about performance optimization', 'performance-lab' ), | ||
| esc_url( add_query_arg( 'action', 'clean_aea_audit', wp_nonce_url( admin_url( 'site-health.php' ), 'clean_aea_audit' ) ) ), | ||
| esc_html__( 'Clean Test Cache', 'performance-lab' ) | ||
| ); | ||
| } | ||
|
|
||
| return $result; | ||
| } | ||
|
|
||
| /** | ||
| * Callback for enqueued_css_assets test. | ||
| * | ||
| * @since 1.0.0 | ||
| * | ||
| * @return array | ||
| */ | ||
| function perflab_aea_enqueued_css_assets_test() { | ||
| /** | ||
| * If the test didn't run yet, deactivate. | ||
| */ | ||
| $enqueued_styles = perflab_aea_get_total_enqueued_styles(); | ||
| if ( false === $enqueued_styles ) { | ||
| return array(); | ||
| } | ||
| $result = array( | ||
| 'label' => esc_html__( 'Enqueued CSS assets', 'performance-lab' ), | ||
| 'status' => 'good', | ||
| 'badge' => array( | ||
| 'label' => esc_html__( 'Performance', 'performance-lab' ), | ||
| 'color' => 'blue', | ||
| ), | ||
| 'description' => sprintf( | ||
| '<p>%s</p>', | ||
| esc_html__( 'The amount of enqueued CSS assets is acceptable.', 'performance-lab' ) | ||
|
manuelRod marked this conversation as resolved.
Outdated
|
||
| ), | ||
| 'actions' => '', | ||
| 'test' => 'enqueued_css_assets', | ||
| ); | ||
|
|
||
| if ( $enqueued_styles > 10 ) { | ||
|
manuelRod marked this conversation as resolved.
Outdated
|
||
| $result['status'] = 'recommended'; | ||
| $result['badge']['color'] = 'orange'; | ||
| $result['description'] = sprintf( | ||
| /* translators: %s: Number of enqueued styles */ | ||
| esc_html__( 'Your website enqueues %s styles. Try to reduce the number of CSS assets, or to concatenate them.', 'performance-lab' ), | ||
|
felixarntz marked this conversation as resolved.
Outdated
|
||
| $enqueued_styles | ||
| ); | ||
|
|
||
| $result['actions'] .= sprintf( | ||
| /* translators: 1: HelpHub URL. 2: Link description. 3.URL to clean cache. 4. Clean Cache text. */ | ||
| '<p><a target="_blank" href="%1$s">%2$s</a></p><p><a href="%3$s">%4$s</a></p>', | ||
| esc_url( __( 'https://wordpress.org/support/article/optimization/', 'performance-lab' ) ), | ||
| esc_html__( 'More info about performance optimization', 'performance-lab' ), | ||
| esc_url( add_query_arg( 'action', 'clean_aea_audit', wp_nonce_url( admin_url( 'site-health.php' ), 'clean_aea_audit' ) ) ), | ||
| esc_html__( 'Clean Test Cache', 'performance-lab' ) | ||
| ); | ||
| } | ||
|
|
||
| return $result; | ||
| } | ||
|
|
||
| /** | ||
| * Invalidate both transients/cache on user clean_aea_audit action. | ||
| * Redirects to site-health.php screen adter clean up. | ||
| * | ||
| * @since 1.0.0 | ||
| */ | ||
| function perflab_clean_aea_audit_action() { | ||
|
manuelRod marked this conversation as resolved.
Outdated
|
||
| if ( isset( $_GET['action'] ) && 'clean_aea_audit' === $_GET['action'] ) { | ||
| check_admin_referer( 'clean_aea_audit' ); | ||
|
manuelRod marked this conversation as resolved.
Outdated
|
||
| perflab_invalidate_cache_transients(); | ||
| wp_safe_redirect( remove_query_arg( array( 'action', '_wpnonce' ), wp_get_referer() ) ); | ||
| } | ||
| } | ||
| add_action( 'admin_init', 'perflab_clean_aea_audit_action' ); | ||
|
|
||
| /** | ||
| * Invalidate both transients/cache. | ||
| * | ||
| * @since 1.0.0 | ||
| */ | ||
| function perflab_invalidate_cache_transients() { | ||
|
manuelRod marked this conversation as resolved.
Outdated
|
||
| delete_transient( 'aea_enqueued_scripts' ); | ||
| delete_transient( 'aea_enqueued_styles' ); | ||
| } | ||
| add_action( 'switch_theme', 'perflab_invalidate_cache_transients' ); | ||
| add_action( 'activated_plugin', 'perflab_invalidate_cache_transients' ); | ||
| add_action( 'deactivated_plugin', 'perflab_invalidate_cache_transients' ); | ||
|
|
||
|
|
||
|
|
||
| /** | ||
| * Convert full URL paths to absolute paths. | ||
| * | ||
| * @since 1.0.0 | ||
| * | ||
| * @param string $resource_url URl resource link. | ||
| * @return string Returns abosulte path to the resource. | ||
| */ | ||
| function perflab_get_path_from_resource_url( $resource_url ) { | ||
|
manuelRod marked this conversation as resolved.
Outdated
|
||
| return ABSPATH . wp_make_link_relative( $resource_url ); | ||
|
manuelRod marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| /** | ||
| * If file exists, returns its size. | ||
| * | ||
| * @since 1.0.0 | ||
| * | ||
| * @param string $file_src Path to the file. | ||
| * @return int Returns size if file exists, 0 if it doesn't. | ||
| */ | ||
| function perflab_get_resource_file_size( $file_src ) { | ||
|
manuelRod marked this conversation as resolved.
Outdated
|
||
| return file_exists( $file_src ) ? filesize( $file_src ) : 0; | ||
| } | ||
|
|
||
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.