Skip to content

Commit

Permalink
removing 'Old' function in blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
iruzevic committed Oct 27, 2023
1 parent 7776018 commit d84ce12
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 130 deletions.
59 changes: 0 additions & 59 deletions src/Blocks/AbstractBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,39 +153,6 @@ public function getAllBlocksList($allowedBlockTypes, WP_Block_Editor_Context $bl
return $this->getAllAllowedBlocksList([], $blockEditorContext);
}

/**
* Get all blocks with full block name - legacy.
*
* Used to limit what blocks are going to be used in your project using allowed_block_types filter.
*
* @hook allowed_block_types This is a WP 5 - WP 5.7 compatible hook callback. Will not work with WP 5.8!
*
* @param bool|string[] $allowedBlockTypes Array of block type slugs, or boolean to enable/disable all.
* @param WP_Post $post The post resource data.
*
* @return bool|string[] Boolean if you want to disallow or allow all blocks, or a list of allowed blocks.
*/
public function getAllBlocksListOld($allowedBlockTypes, WP_Post $post)
{
if (\gettype($allowedBlockTypes) === 'boolean') {
return $allowedBlockTypes;
}

$allowedBlockTypes = \array_map(
function ($block) {
return $block['blockFullName'];
},
Components::getBlocks()
);

// Allow reusable block.
$allowedBlockTypes[] = 'eightshift-forms/forms';
$allowedBlockTypes[] = 'core/block';
$allowedBlockTypes[] = 'core/template';

return $allowedBlockTypes;
}

/**
* Method used to register all custom blocks with data fetched from blocks manifest.json.
*
Expand Down Expand Up @@ -274,32 +241,6 @@ public function getCustomCategory(array $categories, WP_Block_Editor_Context $bl
);
}

/**
* Create custom category to assign all custom blocks - legacy.
*
* This category will be shown on all blocks list in "Add Block" button.
*
* @hook block_categories This is a WP 5 - WP 5.7 compatible hook callback. Will not work with WP 5.8!
*
* @param array<int, array<string, string|null>> $categories Array of categories for block types.
* @param WP_Post $post Post being loaded.
*
* @return array<int, array<string, string|null>> Array of categories for block types.
*/
public function getCustomCategoryOld(array $categories, WP_Post $post): array
{
return \array_merge(
$categories,
[
[
'slug' => 'eightshift',
'title' => \esc_html__('Eightshift', 'eightshift-libs'),
'icon' => 'admin-settings',
],
]
);
}

/**
* Locate and return template part with passed attributes for wrapper.
*
Expand Down
6 changes: 1 addition & 5 deletions src/Blocks/BlocksExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ public function register(): void
\remove_filter('the_content', 'wpautop');

// Create new custom category for custom blocks.
if (\is_wp_version_compatible('5.8')) {
\add_filter('block_categories_all', [$this, 'getCustomCategory'], 10, 2);
} else {
\add_filter('block_categories', [$this, 'getCustomCategoryOld'], 10, 2);
}
\add_filter('block_categories_all', [$this, 'getCustomCategory'], 10, 2);

// Register custom theme support options.
\add_action('after_setup_theme', [$this, 'addThemeSupport'], 25);
Expand Down
66 changes: 0 additions & 66 deletions tests/Unit/Blocks/BlocksExampleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,48 +42,6 @@
expect(\getenv('ALIGN_WIDE'))->toBe('true');
});

test('Asserts that getAllBlocksListOld first argument is boolean and return the provided attribute as return value for older WP versions.', function () {

Functions\when('is_wp_version_compatible')->justReturn(false);

$post = mock('WP_Post');

$blocks = $this->mock->getAllBlocksListOld(true, $post);

expect($blocks)->toBeTrue();

$blocks = $this->mock->getAllBlocksListOld(false, $post);

expect($blocks)->toBeFalse();
});

test('Asserts that getAllBlocksListOld will return only projects blocks for older WP versions.', function () {

Functions\when('is_wp_version_compatible')->justReturn(false);

$post = mock(\WP_Post::class);

buildTestBlocks();

$list = $this->mock->getAllBlocksListOld([], $post);

expect($list)
->toBeArray()
->not->toContain('core/paragraph')
->toContain('eightshift-boilerplate/button', 'core/block', 'core/template');
});


test('Asserts that getAllBlocksList with all types of arguments throw error for older WP versions.', function ($argument) {

Functions\when('is_wp_version_compatible')->justReturn(false);

$post = mock('WP_Post');

$this->mock->getAllBlocksList($argument, $post);
})->throws(\TypeError::class)
->with('getAllAllowedBlocksListAllTypesArguments');

test('Asserts that getAllBlocksList is not influenced by the first parameter', function ($argument) {

$blockContext = mock(WP_Block_Editor_Context::class);
Expand Down Expand Up @@ -146,16 +104,6 @@
->toContain('test', 'eightshift-boilerplate/button', 'core/block', 'core/template');
});

test('Asserts that getAllAllowedBlocksList with all types of arguments throw error for older WP versions.', function ($argument) {

Functions\when('is_wp_version_compatible')->justReturn(false);

$post = mock('WP_Post');

$this->mock->getAllAllowedBlocksList($argument, $post);
})->throws(\TypeError::class)
->with('getAllAllowedBlocksListAllTypesArguments');

test('Asserts that getAllAllowedBlocksList will return projects blocks and passed blocks for WP 5.8.', function () {
$blockContext = mock(WP_Block_Editor_Context::class);
$blockContext->post = null;
Expand Down Expand Up @@ -293,20 +241,6 @@
expect(\getenv('BLOCK_TYPE'))->toBe('true');
});

test('getCustomCategoryOld method will return an array.', function () {
$post = mock('WP_Post');

$categoryList = $this->mock->getCustomCategoryOld([], $post);

expect($categoryList)->toBeArray();

expect($categoryList[0])
->toBeArray()
->toHaveKey('slug')
->toHaveKey('title')
->toHaveKey('icon');
});

test('filterBlocksContent method will return an array.', function () {

$parsedBlock = [
Expand Down

0 comments on commit d84ce12

Please sign in to comment.