diff --git a/asset-manager.php b/asset-manager.php index 702a6ab..e3576d9 100644 --- a/asset-manager.php +++ b/asset-manager.php @@ -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' ), diff --git a/php/class-asset-manager.php b/php/class-asset-manager.php index 9921579..d1ea525 100644 --- a/php/class-asset-manager.php +++ b/php/class-asset-manager.php @@ -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']; } diff --git a/tests/test-scripts.php b/tests/test-scripts.php index 29ac82f..6ab4674 100644 --- a/tests/test-scripts.php +++ b/tests/test-scripts.php @@ -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 ); + } }