From 993fec6e6f3fbb11cb4071b503227305db68512a Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Wed, 15 Jan 2025 15:15:28 -0500 Subject: [PATCH 1/5] Allow version to be null, fix phpstan types --- CHANGELOG.md | 8 +++- src/helpers.php | 110 ++++++++++++++++++++++++------------------------ 2 files changed, 62 insertions(+), 56 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c2bf570..31323cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,14 @@ # Change Log + This project adheres to [Semantic Versioning](http://semver.org/). -## 1.4.0 +## 1.4.1 + +### Fixed + +- Ensure that `$version` can be null when passed to helper methods. +## 1.4.0 ### Fixed diff --git a/src/helpers.php b/src/helpers.php index 2b31dc0..813ed9d 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -32,7 +32,17 @@ function am_validate_path( string $path ): bool { /** * Load an external script. Options can be passed in as an array or individual parameters. * - * @phpstan-type EnqueueScriptArgs array{ + * @param string|array $handle Handle for script. + * @param string|null $src URI to script. + * @param array $deps This script's dependencies. + * @param array|string $condition Corresponds to a configured loading condition that, if matches, + * will allow the script to load. + * 'global' is assumed if no condition is declared. + * @param string $load_method How to load this asset. + * @param string|null $version Version of the script. + * @param string $load_hook Hook on which to load this asset. + * + * @phpstan-param string|array{ * handle: string, * src?: string, * condition?: string, @@ -40,19 +50,9 @@ function am_validate_path( string $path ): bool { * load_hook?: string, * load_method?: string, * version?: string - * } - * - * @param string|EnqueueScriptArgs $handle Handle for script. - * @param string|null $src URI to script. - * @param array $deps This script's dependencies. - * @param array|string $condition Corresponds to a configured loading condition that, if matches, - * will allow the script to load. - * 'global' is assumed if no condition is declared. - * @param string $load_method How to load this asset. - * @param string $version Version of the script. - * @param string $load_hook Hook on which to load this asset. + * } $handle */ - function am_enqueue_script( array|string $handle, ?string $src = null, array $deps = [], array|string $condition = 'global', string $load_method = 'sync', string $version = '1.0.0', string $load_hook = 'wp_head' ): void { + function am_enqueue_script( array|string $handle, ?string $src = null, array $deps = [], array|string $condition = 'global', string $load_method = 'sync', ?string $version = '1.0.0', string $load_hook = 'wp_head' ): void { $defaults = compact( 'handle', 'src', 'deps', 'condition', 'load_method', 'version', 'load_hook' ); $args = is_array( $handle ) ? array_merge( $defaults, $handle ) : $defaults; Scripts::instance()->add_asset( $args ); @@ -79,7 +79,18 @@ function am_modify_load_method( string $handle, string $load_method = 'sync' ): /** * Load an external stylesheet. Options can be passed in as an array or individual parameters. * - * @phpstan-type EnqueueStyleArgs array{ + * @param string|array $handle Handle for stylesheet. This is necessary for dependency management. + * @param string $src URI to stylesheet. + * @param array $deps List of dependencies. + * @param array|string $condition Corresponds to a configured loading condition that, if matches, + * will allow the stylesheet to load. + * 'global' is assumed if no condition is declared. + * @param string $load_method How to load this asset. + * @param string|null $version Version of the script. + * @param string $load_hook Hook on which to load this asset. + * @param string $media Media query to restrict when this asset is loaded. + * + * @phpstan-param string|array{ * handle: string, * src?: string, * deps?: array, @@ -88,20 +99,9 @@ function am_modify_load_method( string $handle, string $load_method = 'sync' ): * version?: string, * load_hook?: string, * media?: string - * } - * - * @param string|EnqueueStyleArgs $handle Handle for stylesheet. This is necessary for dependency management. - * @param string $src URI to stylesheet. - * @param array $deps List of dependencies. - * @param array|string $condition Corresponds to a configured loading condition that, if matches, - * will allow the stylesheet to load. - * 'global' is assumed if no condition is declared. - * @param string $load_method How to load this asset. - * @param string $version Version of the script. - * @param string $load_hook Hook on which to load this asset. - * @param string $media Media query to restrict when this asset is loaded. + * } $handle */ - function am_enqueue_style( array|string $handle, ?string $src = null, array $deps = [], array|string $condition = 'global', string $load_method = 'sync', string $version = '1.0.0', string $load_hook = 'wp_head', ?string $media = null ): void { + function am_enqueue_style( array|string $handle, ?string $src = null, array $deps = [], array|string $condition = 'global', string $load_method = 'sync', ?string $version = '1.0.0', string $load_hook = 'wp_head', ?string $media = null ): void { $defaults = compact( 'handle', 'src', 'deps', 'condition', 'load_method', 'version', 'load_hook', 'media' ); $args = is_array( $handle ) ? array_merge( $defaults, $handle ) : $defaults; @@ -125,7 +125,19 @@ function am_enqueue_style( array|string $handle, ?string $src = null, array $dep /** * Provide an asset with a `preload` resource hint for the browser to prioritize. * - * @phpstan-type PreloadArgs array{ + * @param array|string $handle Handle for asset. This is necessary for dependency management. + * @param string $src URI to asset. + * @param array|string $condition Corresponds to a configured loading condition that, if matches, + * will allow the asset to load. + * 'global' is assumed if no condition is declared. + * @param string|null $version Version of the asset. + * @param string $media Media query to restrict when this asset is loaded. + * @param string $as A hint to the browser about what type of asset this is. + * See $preload_as for valid options. + * @param boolean $crossorigin Preload this asset cross-origin. + * @param string $mime_type The MIME type for the preloaded asset. + * + * @phpstan-param string|array{ * handle: string, * src?: string, * condition?: array|string, @@ -134,21 +146,9 @@ function am_enqueue_style( array|string $handle, ?string $src = null, array $dep * as?: string, * crossorigin?: bool, * mime_type?: string - * } - * - * @param string|PreloadArgs $handle Handle for asset. This is necessary for dependency management. - * @param string $src URI to asset. - * @param array|string $condition Corresponds to a configured loading condition that, if matches, - * will allow the asset to load. - * 'global' is assumed if no condition is declared. - * @param string $version Version of the asset. - * @param string $media Media query to restrict when this asset is loaded. - * @param string $as A hint to the browser about what type of asset this is. - * See $preload_as for valid options. - * @param boolean $crossorigin Preload this asset cross-origin. - * @param string $mime_type The MIME type for the preloaded asset. + * } $handle Handle for asset. This is necessary for dependency management. */ - function am_preload( array|string $handle, ?string $src = null, array|string $condition = 'global', string $version = '1.0.0', string $media = 'all', ?string $as = null, bool $crossorigin = false, ?string $mime_type = null ): void { + function am_preload( array|string $handle, ?string $src = null, array|string $condition = 'global', ?string $version = '1.0.0', string $media = 'all', ?string $as = null, bool $crossorigin = false, ?string $mime_type = null ): void { $defaults = compact( 'handle', 'src', 'condition', 'version', 'media', 'as', 'crossorigin', 'mime_type' ); $args = is_array( $handle ) ? array_merge( $defaults, $handle ) : $defaults; Preload::instance()->add_asset( $args ); @@ -161,23 +161,23 @@ function am_preload( array|string $handle, ?string $src = null, array|string $co /** * Define a symbol to be added to the SVG sprite. * - * @phpstan-type RegisterSymbolArgs array{ + * @param string|array $handle Handle for asset, used to refer to the symbol in `am_use_symbol`. + * @param string $src Absolute path from the current theme root, or a relative path + * based on the current theme root. Use the `am_modify_svg_directory` + * filter to update the directory from which relative paths will be + * completed. + * @param array|string $condition Corresponds to a configured loading condition that, if matches, + * will allow the asset to be added to the sprite sheet. + * 'global' is assumed if no condition is declared. + * @param array $attributes An array of attribute names and values to add to the resulting + * everywhere it is printed. + * + * @phpstan-param string|array{ * handle: string, * src?: string, * condition?: array|string, * attributes?: array - * } - * - * @param string|RegisterSymbolArgs $handle Handle for asset, used to refer to the symbol in `am_use_symbol`. - * @param string $src Absolute path from the current theme root, or a relative path - * based on the current theme root. Use the `am_modify_svg_directory` - * filter to update the directory from which relative paths will be - * completed. - * @param array|string $condition Corresponds to a configured loading condition that, if matches, - * will allow the asset to be added to the sprite sheet. - * 'global' is assumed if no condition is declared. - * @param array $attributes An array of attribute names and values to add to the resulting - * everywhere it is printed. + * } $handle */ function am_register_symbol( array|string $handle, ?string $src = null, array|string $condition = 'global', array $attributes = [] ): void { $defaults = compact( 'handle', 'src', 'condition', 'attributes' ); From 124c68be507c1041d0fcde1b51ba84c5e7fe0f6f Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Wed, 15 Jan 2025 15:16:50 -0500 Subject: [PATCH 2/5] spacing --- src/helpers.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/helpers.php b/src/helpers.php index 813ed9d..4197610 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -83,8 +83,8 @@ function am_modify_load_method( string $handle, string $load_method = 'sync' ): * @param string $src URI to stylesheet. * @param array $deps List of dependencies. * @param array|string $condition Corresponds to a configured loading condition that, if matches, - * will allow the stylesheet to load. - * 'global' is assumed if no condition is declared. + * will allow the stylesheet to load. + * 'global' is assumed if no condition is declared. * @param string $load_method How to load this asset. * @param string|null $version Version of the script. * @param string $load_hook Hook on which to load this asset. @@ -128,8 +128,8 @@ function am_enqueue_style( array|string $handle, ?string $src = null, array $dep * @param array|string $handle Handle for asset. This is necessary for dependency management. * @param string $src URI to asset. * @param array|string $condition Corresponds to a configured loading condition that, if matches, - * will allow the asset to load. - * 'global' is assumed if no condition is declared. + * will allow the asset to load. + * 'global' is assumed if no condition is declared. * @param string|null $version Version of the asset. * @param string $media Media query to restrict when this asset is loaded. * @param string $as A hint to the browser about what type of asset this is. @@ -167,8 +167,8 @@ function am_preload( array|string $handle, ?string $src = null, array|string $co * filter to update the directory from which relative paths will be * completed. * @param array|string $condition Corresponds to a configured loading condition that, if matches, - * will allow the asset to be added to the sprite sheet. - * 'global' is assumed if no condition is declared. + * will allow the asset to be added to the sprite sheet. + * 'global' is assumed if no condition is declared. * @param array $attributes An array of attribute names and values to add to the resulting * everywhere it is printed. * From 2b8d93dcec920f63ed394a21dc28f0931f37736f Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Wed, 15 Jan 2025 15:17:09 -0500 Subject: [PATCH 3/5] Bump version --- package.json | 34 +++++++++++++++++----------------- wp-asset-manager.php | 2 +- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 968d940..0d69ae0 100644 --- a/package.json +++ b/package.json @@ -1,19 +1,19 @@ { - "name": "@alleyinteractive/wp-asset-manager", - "version": "1.4.0", - "description": "Asset Manager is a toolkit for managing front-end assets and more tightly controlling where, when, and how they're loaded.", - "engines": { - "node": "22", - "npm": "10" - }, - "scripts": { - "build": "echo 'No build step defined'", - "release": "npx @alleyinteractive/create-release@latest" - }, - "author": "", - "license": "GPL-2.0+", - "bugs": { - "url": "https://github.com/alleyinteractive/wp-asset-manager/issues" - }, - "homepage": "https://github.com/alleyinteractive/wp-asset-manager#readme" + "name": "@alleyinteractive/wp-asset-manager", + "version": "1.4.1", + "description": "Asset Manager is a toolkit for managing front-end assets and more tightly controlling where, when, and how they're loaded.", + "engines": { + "node": "22", + "npm": "10" + }, + "scripts": { + "build": "echo 'No build step defined'", + "release": "npx @alleyinteractive/create-release@latest" + }, + "author": "", + "license": "GPL-2.0+", + "bugs": { + "url": "https://github.com/alleyinteractive/wp-asset-manager/issues" + }, + "homepage": "https://github.com/alleyinteractive/wp-asset-manager#readme" } diff --git a/wp-asset-manager.php b/wp-asset-manager.php index 6d09db4..95744df 100644 --- a/wp-asset-manager.php +++ b/wp-asset-manager.php @@ -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.4.0 +Version: 1.4.1 License: GPLv2 or later Author URI: https://alley.com */ From 68155e912f3cbf23c21eff4d08d501385ee9747b Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Wed, 15 Jan 2025 15:24:26 -0500 Subject: [PATCH 4/5] Update src/helpers.php Co-authored-by: David Herrera --- src/helpers.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers.php b/src/helpers.php index 4197610..897daad 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -79,7 +79,7 @@ function am_modify_load_method( string $handle, string $load_method = 'sync' ): /** * Load an external stylesheet. Options can be passed in as an array or individual parameters. * - * @param string|array $handle Handle for stylesheet. This is necessary for dependency management. + * @param string|array $handle Handle for stylesheet. This is necessary for dependency management. * @param string $src URI to stylesheet. * @param array $deps List of dependencies. * @param array|string $condition Corresponds to a configured loading condition that, if matches, From 6fe6f4edd96381d3817a5cb20856ec53b747380e Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Wed, 15 Jan 2025 16:12:16 -0500 Subject: [PATCH 5/5] Fix src being an array --- CHANGELOG.md | 6 ++++++ package.json | 2 +- src/class-asset-manager.php | 10 +++++----- src/helpers.php | 22 +++++++++++----------- tests/AssetsTest.php | 23 +++++++++++++++++++++++ wp-asset-manager.php | 2 +- 6 files changed, 47 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31323cc..3c9acb7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ This project adheres to [Semantic Versioning](http://semver.org/). +## 1.4.2 + +### Fixed + +- Fixed issue with array of data being passed to the `$src` argument of `am_enqueue_script()`. + ## 1.4.1 ### Fixed diff --git a/package.json b/package.json index 0d69ae0..8bcc3e7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@alleyinteractive/wp-asset-manager", - "version": "1.4.1", + "version": "1.4.2", "description": "Asset Manager is a toolkit for managing front-end assets and more tightly controlling where, when, and how they're loaded.", "engines": { "node": "22", diff --git a/src/class-asset-manager.php b/src/class-asset-manager.php index 34098d0..4079d9f 100644 --- a/src/class-asset-manager.php +++ b/src/class-asset-manager.php @@ -265,11 +265,11 @@ public function set_core_assets_ref( $assets ) { * @param array $args { * Arguments for loading asset. May differ based on asset type, but most contain the following. * - * @type string $handle Handle for asset. Currently not used, but could be used to dequeue assets in the future. - * @type string $src URI for src attribute of printed asset handle - * @type string $condition Corresponds to a configured condition under which the asset should be loaded - * @type string $load_hook Hook on which to load the asset - * @type string $load_method Style with which to load this asset. Defaults to 'sync'. + * @type string $handle Handle for asset. Currently not used, but could be used to dequeue assets in the future. + * @type array|string $src URI for src attribute of printed asset handle. For scripts, it can be an array of data that should be JSON encoded and printed on the page. + * @type string $condition Corresponds to a configured condition under which the asset should be loaded + * @type string $load_hook Hook on which to load the asset + * @type string $load_method Style with which to load this asset. Defaults to 'sync'. * Accepts 'sync', 'async', 'defer', with additional values for specific asset types. * } * diff --git a/src/helpers.php b/src/helpers.php index 897daad..2f7b922 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -32,19 +32,19 @@ function am_validate_path( string $path ): bool { /** * Load an external script. Options can be passed in as an array or individual parameters. * - * @param string|array $handle Handle for script. - * @param string|null $src URI to script. - * @param array $deps This script's dependencies. - * @param array|string $condition Corresponds to a configured loading condition that, if matches, - * will allow the script to load. - * 'global' is assumed if no condition is declared. - * @param string $load_method How to load this asset. - * @param string|null $version Version of the script. - * @param string $load_hook Hook on which to load this asset. + * @param string|array $handle Handle for script. + * @param array|string|null $src URI to script or array of inline script data. + * @param array $deps This script's dependencies. + * @param array|string $condition Corresponds to a configured loading condition that, if matches, + * will allow the script to load. + * 'global' is assumed if no condition is declared. + * @param string $load_method How to load this asset. + * @param string|null $version Version of the script. + * @param string $load_hook Hook on which to load this asset. * * @phpstan-param string|array{ * handle: string, - * src?: string, + * src?: array|string, * condition?: string, * deps?: array, * load_hook?: string, @@ -52,7 +52,7 @@ function am_validate_path( string $path ): bool { * version?: string * } $handle */ - function am_enqueue_script( array|string $handle, ?string $src = null, array $deps = [], array|string $condition = 'global', string $load_method = 'sync', ?string $version = '1.0.0', string $load_hook = 'wp_head' ): void { + function am_enqueue_script( array|string $handle, array|string|null $src = null, array $deps = [], array|string $condition = 'global', string $load_method = 'sync', ?string $version = '1.0.0', string $load_hook = 'wp_head' ): void { $defaults = compact( 'handle', 'src', 'deps', 'condition', 'load_method', 'version', 'load_hook' ); $args = is_array( $handle ) ? array_merge( $defaults, $handle ) : $defaults; Scripts::instance()->add_asset( $args ); diff --git a/tests/AssetsTest.php b/tests/AssetsTest.php index 38c4bf0..d91ac63 100644 --- a/tests/AssetsTest.php +++ b/tests/AssetsTest.php @@ -61,6 +61,29 @@ function test_load_asset() { $wp_current_filter = $old_filter; } + #[Group( 'assets' )] + function test_load_asset_as_arguments() { + // Temporarily set current filter to 'wp_head' to trick current_filter() + global $wp_current_filter; + $old_filter = $wp_current_filter; + $wp_current_filter = [ 'wp_head' ]; + + am_enqueue_script( + handle: 'test-inline-asset', + src: [ + 'myGlobalVar' => true, + ], + load_method: 'inline', + load_hook: 'wp_head', + ); + $actual_output = get_echo( [ Scripts::instance(), 'load_assets' ] ); + $expected_output = ''; + $this->assertEquals( $expected_output, $actual_output, 'Load assets should call the print_asset() function on each asset and echo the proper results' ); + + // Reset current filter + $wp_current_filter = $old_filter; + } + #[Group( 'assets' )] function test_asset_should_add() { // If no handle, should return false diff --git a/wp-asset-manager.php b/wp-asset-manager.php index 95744df..5f2013c 100644 --- a/wp-asset-manager.php +++ b/wp-asset-manager.php @@ -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.4.1 +Version: 1.4.2 License: GPLv2 or later Author URI: https://alley.com */