true,
‘action’ => ‘edit’,
‘classic-editor’ => true,
‘post’ => $post_id,
),
admin_url( ‘post.php’ )
);
}
return $location;
}
add_filter( ‘redirect_post_location’, ‘gutenberg_meta_box_save_redirect’, 10, 2 );
/**
* Filter out core meta boxes as well as the post thumbnail.
*
* @since 1.5.0
*
* @param array $meta_boxes Meta box data.
* @return array Meta box data without core meta boxes.
*/
function gutenberg_filter_meta_boxes( $meta_boxes ) {
$core_side_meta_boxes = array(
‘submitdiv’,
‘formatdiv’,
‘pageparentdiv’,
‘postimagediv’,
);
$custom_taxonomies = get_taxonomies(
array(
‘show_ui’ => true,
),
‘objects’
);
// Following the same logic as meta box generation in:
// https://github.com/WordPress/wordpress-develop/blob/c896326/src/wp-admin/edit-form-advanced.php#L288-L292.
foreach ( $custom_taxonomies as $custom_taxonomy ) {
$core_side_meta_boxes [] = $custom_taxonomy->hierarchical ?
$custom_taxonomy->name . ‘div’ :
‘tagsdiv-‘ . $custom_taxonomy->name;
}
$core_normal_meta_boxes = array(
‘revisionsdiv’,
‘postexcerpt’,
‘trackbacksdiv’,
‘postcustom’,
‘commentstatusdiv’,
‘commentsdiv’,
‘slugdiv’,
‘authordiv’,
);
$taxonomy_callbacks_to_unset = array(
‘post_tags_meta_box’,
‘post_categories_meta_box’,
);
foreach ( $meta_boxes as $page => $contexts ) {
foreach ( $contexts as $context => $priorities ) {
foreach ( $priorities as $priority => $boxes ) {
foreach ( $boxes as $name => $data ) {
if ( ‘normal’ === $context && in_array( $name, $core_normal_meta_boxes ) ) {
unset( $meta_boxes[ $page ][ $context ][ $priority ][ $name ] );
} elseif ( ‘side’ === $context && in_array( $name, $core_side_meta_boxes ) ) {
unset( $meta_boxes[ $page ][ $context ][ $priority ][ $name ] );
}
// Filter out any taxonomies as Gutenberg already provides JS alternative.
if ( isset( $data[‘callback’] ) && in_array( $data[‘callback’], $taxonomy_callbacks_to_unset ) ) {
unset( $meta_boxes[ $page ][ $context ][ $priority ][ $name ] );
}
// Filter out meta boxes that are just registered for back compat.
if ( isset( $data[‘args’][‘__back_compat_meta_box’] ) && $data[‘args’][‘__back_compat_meta_box’] ) {
unset( $meta_boxes[ $page ][ $context ][ $priority ][ $name ] );
}
}
}
}
}
return $meta_boxes;
}
add_filter( ‘filter_gutenberg_meta_boxes’, ‘gutenberg_filter_meta_boxes’ );
/**
* Go through the global metaboxes, and override the render callback, so we can trigger our warning if needed.
*
* @since 1.8.0
*/
function gutenberg_intercept_meta_box_render() {
global $wp_meta_boxes;
foreach ( $wp_meta_boxes as $post_type => $contexts ) {
foreach ( $contexts as $context => $priorities ) {
foreach ( $priorities as $priority => $boxes ) {
foreach ( $boxes as $id => $box ) {
if ( ! is_array( $box ) ) {
continue;
}
if ( ! is_array( $wp_meta_boxes[ $post_type ][ $context ][ $priority ][ $id ][‘args’] ) ) {
$wp_meta_boxes[ $post_type ][ $context ][ $priority ][ $id ][‘args’] = array();
}
if ( ! isset( $wp_meta_boxes[ $post_type ][ $context ][ $priority ][ $id ][‘args’][‘__original_callback’] ) ) {
$wp_meta_boxes[ $post_type ][ $context ][ $priority ][ $id ][‘args’][‘__original_callback’] = $box[‘callback’];
$wp_meta_boxes[ $post_type ][ $context ][ $priority ][ $id ][‘callback’] = ‘gutenberg_override_meta_box_callback’;
}
}
}
}
}
}
add_action( ‘submitpost_box’, ‘gutenberg_intercept_meta_box_render’ );
add_action( ‘submitpage_box’, ‘gutenberg_intercept_meta_box_render’ );
add_action( ‘edit_page_form’, ‘gutenberg_intercept_meta_box_render’ );
add_action( ‘edit_form_advanced’, ‘gutenberg_intercept_meta_box_render’ );
/**
* Check if this metabox only exists for back compat purposes, show a warning if it doesn’t.
*
* @since 1.8.0
*
* @param mixed $object The object being operated on, on this screen.
* @param array $box The current meta box definition.
*/
function gutenberg_override_meta_box_callback( $object, $box ) {
$callback = $box[‘args’][‘__original_callback’];
unset( $box[‘args’][‘__original_callback’] );
$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’] );
}
if ( ! $block_compatible ) {
gutenberg_show_meta_box_warning( $callback );
}
call_user_func( $callback, $object, $box );
}
/**
* Display a warning in the metabox that the current plugin is causing the fallback to the old editor.
*
* @since 1.8.0
*
* @param callable $callback The function that a plugin has defined to render a meta box.
*/
function gutenberg_show_meta_box_warning( $callback ) {
// Only show the warning when WP_DEBUG is enabled.
if ( ! WP_DEBUG ) {
return;
}
// Don’t show in the Gutenberg meta box UI.
if ( ! isset( $_REQUEST[‘classic-editor’] ) ) {
return;
}
if ( is_array( $callback ) ) {
$reflection = new ReflectionMethod( $callback[0], $callback[1] );
} else {
$reflection = new ReflectionFunction( $callback );
}
if ( $reflection->isInternal() ) {
return;
}
$filename = $reflection->getFileName();
if ( strpos( $filename, WP_PLUGIN_DIR ) !== 0 ) {
return;
}
$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 ) {
?>
`, the inline script needs to be manually printed. Otherwise,
* metaboxes will not display because inline scripts for `wp-edit-post` will not be printed again after this point.
*
* @see https://github.com/WordPress/gutenberg/issues/6963
*/
if ( wp_script_is( ‘wp-edit-post’, ‘done’ ) ) {
printf( “n”, trim( $script ) );
}
// Reset meta box data.
$wp_meta_boxes = $_original_meta_boxes;
}
/**
* Renders the hidden form required for the meta boxes form.
*
* @param WP_Post $post Current post object.
*
* @since 1.8.0
*/
function gutenberg_meta_box_post_form_hidden_fields( $post ) {
$form_extra = ”;
if ( ‘auto-draft’ === $post->post_status ) {
$form_extra .= ““;
}
$form_action = ‘editpost’;
$nonce_action = ‘update-post_’ . $post->ID;
$form_extra .= ““;
$referer = wp_get_referer();
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
wp_nonce_field( $nonce_action );
?>