Skip to content

Commit

Permalink
Merge pull request #59 from humanmade/57-no-fatal-on-missing-manifest
Browse files Browse the repository at this point in the history
#57: Do not fatal if empty/null manifest is passed to registration functions
  • Loading branch information
kadamwhite authored Dec 8, 2023
2 parents 1cc2bdc + 81b60a3 commit ea9b36a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion inc/manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
21 changes: 13 additions & 8 deletions inc/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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'] ) ) {
Expand Down

0 comments on commit ea9b36a

Please sign in to comment.