From a11ce813bb3c302d00469e1d9fe04fa6a1f9557a Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Fri, 3 May 2024 13:29:33 +0200 Subject: [PATCH 1/2] fix(Assets) avoid doing-it-wrong notices, register after init --- src/Assets/Assets.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Assets/Assets.php b/src/Assets/Assets.php index 8cf02c1..018c9d8 100755 --- a/src/Assets/Assets.php +++ b/src/Assets/Assets.php @@ -681,6 +681,23 @@ protected function do_enqueue( Asset $asset, bool $force_enqueue = false ): void * */ public function register_in_wp( $assets = null ) { + if ( ! ( + did_action( 'init' ) || did_action( 'wp_enqueue_scripts' ) + || did_action( 'admin_enqueue_scripts' ) || did_action( 'login_enqueue_scripts' ) + ) + ) { + // Registering the asset now would trigger a doing_it_wrong notice: queue the assets to be registered later. + + if ( ! is_array( $assets ) ) { + $assets = [ $assets ]; + } + + // Register later, avoid the doing_it_wrong notice. + $this->assets = array_merge( $this->assets, $assets ); + + return; + } + if ( is_null( $assets ) ) { $assets = $this->get(); } From 49a903d2f74fac60b2558be63cc567949e663df7 Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Fri, 3 May 2024 14:43:13 +0200 Subject: [PATCH 2/2] test(acceptance) cover immediate registration --- tests/_data/css/module-one.css | 0 tests/_data/css/module-two.css | 0 tests/_data/immediate-enqueue-template.php | 13 ++++ tests/_data/js/module-one.js | 0 tests/_data/js/module-two.js | 0 tests/acceptance/RegisterBeforeInitCest.php | 73 +++++++++++++++++++++ 6 files changed, 86 insertions(+) create mode 100644 tests/_data/css/module-one.css create mode 100644 tests/_data/css/module-two.css create mode 100644 tests/_data/immediate-enqueue-template.php create mode 100644 tests/_data/js/module-one.js create mode 100644 tests/_data/js/module-two.js create mode 100644 tests/acceptance/RegisterBeforeInitCest.php diff --git a/tests/_data/css/module-one.css b/tests/_data/css/module-one.css new file mode 100644 index 0000000..e69de29 diff --git a/tests/_data/css/module-two.css b/tests/_data/css/module-two.css new file mode 100644 index 0000000..e69de29 diff --git a/tests/_data/immediate-enqueue-template.php b/tests/_data/immediate-enqueue-template.php new file mode 100644 index 0000000..85a6dce --- /dev/null +++ b/tests/_data/immediate-enqueue-template.php @@ -0,0 +1,13 @@ +enqueue_on( 'wp_enqueue_scripts' ) + ->register(); + PHP; + + $I->haveMuPlugin( 'enqueue.php', $code ); + + $I->amOnPage( '/' ); + $I->seeElement( 'script', [ 'src' => 'http://wordpress.test/wp-content/plugins/assets/tests/_data/js/fake.js?ver=1.0.0' ] ); + } + + public function it_should_register_and_enqueue_css_before_init( AcceptanceTester $I ) { + $code = file_get_contents( codecept_data_dir( 'immediate-enqueue-template.php' ) ); + $code .= <<enqueue_on( 'wp_enqueue_scripts' ) + ->register(); + PHP; + + $I->haveMuPlugin( 'enqueue.php', $code ); + + $I->amOnPage( '/' ); + $I->seeElement( 'link', [ 'href' => 'http://wordpress.test/wp-content/plugins/assets/tests/_data/css/fake.css?ver=1.0.0' ] ); + } + + public function is_should_register_and_enqueue_js_with_dependencies_before_init( AcceptanceTester $I ) { + $code = file_get_contents( codecept_data_dir( 'immediate-enqueue-template.php' ) ); + $code .= <<register(); + Asset::add( 'module-two', 'module-two.js' )->register(); + Asset::add( 'fake-js', 'fake.js' ) + ->set_dependencies( 'module-one', 'module-two' ) + ->enqueue_on( 'wp_enqueue_scripts' ) + ->register(); + PHP; + + $I->haveMuPlugin( 'enqueue.php', $code ); + + $I->amOnPage( '/' ); + $I->seeElement( 'script', [ 'src' => 'http://wordpress.test/wp-content/plugins/assets/tests/_data/js/module-one.js?ver=1.0.0' ] ); + $I->seeElement( 'script', [ 'src' => 'http://wordpress.test/wp-content/plugins/assets/tests/_data/js/module-two.js?ver=1.0.0' ] ); + $I->seeElement( 'script', [ 'src' => 'http://wordpress.test/wp-content/plugins/assets/tests/_data/js/fake.js?ver=1.0.0' ] ); + } + + public function it_should_register_and_enqueue_css_with_dependencies_before_init( AcceptanceTester $I ) { + $code = file_get_contents( codecept_data_dir( 'immediate-enqueue-template.php' ) ); + $code .= <<register(); + Asset::add( 'module-two', 'module-two.css' )->register(); + Asset::add( 'fake-css', 'fake.css' ) + ->set_dependencies( 'module-one', 'module-two' ) + ->enqueue_on( 'wp_enqueue_scripts' ) + ->register(); + PHP; + + $I->haveMuPlugin( 'enqueue.php', $code ); + + $I->amOnPage( '/' ); + $I->seeElement( 'link', [ 'href' => 'http://wordpress.test/wp-content/plugins/assets/tests/_data/css/module-one.css?ver=1.0.0' ] ); + $I->seeElement( 'link', [ 'href' => 'http://wordpress.test/wp-content/plugins/assets/tests/_data/css/module-two.css?ver=1.0.0' ] ); + $I->seeElement( 'link', [ 'href' => 'http://wordpress.test/wp-content/plugins/assets/tests/_data/css/fake.css?ver=1.0.0' ] ); + } +}