Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored meta box data collection. Fixed a bug where meta boxes may not be shown. #5619

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
* Redirects to classic editor if a meta box 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