Skip to content
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

Add/sitehealth enqueued assets #1

Merged
merged 22 commits into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a7883dc
Plugin https://github.com/audrasjb/site-health-audit-enqueued-assets/…
manuelRod Dec 8, 2021
83fd785
partial unit testing added.
manuelRod Dec 9, 2021
69d1208
Adding more unit testing.
manuelRod Dec 9, 2021
bda604d
adding missing tests.
manuelRod Dec 10, 2021
663fe3a
removing duplicated get_transient
manuelRod Dec 15, 2021
3a5cc54
Adding filter for transient, and invalidate cache on switch_theme, ac…
manuelRod Dec 15, 2021
2e555a1
Adding user action to null transient cache.
manuelRod Dec 15, 2021
c668003
redirects after cleaning cache to site-health.php without query args
manuelRod Dec 16, 2021
2baf8fe
All prefix needs to be updated to perflab_
manuelRod Dec 16, 2021
327bed3
adding functionality to get resources sizes
manuelRod Dec 16, 2021
f7c2a60
Adding filesize to the transient cache, and functionality related to it.
manuelRod Dec 17, 2021
5333d7d
Merge branch 'trunk' into add/sitehealth-enqueued-assets
manuelRod Jan 10, 2022
b0ccda4
migrating module to new module structure
manuelRod Jan 10, 2022
2757e8c
migration to new module structure
manuelRod Jan 10, 2022
07a6a70
split code refactor, creation of a new helper file with helper method…
manuelRod Jan 10, 2022
47f4f9f
prefix fix
manuelRod Jan 10, 2022
84af518
refactor adding suggestions:
manuelRod Jan 10, 2022
a528fb4
changing wording and adding information about bellow threshold limits.
manuelRod Jan 11, 2022
e42b9c2
implementing new threshold limits
manuelRod Jan 11, 2022
90c17f8
Adding filters for limits. Fixing _n translatable strings.
manuelRod Jan 26, 2022
35edbca
nit-pick fixes
manuelRod Jan 27, 2022
780b098
Adding new supported wp setups for perflab_aea_get_path_from_resource…
manuelRod Jan 28, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
242 changes: 242 additions & 0 deletions modules/audit-enqueued-assets/load.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
<?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() {
manuelRod marked this conversation as resolved.
Show resolved Hide resolved
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;
}
}
$expiration = apply_filters( 'aea_audit_enqueued_scripts_expiration_in_seconds', 12 * HOUR_IN_SECONDS );
set_transient( 'aea_enqueued_scripts', $enqueued_scripts, $expiration );
}
}
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' ) ) {
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;
}
}
$expiration = apply_filters( 'aea_audit_enqueued_styles_expiration_in_seconds', 12 * HOUR_IN_SECONDS );
set_transient( 'aea_enqueued_styles', $enqueued_styles, $expiration );
}
}
add_action( 'wp_print_styles', '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 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 of enqueued styles.
*
* @since 1.0.0
*
* @return int|false Number of total styles or false if transient hasn't been set.
*/
function 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;
}

/**
* 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' )
),
'actions' => '',
'test' => 'enqueued_js_assets',
);

if ( $enqueued_scripts > 10 ) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as for the stylesheet, we need to grab and sum the total size of assets loaded.

$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' ),
$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 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' )
),
'actions' => '',
manuelRod marked this conversation as resolved.
Show resolved Hide resolved
'test' => 'enqueued_css_assets',
);

if ( $enqueued_styles > 10 ) {
Copy link

@ThierryA ThierryA Dec 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This signal needs to be more robust. The number of stylesheets is somehow a week signal, what really matter is the totally size. We need to grab and sum the size of all stylesheets and define a threshold for the warning.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ThierryA I've opened the PR in WP WordPress#54 we can follow up in there.

Copy link

@ThierryA ThierryA Dec 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @manuelRod, much appreciated. Left the same comments on the new PR to continue the conversation there.

$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' ),
$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.
*
* @since 1.0.0
*/
function clean_aea_audit_action() {
if ( isset( $_GET['action'] ) && 'clean_aea_audit' === $_GET['action'] ) {
check_admin_referer( 'clean_aea_audit' );
invalidate_cache_transients();
manuelRod marked this conversation as resolved.
Show resolved Hide resolved
}
}
add_action( 'admin_init', 'clean_aea_audit_action' );

/**
* Invalidate both transients/cache.
*
* @since 1.0.0
*/
function invalidate_cache_transients() {
delete_transient( 'aea_enqueued_scripts' );
delete_transient( 'aea_enqueued_styles' );
}
add_action( 'switch_theme', 'invalidate_cache_transients' );
add_action( 'activated_plugin', 'invalidate_cache_transients' );
add_action( 'deactivated_plugin', 'invalidate_cache_transients' );

Loading