Skip to content

Commit

Permalink
Merge pull request #61 from alleyinteractive/feature/core-script-load…
Browse files Browse the repository at this point in the history
…ing-strategy-update

Use wp_enqueue_script() for async and defer load methods
  • Loading branch information
jacobschweitzer authored Mar 22, 2024
2 parents 12b8508 + 60b47b5 commit 6a892e0
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Change Log
This project adheres to [Semantic Versioning](http://semver.org/).

## 1.3.7

* Adds support for async and defer using the 'strategy' argument added for wp_enqueue_script in WordPress 6.3.

## 1.3.6

* Adds support for running the plugin on a Windows hosting environment (#57)
Expand Down
2 changes: 1 addition & 1 deletion asset-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Plugin URI: https://github.com/alleyinteractive/wp-asset-manager
Description: Add more robust functionality to enqueuing static assets
Author: Alley Interactive
Version: 1.3.6
Version: 1.3.7
License: GPLv2 or later
Author URI: https://www.alleyinteractive.com/
*/
Expand Down
8 changes: 7 additions & 1 deletion php/class-asset-manager-scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,14 @@ public function post_validate_asset( $script ) {
* @param array $script Script to add.
*/
public function add_to_async( $script ) {
// For version of WordPress 6.3+ async and defer can use the core strategy for loading.
if ( version_compare( $GLOBALS['wp_version'], '6.3', '<' ) ) {
$load_methods_to_async = [ 'async', 'defer', 'async-defer' ];
} else {
$load_methods_to_async = [ 'async-defer' ];
}
if (
( 'defer' === $script['load_method'] || 'async' === $script['load_method'] || 'async-defer' === $script['load_method'] ) &&
in_array( $script['load_method'], $load_methods_to_async, true ) &&
! in_array( $script['handle'], $this->async_scripts, true )
) {
$this->async_scripts[] = $script['handle'];
Expand Down
23 changes: 22 additions & 1 deletion php/class-asset-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,34 @@ public function add_asset( $args ) {
// Enqueue asset if applicable.
if ( in_array( $args['load_method'], $this->wp_enqueue_methods, true ) && empty( $args['loaded'] ) ) {
if ( function_exists( $wp_enqueue_function ) ) {

// If this is for a style, just pass the media argument.
if ( 'style' === $args['type'] ) {
$enqueue_options = $args['media'];
} else {
// If this is for a script, pass the in_footer argument when on a version prior to 6.3.
if ( version_compare( $GLOBALS['wp_version'], '6.3', '<' ) ) {
$enqueue_options = $args['in_footer'];
} else {
// We are on a version of WordPress 6.3+ so the last argument is an array.
$enqueue_options = [
'in_footer' => $args['in_footer'],
];
// If the load method is async or defer, set the strategy.
if ( in_array( $args['load_method'], [ 'async', 'defer' ], true ) ) {
$enqueue_options['strategy'] = $args['load_method'];
}
}
}

$wp_enqueue_function(
$args['handle'],
$args['src'],
$args['deps'],
$args['version'],
'style' === $args['type'] ? $args['media'] : $args['in_footer']
$enqueue_options
);

$args['loaded'] = true;
} else {
echo wp_kses_post( $this->format_error( $this->generate_asset_error( 'invalid_enqueue_function', false, $wp_enqueue_function ) ) );
Expand Down
22 changes: 19 additions & 3 deletions tests/test-scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ function test_add_attributes() {
$async_asset = [
'handle' => 'async-asset',
'src' => get_stylesheet_directory_uri() . '/static/js/async-test.js',
'load_method' => 'async',
'load_method' => 'async-defer',
];
$original_tag = '<script type="text/javascript" src="http://example.com/wp-content/themes/twentytwelve/static/js/async-test.js"></script>';
am_enqueue_script( $async_asset );
\Asset_Manager_Scripts::instance()->add_to_async( $async_asset );
$expected_async_tag = '<script type="text/javascript" async src="http://example.com/wp-content/themes/twentytwelve/static/js/async-test.js"></script>';
$expected_async_tag = '<script type="text/javascript" async defer src="http://example.com/wp-content/themes/twentytwelve/static/js/async-test.js"></script>';
$actual_async_tag = \Asset_Manager_Scripts::instance()->add_attributes( $original_tag, 'async-asset' );
$this->assertEquals( $expected_async_tag, $actual_async_tag, 'add_to_async should add the approprate attribute (async or defer) to a script' );
}
Expand Down Expand Up @@ -152,7 +152,7 @@ function test_add_to_async() {
$this->test_script_two,
[
'handle' => 'async-script-test',
'load_method' => 'defer',
'load_method' => 'async-defer',
]
);
\Asset_Manager_Scripts::instance()->add_to_async( $async_script );
Expand All @@ -162,4 +162,20 @@ function test_add_to_async() {
\Asset_Manager_Scripts::instance()->add_to_async( $async_script );
$this->assertContains( 'async-script-test', \Asset_Manager_Scripts::instance()->async_scripts, 'A script should not be added to the $async_scripts property twice' );
}

/**
* Test defer attribute handling.
* @group assets
*/
public function test_defer_attribute() {

// Enqueue the script with the defer attribute.
am_enqueue_script( $this->test_script['handle'], $this->test_script['src'], [], 'global', 'defer' );

// Get the script tag output.
$script_output = get_echo( 'wp_print_scripts', [ $this->test_script['handle'] ] );

// Check if the script tag has the defer attribute.
$this->assertStringContainsString( 'defer"', $script_output );
}
}

0 comments on commit 6a892e0

Please sign in to comment.