WordPress.org

Make WordPress Core

Changeset 43837


Ignore:
Timestamp:
10/29/2018 02:42:23 AM (13 months ago)
Author:
pento
Message:

Block Editor: Fix meta boxes not showing.

The block editor needs to duplicate the classic meta box behaviour, so it can extract the registered meta boxes, and import them into the block editor.

To match the classic editor behaviour as closely as possible, this moves the relevant code from the classic editor, into a new function, so it can be called by both.

Props pento, peterwilsoncc.
Fixes #45172.

Location:
branches/5.0/src/wp-admin
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • branches/5.0/src/wp-admin/edit-form-advanced.php

    r43779 r43837  
    226226require_once( ABSPATH . 'wp-admin/includes/meta-boxes.php' );
    227227
    228 
    229 $publish_callback_args = array( '__back_compat_meta_box' => true );
    230 if ( post_type_supports($post_type, 'revisions') && 'auto-draft' != $post->post_status ) {
    231     $revisions = wp_get_post_revisions( $post_ID );
    232 
    233     // We should aim to show the revisions meta box only when there are revisions.
    234     if ( count( $revisions ) > 1 ) {
    235         reset( $revisions ); // Reset pointer for key()
    236         $publish_callback_args = array( 'revisions_count' => count( $revisions ), 'revision_id' => key( $revisions ), '__back_compat_meta_box' => true );
    237         add_meta_box('revisionsdiv', __('Revisions'), 'post_revisions_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) );
    238     }
    239 }
    240 
    241 if ( 'attachment' == $post_type ) {
    242     wp_enqueue_script( 'image-edit' );
    243     wp_enqueue_style( 'imgareaselect' );
    244     add_meta_box( 'submitdiv', __('Save'), 'attachment_submit_meta_box', null, 'side', 'core', array( '__back_compat_meta_box' => true ) );
    245     add_action( 'edit_form_after_title', 'edit_form_image_editor' );
    246 
    247     if ( wp_attachment_is( 'audio', $post ) ) {
    248         add_meta_box( 'attachment-id3', __( 'Metadata' ), 'attachment_id3_data_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) );
    249     }
    250 } else {
    251     add_meta_box( 'submitdiv', __( 'Publish' ), 'post_submit_meta_box', null, 'side', 'core', $publish_callback_args );
    252 }
    253 
    254 if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post_type, 'post-formats' ) )
    255     add_meta_box( 'formatdiv', _x( 'Format', 'post format' ), 'post_format_meta_box', null, 'side', 'core', array( '__back_compat_meta_box' => true ) );
    256 
    257 // all taxonomies
    258 foreach ( get_object_taxonomies( $post ) as $tax_name ) {
    259     $taxonomy = get_taxonomy( $tax_name );
    260     if ( ! $taxonomy->show_ui || false === $taxonomy->meta_box_cb )
    261         continue;
    262 
    263     $label = $taxonomy->labels->name;
    264 
    265     if ( ! is_taxonomy_hierarchical( $tax_name ) )
    266         $tax_meta_box_id = 'tagsdiv-' . $tax_name;
    267     else
    268         $tax_meta_box_id = $tax_name . 'div';
    269 
    270     add_meta_box( $tax_meta_box_id, $label, $taxonomy->meta_box_cb, null, 'side', 'core', array( 'taxonomy' => $tax_name, '__back_compat_meta_box' => true ) );
    271 }
    272 
    273 if ( post_type_supports( $post_type, 'page-attributes' ) || count( get_page_templates( $post ) ) > 0 ) {
    274     add_meta_box( 'pageparentdiv', $post_type_object->labels->attributes, 'page_attributes_meta_box', null, 'side', 'core', array( '__back_compat_meta_box' => true ) );
    275 }
    276 
    277 if ( $thumbnail_support && current_user_can( 'upload_files' ) )
    278     add_meta_box('postimagediv', esc_html( $post_type_object->labels->featured_image ), 'post_thumbnail_meta_box', null, 'side', 'low', array( '__back_compat_meta_box' => true ) );
    279 
    280 if ( post_type_supports($post_type, 'excerpt') )
    281     add_meta_box('postexcerpt', __('Excerpt'), 'post_excerpt_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) );
    282 
    283 if ( post_type_supports($post_type, 'trackbacks') )
    284     add_meta_box('trackbacksdiv', __('Send Trackbacks'), 'post_trackback_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) );
    285 
    286 if ( post_type_supports($post_type, 'custom-fields') )
    287     add_meta_box('postcustom', __('Custom Fields'), 'post_custom_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) );
    288 
    289 /**
    290  * Fires in the middle of built-in meta box registration.
    291  *
    292  * @since 2.1.0
    293  * @deprecated 3.7.0 Use 'add_meta_boxes' instead.
    294  *
    295  * @param WP_Post $post Post object.
    296  */
    297 do_action( 'dbx_post_advanced', $post );
    298 
    299 // Allow the Discussion meta box to show up if the post type supports comments,
    300 // or if comments or pings are open.
    301 if ( comments_open( $post ) || pings_open( $post ) || post_type_supports( $post_type, 'comments' ) ) {
    302     add_meta_box( 'commentstatusdiv', __( 'Discussion' ), 'post_comment_status_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) );
    303 }
    304 
    305 $stati = get_post_stati( array( 'public' => true ) );
    306 if ( empty( $stati ) ) {
    307     $stati = array( 'publish' );
    308 }
    309 $stati[] = 'private';
    310 
    311 if ( in_array( get_post_status( $post ), $stati ) ) {
    312     // If the post type support comments, or the post has comments, allow the
    313     // Comments meta box.
    314     if ( comments_open( $post ) || pings_open( $post ) || $post->comment_count > 0 || post_type_supports( $post_type, 'comments' ) ) {
    315         add_meta_box( 'commentsdiv', __( 'Comments' ), 'post_comment_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) );
    316     }
    317 }
    318 
    319 if ( ! ( 'pending' == get_post_status( $post ) && ! current_user_can( $post_type_object->cap->publish_posts ) ) )
    320     add_meta_box('slugdiv', __('Slug'), 'post_slug_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) );
    321 
    322 if ( post_type_supports( $post_type, 'author' ) && current_user_can( $post_type_object->cap->edit_others_posts ) ) {
    323     add_meta_box( 'authordiv', __( 'Author' ), 'post_author_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) );
    324 }
    325 
    326 /**
    327  * Fires after all built-in meta boxes have been added.
    328  *
    329  * @since 3.0.0
    330  *
    331  * @param string  $post_type Post type.
    332  * @param WP_Post $post      Post object.
    333  */
    334 do_action( 'add_meta_boxes', $post_type, $post );
    335 
    336 /**
    337  * Fires after all built-in meta boxes have been added, contextually for the given post type.
    338  *
    339  * The dynamic portion of the hook, `$post_type`, refers to the post type of the post.
    340  *
    341  * @since 3.0.0
    342  *
    343  * @param WP_Post $post Post object.
    344  */
    345 do_action( "add_meta_boxes_{$post_type}", $post );
    346 
    347 /**
    348  * Fires after meta boxes have been added.
    349  *
    350  * Fires once for each of the default meta box contexts: normal, advanced, and side.
    351  *
    352  * @since 3.0.0
    353  *
    354  * @param string  $post_type Post type of the post.
    355  * @param string  $context   string  Meta box context.
    356  * @param WP_Post $post      Post object.
    357  */
    358 do_action( 'do_meta_boxes', $post_type, 'normal', $post );
    359 /** This action is documented in wp-admin/edit-form-advanced.php */
    360 do_action( 'do_meta_boxes', $post_type, 'advanced', $post );
    361 /** This action is documented in wp-admin/edit-form-advanced.php */
    362 do_action( 'do_meta_boxes', $post_type, 'side', $post );
     228register_and_do_post_meta_boxes( $post );
    363229
    364230add_screen_option('layout_columns', array('max' => 2, 'default' => 2) );
  • branches/5.0/src/wp-admin/edit-form-blocks.php

    r43833 r43837  
    369369do_action( 'enqueue_block_editor_assets' );
    370370
     371// In order to duplicate classic meta box behaviour, we need to run the classic meta box actions.
     372require_once( ABSPATH . 'wp-admin/includes/meta-boxes.php' );
     373register_and_do_post_meta_boxes( $post );
     374
     375// Some meta boxes hook into the 'edit_form_advanced' filter.
     376/** This action is documented in wp-admin/edit-form-advanced.php */
     377do_action( 'edit_form_advanced', $post );
     378
    371379require_once( ABSPATH . 'wp-admin/admin-header.php' );
    372380?>
  • branches/5.0/src/wp-admin/edit-form-comment.php

    r43301 r43837  
    178178<div id="postbox-container-2" class="postbox-container">
    179179<?php
    180 /** This action is documented in wp-admin/edit-form-advanced.php */
     180/** This action is documented in wp-admin/includes/meta-boxes.php */
    181181do_action( 'add_meta_boxes', 'comment', $comment );
    182182
  • branches/5.0/src/wp-admin/edit-link-form.php

    r39537 r43837  
    3131add_meta_box('linkadvanceddiv', __('Advanced'), 'link_advanced_meta_box', null, 'normal', 'core');
    3232
    33 /** This action is documented in wp-admin/edit-form-advanced.php */
     33/** This action is documented in wp-admin/includes/meta-boxes.php */
    3434do_action( 'add_meta_boxes', 'link', $link );
    3535
     
    4343do_action( 'add_meta_boxes_link', $link );
    4444
    45 /** This action is documented in wp-admin/edit-form-advanced.php */
     45/** This action is documented in wp-admin/includes/meta-boxes.php */
    4646do_action( 'do_meta_boxes', 'link', 'normal', $link );
    47 /** This action is documented in wp-admin/edit-form-advanced.php */
     47/** This action is documented in wp-admin/includes/meta-boxes.php */
    4848do_action( 'do_meta_boxes', 'link', 'advanced', $link );
    49 /** This action is documented in wp-admin/edit-form-advanced.php */
     49/** This action is documented in wp-admin/includes/meta-boxes.php */
    5050do_action( 'do_meta_boxes', 'link', 'side', $link );
    5151
  • branches/5.0/src/wp-admin/includes/dashboard.php

    r43807 r43837  
    123123    }
    124124
    125     /** This action is documented in wp-admin/edit-form-advanced.php */
     125    /** This action is documented in wp-admin/includes/meta-boxes.php */
    126126    do_action( 'do_meta_boxes', $screen->id, 'normal', '' );
    127127
    128     /** This action is documented in wp-admin/edit-form-advanced.php */
     128    /** This action is documented in wp-admin/includes/meta-boxes.php */
    129129    do_action( 'do_meta_boxes', $screen->id, 'side', '' );
    130130}
  • branches/5.0/src/wp-admin/includes/meta-boxes.php

    r42219 r43837  
    12831283    endforeach;
    12841284}
     1285
     1286/**
     1287 * Registers the default post meta boxes, and runs the `do_meta_boxes` actions.
     1288 *
     1289 * @since 5.0.0
     1290 *
     1291 * @param WP_Post $post The post object that these meta boxes are being generated for.
     1292 */
     1293function register_and_do_post_meta_boxes( $post ) {
     1294    $post_type = $post->post_type;
     1295    $post_type_object = get_post_type_object( $post_type );
     1296
     1297    $thumbnail_support = current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' );
     1298    if ( ! $thumbnail_support && 'attachment' === $post_type && $post->post_mime_type ) {
     1299        if ( wp_attachment_is( 'audio', $post ) ) {
     1300            $thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' );
     1301        } elseif ( wp_attachment_is( 'video', $post ) ) {
     1302            $thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' );
     1303        }
     1304    }
     1305
     1306    $publish_callback_args = array( '__back_compat_meta_box' => true );
     1307    if ( post_type_supports($post_type, 'revisions') && 'auto-draft' != $post->post_status ) {
     1308        $revisions = wp_get_post_revisions( $post->ID );
     1309
     1310        // We should aim to show the revisions meta box only when there are revisions.
     1311        if ( count( $revisions ) > 1 ) {
     1312            reset( $revisions ); // Reset pointer for key()
     1313            $publish_callback_args = array( 'revisions_count' => count( $revisions ), 'revision_id' => key( $revisions ), '__back_compat_meta_box' => true );
     1314            add_meta_box('revisionsdiv', __('Revisions'), 'post_revisions_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) );
     1315        }
     1316    }
     1317
     1318    if ( 'attachment' == $post_type ) {
     1319        wp_enqueue_script( 'image-edit' );
     1320        wp_enqueue_style( 'imgareaselect' );
     1321        add_meta_box( 'submitdiv', __('Save'), 'attachment_submit_meta_box', null, 'side', 'core', array( '__back_compat_meta_box' => true ) );
     1322        add_action( 'edit_form_after_title', 'edit_form_image_editor' );
     1323
     1324        if ( wp_attachment_is( 'audio', $post ) ) {
     1325            add_meta_box( 'attachment-id3', __( 'Metadata' ), 'attachment_id3_data_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) );
     1326        }
     1327    } else {
     1328        add_meta_box( 'submitdiv', __( 'Publish' ), 'post_submit_meta_box', null, 'side', 'core', $publish_callback_args );
     1329    }
     1330
     1331    if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post_type, 'post-formats' ) )
     1332        add_meta_box( 'formatdiv', _x( 'Format', 'post format' ), 'post_format_meta_box', null, 'side', 'core', array( '__back_compat_meta_box' => true ) );
     1333
     1334    // all taxonomies
     1335    foreach ( get_object_taxonomies( $post ) as $tax_name ) {
     1336        $taxonomy = get_taxonomy( $tax_name );
     1337        if ( ! $taxonomy->show_ui || false === $taxonomy->meta_box_cb )
     1338            continue;
     1339
     1340        $label = $taxonomy->labels->name;
     1341
     1342        if ( ! is_taxonomy_hierarchical( $tax_name ) )
     1343            $tax_meta_box_id = 'tagsdiv-' . $tax_name;
     1344        else
     1345            $tax_meta_box_id = $tax_name . 'div';
     1346
     1347        add_meta_box( $tax_meta_box_id, $label, $taxonomy->meta_box_cb, null, 'side', 'core', array( 'taxonomy' => $tax_name, '__back_compat_meta_box' => true ) );
     1348    }
     1349
     1350    if ( post_type_supports( $post_type, 'page-attributes' ) || count( get_page_templates( $post ) ) > 0 ) {
     1351        add_meta_box( 'pageparentdiv', $post_type_object->labels->attributes, 'page_attributes_meta_box', null, 'side', 'core', array( '__back_compat_meta_box' => true ) );
     1352    }
     1353
     1354    if ( $thumbnail_support && current_user_can( 'upload_files' ) )
     1355        add_meta_box('postimagediv', esc_html( $post_type_object->labels->featured_image ), 'post_thumbnail_meta_box', null, 'side', 'low', array( '__back_compat_meta_box' => true ) );
     1356
     1357    if ( post_type_supports($post_type, 'excerpt') )
     1358        add_meta_box('postexcerpt', __('Excerpt'), 'post_excerpt_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) );
     1359
     1360    if ( post_type_supports($post_type, 'trackbacks') )
     1361        add_meta_box('trackbacksdiv', __('Send Trackbacks'), 'post_trackback_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) );
     1362
     1363    if ( post_type_supports($post_type, 'custom-fields') )
     1364        add_meta_box('postcustom', __('Custom Fields'), 'post_custom_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) );
     1365
     1366    /**
     1367     * Fires in the middle of built-in meta box registration.
     1368     *
     1369     * @since 2.1.0
     1370     * @deprecated 3.7.0 Use 'add_meta_boxes' instead.
     1371     *
     1372     * @param WP_Post $post Post object.
     1373     */
     1374    do_action( 'dbx_post_advanced', $post );
     1375
     1376    // Allow the Discussion meta box to show up if the post type supports comments,
     1377    // or if comments or pings are open.
     1378    if ( comments_open( $post ) || pings_open( $post ) || post_type_supports( $post_type, 'comments' ) ) {
     1379        add_meta_box( 'commentstatusdiv', __( 'Discussion' ), 'post_comment_status_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) );
     1380    }
     1381
     1382    $stati = get_post_stati( array( 'public' => true ) );
     1383    if ( empty( $stati ) ) {
     1384        $stati = array( 'publish' );
     1385    }
     1386    $stati[] = 'private';
     1387
     1388    if ( in_array( get_post_status( $post ), $stati ) ) {
     1389        // If the post type support comments, or the post has comments, allow the
     1390        // Comments meta box.
     1391        if ( comments_open( $post ) || pings_open( $post ) || $post->comment_count > 0 || post_type_supports( $post_type, 'comments' ) ) {
     1392            add_meta_box( 'commentsdiv', __( 'Comments' ), 'post_comment_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) );
     1393        }
     1394    }
     1395
     1396    if ( ! ( 'pending' == get_post_status( $post ) && ! current_user_can( $post_type_object->cap->publish_posts ) ) )
     1397        add_meta_box('slugdiv', __('Slug'), 'post_slug_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) );
     1398
     1399    if ( post_type_supports( $post_type, 'author' ) && current_user_can( $post_type_object->cap->edit_others_posts ) ) {
     1400        add_meta_box( 'authordiv', __( 'Author' ), 'post_author_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) );
     1401    }
     1402
     1403    /**
     1404     * Fires after all built-in meta boxes have been added.
     1405     *
     1406     * @since 3.0.0
     1407     *
     1408     * @param string  $post_type Post type.
     1409     * @param WP_Post $post      Post object.
     1410     */
     1411    do_action( 'add_meta_boxes', $post_type, $post );
     1412
     1413    /**
     1414     * Fires after all built-in meta boxes have been added, contextually for the given post type.
     1415     *
     1416     * The dynamic portion of the hook, `$post_type`, refers to the post type of the post.
     1417     *
     1418     * @since 3.0.0
     1419     *
     1420     * @param WP_Post $post Post object.
     1421     */
     1422    do_action( "add_meta_boxes_{$post_type}", $post );
     1423
     1424    /**
     1425     * Fires after meta boxes have been added.
     1426     *
     1427     * Fires once for each of the default meta box contexts: normal, advanced, and side.
     1428     *
     1429     * @since 3.0.0
     1430     *
     1431     * @param string  $post_type Post type of the post.
     1432     * @param string  $context   string  Meta box context.
     1433     * @param WP_Post $post      Post object.
     1434     */
     1435    do_action( 'do_meta_boxes', $post_type, 'normal', $post );
     1436    /** This action is documented in wp-admin/includes/meta-boxes.php */
     1437    do_action( 'do_meta_boxes', $post_type, 'advanced', $post );
     1438    /** This action is documented in wp-admin/includes/meta-boxes.php */
     1439    do_action( 'do_meta_boxes', $post_type, 'side', $post );
     1440}
Note: See TracChangeset for help on using the changeset viewer.