Skip to content

Commit

Permalink
Merge pull request #2576 from woocommerce/fix/1659-asset-enqueued-bef…
Browse files Browse the repository at this point in the history
…ore-registered

Log Exception if an asset is enqueued before being registered
  • Loading branch information
martynmjones authored Sep 5, 2024
2 parents 7210b14 + 0baf754 commit bf5c219
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Assets/BaseAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,11 @@ protected function get_dequeue_action(): string {
*/
protected function defer_action( string $action, callable $callback, int $priority = 10 ): void {
if ( did_action( $action ) ) {
$callback();
try {
$callback();
} catch ( InvalidAsset $exception ) {
do_action( 'woocommerce_gla_exception', $exception, __METHOD__ );
}

return;
}
Expand Down
15 changes: 15 additions & 0 deletions tests/Unit/Assets/ScriptAssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,19 @@ public function test_can_enqueue() {
$asset = new ScriptAsset( __FUNCTION__, self::URI, [], '' );
$this->assertTrue( $asset->can_enqueue() );
}

/**
* Confirm an exception is logged using the `woocommerce_gla_exception`
* action if an asset is enqueued before it is registered.
*
* @return void
*/
public function test_exception_logged_if_asset_enqueued_before_registration() {
do_action( 'wp_enqueue_scripts' );

$asset = new ScriptAsset( __FUNCTION__, self::URI, [], '', '__return_true' );
$asset->enqueue();

$this->assertEquals( 1, did_action( 'woocommerce_gla_exception' ) );
}
}

0 comments on commit bf5c219

Please sign in to comment.