Skip to content

Commit

Permalink
Refactored the collect of meta box data. Fixed a bug where meta boxes…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
jorgefilipecosta committed Mar 14, 2018
1 parent e9c1c20 commit 1e081b4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 125 deletions.
45 changes: 17 additions & 28 deletions lib/meta-box-partial-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );

/**
Expand Down Expand Up @@ -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.
?>
<form class="metabox-base-form">
Expand All @@ -328,18 +302,33 @@ function the_gutenberg_metaboxes() {
<div id="poststuff" class="sidebar-open">
<div id="postbox-container-2" class="postbox-container">
<?php
do_meta_boxes(
$number_metaboxes = do_meta_boxes(
$current_screen,
$location,
$post
);

$meta_box_data[ $location ] = $number_metaboxes > 0;
?>
</div>
</div>
</form>
<?php endforeach; ?>
<?php

/**
* Sadly we probably can not add this data directly into editor settings.
*
* ACF and other meta boxes need admin_head to fire for meta box registry.
* admin_head fires after admin_enqueue_scripts which is where we create our
* editor instance. If a cleaner solution can be imagined, please change
* this, and try to get this data to load directly into the editor settings.
*/
wp_add_inline_script(
'wp-edit-post',
'window._wpLoadGutenbergEditor.then( function( editor ) { editor.initializeMetaBoxes( ' . wp_json_encode( $meta_box_data ) . ' ) } );'
);

// Reset meta box data.
$wp_meta_boxes = $_original_meta_boxes;
}
Expand Down
74 changes: 26 additions & 48 deletions lib/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ function gutenberg_trick_plugins_into_registering_meta_boxes() {
/**
* Collect information about meta_boxes registered for the current post.
*
* This is used to tell React and Redux whether the meta box location has
* meta boxes.
* Redirect to classic editor if a metabox is incompatible.
*
* @since 1.5.0
*/
Expand Down Expand Up @@ -210,57 +209,36 @@ function gutenberg_collect_meta_box_data() {

$meta_box_data = array();

// If the meta box should be empty set to false.
// Redirect to classic editor if a meta box is incompatible.
foreach ( $locations as $location ) {
if ( gutenberg_is_meta_box_empty( $_meta_boxes_copy, $location, $post->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;
?>
<script type="text/javascript">
var joiner = '?';
if ( window.location.search ) {
joiner = '&';
}
window.location.href += joiner + 'classic-editor';
</script>
<?php
exit;
}
}

// Incompatible meta boxes require an immediate redirect to the classic editor.
if ( $incompatible_meta_box ) {
?>
<script type="text/javascript">
var joiner = '?';
if ( window.location.search ) {
joiner = '&';
}
window.location.href += joiner + 'classic-editor';
</script>
<?php
exit;
}
}
}

/**
* Sadly we probably can not add this data directly into editor settings.
*
* ACF and other meta boxes need admin_head to fire for meta box registry.
* admin_head fires after admin_enqueue_scripts which is where we create our
* editor instance. If a cleaner solution can be imagined, please change
* this, and try to get this data to load directly into the editor settings.
*/
wp_add_inline_script(
'wp-edit-post',
'window._wpLoadGutenbergEditor.then( function( editor ) { editor.initializeMetaBoxes( ' . wp_json_encode( $meta_box_data ) . ' ) } );'
);
}

/**
Expand Down
49 changes: 0 additions & 49 deletions phpunit/class-meta-box-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,55 +111,6 @@ public function setUp() {
);
}

/**
* Tests for empty meta box.
*/
public function test_gutenberg_is_meta_box_empty_with_empty_meta_box() {
$context = 'side';
$post_type = 'post';
$meta_boxes = $this->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.
*/
Expand Down

0 comments on commit 1e081b4

Please sign in to comment.