Make WordPress Core

Changeset 44131


Ignore:
Timestamp:
12/14/2018 01:10:20 AM (6 years 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.

Merges [43778] from the 5.0 branch to trunk.

See #45112.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/wp-admin/includes/post.php

    r44124 r44131  
    20372037
    20382038    if ( ! $post ) {
     2039        return false;
     2040    }
     2041
     2042    // We're in the meta box loader, so don't use the block editor.
     2043    if ( isset( $_GET['meta-box-loader'] ) ) {
     2044        check_admin_referer( 'meta-box-loader' );
    20392045        return false;
    20402046    }
     
    21672173    return $blocks;
    21682174}
     2175
     2176/**
     2177 * Renders the meta boxes forms.
     2178 *
     2179 * @since 5.0.0
     2180 */
     2181function the_block_editor_meta_boxes() {
     2182    global $post, $current_screen, $wp_meta_boxes;
     2183
     2184    // Handle meta box state.
     2185    $_original_meta_boxes = $wp_meta_boxes;
     2186
     2187    /**
     2188     * Fires right before the meta boxes are rendered.
     2189     *
     2190     * This allows for the filtering of meta box data, that should already be
     2191     * present by this point. Do not use as a means of adding meta box data.
     2192     *
     2193     * @since 5.0.0
     2194     *
     2195     * @param array $wp_meta_boxes Global meta box state.
     2196     */
     2197    $wp_meta_boxes = apply_filters( 'filter_block_editor_meta_boxes', $wp_meta_boxes );
     2198    $locations     = array( 'side', 'normal', 'advanced' );
     2199    $priorities    = array( 'high', 'sorted', 'core', 'default', 'low' );
     2200
     2201    // Render meta boxes.
     2202    ?>
     2203    <form class="metabox-base-form">
     2204    <?php the_block_editor_meta_box_post_form_hidden_fields( $post ); ?>
     2205    </form>
     2206    <?php foreach ( $locations as $location ) : ?>
     2207        <form class="metabox-location-<?php echo esc_attr( $location ); ?>">
     2208            <div id="poststuff" class="sidebar-open">
     2209                <div id="postbox-container-2" class="postbox-container">
     2210                    <?php
     2211                    do_meta_boxes(
     2212                        $current_screen,
     2213                        $location,
     2214                        $post
     2215                    );
     2216                    ?>
     2217                </div>
     2218            </div>
     2219        </form>
     2220    <?php endforeach; ?>
     2221    <?php
     2222
     2223    $meta_boxes_per_location = array();
     2224    foreach ( $locations as $location ) {
     2225        $meta_boxes_per_location[ $location ] = array();
     2226
     2227        if ( ! isset( $wp_meta_boxes[ $current_screen->id ][ $location ] ) ) {
     2228            continue;
     2229        }
     2230
     2231        foreach ( $priorities as $priority ) {
     2232            if ( ! isset( $wp_meta_boxes[ $current_screen->id ][ $location ][ $priority ] ) ) {
     2233                continue;
     2234            }
     2235
     2236            $meta_boxes = (array) $wp_meta_boxes[ $current_screen->id ][ $location ][ $priority ];
     2237            foreach ( $meta_boxes as $meta_box ) {
     2238                if ( ! empty( $meta_box['title'] ) ) {
     2239                    $meta_boxes_per_location[ $location ][] = array(
     2240                        'id'    => $meta_box['id'],
     2241                        'title' => $meta_box['title'],
     2242                    );
     2243                }
     2244            }
     2245        }
     2246    }
     2247
     2248    /**
     2249     * Sadly we probably can not add this data directly into editor settings.
     2250     *
     2251     * Some meta boxes need admin_head to fire for meta box registry.
     2252     * admin_head fires after admin_enqueue_scripts, which is where we create our
     2253     * editor instance.
     2254     */
     2255    $script = 'window._wpLoadBlockEditor.then( function() {
     2256        wp.data.dispatch( \'core/edit-post\' ).setAvailableMetaBoxesPerLocation( ' . wp_json_encode( $meta_boxes_per_location ) . ' );
     2257    } );';
     2258
     2259    wp_add_inline_script( 'wp-edit-post', $script );
     2260
     2261    /**
     2262     * When `wp-edit-post` is output in the `<head>`, the inline script needs to be manually printed. Otherwise,
     2263     * meta boxes will not display because inline scripts for `wp-edit-post` will not be printed again after this point.
     2264     */
     2265    if ( wp_script_is( 'wp-edit-post', 'done' ) ) {
     2266        printf( "<script type='text/javascript'>\n%s\n</script>\n", trim( $script ) );
     2267    }
     2268
     2269    // Reset meta box data.
     2270    $wp_meta_boxes = $_original_meta_boxes;
     2271}
     2272
     2273/**
     2274 * Renders the hidden form required for the meta boxes form.
     2275 *
     2276 * @since 5.0.0
     2277 *
     2278 * @param WP_Post $post Current post object.
     2279 */
     2280function the_block_editor_meta_box_post_form_hidden_fields( $post ) {
     2281    $form_extra = '';
     2282    if ( 'auto-draft' === $post->post_status ) {
     2283        $form_extra .= "<input type='hidden' id='auto_draft' name='auto_draft' value='1' />";
     2284    }
     2285    $form_action  = 'editpost';
     2286    $nonce_action = 'update-post_' . $post->ID;
     2287    $form_extra  .= "<input type='hidden' id='post_ID' name='post_ID' value='" . esc_attr( $post->ID ) . "' />";
     2288    $referer      = wp_get_referer();
     2289    $current_user = wp_get_current_user();
     2290    $user_id      = $current_user->ID;
     2291    wp_nonce_field( $nonce_action );
     2292    ?>
     2293    <input type="hidden" id="user-id" name="user_ID" value="<?php echo (int) $user_id; ?>" />
     2294    <input type="hidden" id="hiddenaction" name="action" value="<?php echo esc_attr( $form_action ); ?>" />
     2295    <input type="hidden" id="originalaction" name="originalaction" value="<?php echo esc_attr( $form_action ); ?>" />
     2296    <input type="hidden" id="post_type" name="post_type" value="<?php echo esc_attr( $post->post_type ); ?>" />
     2297    <input type="hidden" id="original_post_status" name="original_post_status" value="<?php echo esc_attr( $post->post_status ); ?>" />
     2298    <input type="hidden" id="referredby" name="referredby" value="<?php echo $referer ? esc_url( $referer ) : ''; ?>" />
     2299
     2300    <?php
     2301    if ( 'draft' !== get_post_status( $post ) ) {
     2302        wp_original_referer_field( true, 'previous' );
     2303    }
     2304    echo $form_extra;
     2305    wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
     2306    wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
     2307    // Permalink title nonce.
     2308    wp_nonce_field( 'samplepermalink', 'samplepermalinknonce', false );
     2309}
Note: See TracChangeset for help on using the changeset viewer.