From 4f3fe0dc03437b8cc130d0c0b36cdf31e49226ad Mon Sep 17 00:00:00 2001
From: jacobschweitzer <3016425+jacobschweitzer@users.noreply.github.com>
Date: Wed, 20 Mar 2024 15:57:02 -0500
Subject: [PATCH] Backwards compatibility
---
README.md | 2 +-
asset-manager.php | 36 ++++++++---------------------
php/class-asset-manager-scripts.php | 8 ++++++-
php/class-asset-manager.php | 14 +++++++----
tests/test-scripts.php | 10 ++++----
5 files changed, 31 insertions(+), 39 deletions(-)
diff --git a/README.md b/README.md
index 6cc2634..dbfec65 100644
--- a/README.md
+++ b/README.md
@@ -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.
diff --git a/asset-manager.php b/asset-manager.php
index e3576d9..ccf0350 100644
--- a/asset-manager.php
+++ b/asset-manager.php
@@ -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( '
', $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.
diff --git a/php/class-asset-manager-scripts.php b/php/class-asset-manager-scripts.php
index 19e40fd..689d886 100644
--- a/php/class-asset-manager-scripts.php
+++ b/php/class-asset-manager-scripts.php
@@ -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'];
diff --git a/php/class-asset-manager.php b/php/class-asset-manager.php
index d1ea525..6a9c093 100644
--- a/php/class-asset-manager.php
+++ b/php/class-asset-manager.php
@@ -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'];
+ }
}
}
diff --git a/tests/test-scripts.php b/tests/test-scripts.php
index 6ab4674..14f52f9 100644
--- a/tests/test-scripts.php
+++ b/tests/test-scripts.php
@@ -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 );
}
}