WordPress Version: 6.2
/**
* Renders the meta boxes forms.
*
* @since 5.0.0
*
* @global WP_Post $post Global post object.
* @global WP_Screen $current_screen WordPress current screen object.
* @global array $wp_meta_boxes
*/
function the_block_editor_meta_boxes()
{
global $post, $current_screen, $wp_meta_boxes;
// Handle meta box state.
$_original_meta_boxes = $wp_meta_boxes;
/**
* Fires right before the meta boxes are rendered.
*
* This allows for the filtering of meta box data, that should already be
* present by this point. Do not use as a means of adding meta box data.
*
* @since 5.0.0
*
* @param array $wp_meta_boxes Global meta box state.
*/
$wp_meta_boxes = apply_filters('filter_block_editor_meta_boxes', $wp_meta_boxes);
$locations = array('side', 'normal', 'advanced');
$priorities = array('high', 'sorted', 'core', 'default', 'low');
// Render meta boxes.
?>
<form class="metabox-base-form">
<?php
the_block_editor_meta_box_post_form_hidden_fields($post);
?>
</form>
<form id="toggle-custom-fields-form" method="post" action="<?php
echo esc_url(admin_url('post.php'));
?>">
<?php
wp_nonce_field('toggle-custom-fields', 'toggle-custom-fields-nonce');
?>
<input type="hidden" name="action" value="toggle-custom-fields" />
</form>
<?php
foreach ($locations as $location) {
?>
<form class="metabox-location-<?php
echo esc_attr($location);
?>" onsubmit="return false;">
<div id="poststuff" class="sidebar-open">
<div id="postbox-container-2" class="postbox-container">
<?php
do_meta_boxes($current_screen, $location, $post);
?>
</div>
</div>
</form>
<?php
}
?>
<?php
$meta_boxes_per_location = array();
foreach ($locations as $location) {
$meta_boxes_per_location[$location] = array();
if (!isset($wp_meta_boxes[$current_screen->id][$location])) {
continue;
}
foreach ($priorities as $priority) {
if (!isset($wp_meta_boxes[$current_screen->id][$location][$priority])) {
continue;
}
$meta_boxes = (array) $wp_meta_boxes[$current_screen->id][$location][$priority];
foreach ($meta_boxes as $meta_box) {
if (false == $meta_box || !$meta_box['title']) {
continue;
}
// If a meta box is just here for back compat, don't show it in the block editor.
if (isset($meta_box['args']['__back_compat_meta_box']) && $meta_box['args']['__back_compat_meta_box']) {
continue;
}
$meta_boxes_per_location[$location][] = array('id' => $meta_box['id'], 'title' => $meta_box['title']);
}
}
}
/*
* Sadly we probably cannot add this data directly into editor settings.
*
* Some 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.
*/
$script = 'window._wpLoadBlockEditor.then( function() {
wp.data.dispatch( \'core/edit-post\' ).setAvailableMetaBoxesPerLocation( ' . wp_json_encode($meta_boxes_per_location) . ' );
} );';
wp_add_inline_script('wp-edit-post', $script);
/*
* When `wp-edit-post` is output in the `<head>`, the inline script needs to be manually printed.
* Otherwise, meta boxes will not display because inline scripts for `wp-edit-post`
* will not be printed again after this point.
*/
if (wp_script_is('wp-edit-post', 'done')) {
printf("<script type='text/javascript'>\n%s\n</script>\n", trim($script));
}
/*
* If the 'postcustom' meta box is enabled, then we need to perform
* some extra initialization on it.
*/
$enable_custom_fields = (bool) get_user_meta(get_current_user_id(), 'enable_custom_fields', true);
if ($enable_custom_fields) {
$script = "( function( \$ ) {\n\t\t\tif ( \$('#postcustom').length ) {\n\t\t\t\t\$( '#the-list' ).wpList( {\n\t\t\t\t\taddBefore: function( s ) {\n\t\t\t\t\t\ts.data += '&post_id={$post->ID}';\n\t\t\t\t\t\treturn s;\n\t\t\t\t\t},\n\t\t\t\t\taddAfter: function() {\n\t\t\t\t\t\t\$('table#list-table').show();\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t} )( jQuery );";
wp_enqueue_script('wp-lists');
wp_add_inline_script('wp-lists', $script);
}
/*
* Refresh nonces used by the meta box loader.
*
* The logic is very similar to that provided by post.js for the classic editor.
*/
$script = "( function( \$ ) {\n\t\tvar check, timeout;\n\n\t\tfunction schedule() {\n\t\t\tcheck = false;\n\t\t\twindow.clearTimeout( timeout );\n\t\t\ttimeout = window.setTimeout( function() { check = true; }, 300000 );\n\t\t}\n\n\t\t\$( document ).on( 'heartbeat-send.wp-refresh-nonces', function( e, data ) {\n\t\t\tvar post_id, \$authCheck = \$( '#wp-auth-check-wrap' );\n\n\t\t\tif ( check || ( \$authCheck.length && ! \$authCheck.hasClass( 'hidden' ) ) ) {\n\t\t\t\tif ( ( post_id = \$( '#post_ID' ).val() ) && \$( '#_wpnonce' ).val() ) {\n\t\t\t\t\tdata['wp-refresh-metabox-loader-nonces'] = {\n\t\t\t\t\t\tpost_id: post_id\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\t\t}).on( 'heartbeat-tick.wp-refresh-nonces', function( e, data ) {\n\t\t\tvar nonces = data['wp-refresh-metabox-loader-nonces'];\n\n\t\t\tif ( nonces ) {\n\t\t\t\tif ( nonces.replace ) {\n\t\t\t\t\tif ( nonces.replace.metabox_loader_nonce && window._wpMetaBoxUrl && wp.url ) {\n\t\t\t\t\t\twindow._wpMetaBoxUrl= wp.url.addQueryArgs( window._wpMetaBoxUrl, { 'meta-box-loader-nonce': nonces.replace.metabox_loader_nonce } );\n\t\t\t\t\t}\n\n\t\t\t\t\tif ( nonces.replace._wpnonce ) {\n\t\t\t\t\t\t\$( '#_wpnonce' ).val( nonces.replace._wpnonce );\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}).ready( function() {\n\t\t\tschedule();\n\t\t});\n\t} )( jQuery );";
wp_add_inline_script('heartbeat', $script);
// Reset meta box data.
$wp_meta_boxes = $_original_meta_boxes;
}