Skip to content

Commit

Permalink
Backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobschweitzer committed Mar 20, 2024
1 parent 38c837c commit 4f3fe0d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 39 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ $logomark_svg_markup = am_get_symbol(
## Requirements
* WordPress: 6.3.0+
* WordPress: 5.2.0+
* PHP: 7.4+
## Downloads and Versioning.
Expand Down
36 changes: 9 additions & 27 deletions asset-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,15 @@
* @package AssetManager
*/

/**
* Plugin Name: Asset Manager
* Plugin URI: https://github.com/alleyinteractive/wp-asset-manager
* Description: Add more robust functionality to enqueuing static assets
* Author: Alley Interactive
* Version: 1.3.7
* License: GPLv2 or later
* Author URI: https://www.alleyinteractive.com/
* Requires at least: 6.3
*/

// 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' ),
'6.3'
);
printf( '<div class="notice notice-error"><p>%s</p></div>', $message );
}
/*
Plugin Name: Asset Manager
Plugin URI: https://github.com/alleyinteractive/wp-asset-manager
Description: Add more robust functionality to enqueuing static assets
Author: Alley Interactive
Version: 1.3.7
License: GPLv2 or later
Author URI: https://www.alleyinteractive.com/
*/

/**
* Filesystem path to AssetManager.
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 (
( '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
14 changes: 9 additions & 5 deletions php/class-asset-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,15 @@ public function add_asset( $args ) {
if ( 'style' === $args['type'] ) {
$enqueue_options = $args['media'];
} else {
$enqueue_options = [
'in_footer' => $args['in_footer'],
];
if ( in_array( $args['load_method'], [ 'async', 'defer' ], true ) ) {
$enqueue_options['strategy'] = $args['load_method'];
if ( version_compare( $GLOBALS['wp_version'], '6.3', '<' ) ) {
$enqueue_options = $args['in_footer'];
} else {
$enqueue_options = [
'in_footer' => $args['in_footer'],
];
if ( in_array( $args['load_method'], [ 'async', 'defer' ], true ) ) {
$enqueue_options['strategy'] = $args['load_method'];
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions tests/test-scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,17 @@ function test_add_to_async() {
}

/**
* Test defer attribute handling.
*/
* 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'] ] );
// 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 );
$this->assertStringContainsString( 'defer"', $script_output );
}
}

0 comments on commit 4f3fe0d

Please sign in to comment.