From 1a2dc81e4d4575a4fd545d0acc8ae5e6d9243164 Mon Sep 17 00:00:00 2001 From: Jorge Date: Wed, 14 Mar 2018 21:48:57 +0000 Subject: [PATCH] Refactored the collect of meta box data. Fixed a bug where meta boxes may not be shown if they were moved to other location by the user. We were checking the locations where we have meta boxes in gutenberg_collect_meta_box_data. But our logic did not take into account that the locations of the meta boxes may be changed by the user. So if the user moved meta boxes that by default appeared in the normal location to the sidebar and had no default sidebar meta box, sidebar meta boxes would not be visible. To solve that we would need to copy the mechanism that reads user preferences for meta boxes to check their location from do_meta_boxes. Copying this logic makes gutenberg_collect_meta_box_data more complex and increases the maintainability efforts. It would also be very inefficient as that code would be executed multiple times. So the computing of the array of locations where we have meta boxes was moved to the_gutenberg_metaboxes function where we invoke do_meta_boxes function. This way we can take advantage of the fact that do_meta_boxes returns the number of meta boxes that were outputted. --- lib/meta-box-partial-page.php | 45 ++++++++------------ lib/register.php | 74 ++++++++++++--------------------- phpunit/class-meta-box-test.php | 49 ---------------------- 3 files changed, 43 insertions(+), 125 deletions(-) diff --git a/lib/meta-box-partial-page.php b/lib/meta-box-partial-page.php index 4041857be3216..bb4076fa1deb3 100644 --- a/lib/meta-box-partial-page.php +++ b/lib/meta-box-partial-page.php @@ -150,32 +150,6 @@ function gutenberg_filter_meta_boxes( $meta_boxes ) { return $meta_boxes; } -/** - * Check whether a meta box is empty. - * - * @since 1.5.0 - * - * @param array $meta_boxes Meta box data. - * @param string $context Location of meta box, one of side, advanced, normal. - * @param string $post_type Post type to investigate. - * @return boolean Whether the meta box is empty. - */ -function gutenberg_is_meta_box_empty( $meta_boxes, $context, $post_type ) { - $page = $post_type; - - if ( ! isset( $meta_boxes[ $page ][ $context ] ) ) { - return true; - } - - foreach ( $meta_boxes[ $page ][ $context ] as $priority => $boxes ) { - if ( ! empty( $boxes ) ) { - return false; - } - } - - return true; -} - add_filter( 'filter_gutenberg_meta_boxes', 'gutenberg_filter_meta_boxes' ); /** @@ -317,7 +291,7 @@ function the_gutenberg_metaboxes() { */ $wp_meta_boxes = apply_filters( 'filter_gutenberg_meta_boxes', $wp_meta_boxes ); $locations = array( 'side', 'normal', 'advanced' ); - + $meta_box_data = array(); // Render meta boxes. ?>
@@ -328,11 +302,13 @@ function the_gutenberg_metaboxes() { @@ -340,6 +316,19 @@ function the_gutenberg_metaboxes() { post_type ) ) { - $meta_box_data[ $location ] = false; - } else { - $meta_box_data[ $location ] = true; - $incompatible_meta_box = false; - // Check if we have a meta box that has declared itself incompatible with the block editor. - foreach ( $_meta_boxes_copy[ $post->post_type ][ $location ] as $boxes ) { - foreach ( $boxes as $box ) { - /* - * If __block_editor_compatible_meta_box is declared as a false-y value, - * the meta box is not compatible with the block editor. - */ - if ( is_array( $box['args'] ) - && isset( $box['args']['__block_editor_compatible_meta_box'] ) - && ! $box['args']['__block_editor_compatible_meta_box'] ) { - $incompatible_meta_box = true; - break 2; - } + if ( ! isset( $_meta_boxes_copy[ $post->post_type ][ $location ] ) ) { + continue; + } + // Check if we have a meta box that has declared itself incompatible with the block editor. + foreach ( $_meta_boxes_copy[ $post->post_type ][ $location ] as $boxes ) { + foreach ( $boxes as $box ) { + /* + * If __block_editor_compatible_meta_box is declared as a false-y value, + * the meta box is not compatible with the block editor. + */ + if ( is_array( $box['args'] ) + && isset( $box['args']['__block_editor_compatible_meta_box'] ) + && ! $box['args']['__block_editor_compatible_meta_box'] ) { + $incompatible_meta_box = true; + ?> + + - - meta_boxes; - $meta_boxes[ $post_type ][ $context ] = array(); - - $is_empty = gutenberg_is_meta_box_empty( $meta_boxes, $context, $post_type ); - $this->assertTrue( $is_empty ); - } - - /** - * Tests for non empty meta box area. - */ - public function test_gutenberg_is_meta_box_empty_with_non_empty_meta_box() { - $context = 'normal'; - $post_type = 'post'; - $meta_boxes = $this->meta_boxes; - - $is_empty = gutenberg_is_meta_box_empty( $meta_boxes, $context, $post_type ); - $this->assertFalse( $is_empty ); - } - - /** - * Tests for non existant location. - */ - public function test_gutenberg_is_meta_box_empty_with_non_existant_location() { - $context = 'test'; - $post_type = 'post'; - $meta_boxes = $this->meta_boxes; - - $is_empty = gutenberg_is_meta_box_empty( $meta_boxes, $context, $post_type ); - $this->assertTrue( $is_empty ); - } - - /** - * Tests for non existant page. - */ - public function test_gutenberg_is_meta_box_empty_with_non_existant_page() { - $context = 'normal'; - $post_type = 'test'; - $meta_boxes = $this->meta_boxes; - - $is_empty = gutenberg_is_meta_box_empty( $meta_boxes, $context, $post_type ); - $this->assertTrue( $is_empty ); - } - /** * Test filtering of meta box data. */