Make WordPress Core


Ignore:
Timestamp:
06/21/2023 04:21:04 AM (3 years ago)
Author:
isabel_brison
Message:

Editor: add Post Content attributes to block editor settings.

Adds a new block editor setting containing the Post Content block attributes, if they exist.

Props audrasjb, spacedmonkey, jeremyfelt, mukesh27, flixos90, andrewserong.
Fixes #58534.

File:
1 edited

Legend:

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

    r55438 r55955  
    360360        'scripts' => $scripts,
    361361    );
     362}
     363
     364/**
     365 * Finds the first occurrence of a specific block in an array of blocks.
     366 *
     367 * @since 6.3.0
     368 *
     369 * @param array  $blocks     Array of blocks.
     370 * @param string $block_name Name of the block to find.
     371 * @return array Found block, or empty array if none found.
     372 */
     373function wp_get_first_block( $blocks, $block_name ) {
     374    foreach ( $blocks as $block ) {
     375        if ( $block_name === $block['blockName'] ) {
     376            return $block;
     377        }
     378        if ( ! empty( $block['innerBlocks'] ) ) {
     379            $found_block = wp_get_first_block( $block['innerBlocks'], $block_name );
     380
     381            if ( ! empty( $found_block ) ) {
     382                return $found_block;
     383            }
     384        }
     385    }
     386
     387    return array();
     388}
     389
     390/**
     391 * Retrieves Post Content block attributes from the current post template.
     392 *
     393 * @since 6.3.0
     394 * @access private
     395 *
     396 * @global int $post_ID
     397 *
     398 * @return array Post Content block attributes or empty array if they don't exist.
     399 */
     400function wp_get_post_content_block_attributes() {
     401    global $post_ID;
     402
     403    $is_block_theme = wp_is_block_theme();
     404
     405    if ( ! $is_block_theme || ! $post_ID ) {
     406        return array();
     407    }
     408
     409    $template_slug = get_page_template_slug( $post_ID );
     410
     411    if ( ! $template_slug ) {
     412        $post_slug      = 'singular';
     413        $page_slug      = 'singular';
     414        $template_types = get_block_templates();
     415
     416        foreach ( $template_types as $template_type ) {
     417            if ( 'page' === $template_type->slug ) {
     418                $page_slug = 'page';
     419            }
     420            if ( 'single' === $template_type->slug ) {
     421                $post_slug = 'single';
     422            }
     423        }
     424
     425        $what_post_type = get_post_type( $post_ID );
     426        switch ( $what_post_type ) {
     427            case 'page':
     428                $template_slug = $page_slug;
     429                break;
     430            default:
     431                $template_slug = $post_slug;
     432                break;
     433        }
     434    }
     435
     436    $current_template = get_block_templates( array( 'slug__in' => array( $template_slug ) ) );
     437
     438    if ( ! empty( $current_template ) ) {
     439        $template_blocks    = parse_blocks( $current_template[0]->content );
     440        $post_content_block = wp_get_first_block( $template_blocks, 'core/post-content' );
     441
     442        if ( ! empty( $post_content_block['attrs'] ) ) {
     443            return $post_content_block['attrs'];
     444        }
     445    }
     446
     447    return array();
    362448}
    363449
     
    529615        ),
    530616    );
     617
     618    $post_content_block_attributes = wp_get_post_content_block_attributes();
     619
     620    if ( ! empty( $post_content_block_attributes ) ) {
     621        $editor_settings['postContentAttributes'] = $post_content_block_attributes;
     622    }
    531623
    532624    /**
Note: See TracChangeset for help on using the changeset viewer.