Meta Boxes: Add __back_compat_meta_box and __block_editor_compatible_meta_box flags to meta boxes.

When meta boxes are registered, they can use the `__back_compat_meta_box` and `__block_editor_compatible_meta_box` flags, to show whether this registration just exists for if the classic editor is loaded, and whether this meta box is compatible with the block editor.

When a meta box marks itself as incompatible with the block editor, and `WP_DEBUG` is enabled, a warning will show inside that meta box in the classic editor.

As all core meta boxes have been recreated in the block editor, they can be marked with the `__back_compat_meta_box` flag.

Merges [43779] from the 5.0 branch to trunk.

See #45112.



git-svn-id: https://develop.svn.wordpress.org/trunk@44132 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast
2018-12-14 01:17:57 +00:00
parent cb0b292c87
commit 23efeb45c8
2 changed files with 87 additions and 17 deletions

View File

@@ -1136,6 +1136,28 @@ function do_meta_boxes( $screen, $context, $object ) {
if ( false == $box || ! $box['title'] ) {
continue;
}
// Don't show boxes in the block editor, if they're just here for back compat.
if ( $screen->is_block_editor() && isset( $box['args']['__back_compat_meta_box'] ) && $box['args']['__back_compat_meta_box'] ) {
continue;
}
// Don't show boxes in the block editor that aren't compatible with the block editor.
if ( $screen->is_block_editor() && isset( $box['args']['__block_editor_compatible_meta_box'] ) && ! $box['args']['__block_editor_compatible_meta_box'] ) {
continue;
}
$block_compatible = true;
if ( isset( $box['args']['__block_editor_compatible_meta_box'] ) ) {
$block_compatible = (bool) $box['args']['__block_editor_compatible_meta_box'];
unset( $box['args']['__block_editor_compatible_meta_box'] );
}
if ( isset( $box['args']['__back_compat_meta_box'] ) ) {
$block_compatible |= (bool) $box['args']['__back_compat_meta_box'];
unset( $box['args']['__back_compat_meta_box'] );
}
$i++;
$hidden_class = in_array( $box['id'], $hidden ) ? ' hide-if-js' : '';
echo '<div id="' . $box['id'] . '" class="postbox ' . postbox_classes( $box['id'], $page ) . $hidden_class . '" ' . '>' . "\n";
@@ -1161,6 +1183,42 @@ function do_meta_boxes( $screen, $context, $object ) {
echo "<span>{$box['title']}</span>";
echo "</h2>\n";
echo '<div class="inside">' . "\n";
if ( WP_DEBUG && ! $screen->is_block_editor() && ! isset( $_GET['meta-box-loader'] ) ) {
if ( is_array( $box['callback'] ) ) {
$reflection = new ReflectionMethod( $box['callback'][0], $box['callback'][1] );
} else {
$reflection = new ReflectionFunction( $box['callback'] );
}
// Don't show an error if it's an internal PHP function.
if ( ! $reflection->isInternal() ) {
// Only show errors if the meta box was registered by a plugin.
$filename = $reflection->getFileName();
if ( strpos( $filename, WP_PLUGIN_DIR ) === 0 ) {
$filename = str_replace( WP_PLUGIN_DIR, '', $filename );
$filename = preg_replace( '|^/([^/]*/).*$|', '\\1', $filename );
$plugins = get_plugins();
foreach ( $plugins as $name => $plugin ) {
if ( strpos( $name, $filename ) === 0 ) {
?>
<div class="error inline">
<p>
<?php
/* translators: %s: the name of the plugin that generated this meta box. */
printf( __( "This meta box, from the %s plugin, isn't compatible with the block editor." ), "<strong>{$plugin['Name']}</strong>" );
?>
</p>
</div>
<?php
}
}
}
}
}
call_user_func( $box['callback'], $object, $box );
echo "</div>\n";
echo "</div>\n";