WordPress.org

Make WordPress Core

Changeset 43778


Ignore:
Timestamp:
10/22/2018 02:15:28 AM (13 months ago)
Author:
pento
Message:

Block Editor: Add helper functions to collect meta box information.

To allow the block editor to render meta boxes, it needs to collect information about how those meta boxes are registered, and format it for the block editor to make use of.

See #45112.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/5.0/src/wp-admin/includes/post.php

    r43764 r43778  
    18911891    }
    18921892
     1893    // We're in the meta box loader, so don't use the block editor.
     1894    if ( isset( $_GET['meta-box-loader'] ) ) {
     1895        check_admin_referer( 'meta-box-loader' );
     1896        return false;
     1897    }
     1898
    18931899    $use_block_editor = use_block_editor_for_post_type( $post->post_type );
    18941900
     
    20182024    return $blocks;
    20192025}
     2026
     2027/**
     2028 * Renders the meta boxes forms.
     2029 *
     2030 * @since 5.0.0
     2031 */
     2032function the_block_editor_meta_boxes() {
     2033    global $post, $current_screen, $wp_meta_boxes;
     2034
     2035    // Handle meta box state.
     2036    $_original_meta_boxes = $wp_meta_boxes;
     2037
     2038    /**
     2039     * Fires right before the meta boxes are rendered.
     2040     *
     2041     * This allows for the filtering of meta box data, that should already be
     2042     * present by this point. Do not use as a means of adding meta box data.
     2043     *
     2044     * @since 5.0.0
     2045     *
     2046     * @param array $wp_meta_boxes Global meta box state.
     2047     */
     2048    $wp_meta_boxes = apply_filters( 'filter_block_editor_meta_boxes', $wp_meta_boxes );
     2049    $locations     = array( 'side', 'normal', 'advanced' );
     2050    $priorities    = array( 'high', 'sorted', 'core', 'default', 'low' );
     2051
     2052    // Render meta boxes.
     2053    ?>
     2054    <form class="metabox-base-form">
     2055    <?php the_block_editor_meta_box_post_form_hidden_fields( $post ); ?>
     2056    </form>
     2057    <?php foreach ( $locations as $location ) : ?>
     2058        <form class="metabox-location-<?php echo esc_attr( $location ); ?>">
     2059            <div id="poststuff" class="sidebar-open">
     2060                <div id="postbox-container-2" class="postbox-container">
     2061                    <?php
     2062                    do_meta_boxes(
     2063                        $current_screen,
     2064                        $location,
     2065                        $post
     2066                    );
     2067                    ?>
     2068                </div>
     2069            </div>
     2070        </form>
     2071    <?php endforeach; ?>
     2072    <?php
     2073
     2074    $meta_boxes_per_location = array();
     2075    foreach ( $locations as $location ) {
     2076        $meta_boxes_per_location[ $location ] = array();
     2077
     2078        if ( ! isset( $wp_meta_boxes[ $current_screen->id ][ $location ] ) ) {
     2079            continue;
     2080        }
     2081
     2082        foreach ( $priorities as $priority ) {
     2083            if ( ! isset( $wp_meta_boxes[ $current_screen->id ][ $location ][ $priority ] ) ) {
     2084                continue;
     2085            }
     2086
     2087            $meta_boxes = (array) $wp_meta_boxes[ $current_screen->id ][ $location ][ $priority ];
     2088            foreach ( $meta_boxes as $meta_box ) {
     2089                if ( ! empty( $meta_box['title'] ) ) {
     2090                    $meta_boxes_per_location[ $location ][] = array(
     2091                        'id'    => $meta_box['id'],
     2092                        'title' => $meta_box['title'],
     2093                    );
     2094                }
     2095            }
     2096        }
     2097    }
     2098
     2099    /**
     2100     * Sadly we probably can not add this data directly into editor settings.
     2101     *
     2102     * Some meta boxes need admin_head to fire for meta box registry.
     2103     * admin_head fires after admin_enqueue_scripts, which is where we create our
     2104     * editor instance.
     2105     */
     2106    $script = 'window._wpLoadBlockEditor.then( function() {
     2107        wp.data.dispatch( \'core/edit-post\' ).setAvailableMetaBoxesPerLocation( ' . wp_json_encode( $meta_boxes_per_location ) . ' );
     2108    } );';
     2109
     2110    wp_add_inline_script( 'wp-edit-post', $script );
     2111
     2112    /**
     2113     * When `wp-edit-post` is output in the `<head>`, the inline script needs to be manually printed. Otherwise,
     2114     * meta boxes will not display because inline scripts for `wp-edit-post` will not be printed again after this point.
     2115     */
     2116    if ( wp_script_is( 'wp-edit-post', 'done' ) ) {
     2117        printf( "<script type='text/javascript'>\n%s\n</script>\n", trim( $script ) );
     2118    }
     2119
     2120    // Reset meta box data.
     2121    $wp_meta_boxes = $_original_meta_boxes;
     2122}
     2123
     2124/**
     2125 * Renders the hidden form required for the meta boxes form.
     2126 *
     2127 * @since 5.0.0
     2128 *
     2129 * @param WP_Post $post Current post object.
     2130 */
     2131function the_block_editor_meta_box_post_form_hidden_fields( $post ) {
     2132    $form_extra = '';
     2133    if ( 'auto-draft' === $post->post_status ) {
     2134        $form_extra .= "<input type='hidden' id='auto_draft' name='auto_draft' value='1' />";
     2135    }
     2136    $form_action  = 'editpost';
     2137    $nonce_action = 'update-post_' . $post->ID;
     2138    $form_extra  .= "<input type='hidden' id='post_ID' name='post_ID' value='" . esc_attr( $post->ID ) . "' />";
     2139    $referer      = wp_get_referer();
     2140    $current_user = wp_get_current_user();
     2141    $user_id      = $current_user->ID;
     2142    wp_nonce_field( $nonce_action );
     2143    ?>
     2144    <input type="hidden" id="user-id" name="user_ID" value="<?php echo (int) $user_id; ?>" />
     2145    <input type="hidden" id="hiddenaction" name="action" value="<?php echo esc_attr( $form_action ); ?>" />
     2146    <input type="hidden" id="originalaction" name="originalaction" value="<?php echo esc_attr( $form_action ); ?>" />
     2147    <input type="hidden" id="post_type" name="post_type" value="<?php echo esc_attr( $post->post_type ); ?>" />
     2148    <input type="hidden" id="original_post_status" name="original_post_status" value="<?php echo esc_attr( $post->post_status ); ?>" />
     2149    <input type="hidden" id="referredby" name="referredby" value="<?php echo $referer ? esc_url( $referer ) : ''; ?>" />
     2150
     2151    <?php
     2152    if ( 'draft' !== get_post_status( $post ) ) {
     2153        wp_original_referer_field( true, 'previous' );
     2154    }
     2155    echo $form_extra;
     2156    wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
     2157    wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
     2158    // Permalink title nonce.
     2159    wp_nonce_field( 'samplepermalink', 'samplepermalinknonce', false );
     2160}
Note: See TracChangeset for help on using the changeset viewer.