| 1 |
<?php |
| 2 |
/** |
| 3 |
* Asset Loader Utility Class |
| 4 |
* |
| 5 |
* Provides static methods to enqueue scripts and styles with WordPress. |
| 6 |
* Automatically handles .asset.php metadata files generated by build tools like wp-scripts. |
| 7 |
* Falls back to file modification time for versioning if asset metadata is not available. |
| 8 |
* |
| 9 |
* Usage: |
| 10 |
* AssetLoader::enqueue_script( 'my-handle', 'my-script' ); |
| 11 |
* AssetLoader::enqueue_style( 'my-style', 'my-stylesheet' ); |
| 12 |
* |
| 13 |
* @package WordPress\AI |
| 14 |
*/ |
| 15 |
|
| 16 |
declare( strict_types=1 ); |
| 17 |
|
| 18 |
namespace WordPress\AI; |
| 19 |
|
| 20 |
/** |
| 21 |
* Class Asset_Loader |
| 22 |
* |
| 23 |
* A utility class for registering and enqueuing assets (scripts and styles) |
| 24 |
* with support for asset metadata files. |
| 25 |
* |
| 26 |
* @since 0.1.0 |
| 27 |
*/ |
| 28 |
class Asset_Loader { |
| 29 |
|
| 30 |
/** |
| 31 |
* Enqueue a script using a script path and its asset metadata. |
| 32 |
* |
| 33 |
* @since 0.1.0 |
| 34 |
* |
| 35 |
* @param string $handle The handle for the script. |
| 36 |
* @param string $file_name The script file name. |
| 37 |
* @param array<string, mixed>|null $asset_data Optional asset metadata (dependencies and version). |
| 38 |
*/ |
| 39 |
public static function enqueue_script( string $handle, string $file_name, ?array $asset_data = null ): void { |
| 40 |
$script_path = AI_EXPERIMENTS_DIR . 'build/' . $file_name . '.js'; |
| 41 |
$script_url = AI_EXPERIMENTS_PLUGIN_URL . 'build/' . $file_name . '.js'; |
| 42 |
$script_asset_path = substr( $script_path, 0, -3 ) . '.asset.php'; |
| 43 |
|
| 44 |
if ( is_array( $asset_data ) ) { |
| 45 |
if ( ! isset( $asset_data['dependencies'] ) ) { |
| 46 |
$asset_data['dependencies'] = array(); |
| 47 |
} |
| 48 |
if ( ! isset( $asset_data['version'] ) ) { |
| 49 |
$asset_data['version'] = filemtime( $script_path ); |
| 50 |
} |
| 51 |
} elseif ( file_exists( $script_asset_path ) ) { |
| 52 |
$asset_data = require $script_asset_path; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable |
| 53 |
} else { |
| 54 |
$asset_data = array( |
| 55 |
'dependencies' => array(), |
| 56 |
'version' => filemtime( $script_path ), |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
wp_enqueue_script( |
| 61 |
'ai_' . $handle, |
| 62 |
$script_url, |
| 63 |
$asset_data['dependencies'], |
| 64 |
$asset_data['version'], |
| 65 |
array( 'strategy' => 'defer' ) |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Enqueue a style using a style path and its asset metadata. |
| 71 |
* |
| 72 |
* @since 0.1.0 |
| 73 |
* |
| 74 |
* @param string $handle The handle for the style. |
| 75 |
* @param string $file_name The script file name. |
| 76 |
* @param array<string, mixed>|null $asset_data Optional asset metadata (dependencies and version). |
| 77 |
*/ |
| 78 |
public static function enqueue_style( string $handle, string $file_name, ?array $asset_data = null ): void { |
| 79 |
$style_path = AI_EXPERIMENTS_DIR . 'build/' . $file_name . '.css'; |
| 80 |
$style_url = AI_EXPERIMENTS_PLUGIN_URL . 'build/' . $file_name . '.css'; |
| 81 |
$style_asset_path = substr( $style_path, 0, -4 ) . '.asset.php'; |
| 82 |
|
| 83 |
if ( is_array( $asset_data ) ) { |
| 84 |
if ( ! isset( $asset_data['dependencies'] ) ) { |
| 85 |
$asset_data['dependencies'] = array(); |
| 86 |
} |
| 87 |
if ( ! isset( $asset_data['version'] ) ) { |
| 88 |
$asset_data['version'] = filemtime( $style_path ); |
| 89 |
} |
| 90 |
} elseif ( file_exists( $style_asset_path ) ) { |
| 91 |
$asset_data = require $style_asset_path; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable |
| 92 |
} else { |
| 93 |
$asset_data = array( |
| 94 |
'dependencies' => array(), |
| 95 |
'version' => filemtime( $style_path ), |
| 96 |
); |
| 97 |
} |
| 98 |
|
| 99 |
wp_enqueue_style( |
| 100 |
'ai_' . $handle, |
| 101 |
$style_url, |
| 102 |
array(), |
| 103 |
$asset_data['version'] |
| 104 |
); |
| 105 |
wp_style_add_data( 'ai_' . $handle, 'path', $style_path ); |
| 106 |
|
| 107 |
$rtl_style_path = str_replace( '.css', '-rtl.css', $style_path ); |
| 108 |
if ( ! file_exists( $rtl_style_path ) ) { |
| 109 |
return; |
| 110 |
} |
| 111 |
wp_style_add_data( 'ai_' . $handle, 'rtl', 'replace' ); |
| 112 |
if ( ! is_rtl() ) { |
| 113 |
return; |
| 114 |
} |
| 115 |
wp_style_add_data( 'ai_' . $handle, 'path', $rtl_style_path ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Localize data for an enqueued script. |
| 120 |
* |
| 121 |
* This method allows passing PHP data to JavaScript using `wp_localize_script()`. |
| 122 |
* It must be called after the script has been enqueued using `enqueue_script()`. |
| 123 |
* |
| 124 |
* @since 0.1.0 |
| 125 |
* |
| 126 |
* @param string $handle The script handle used in `enqueue_script()` (without prefix). |
| 127 |
* @param string $object_name The name of the JavaScript object to contain the data. |
| 128 |
* @param array<string, mixed> $data The data to localize. |
| 129 |
*/ |
| 130 |
public static function localize_script( string $handle, string $object_name, array $data ): void { |
| 131 |
wp_localize_script( |
| 132 |
'ai_' . $handle, |
| 133 |
'ai' . $object_name, |
| 134 |
$data |
| 135 |
); |
| 136 |
} |
| 137 |
} |
| 138 |
|