Skip to content
Draft
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
31 changes: 28 additions & 3 deletions src/Assets/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,16 +403,41 @@ public function call_after_enqueue( $callable ) {

/**
* Adds a wp_localize_script object to the asset.
* If the object already exists, updates it with the new data.
*
* @since 1.0.0
* @since TBD Add a conditional and support for updating existing objects.
*
* @param string $object_name JS object name.
* @param array $data Data to be assigned to the JS object.
* @param bool $force_overwrite Whether to force the overwrite of the object.
*
* @return static
*/
public function add_localize_script( string $object_name, array $data, $force_overwrite = false ) {
if ( $force_overwrite || ! isset( $this->wp_localize_script_objects[ $object_name ] ) ) {
$this->wp_localize_script_objects[ $object_name ] = $data;
return $this;
}

return $this->update_localize_script( $object_name, $data );
}

/**
* Updates a wp_localize_script object with new data.
*
* @since TBD
*
* @param string $object_name JS object name.
* @param array $data Data assigned to the JS object.
* @param array $data Data to be assigned to the JS object.
*
* @return static
*/
public function add_localize_script( string $object_name, array $data ) {
$this->wp_localize_script_objects[ $object_name ] = $data;
public function update_localize_script( string $object_name, array $data ) {
$this->wp_localize_script_objects[ $object_name ] = array_merge_recursive(
$this->wp_localize_script_objects[ $object_name ],
$data
);
return $this;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Assets/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public function filter_add_localization_data( $tag, $handle ) {
$wp_scripts->print_extra_script( $asset->get_slug(), true );
$localization_html = ob_get_clean();

// After printing it remove data;|
// After printing it remove data;
$wp_scripts->add_data( $asset->get_slug(), 'data', '' );

return $localization_html . $tag;
Expand Down
30 changes: 0 additions & 30 deletions tests/acceptance/EnqueueJSCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,34 +216,4 @@ public function it_should_be_a_module( AcceptanceTester $I ) {
$I->amOnPage( '/' );
$I->seeElement( '#fake-js-js[type=module]' );
}

public function it_should_localize( AcceptanceTester $I ) {
$code = file_get_contents( codecept_data_dir( 'enqueue-template.php' ) );
$code .= <<<PHP
add_action( 'wp_enqueue_scripts', function() {
Asset::add( 'fake-js', 'fake.js' )
->enqueue_on( 'wp_enqueue_scripts' )
->add_localize_script(
'animal',
[
'cow' => 'true',
]
)
->add_localize_script(
'color',
[ 'blue' ]
)
->register();
}, 100 );
PHP;

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


$I->amOnPage( '/' );
$I->seeElement( '#fake-js-js-extra' );
$contents = $I->grabTextFrom( '#fake-js-js-extra' );
Assert::assertContains( 'var animal', $contents );
Assert::assertContains( 'var color', $contents );
}
}
160 changes: 160 additions & 0 deletions tests/acceptance/LocalizeJSCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php
namespace StellarWP\Assets\Tests;

use AcceptanceTester;
use PHPUnit\Framework\Assert;

class LocalizeJSCest {

public function it_should_localize( AcceptanceTester $I ) {
$code = file_get_contents( codecept_data_dir( 'enqueue-template.php' ) );
$code .= <<<PHP
add_action( 'wp_enqueue_scripts', function() {
Asset::add( 'fake-js', 'fake.js' )
->enqueue_on( 'wp_enqueue_scripts' )
->add_localize_script(
'animal',
[
'cow' => 'true',
]
)
->add_localize_script(
'color',
[ 'blue' ]
)
->register();
}, 100 );
PHP;

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


$I->amOnPage( '/' );
$I->seeElement( '#fake-js-js-extra' );
$contents = $I->grabTextFrom( '#fake-js-js-extra' );
Assert::assertContains( 'var animal', $contents );
Assert::assertContains( 'var color', $contents );
}

public function immediate_localize_should_append( AcceptanceTester $I ) {
$code = file_get_contents( codecept_data_dir( 'enqueue-template.php' ) );
$code .= <<<PHP
add_action( 'wp_enqueue_scripts', function() {
Asset::add( 'fake-js', 'fake.js' )
->enqueue_on( 'wp_enqueue_scripts' )
->add_localize_script(
'animal',
[
'cow' => 'black',
]
)
->add_localize_script(
'color',
[ 'blue' ]
)
->add_localize_script(
'animal',
[
'cow' => 'brown',
]
)
->register();
}, 100 );
PHP;

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


$I->amOnPage( '/' );
$I->seeElement( '#fake-js-js-extra' );
$contents = $I->grabTextFrom( '#fake-js-js-extra' );
Assert::assertContains( 'var animal', $contents );
Assert::assertContains( 'var color', $contents );
Assert::assertContains( 'brown', $contents );
Assert::assertContains( 'black', $contents );
}

public function it_should_overwrite_localize_with_force( AcceptanceTester $I ) {
$code = file_get_contents( codecept_data_dir( 'enqueue-template.php' ) );
$code .= <<<PHP
add_action( 'wp_enqueue_scripts', function() {
Asset::add( 'fake-js', 'fake.js' )
->enqueue_on( 'wp_enqueue_scripts' )
->add_localize_script(
'animal',
[
'cow' => 'black',
]
)
->add_localize_script(
'color',
[ 'blue' ]
)
->add_localize_script(
'animal',
[
'cow' => 'brown',
],
true
)
->register();
}, 100 );
PHP;

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


$I->amOnPage( '/' );
$I->seeElement( '#fake-js-js-extra' );
$contents = $I->grabTextFrom( '#fake-js-js-extra' );
Assert::assertContains( 'var animal', $contents );
Assert::assertContains( 'var color', $contents );
Assert::assertContains( 'brown', $contents );
Assert::assertNotContains( 'black', $contents );
}

public function it_should_append_to_localize_with_same_handle( AcceptanceTester $I ) {
$code_base = file_get_contents( codecept_data_dir( 'enqueue-template.php' ) );
$code = $code_base . <<<PHP
add_action( 'wp_enqueue_scripts', function() {
Asset::add( 'fake-js', 'fake.js' )
->enqueue_on( 'wp_enqueue_scripts' )
->add_localize_script(
'animal',
[
'cow' => 'true',
]
)
->add_localize_script(
'color',
[ 'blue' ]
)
->register();
}, 100 );
PHP;

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

$code_2 = $code_base . <<<PHP
add_action( 'wp_enqueue_scripts', function() {
Assets::get( 'fake-js' )
->add_localize_script(
'animal',
[
'sheep' => 'true',
]
);
}, 110 );
PHP;

$I->haveMuPlugin( 'enqueue_2.php', $code_2 );

$I->amOnPage( '/' );
$I->seeElement( '#fake-js-js-extra' );
$contents = $I->grabTextFrom( '#fake-js-js-extra' );
Assert::assertContains( 'var animal', $contents );
Assert::assertContains( 'var color', $contents );
Assert::assertContains( 'cow', $contents );
Assert::assertContains( 'sheep', $contents );
}
}