Make WordPress Core


Ignore:
Timestamp:
12/16/2018 01:18:22 AM (7 years ago)
Author:
jeremyfelt
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.

Merges [43837] from the 5.0 branch to trunk.

Props pento, peterwilsoncc.
Fixes #45172.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/wp-admin/includes/meta-boxes.php

    r43571 r44214  
    13641364    endforeach;
    13651365}
     1366
     1367/**
     1368 * Registers the default post meta boxes, and runs the `do_meta_boxes` actions.
     1369 *
     1370 * @since 5.0.0
     1371 *
     1372 * @param WP_Post $post The post object that these meta boxes are being generated for.
     1373 */
     1374function register_and_do_post_meta_boxes( $post ) {
     1375    $post_type        = $post->post_type;
     1376    $post_type_object = get_post_type_object( $post_type );
     1377
     1378    $thumbnail_support = current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' );
     1379    if ( ! $thumbnail_support && 'attachment' === $post_type && $post->post_mime_type ) {
     1380        if ( wp_attachment_is( 'audio', $post ) ) {
     1381            $thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' );
     1382        } elseif ( wp_attachment_is( 'video', $post ) ) {
     1383            $thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' );
     1384        }
     1385    }
     1386
     1387    $publish_callback_args = array( '__back_compat_meta_box' => true );
     1388    if ( post_type_supports( $post_type, 'revisions' ) && 'auto-draft' != $post->post_status ) {
     1389        $revisions = wp_get_post_revisions( $post->ID );
     1390
     1391        // We should aim to show the revisions meta box only when there are revisions.
     1392        if ( count( $revisions ) > 1 ) {
     1393            reset( $revisions ); // Reset pointer for key()
     1394            $publish_callback_args = array(
     1395                'revisions_count'        => count( $revisions ),
     1396                'revision_id'            => key( $revisions ),
     1397                '__back_compat_meta_box' => true,
     1398            );
     1399            add_meta_box( 'revisionsdiv', __( 'Revisions' ), 'post_revisions_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) );
     1400        }
     1401    }
     1402
     1403    if ( 'attachment' == $post_type ) {
     1404        wp_enqueue_script( 'image-edit' );
     1405        wp_enqueue_style( 'imgareaselect' );
     1406        add_meta_box( 'submitdiv', __( 'Save' ), 'attachment_submit_meta_box', null, 'side', 'core', array( '__back_compat_meta_box' => true ) );
     1407        add_action( 'edit_form_after_title', 'edit_form_image_editor' );
     1408
     1409        if ( wp_attachment_is( 'audio', $post ) ) {
     1410            add_meta_box( 'attachment-id3', __( 'Metadata' ), 'attachment_id3_data_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) );
     1411        }
     1412    } else {
     1413        add_meta_box( 'submitdiv', __( 'Publish' ), 'post_submit_meta_box', null, 'side', 'core', $publish_callback_args );
     1414    }
     1415
     1416    if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post_type, 'post-formats' ) ) {
     1417        add_meta_box( 'formatdiv', _x( 'Format', 'post format' ), 'post_format_meta_box', null, 'side', 'core', array( '__back_compat_meta_box' => true ) );
     1418    }
     1419
     1420    // all taxonomies
     1421    foreach ( get_object_taxonomies( $post ) as $tax_name ) {
     1422        $taxonomy = get_taxonomy( $tax_name );
     1423        if ( ! $taxonomy->show_ui || false === $taxonomy->meta_box_cb ) {
     1424            continue;
     1425        }
     1426
     1427        $label = $taxonomy->labels->name;
     1428
     1429        if ( ! is_taxonomy_hierarchical( $tax_name ) ) {
     1430            $tax_meta_box_id = 'tagsdiv-' . $tax_name;
     1431        } else {
     1432            $tax_meta_box_id = $tax_name . 'div';
     1433        }
     1434
     1435        add_meta_box(
     1436            $tax_meta_box_id,
     1437            $label,
     1438            $taxonomy->meta_box_cb,
     1439            null,
     1440            'side',
     1441            'core',
     1442            array(
     1443                'taxonomy'               => $tax_name,
     1444                '__back_compat_meta_box' => true,
     1445            )
     1446        );
     1447    }
     1448
     1449    if ( post_type_supports( $post_type, 'page-attributes' ) || count( get_page_templates( $post ) ) > 0 ) {
     1450        add_meta_box( 'pageparentdiv', $post_type_object->labels->attributes, 'page_attributes_meta_box', null, 'side', 'core', array( '__back_compat_meta_box' => true ) );
     1451    }
     1452
     1453    if ( $thumbnail_support && current_user_can( 'upload_files' ) ) {
     1454        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 ) );
     1455    }
     1456
     1457    if ( post_type_supports( $post_type, 'excerpt' ) ) {
     1458        add_meta_box( 'postexcerpt', __( 'Excerpt' ), 'post_excerpt_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) );
     1459    }
     1460
     1461    if ( post_type_supports( $post_type, 'trackbacks' ) ) {
     1462        add_meta_box( 'trackbacksdiv', __( 'Send Trackbacks' ), 'post_trackback_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) );
     1463    }
     1464
     1465    if ( post_type_supports( $post_type, 'custom-fields' ) ) {
     1466        add_meta_box( 'postcustom', __( 'Custom Fields' ), 'post_custom_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) );
     1467    }
     1468
     1469    /**
     1470     * Fires in the middle of built-in meta box registration.
     1471     *
     1472     * @since 2.1.0
     1473     * @deprecated 3.7.0 Use 'add_meta_boxes' instead.
     1474     *
     1475     * @param WP_Post $post Post object.
     1476     */
     1477    do_action( 'dbx_post_advanced', $post );
     1478
     1479    // Allow the Discussion meta box to show up if the post type supports comments,
     1480    // or if comments or pings are open.
     1481    if ( comments_open( $post ) || pings_open( $post ) || post_type_supports( $post_type, 'comments' ) ) {
     1482        add_meta_box( 'commentstatusdiv', __( 'Discussion' ), 'post_comment_status_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) );
     1483    }
     1484
     1485    $stati = get_post_stati( array( 'public' => true ) );
     1486    if ( empty( $stati ) ) {
     1487        $stati = array( 'publish' );
     1488    }
     1489    $stati[] = 'private';
     1490
     1491    if ( in_array( get_post_status( $post ), $stati ) ) {
     1492        // If the post type support comments, or the post has comments, allow the
     1493        // Comments meta box.
     1494        if ( comments_open( $post ) || pings_open( $post ) || $post->comment_count > 0 || post_type_supports( $post_type, 'comments' ) ) {
     1495            add_meta_box( 'commentsdiv', __( 'Comments' ), 'post_comment_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) );
     1496        }
     1497    }
     1498
     1499    if ( ! ( 'pending' == get_post_status( $post ) && ! current_user_can( $post_type_object->cap->publish_posts ) ) ) {
     1500        add_meta_box( 'slugdiv', __( 'Slug' ), 'post_slug_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) );
     1501    }
     1502
     1503    if ( post_type_supports( $post_type, 'author' ) && current_user_can( $post_type_object->cap->edit_others_posts ) ) {
     1504        add_meta_box( 'authordiv', __( 'Author' ), 'post_author_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) );
     1505    }
     1506
     1507    /**
     1508     * Fires after all built-in meta boxes have been added.
     1509     *
     1510     * @since 3.0.0
     1511     *
     1512     * @param string  $post_type Post type.
     1513     * @param WP_Post $post      Post object.
     1514     */
     1515    do_action( 'add_meta_boxes', $post_type, $post );
     1516
     1517    /**
     1518     * Fires after all built-in meta boxes have been added, contextually for the given post type.
     1519     *
     1520     * The dynamic portion of the hook, `$post_type`, refers to the post type of the post.
     1521     *
     1522     * @since 3.0.0
     1523     *
     1524     * @param WP_Post $post Post object.
     1525     */
     1526    do_action( "add_meta_boxes_{$post_type}", $post );
     1527
     1528    /**
     1529     * Fires after meta boxes have been added.
     1530     *
     1531     * Fires once for each of the default meta box contexts: normal, advanced, and side.
     1532     *
     1533     * @since 3.0.0
     1534     *
     1535     * @param string  $post_type Post type of the post.
     1536     * @param string  $context   string  Meta box context.
     1537     * @param WP_Post $post      Post object.
     1538     */
     1539    do_action( 'do_meta_boxes', $post_type, 'normal', $post );
     1540    /** This action is documented in wp-admin/includes/meta-boxes.php */
     1541    do_action( 'do_meta_boxes', $post_type, 'advanced', $post );
     1542    /** This action is documented in wp-admin/includes/meta-boxes.php */
     1543    do_action( 'do_meta_boxes', $post_type, 'side', $post );
     1544}
Note: See TracChangeset for help on using the changeset viewer.