Make WordPress Core

Changeset 54132


Ignore:
Timestamp:
09/12/2022 01:12:21 PM (4 years ago)
Author:
gziolo
Message:

Blocks: Add new render property in block.json for block types

New render field in block.json file that accepts a string value. It allows to pass a path to the PHP file that is going to be used to render the block on the server. Related PR in Gutenberg: ​https://github.com/WordPress/gutenberg/pull/42430.

Props spacedmonkey, luisherranz, welcher, noisysocks, matveb, fabiankaegy, aristath, zieladam.
Fixes #53148.

Location:
trunk
Files:
1 added
4 edited

Legend:

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

    r53892 r54132  
    236236 * @since 5.7.0 Added support for `textdomain` field and i18n handling for all translatable fields.
    237237 * @since 5.9.0 Added support for `variations` and `viewScript` fields.
     238 * @since 6.1.0 Added support for `render` field.
    238239 *
    239240 * @param string $file_or_folder Path to the JSON file with metadata definition for
    … …  
    344345                        'style'
    345346                );
     347        }
     348
     349        if ( ! empty( $metadata['render'] ) ) {
     350                $template_path = wp_normalize_path(
     351                        realpath(
     352                                dirname( $metadata['file'] ) . '/' .
     353                                remove_block_asset_path_prefix( $metadata['render'] )
     354                        )
     355                );
     356                if ( file_exists( $template_path ) ) {
     357                        /**
     358                         * Renders the block on the server.
     359                         *
     360                         * @since 6.1.0
     361                         *
     362                         * @param array    $attributes Block attributes.
     363                         * @param string   $content    Block default content.
     364                         * @param WP_Block $block      Block instance.
     365                         *
     366                         * @return string Returns the block content.
     367                         */
     368                        $settings['render_callback'] = function( $attributes, $content, $block ) use ( $template_path ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
     369                                ob_start();
     370                                require $template_path;
     371                                return ob_get_clean();
     372                        };
     373                }
    346374        }
    347375
  • trunk/tests/phpunit/data/blocks/notice/block.json

    r53718 r54132  
    2525        "attributes": {
    2626                "message": {
    27                         "type": "string",
    28                         "source": "html",
    29                         "selector": ".message"
     27                        "type": "string"
    3028                }
    3129        },
    … …  
    6260        "viewScript": "tests-notice-view-script",
    6361        "editorStyle": "tests-notice-editor-style",
    64         "style": "tests-notice-style"
     62        "style": "tests-notice-style",
     63        "render": "file:./render.php"
    6564}
  • trunk/tests/phpunit/tests/blocks/register.php

    r53858 r54132  
    390390                        array(
    391391                                'message' => array(
    392                                         'type'     => 'string',
    393                                         'source'   => 'html',
    394                                         'selector' => '.message',
     392                                        'type' => 'string',
    395393                                ),
    396394                                'lock'    => array( 'type' => 'object' ),
    … …  
    456454                        wp_normalize_path( wp_styles()->get_data( 'unit-tests-test-block-style', 'path' ) )
    457455                );
     456
     457                // @ticket 53148
     458                $this->assertIsCallable( $result->render_callback );
    458459        }
    459460
  • trunk/tests/phpunit/tests/blocks/render.php

    r53157 r54132  
    4848                        $registry->unregister( 'core/dynamic' );
    4949                }
     50                if ( $registry->is_registered( 'tests/notice' ) ) {
     51                        $registry->unregister( 'tests/notice' );
     52                }
    5053
    5154                parent::tear_down();
    … …  
    237240                );
    238241        }
     242
     243        /**
     244         * @ticket 53148
     245         */
     246        public function test_render_field_in_block_json() {
     247                $result = register_block_type(
     248                        DIR_TESTDATA . '/blocks/notice'
     249                );
     250
     251                $actual_content = do_blocks( '<!-- wp:tests/notice {"message":"Hello from the test"} --><!-- /wp:tests/notice -->' );
     252                $this->assertSame( '<p class="wp-block-tests-notice">Hello from the test</p>', trim( $actual_content ) );
     253        }
     254
    239255
    240256        /**
Note: See TracChangeset for help on using the changeset viewer.