Make WordPress Core

Changeset 48224


Ignore:
Timestamp:
06/30/2020 11:02:22 AM (4 years ago)
Author:
gziolo
Message:

Editor: Introduce block context

Backports a new block context feature from Gutenberg. The purpose of this feature is to be able to establish values in a block hierarchy which can be consumed by blocks anywhere lower in the same hierarchy. These values can be established either by the framework, or by other blocks which provide these values. See documentation: https://github.com/WordPress/gutenberg/blob/master/docs/designers-developers/developers/block-api/block-context.md

Props aduth, epiqueras.
Fixes #49927.

Location:
trunk
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks.php

    r48185 r48224  
    650650 * @since 5.0.0
    651651 *
    652  * @global WP_Post $post The post to edit.
    653  *
    654  * @param array $block A single parsed block object.
     652 * @global WP_Post  $post         The post to edit.
     653 * @global WP_Query $wp_the_query WordPress Query object.
     654 *
     655 * @param array $parsed_block A single parsed block object.
    655656 * @return string String of rendered HTML.
    656657 */
    657 function render_block( $block ) {
    658     global $post;
     658function render_block( $parsed_block ) {
     659    global $post, $wp_query;
    659660
    660661    /**
     
    663664     * @since 5.1.0
    664665     *
    665      * @param string|null $pre_render The pre-rendered content. Default null.
    666      * @param array       $block      The block being rendered.
     666     * @param string|null $pre_render   The pre-rendered content. Default null.
     667     * @param array       $parsed_block The block being rendered.
    667668     */
    668     $pre_render = apply_filters( 'pre_render_block', null, $block );
     669    $pre_render = apply_filters( 'pre_render_block', null, $parsed_block );
    669670    if ( ! is_null( $pre_render ) ) {
    670671        return $pre_render;
    671672    }
    672673
    673     $source_block = $block;
     674    $source_block = $parsed_block;
    674675
    675676    /**
     
    678679     * @since 5.1.0
    679680     *
    680      * @param array $block        The block being rendered.
    681      * @param array $source_block An un-modified copy of $block, as it appeared in the source content.
     681     * @param array $parsed_block The block being rendered.
     682     * @param array $source_block An un-modified copy of $parsed_block, as it appeared in the source content.
    682683     */
    683     $block = apply_filters( 'render_block_data', $block, $source_block );
    684 
    685     $block_type    = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
    686     $is_dynamic    = $block['blockName'] && null !== $block_type && $block_type->is_dynamic();
    687     $block_content = '';
    688     $index         = 0;
    689 
    690     foreach ( $block['innerContent'] as $chunk ) {
    691         $block_content .= is_string( $chunk ) ? $chunk : render_block( $block['innerBlocks'][ $index++ ] );
    692     }
    693 
    694     if ( ! is_array( $block['attrs'] ) ) {
    695         $block['attrs'] = array();
    696     }
    697 
    698     if ( $is_dynamic ) {
    699         $global_post   = $post;
    700         $block_content = $block_type->render( $block['attrs'], $block_content );
    701         $post          = $global_post;
     684    $parsed_block = apply_filters( 'render_block_data', $parsed_block, $source_block );
     685
     686    $context = array();
     687
     688    if ( ! empty( $post ) ) {
     689        $context['postId'] = $post->ID;
     690
     691        /*
     692        * The `postType` context is largely unnecessary server-side, since the
     693        * ID is usually sufficient on its own. That being said, since a block's
     694        * manifest is expected to be shared between the server and the client,
     695        * it should be included to consistently fulfill the expectation.
     696        */
     697        $context['postType'] = $post->post_type;
     698    }
     699
     700    if ( isset( $wp_query->tax_query->queried_terms['category'] ) ) {
     701        $context['query'] = array( 'categoryIds' => array() );
     702        foreach ( $wp_query->tax_query->queried_terms['category']['terms'] as $category_slug_or_id ) {
     703            $context['query']['categoryIds'][] = 'slug' === $wp_query->tax_query->queried_terms['category']['field'] ? get_cat_ID( $category_slug_or_id ) : $category_slug_or_id;
     704        }
    702705    }
    703706
    704707    /**
    705      * Filters the content of a single block.
    706      *
    707      * @since 5.0.0
    708      *
    709      * @param string $block_content The block content about to be appended.
    710      * @param array  $block         The full block, including name and attributes.
     708     * Filters the default context provided to a rendered block.
     709     *
     710     * @since 5.5.0
     711     *
     712     * @param array $context      Default context.
     713     * @param array $parsed_block Block being rendered, filtered by `render_block_data`.
    711714     */
    712     return apply_filters( 'render_block', $block_content, $block );
     715    $context = apply_filters( 'render_block_context', $context, $parsed_block );
     716
     717    $block = new WP_Block( $parsed_block, $context );
     718
     719    return $block->render();
    713720}
    714721
  • trunk/src/wp-includes/class-wp-block.php

    r48159 r48224  
    227227        }
    228228
    229         /** This filter is documented in src/wp-includes/blocks.php */
     229        /**
     230         * Filters the content of a single block.
     231         *
     232         * @since 5.0.0
     233         *
     234         * @param string $block_content The block content about to be appended.
     235         * @param array  $block         The full block, including name and attributes.
     236         */
    230237        return apply_filters( 'render_block', $block_content, $this->parsed_block );
    231238    }
Note: See TracChangeset for help on using the changeset viewer.