From 81b60a31f9a5660cb2660af8f83aff95f78567fb Mon Sep 17 00:00:00 2001 From: "K. Adam White" Date: Fri, 8 Dec 2023 16:37:42 -0500 Subject: [PATCH] 57: Do not fatal if empty/null manifest is passed to register/enqueue functions --- inc/manifest.php | 2 +- inc/namespace.php | 21 +++++++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/inc/manifest.php b/inc/manifest.php index 71708d7..a382415 100644 --- a/inc/manifest.php +++ b/inc/manifest.php @@ -43,7 +43,7 @@ function load_asset_manifest( $path ) { return $manifests[ $path ]; } - if ( ! is_readable( $path ) ) { + if ( empty( $path ) || ! is_readable( $path ) ) { return null; } $contents = file_get_contents( $path ); diff --git a/inc/namespace.php b/inc/namespace.php index 61f786f..c26d333 100644 --- a/inc/namespace.php +++ b/inc/namespace.php @@ -59,15 +59,20 @@ function _register_or_update_script( string $handle, string $asset_uri, array $d /** * Attempt to register a particular script bundle from a manifest. * - * @param string $manifest_path File system path for an asset manifest JSON file. - * @param string $target_asset Asset to retrieve within the specified manifest. - * @param array $options { + * @param ?string $manifest_path File system path for an asset manifest JSON file. + * @param string $target_asset Asset to retrieve within the specified manifest. + * @param array $options { * @type string $handle Handle to use when enqueuing the asset. Optional. * @type array $dependencies Script or Style dependencies. Optional. * } * @return array Array detailing which script and/or style handles got registered. */ -function register_asset( string $manifest_path, string $target_asset, array $options = [] ) : array { +function register_asset( ?string $manifest_path, string $target_asset, array $options = [] ) : array { + if ( empty( $manifest_path ) ) { + trigger_error( "No manifest specified when loading $target_asset", E_USER_NOTICE ); + return []; + } + $defaults = [ 'dependencies' => [], 'in-footer' => true, @@ -160,14 +165,14 @@ function register_asset( string $manifest_path, string $target_asset, array $opt /** * Register and immediately enqueue a particular asset within a manifest. * - * @param string $manifest_path File system path for an asset manifest JSON file. - * @param string $target_asset Asset to retrieve within the specified manifest. - * @param array $options { + * @param ?string $manifest_path File system path for an asset manifest JSON file. + * @param string $target_asset Asset to retrieve within the specified manifest. + * @param array $options { * @type string $handle Handle to use when enqueuing the asset. Optional. * @type array $dependencies Script or Style dependencies. Optional. * } */ -function enqueue_asset( string $manifest_path, string $target_asset, array $options = [] ) : void { +function enqueue_asset( ?string $manifest_path, string $target_asset, array $options = [] ) : void { $registered_handles = register_asset( $manifest_path, $target_asset, $options ); if ( isset( $registered_handles['script'] ) ) {