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

Exctract handles to public methods #306

Merged
merged 6 commits into from
Jun 30, 2022
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ latest_dump.tar.gz

# Editor settings
.vscode/settings.json
.vscode/launch.json
.idea/*
25 changes: 23 additions & 2 deletions src/Enqueue/Admin/AbstractEnqueueAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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,
Expand All @@ -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,
Expand Down
74 changes: 62 additions & 12 deletions src/Enqueue/Blocks/AbstractEnqueueBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,64 @@ 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.
*
* @return void
*/
public function enqueueBlockEditorScript(): void
{
$handle = "{$this->getAssetsPrefix()}-block-editor-scripts";
$handle = $this->getBlockEditorScriptsHandle();

\wp_register_script(
$handle,
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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,
Expand All @@ -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);
}
}

Expand All @@ -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);
}

/**
Expand Down
24 changes: 22 additions & 2 deletions src/Enqueue/Theme/AbstractEnqueueTheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,34 @@ 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.
*
* @return void
*/
public function enqueueStyles(): void
{
$handle = "{$this->getAssetsPrefix()}-theme-styles";
$handle = $this->getThemeStyleHandle();

\wp_register_style(
$handle,
Expand All @@ -55,7 +75,7 @@ public function enqueueStyles(): void
*/
public function enqueueScripts(): void
{
$handle = "{$this->getAssetsPrefix()}-scripts";
$handle = $this->getThemeScriptHandle();

\wp_register_script(
$handle,
Expand Down
2 changes: 1 addition & 1 deletion tests/Datasets/Arguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
dataset('classesArray', [
[['medium', 'large']],
[['small']],
[['key' => 'bold--all']]
[['key' => 'bold--all']],
]);

// Rest/Fields arguments
Expand Down
41 changes: 41 additions & 0 deletions tests/Enqueue/Admin/EnqueueAdminExampleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
40 changes: 40 additions & 0 deletions tests/Enqueue/Blocks/EnqueueBlockExampleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
16 changes: 16 additions & 0 deletions tests/Enqueue/Theme/EnqueueThemeExampleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
1 change: 0 additions & 1 deletion tests/Helpers/SelectorsTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,3 @@
Components::classnames($argument);
})->throws(\TypeError::class)
->with('errorStringArguments');