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

False by default for asset file for styles #30

Merged
merged 3 commits into from
Jan 10, 2025
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
2 changes: 1 addition & 1 deletion assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Plugin Name: Assets
* Description: Asset library with a plugin bootstrap file for automated testing.
* Version: 1.4.2
* Version: 1.4.4
* Author: StellarWP
* Author URI: https://stellarwp.com
*/
8 changes: 6 additions & 2 deletions src/Assets/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ class Asset {
/**
* Whether or not to attempt to load an .asset.php file.
*
* By default is true for scripts and false for styles.
*
* @since 1.3.1
*
* @var bool
Expand All @@ -287,7 +289,7 @@ class Asset {
* This flag will be raised when the asset is added to a group path
* and lowered when it's removed from it.
*
* @since TBD
* @since 1.4.3
*
* @var bool
*/
Expand Down Expand Up @@ -1173,12 +1175,14 @@ public function in_header() {
* Set the asset type.
*
* @since 1.0.0
* @since 1.4.4 - For css files, we dont want to use asset file for dependencies by default.
*/
protected function infer_type() {
if ( substr( $this->file, -3, 3 ) === '.js' ) {
$this->type = 'js';
} elseif ( substr( $this->file, -4, 4 ) === '.css' ) {
$this->type = 'css';
$this->use_asset_file( false );
}
}

Expand Down Expand Up @@ -1530,7 +1534,7 @@ public function set_action( string $action ) {
/**
* Set the asset file path for the asset.
*
* @since TBD
* @since 1.3.0
*
* @param string $path The partial path to the asset.
*
Expand Down
20 changes: 20 additions & 0 deletions tests/acceptance/EnqueueCSSCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,31 @@ public function it_should_set_media( AcceptanceTester $I ) {
$I->dontSeeElement( 'link', [ 'href' => 'http://wordpress.test/wp-content/plugins/assets/tests/_data/css/fake.css?ver=1.0.0', 'media' => 'screen' ] );
}

public function it_should_enqueue_css_on_default_version_when_using_register_with_js( AcceptanceTester $I ) {
$code = file_get_contents( codecept_data_dir( 'enqueue-template.php' ) );
$code .= <<<PHP
add_action( 'wp_enqueue_scripts', function() {
Asset::add( 'something-css' . uniqid(), 'something.css' )
->set_path( 'tests/_data/build' )
->enqueue_on( 'wp_enqueue_scripts' )
->register_with_js();
}, 100 );
PHP;

$I->haveMuPlugin( 'enqueue.php', $code );


$I->amOnPage( '/' );
$I->seeElement( 'link', [ 'href' => 'http://wordpress.test/wp-content/plugins/assets/tests/_data/build/something.css?ver=1.0.0' ] );
$I->seeElement( 'script', [ 'src' => 'http://wordpress.test/wp-content/plugins/assets/tests/_data/build/something.js?ver=1.0.0' ] );
}

public function it_should_enqueue_js_when_using_register_with_js( AcceptanceTester $I ) {
$code = file_get_contents( codecept_data_dir( 'enqueue-template.php' ) );
$code .= <<<PHP
add_action( 'wp_enqueue_scripts', function() {
Asset::add( 'something-css' . uniqid(), 'something.css' )
->use_asset_file( true )
->set_path( 'tests/_data/build' )
->enqueue_on( 'wp_enqueue_scripts' )
->register_with_js();
Expand Down
33 changes: 25 additions & 8 deletions tests/wpunit/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -637,28 +637,36 @@ public function should_allow_setting_dependencies_with_a_callable(): void {
/**
* @test
*/
public function it_should_use_include_css_asset_file_dependencies_when_no_dependencies_are_set(): void {
public function it_should_not_use_include_css_asset_file_dependencies_when_no_dependencies_are_set(): void {
$asset = Asset::add( 'my-style' . uniqid(), 'fake4.css' );

$this->assertContains( 'some-dependency', $asset->get_dependencies() );
$this->assertEmpty( $asset->get_dependencies() );
}

/**
* @test
*/
public function it_should_use_include_css_asset_file_dependencies_when_no_dependencies_are_set(): void {
$asset = Asset::add( 'my-style' . uniqid(), 'fake4.css' )->use_asset_file( true );

$this->assertEquals( ['some-dependency'], $asset->get_dependencies() );
}

/**
* @test
*/
public function it_should_use_include_css_asset_file_dependencies_when_dependencies_are_set(): void {
$asset = Asset::add( 'my-style' . uniqid(), 'fake4.css' );
$asset = Asset::add( 'my-style' . uniqid(), 'fake4.css' )->use_asset_file( true );
$asset->set_dependencies( 'fake1' );

$this->assertContains( 'fake1', $asset->get_dependencies() );
$this->assertContains( 'some-dependency', $asset->get_dependencies() );
$this->assertEquals( [ 'some-dependency', 'fake1' ], $asset->get_dependencies() );
}

/**
* @test
*/
public function it_should_use_include_css_asset_file_dependencies_when_dependencies_are_set_as_callable(): void {
$asset = Asset::add( 'my-style' . uniqid(), 'fake4.css' );
$asset = Asset::add( 'my-style' . uniqid(), 'fake4.css' )->use_asset_file( true );
$asset->set_dependencies( static function() {
return [ 'fake1' ];
} );
Expand All @@ -670,17 +678,26 @@ public function it_should_use_include_css_asset_file_dependencies_when_dependenc
/**
* @test
*/
public function it_should_use_css_asset_file_version_when_no_version_is_set(): void {
public function it_should_not_use_css_asset_file_version_when_no_version_is_set(): void {
$asset = Asset::add( 'my-style' . uniqid(), 'fake4.css' );

$this->assertEquals( '1.0.0', $asset->get_version() );
}

/**
* @test
*/
public function it_should_use_css_asset_file_version_when_no_version_is_set(): void {
$asset = Asset::add( 'my-style' . uniqid(), 'fake4.css' )->use_asset_file( true );

$this->assertEquals( '12345', $asset->get_version() );
}

/**
* @test
*/
public function it_should_use_css_asset_file_version_when_version_is_set(): void {
$asset = Asset::add( 'my-style' . uniqid(), 'fake4.css', '1.0' );
$asset = Asset::add( 'my-style' . uniqid(), 'fake4.css', '1.0' )->use_asset_file( true );

$this->assertEquals( '12345', $asset->get_version() );
}
Expand Down
Loading