Skip to content

Commit

Permalink
Sync with latest from production
Browse files Browse the repository at this point in the history
  • Loading branch information
renatonascalves committed Jan 16, 2025
2 parents 324187d + 1f71b35 commit dfc7b0f
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 79 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Change Log

This project adheres to [Semantic Versioning](http://semver.org/).


Expand All @@ -8,6 +9,18 @@ This project adheres to [Semantic Versioning](http://semver.org/).

* Register an asset to the WP dependency registry. (#66)

## 1.4.2

### Fixed

- Fixed issue with array of data being passed to the `$src` argument of `am_enqueue_script()`.

## 1.4.1

### Fixed

- Ensure that `$version` can be null when passed to helper methods.

## 1.4.0

### Fixed
Expand Down
34 changes: 17 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
@@ -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.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",
"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"
}
10 changes: 5 additions & 5 deletions src/class-asset-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,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.
* }
*/
Expand Down
112 changes: 56 additions & 56 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,27 @@ 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{
* handle: string,
* src?: string,
* condition?: string,
* deps?: array<string>,
* load_hook?: string,
* load_method?: string,
* version?: string
* }
*
* @param string|EnqueueScriptArgs $handle Handle for script.
* @param string|null $src URI to script.
* @param string|array $handle Handle for script.
* @param array<mixed>|string|null $src URI to script or array of inline script data.
* @param array<string> $deps This script's dependencies.
* @param array<string>|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|null $version Version of the script.
* @param string $load_hook Hook on which to load this asset.
*
* @phpstan-param string|array{
* handle: string,
* src?: array<mixed>|string,
* condition?: string,
* deps?: array<string>,
* load_hook?: string,
* load_method?: string,
* 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 );
Expand All @@ -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<string>,
Expand All @@ -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;

Expand All @@ -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>|string,
Expand All @@ -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 );
Expand All @@ -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 <svg>
* everywhere it is printed.
*
* @phpstan-param string|array{
* handle: string,
* src?: string,
* condition?: array<string>|string,
* attributes?: array<string, string>
* }
*
* @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 <svg>
* 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' );
Expand Down
23 changes: 23 additions & 0 deletions tests/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,29 @@ public 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 = '<script class="wp-asset-manager test-inline-asset" type="text/javascript">window.amScripts = window.amScripts || {}; window.amScripts["test-inline-asset"] = {"myGlobalVar":true}</script>';
$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' )]
public function test_asset_should_add() {
// If no handle, should return false
Expand Down
2 changes: 1 addition & 1 deletion wp-asset-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
Description: Add more robust functionality to enqueuing static assets
Author: Alley Interactive
Author URI: https://alley.com
Version: 1.4.0
Version: 1.4.2
License: GPLv2 or later
*/

Expand Down

0 comments on commit dfc7b0f

Please sign in to comment.