From ba7503322fc434e3b89798e5f905d348076091e0 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 23 Aug 2022 14:50:56 +0000 Subject: [PATCH] Editor: Ensure `get_block_templates()` returns unique templates or template parts. The function was using the `array_column()` PHP function on an array of objects, which works as expected on PHP 7 or later, but not on PHP 5.6. This resulted in customized templates being listed multiple times on the Templates and Template Parts screens, and being repeatedly added between lists when switching between the screens. The issue is now resolved by replacing `array_column()` with the `wp_list_pluck()` WordPress core function, which provides consistent behavior beetween PHP versions. Reference: [https://github.com/php/php-src/blob/PHP-7.0.0/UPGRADING#L626 PHP 7.0 Upgrade Notes: array_column()]. Props uofaberdeendarren, antonvlasenko, ironprogrammer, jonmackintosh, costdev, hellofromTonya, swissspidy, rudlinkon. Fixes #56271. git-svn-id: https://develop.svn.wordpress.org/trunk@53927 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/block-template-utils.php | 2 +- .../tests/blocks/getBlockTemplates.php | 116 ++++++++++++++++++ 2 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/tests/blocks/getBlockTemplates.php diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 951bd523c2..08933d4f81 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -696,7 +696,7 @@ function get_block_templates( $query = array(), $template_type = 'wp_template' ) $is_not_custom = false === array_search( wp_get_theme()->get_stylesheet() . '//' . $template_file['slug'], - array_column( $query_result, 'id' ), + wp_list_pluck( $query_result, 'id' ), true ); $fits_slug_query = diff --git a/tests/phpunit/tests/blocks/getBlockTemplates.php b/tests/phpunit/tests/blocks/getBlockTemplates.php new file mode 100644 index 0000000000..89754f12ef --- /dev/null +++ b/tests/phpunit/tests/blocks/getBlockTemplates.php @@ -0,0 +1,116 @@ +post->create_and_get( + array( + 'post_type' => 'wp_template', + 'post_name' => 'index', + 'tax_input' => array( + 'wp_theme' => array( + static::TEST_THEME, + ), + ), + ) + ); + + wp_set_post_terms( static::$template->ID, static::TEST_THEME, 'wp_theme' ); + + /* + * This template part has to have the same ID ("block-theme/small-header") as the template part + * that is shipped with the "block-theme" theme. This is needed for testing purposes. + */ + self::$template_part = self::factory()->post->create_and_get( + array( + 'post_type' => 'wp_template_part', + 'post_name' => 'small-header', + 'tax_input' => array( + 'wp_theme' => array( + static::TEST_THEME, + ), + 'wp_template_part_area' => array( + WP_TEMPLATE_PART_AREA_HEADER, + ), + ), + ) + ); + + wp_set_post_terms( self::$template_part->ID, WP_TEMPLATE_PART_AREA_HEADER, 'wp_template_part_area' ); + wp_set_post_terms( self::$template_part->ID, static::TEST_THEME, 'wp_theme' ); + } + + public static function tear_down_after_class() { + wp_delete_post( static::$template->ID ); + wp_delete_post( static::$template_part->ID ); + + parent::tear_down_after_class(); + } + + public function set_up() { + parent::set_up(); + switch_theme( static::TEST_THEME ); + } + + /** + * @ticket 56271 + * + * @dataProvider data_get_block_templates_returns_unique_entities + * + * @param string $template_type The template type. + * @param string $original_template_id ID (slug) of the default entity. + * @param string $error_message An error message to display if the test fails. + */ + public function test_get_block_templates_returns_unique_entities( $template_type, $original_template_id, $error_message ) { + $original_template = _get_block_template_file( $template_type, $original_template_id ); + $this->assertNotEmpty( $original_template, 'An original (non-duplicate) template must exist for this test to work correctly.' ); + + $block_templates = get_block_templates( array(), $template_type ); + $this->assertNotEmpty( $block_templates, 'get_block_templates() must return a non-empty value.' ); + + $block_template_ids = wp_list_pluck( $block_templates, 'id' ); + $this->assertSame( count( array_unique( $block_template_ids ) ), count( $block_template_ids ), $error_message ); + } + + /** + * Data provider for test_get_block_templates_returns_unique_entities(). + * + * @return array + */ + public function data_get_block_templates_returns_unique_entities() { + return array( + 'wp_template template type' => array( + 'template_type' => 'wp_template', + 'original_template_id' => 'index', + 'error_message' => 'get_block_templates() must return unique templates.', + ), + 'wp_template_part template type' => array( + 'template_type' => 'wp_template_part', + 'original_template_id' => 'small-header', + 'error_message' => 'get_block_templates() must return unique template parts.', + ), + ); + } +}