Make WordPress Core

Changeset 55955


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.

Location:
trunk
Files:
1 added
2 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        /**
  • trunk/tests/phpunit/tests/blocks/editor.php

    r55822 r55955  
    2828                $wp_rest_server = new Spy_REST_Server();
    2929                do_action( 'rest_api_init', $wp_rest_server );
     30
     31                global $post_ID;
     32                $post_ID = 1;
    3033        }
    3134
     
    3437                global $wp_rest_server;
    3538                $wp_rest_server = null;
     39                global $post_ID;
     40                $post_ID = null;
    3641                parent::tear_down();
    3742        }
     
    398403
    399404        /**
     405         * @ticket 58534
     406         */
     407        public function test_wp_get_first_block() {
     408                $block_name               = 'core/paragraph';
     409                $blocks                   = array(
     410                        array(
     411                                'blockName' => 'core/image',
     412                        ),
     413                        array(
     414                                'blockName' => $block_name,
     415                                'attrs'     => array(
     416                                        'content' => 'Hello World!',
     417                                ),
     418                        ),
     419                        array(
     420                                'blockName' => 'core/heading',
     421                        ),
     422                        array(
     423                                'blockName' => $block_name,
     424                        ),
     425                );
     426                $blocks_with_no_paragraph = array(
     427                        array(
     428                                'blockName' => 'core/image',
     429                        ),
     430                        array(
     431                                'blockName' => 'core/heading',
     432                        ),
     433                );
     434
     435                $this->assertSame( $blocks[1], wp_get_first_block( $blocks, $block_name ) );
     436
     437                $this->assertSame( array(), wp_get_first_block( $blocks_with_no_paragraph, $block_name ) );
     438        }
     439
     440        /**
     441         * @ticket 58534
     442         */
     443        public function test_wp_get_post_content_block_attributes() {
     444                $attributes_with_layout = array(
     445                        'layout' => array(
     446                                'type' => 'constrained',
     447                        ),
     448                );
     449                // With no block theme, expect an empty array.
     450                $this->assertSame( array(), wp_get_post_content_block_attributes() );
     451
     452                switch_theme( 'block-theme' );
     453
     454                $this->assertSame( $attributes_with_layout, wp_get_post_content_block_attributes() );
     455        }
     456
     457        /**
    400458         * @ticket 53458
    401459         */
     
    457515                // settings.spacing.customPadding
    458516                $this->assertTrue( $settings['enableCustomSpacing'] );
     517                // settings.postContentAttributes
     518                $this->assertSameSets(
     519                        array(
     520                                'layout' => array(
     521                                        'type' => 'constrained',
     522                                ),
     523                        ),
     524                        $settings['postContentAttributes']
     525                );
    459526
    460527                switch_theme( WP_DEFAULT_THEME );
Note: See TracChangeset for help on using the changeset viewer.