Skip to content

Commit

Permalink
Add unit test for defer strategy and add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobschweitzer committed Mar 19, 2024
1 parent b91f293 commit 38c837c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
5 changes: 4 additions & 1 deletion asset-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
* Requires at least: 6.3
*/

// Check the minimum required WordPress version
// Check the minimum required WordPress version.
if ( version_compare( $GLOBALS['wp_version'], '6.3', '<' ) ) {
add_action( 'admin_notices', 'am_minimum_wp_version_notice' );
return;
}

/**
* Display an admin notice if the minimum required WordPress version is not met.
*/
function am_minimum_wp_version_notice() {
$message = sprintf(
__( 'Asset Manager requires WordPress version %s or higher. Please update WordPress to use this plugin.', 'am' ),
Expand Down
5 changes: 3 additions & 2 deletions php/class-asset-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,12 @@ 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 ) ) {
$enqueue_options = [];
if ( 'style' === $args['type'] ) {
$enqueue_options = $args['media'];
} else {
$enqueue_options['in_footer'] = $args['in_footer'];
$enqueue_options = [
'in_footer' => $args['in_footer'],
];
if ( in_array( $args['load_method'], [ 'async', 'defer' ], true ) ) {
$enqueue_options['strategy'] = $args['load_method'];
}
Expand Down
15 changes: 15 additions & 0 deletions tests/test-scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,19 @@ 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.
*/
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( 'data-wp-strategy="defer"', $script_output );
}
}

0 comments on commit 38c837c

Please sign in to comment.