diff --git a/.gitignore b/.gitignore index b69fa788f..14efaa7ae 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,5 @@ latest_dump.tar.gz # Editor settings .vscode/settings.json +.vscode/launch.json .idea/* diff --git a/src/Enqueue/Admin/AbstractEnqueueAdmin.php b/src/Enqueue/Admin/AbstractEnqueueAdmin.php index dde77f4ae..5f744f18a 100644 --- a/src/Enqueue/Admin/AbstractEnqueueAdmin.php +++ b/src/Enqueue/Admin/AbstractEnqueueAdmin.php @@ -30,6 +30,27 @@ abstract class AbstractEnqueueAdmin extends AbstractAssets */ protected ManifestInterface $manifest; + + /** + * Get admin Stylesheet handle. + * + * @return string + */ + public function getAdminStyleHandle(): string + { + return "{$this->getAssetsPrefix()}-styles"; + } + + /** + * Get admin JavaScript handle. + * + * @return string + */ + public function getAdminScriptHandle(): string + { + return "{$this->getAssetsPrefix()}-scripts"; + } + /** * Register the Stylesheets for the admin area. * @@ -38,7 +59,7 @@ abstract class AbstractEnqueueAdmin extends AbstractAssets public function enqueueStyles(): void { if (!$this->getConditionUse()) { - $handle = "{$this->getAssetsPrefix()}-styles"; + $handle = $this->getAdminStyleHandle(); \wp_register_style( $handle, @@ -60,7 +81,7 @@ public function enqueueStyles(): void public function enqueueScripts(): void { if (!$this->getConditionUse()) { - $handle = "{$this->getAssetsPrefix()}-scripts"; + $handle = $this->getAdminScriptHandle(); \wp_register_script( $handle, diff --git a/src/Enqueue/Blocks/AbstractEnqueueBlocks.php b/src/Enqueue/Blocks/AbstractEnqueueBlocks.php index 4673588ff..7708b43ba 100644 --- a/src/Enqueue/Blocks/AbstractEnqueueBlocks.php +++ b/src/Enqueue/Blocks/AbstractEnqueueBlocks.php @@ -33,6 +33,56 @@ abstract class AbstractEnqueueBlocks extends AbstractAssets */ protected ManifestInterface $manifest; + /** + * Get block editor JavaScript handle. + * + * @return string + */ + public function getBlockEditorScriptsHandle(): string + { + return "{$this->getAssetsPrefix()}-block-editor-scripts"; + } + + /** + * Get block editor Stylesheet handle. + * + * @return string + */ + public function getBlockEditorStyleHandle(): string + { + return "{$this->getAssetsPrefix()}-block-editor-style"; + } + + /** + * Get block Stylesheet handle. + * + * @return string + */ + public function getBlockStyleHandle(): string + { + return "{$this->getAssetsPrefix()}-block-style"; + } + + /** + * Get block frontend JavaScript handle. + * + * @return string + */ + public function getBlockFrontentScriptHandle(): string + { + return "{$this->getAssetsPrefix()}-block-frontend-scripts"; + } + + /** + * Get block frontend Stylesheet handle. + * + * @return string + */ + public function getBlockFrontentStyleHandle(): string + { + return "{$this->getAssetsPrefix()}-block-frontend-style"; + } + /** * Enqueue blocks script for editor only. * @@ -40,7 +90,7 @@ abstract class AbstractEnqueueBlocks extends AbstractAssets */ public function enqueueBlockEditorScript(): void { - $handle = "{$this->getAssetsPrefix()}-block-editor-scripts"; + $handle = $this->getBlockEditorScriptsHandle(); \wp_register_script( $handle, @@ -64,17 +114,17 @@ public function enqueueBlockEditorScript(): void */ public function enqueueBlockEditorStyle(): void { - $handler = "{$this->getAssetsPrefix()}-block-editor-style"; + $handle = $this->getBlockEditorStyleHandle(); \wp_register_style( - $handler, + $handle, $this->manifest->getAssetsManifestItem(static::BLOCKS_EDITOR_STYLE_URI), $this->getAdminStyleDependencies(), $this->getAssetsVersion(), $this->getMedia() ); - \wp_enqueue_style($handler); + \wp_enqueue_style($handle); } /** @@ -84,17 +134,17 @@ public function enqueueBlockEditorStyle(): void */ public function enqueueBlockStyle(): void { - $handler = "{$this->getAssetsPrefix()}-block-style"; + $handle = $this->getBlockStyleHandle(); \wp_register_style( - $handler, + $handle, $this->manifest->getAssetsManifestItem(static::BLOCKS_STYLE_URI), $this->getFrontendStyleDependencies(), $this->getAssetsVersion(), $this->getMedia() ); - \wp_enqueue_style($handler); + \wp_enqueue_style($handle); } /** @@ -104,7 +154,7 @@ public function enqueueBlockStyle(): void */ public function enqueueBlockFrontendScript(): void { - $handle = "{$this->getAssetsPrefix()}-block-frontend-scripts"; + $handle = $this->getBlockFrontentScriptHandle(); \wp_register_script( $handle, @@ -118,7 +168,7 @@ public function enqueueBlockFrontendScript(): void foreach ($this->getLocalizations() as $objectName => $dataArray) { - \wp_localize_script($handle, $objectName, $dataArray); + \wp_localize_script($this->getBlockFrontentScriptHandle(), $objectName, $dataArray); } } @@ -129,17 +179,17 @@ public function enqueueBlockFrontendScript(): void */ public function enqueueBlockFrontendStyle(): void { - $handler = "{$this->getAssetsPrefix()}-block-frontend-style"; + $handle = $this->getBlockFrontentStyleHandle(); \wp_register_style( - $handler, + $handle, $this->manifest->getAssetsManifestItem(static::BLOCKS_FRONTEND_STYLE_URI), $this->getFrontendStyleDependencies(), $this->getAssetsVersion(), $this->getMedia() ); - \wp_enqueue_style($handler); + \wp_enqueue_style($handle); } /** diff --git a/src/Enqueue/Theme/AbstractEnqueueTheme.php b/src/Enqueue/Theme/AbstractEnqueueTheme.php index e10463f27..9b30f0b88 100644 --- a/src/Enqueue/Theme/AbstractEnqueueTheme.php +++ b/src/Enqueue/Theme/AbstractEnqueueTheme.php @@ -28,6 +28,26 @@ abstract class AbstractEnqueueTheme extends AbstractAssets */ protected ManifestInterface $manifest; + /** + * Get theme Stylesheet handle. + * + * @return string + */ + public function getThemeStyleHandle(): string + { + return "{$this->getAssetsPrefix()}-theme-styles"; + } + + /** + * Get theme JavaScript handle. + * + * @return string + */ + public function getThemeScriptHandle(): string + { + return "{$this->getAssetsPrefix()}-scripts"; + } + /** * Register the Stylesheets for the front end of the theme. * @@ -35,7 +55,7 @@ abstract class AbstractEnqueueTheme extends AbstractAssets */ public function enqueueStyles(): void { - $handle = "{$this->getAssetsPrefix()}-theme-styles"; + $handle = $this->getThemeStyleHandle(); \wp_register_style( $handle, @@ -55,7 +75,7 @@ public function enqueueStyles(): void */ public function enqueueScripts(): void { - $handle = "{$this->getAssetsPrefix()}-scripts"; + $handle = $this->getThemeScriptHandle(); \wp_register_script( $handle, diff --git a/tests/Datasets/Arguments.php b/tests/Datasets/Arguments.php index cf29fec22..51bbd3ee4 100644 --- a/tests/Datasets/Arguments.php +++ b/tests/Datasets/Arguments.php @@ -21,7 +21,7 @@ dataset('classesArray', [ [['medium', 'large']], [['small']], - [['key' => 'bold--all']] + [['key' => 'bold--all']], ]); // Rest/Fields arguments diff --git a/tests/Enqueue/Admin/EnqueueAdminExampleTest.php b/tests/Enqueue/Admin/EnqueueAdminExampleTest.php index b109ec62c..74accc1d6 100644 --- a/tests/Enqueue/Admin/EnqueueAdminExampleTest.php +++ b/tests/Enqueue/Admin/EnqueueAdminExampleTest.php @@ -153,3 +153,44 @@ public function getLocalizations(): array $this->assertIsArray($localizationExample->getLocalizations()); $this->assertEmpty($localizationExample->getLocalizations()); }); + +test('getAdminStyleHandle will return string', function () { + $adminHandle = $this->adminEnqueue->getAdminStyleHandle(); + + expect($adminHandle) + ->toBeString() + ->not->toBeArray(); +}); + +test('getAdminScriptHandle will return string', function () { + $adminHandle = $this->adminEnqueue->getAdminScriptHandle(); + + expect($adminHandle) + ->toBeString() + ->not->toBeArray(); +}); + +test('getConditionUse will be false if outside of admin', function () { + Functions\when('is_admin')->justReturn(false); + + $conditionUse = $this->adminEnqueue->getConditionUse(); + + expect($conditionUse) + ->toBeFalse() + ->not->toBeNull(); +}); + +test('getConditionUse will be true if inside block editor', function () { + Functions\when('get_current_screen')->alias(function () { + return new class + { + public $is_block_editor = true; // We are in the block editor. + }; + }); + + $conditionUse = $this->adminEnqueue->getConditionUse(); + + expect($conditionUse) + ->toBeTrue() + ->not->toBeNull(); +}); diff --git a/tests/Enqueue/Blocks/EnqueueBlockExampleTest.php b/tests/Enqueue/Blocks/EnqueueBlockExampleTest.php index 9f5b4b976..12465fc2e 100644 --- a/tests/Enqueue/Blocks/EnqueueBlockExampleTest.php +++ b/tests/Enqueue/Blocks/EnqueueBlockExampleTest.php @@ -166,3 +166,43 @@ protected function getLocalizations(): array $this->assertSame(\getenv('REGISTER_STYLE'), "{$this->projectName}-block-frontend-style", 'Method enqueueStyles() failed to register style'); $this->assertSame(\getenv('ENQUEUE_STYLE'), "{$this->projectName}-block-frontend-style", 'Method enqueueScripts() failed to enqueue style'); }); + +test('getBlockEditorScriptsHandle will return string', function () { + $adminHandle = $this->blockEnqueue->getBlockEditorScriptsHandle(); + + expect($adminHandle) + ->toBeString() + ->not->toBeArray(); +}); + +test('getBlockEditorStyleHandle will return string', function () { + $adminHandle = $this->blockEnqueue->getBlockEditorStyleHandle(); + + expect($adminHandle) + ->toBeString() + ->not->toBeArray(); +}); + +test('getBlockFrontentScriptHandle will return string', function () { + $adminHandle = $this->blockEnqueue->getBlockFrontentScriptHandle(); + + expect($adminHandle) + ->toBeString() + ->not->toBeArray(); +}); + +test('getBlockFrontentStyleHandle will return string', function () { + $adminHandle = $this->blockEnqueue->getBlockFrontentStyleHandle(); + + expect($adminHandle) + ->toBeString() + ->not->toBeArray(); +}); + +test('getBlockStyleHandle will return string', function () { + $adminHandle = $this->blockEnqueue->getBlockStyleHandle(); + + expect($adminHandle) + ->toBeString() + ->not->toBeArray(); +}); diff --git a/tests/Enqueue/Theme/EnqueueThemeExampleTest.php b/tests/Enqueue/Theme/EnqueueThemeExampleTest.php index 145218af3..0d29cbd8a 100644 --- a/tests/Enqueue/Theme/EnqueueThemeExampleTest.php +++ b/tests/Enqueue/Theme/EnqueueThemeExampleTest.php @@ -109,3 +109,19 @@ protected function getLocalizations(): array $this->assertSame(\getenv('ENQUEUE_SCRIPT'), 'MyProject-scripts', 'Method enqueueScripts() failed to enqueue style'); $this->assertSame(\getenv('SIDEAFFECT'), 'localize', 'Method wp_localize_script() failed'); }); + +test('getThemeScriptHandle will return string', function () { + $adminHandle = $this->themeEnqueue->getThemeScriptHandle(); + + expect($adminHandle) + ->toBeString() + ->not->toBeArray(); +}); + +test('getThemeStyleHandle will return string', function () { + $adminHandle = $this->themeEnqueue->getThemeStyleHandle(); + + expect($adminHandle) + ->toBeString() + ->not->toBeArray(); +}); diff --git a/tests/Helpers/SelectorsTraitTest.php b/tests/Helpers/SelectorsTraitTest.php index 8d97f10c3..d4aa6d0df 100644 --- a/tests/Helpers/SelectorsTraitTest.php +++ b/tests/Helpers/SelectorsTraitTest.php @@ -110,4 +110,3 @@ Components::classnames($argument); })->throws(\TypeError::class) ->with('errorStringArguments'); -