From d84ce12a030e182adb7f4587ebb3ccd66e9b8cc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Ruz=CC=8Cevic=CC=81?= Date: Fri, 27 Oct 2023 15:21:19 +0200 Subject: [PATCH] removing 'Old' function in blocks --- src/Blocks/AbstractBlocks.php | 59 ---------------------- src/Blocks/BlocksExample.php | 6 +-- tests/Unit/Blocks/BlocksExampleTest.php | 66 ------------------------- 3 files changed, 1 insertion(+), 130 deletions(-) diff --git a/src/Blocks/AbstractBlocks.php b/src/Blocks/AbstractBlocks.php index db1b10e2c..da35b2547 100644 --- a/src/Blocks/AbstractBlocks.php +++ b/src/Blocks/AbstractBlocks.php @@ -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. * @@ -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> $categories Array of categories for block types. - * @param WP_Post $post Post being loaded. - * - * @return array> 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. * diff --git a/src/Blocks/BlocksExample.php b/src/Blocks/BlocksExample.php index 81a358002..35f5bcaa9 100644 --- a/src/Blocks/BlocksExample.php +++ b/src/Blocks/BlocksExample.php @@ -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); diff --git a/tests/Unit/Blocks/BlocksExampleTest.php b/tests/Unit/Blocks/BlocksExampleTest.php index c45e45add..8850c5c73 100644 --- a/tests/Unit/Blocks/BlocksExampleTest.php +++ b/tests/Unit/Blocks/BlocksExampleTest.php @@ -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); @@ -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; @@ -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 = [