Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid doing-it-wrong notices with direct calls #8

Merged
merged 2 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Assets/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Empty file added tests/_data/css/module-one.css
Empty file.
Empty file added tests/_data/css/module-two.css
Empty file.
13 changes: 13 additions & 0 deletions tests/_data/immediate-enqueue-template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

require_once WP_CONTENT_DIR . '/plugins/assets/vendor/autoload.php';

use StellarWP\Assets\Asset;
use StellarWP\Assets\Assets;
use StellarWP\Assets\Config;

Config::set_path( WP_CONTENT_DIR . '/plugins/assets/' );
Config::set_relative_asset_path( 'tests/_data/' );
Config::set_hook_prefix( 'bork' );
Config::set_version( '1.0.0' );
Assets::init();
Empty file added tests/_data/js/module-one.js
Empty file.
Empty file added tests/_data/js/module-two.js
Empty file.
73 changes: 73 additions & 0 deletions tests/acceptance/RegisterBeforeInitCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace StellarWP\Assets\Tests;

use AcceptanceTester;

class RegisterBeforeInitCest {
public function it_should_register_and_enqueue_js_before_init( AcceptanceTester $I ) {
$code = file_get_contents( codecept_data_dir( 'immediate-enqueue-template.php' ) );
$code .= <<<PHP
Asset::add( 'fake-js', 'fake.js' )
->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 .= <<<PHP
Asset::add( 'fake-css', 'fake.css' )
->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 .= <<<PHP
Asset::add( 'module-one', 'module-one.js' )->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 .= <<<PHP
Asset::add( 'module-one', 'module-one.css' )->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' ] );
}
}
Loading