Make WordPress Core


Ignore:
Timestamp:
05/24/2021 09:20:31 PM (5 years ago)
Author:
gziolo
Message:

Editor: Use the block editor context in filters that used the editor name

Follow-up for [50956].
Props azaozz, chrisvanpatten, timothyblynjacobs, youknowriad.
Fixes #52920.

File:
1 edited

Legend:

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

    r50973 r50983  
    5959 *
    6060 * @since 5.0.0
    61  *
    62  * @param string|WP_Post $editor_name_or_post The name of the editor (e.g. 'post-editor')
    63  *                                            or the post object.
     61 * @since 5.8.0 It is possible to pass the block editor context as param.
     62 *
     63 * @param WP_Post|WP_Block_Editor_Context $post_or_block_editor_context The current post object or
     64 *                                                                      the block editor context.
    6465 *
    6566 * @return array[] Array of categories for block types.
    6667 */
    67 function get_block_categories( $editor_name_or_post ) {
    68     // Assume the post editor when the WP_Post object passed.
    69     $editor_name      = is_object( $editor_name_or_post ) ? 'post-editor' : $editor_name_or_post;
    70     $block_categories = get_default_block_categories();
     68function get_block_categories( $post_or_block_editor_context ) {
     69    $block_categories     = get_default_block_categories();
     70    $block_editor_context = $post_or_block_editor_context instanceof WP_Post ?
     71        new WP_Block_Editor_Context(
     72            array(
     73                'post' => $post_or_block_editor_context,
     74            )
     75        ) : $post_or_block_editor_context;
    7176
    7277    /**
     
    7580     * @since 5.8.0
    7681     *
    77      * @param array[] $block_categories Array of categories for block types.
    78      * @param string  $editor_name      The name of the editor, e.g. 'post-editor'.
     82     * @param array[]                 $block_categories    Array of categories for block types.
     83     * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
    7984     */
    80     $block_categories = apply_filters( 'block_categories_all', $block_categories, $editor_name );
    81     if ( 'post-editor' === $editor_name ) {
    82         $post = is_object( $editor_name_or_post ) ? $editor_name_or_post : get_post();
     85    $block_categories = apply_filters( 'block_categories_all', $block_categories, $block_editor_context );
     86    if ( ! empty( $block_editor_context->post ) ) {
     87        $post = $block_editor_context->post;
    8388
    8489        /**
     
    102107 * @since 5.8.0
    103108 *
    104  * @param string $editor_name The name of the editor (e.g. 'post-editor').
     109 * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
    105110 *
    106111 * @return bool|array Array of block type slugs, or boolean to enable/disable all.
    107112 */
    108 function get_allowed_block_types( $editor_name ) {
     113function get_allowed_block_types( $block_editor_context ) {
    109114    $allowed_block_types = true;
    110115
     
    116121     * @since 5.8.0
    117122     *
    118      * @param bool|array $allowed_block_types Array of block type slugs, or
    119      *                                        boolean to enable/disable all.
    120      * @param string     $editor_name         The name of the editor, e.g. 'post-editor'.
     123     * @param bool|array              $allowed_block_types Array of block type slugs, or
     124     *                                                      boolean to enable/disable all.
     125     * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
    121126     */
    122     $allowed_block_types = apply_filters( 'allowed_block_types_all', $allowed_block_types, $editor_name );
    123     if ( 'post-editor' === $editor_name ) {
    124         $post = get_post();
     127    $allowed_block_types = apply_filters( 'allowed_block_types_all', $allowed_block_types, $block_editor_context );
     128    if ( ! empty( $block_editor_context->post ) ) {
     129        $post = $block_editor_context->post;
    125130
    126131        /**
     
    225230
    226231/**
    227  * Returns the contextualized block editor settings settings for a selected editor type.
     232 * Returns the contextualized block editor settings settings for a selected editor context.
    228233 *
    229234 * @since 5.8.0
    230235 *
    231  * @param string $editor_name     The name of the editor (e.g. 'post-editor').
    232  * @param array  $custom_settings Optional custom settings to use with the editor type.
     236 * @param array                   $custom_settings      Custom settings to use with the given editor type.
     237 * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
    233238 *
    234239 * @return array The contextualized block editor settings.
    235240 */
    236 function get_block_editor_settings( $editor_name, $custom_settings = array() ) {
     241function get_block_editor_settings( array $custom_settings, $block_editor_context ) {
    237242    $editor_settings = array_merge(
    238243        get_default_block_editor_settings(),
    239244        array(
    240             'allowedBlockTypes' => get_allowed_block_types( $editor_name ),
    241             'blockCategories'   => get_block_categories( $editor_name ),
     245            'allowedBlockTypes' => get_allowed_block_types( $block_editor_context ),
     246            'blockCategories'   => get_block_categories( $block_editor_context ),
    242247        ),
    243248        $custom_settings
     
    302307     * @since 5.8.0
    303308     *
    304      * @param array  $editor_settings Default editor settings.
    305      * @param string $editor_name     The name of the editor, e.g. 'post-editor'.
     309     * @param array                   $editor_settings      Default editor settings.
     310     * @param WP_Block_Editor_Context $block_editor_context The current block editor context.
    306311     */
    307     $editor_settings = apply_filters( 'block_editor_settings_all', $editor_settings, $editor_name );
    308     if ( 'post-editor' === $editor_name ) {
    309         $post = get_post();
     312    $editor_settings = apply_filters( 'block_editor_settings_all', $editor_settings, $block_editor_context );
     313    if ( ! empty( $block_editor_context->post ) ) {
     314        $post = $block_editor_context->post;
    310315
    311316        /**
Note: See TracChangeset for help on using the changeset viewer.