diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index ab82dcb..dee33ea 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -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; } diff --git a/src/Assets/Assets.php b/src/Assets/Assets.php index fb0da65..c372561 100755 --- a/src/Assets/Assets.php +++ b/src/Assets/Assets.php @@ -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; diff --git a/tests/acceptance/EnqueueJSCest.php b/tests/acceptance/EnqueueJSCest.php index 1aac8a3..58f5563 100644 --- a/tests/acceptance/EnqueueJSCest.php +++ b/tests/acceptance/EnqueueJSCest.php @@ -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 .= <<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 ); - } } diff --git a/tests/acceptance/LocalizeJSCest.php b/tests/acceptance/LocalizeJSCest.php new file mode 100644 index 0000000..f857f4b --- /dev/null +++ b/tests/acceptance/LocalizeJSCest.php @@ -0,0 +1,160 @@ +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 .= <<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 .= <<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 . <<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 . <<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 ); + } +}