-
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 1 commit
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
Next
Next commit
…blob/master/site-health-audit-enqueued-assets.php imported as a module and some little refactoring.
- Loading branch information
commit a7883dc5e5e4ab82ecbd9f533f7d9ae5dfeeb194
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,208 @@ | ||
| <?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 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[] = $src; | ||
| } | ||
| } | ||
| set_transient( 'aea_enqueued_scripts', $enqueued_scripts, 12 * HOUR_IN_SECONDS ); | ||
| } | ||
| } | ||
| add_action( 'wp_print_scripts', '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 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[] = $src; | ||
| } | ||
| } | ||
| set_transient( 'aea_enqueued_styles', $enqueued_styles, 12 * HOUR_IN_SECONDS ); | ||
| } | ||
| } | ||
| add_action( 'wp_print_styles', 'aea_audit_enqueued_styles' ); | ||
|
|
||
| /** | ||
| * Gets total of enqueued scripts. | ||
| * | ||
| * @since 1.0.0 | ||
| * | ||
| * @return int Number of total scripts. | ||
| */ | ||
| function aea_get_total_enqueued_scripts() { | ||
| $enqueued_scripts = false; | ||
| if ( get_transient( 'aea_enqueued_scripts' ) ) { | ||
| $list_enqueued_scripts = get_transient( 'aea_enqueued_scripts' ); | ||
| $enqueued_scripts = count( $list_enqueued_scripts ); | ||
| } | ||
| return $enqueued_scripts; | ||
| } | ||
|
|
||
| /** | ||
| * Gets total of enqueued styles. | ||
| * | ||
| * @since 1.0.0 | ||
| */ | ||
| function aea_get_total_enqueued_styles() { | ||
| $enqueued_styles = false; | ||
| if ( get_transient( 'aea_enqueued_styles' ) ) { | ||
| $list_enqueued_styles = get_transient( 'aea_enqueued_styles' ); | ||
| $enqueued_styles = count( $list_enqueued_styles ); | ||
| } | ||
| return $enqueued_styles; | ||
| } | ||
|
|
||
| /** | ||
| * Adds tests to site health. | ||
| * | ||
| * @since 1.0.0 | ||
| * | ||
| * @param array $tests Site Health Tests. | ||
| * @return array | ||
| */ | ||
| function aea_add_enqueued_assets_test( $tests ) { | ||
| $tests['direct']['enqueued_js_assets'] = array( | ||
| 'label' => esc_html__( 'JS assets', 'performance-lab' ), | ||
| 'test' => 'aea_enqueued_js_assets_test', | ||
| ); | ||
| $tests['direct']['enqueued_css_assets'] = array( | ||
| 'label' => esc_html__( 'CSS assets', 'performance-lab' ), | ||
| 'test' => 'aea_enqueued_css_assets_test', | ||
| ); | ||
|
|
||
| return $tests; | ||
| } | ||
| add_filter( 'site_status_tests', 'aea_add_enqueued_assets_test' ); | ||
|
|
||
| /** | ||
| * Callback for enqueued_js_assets test. | ||
| * | ||
| * @since 1.0.0 | ||
| * | ||
| * @return array | ||
| */ | ||
| function aea_enqueued_js_assets_test() { | ||
| /** | ||
| * If the test didn't run yet, deactivate. | ||
| */ | ||
| $enqueued_scripts = 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. */ | ||
| '<p><a target="_blank" href="%1$s">%2$s</a></p>', | ||
| esc_url( __( 'https://wordpress.org/support/article/optimization/', 'performance-lab' ) ), | ||
| esc_html__( 'More info about performance optimization', 'performance-lab' ) | ||
| ); | ||
| } | ||
|
|
||
| return $result; | ||
| } | ||
|
|
||
| /** | ||
| * Callback for enqueued_css_assets test. | ||
| * | ||
| * @since 1.0.0 | ||
| * | ||
| * @return array | ||
| */ | ||
| function aea_enqueued_css_assets_test() { | ||
| /** | ||
| * If the test didn't run yet, deactivate. | ||
| */ | ||
| $enqueued_styles = 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. */ | ||
| '<p><a target="_blank" href="%1$s">%2$s</a></p>', | ||
| esc_url( __( 'https://wordpress.org/support/article/optimization/', 'performance-lab' ) ), | ||
| esc_html__( 'More info about performance optimization', 'performance-lab' ) | ||
| ); | ||
| } | ||
|
|
||
| return $result; | ||
| } | ||
|
|
||
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.