From f1f1b37f2c0382df1e82dcaf04e97276f352dc9c Mon Sep 17 00:00:00 2001 From: Grzegorz Ziolkowski Date: Thu, 14 Jul 2022 14:55:24 +0200 Subject: [PATCH] Blocks: Add new `renderTemplate` field to `block.json` --- lib/blocks.php | 2 +- lib/compat/wordpress-6.1/blocks.php | 41 +++++ .../block-library/src/archives/block.json | 3 +- packages/block-library/src/archives/index.php | 168 ++++++++---------- schemas/json/block.json | 4 + 5 files changed, 121 insertions(+), 97 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 18a9eb34ff166..cfb623dd95687 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -14,6 +14,7 @@ function gutenberg_reregister_core_block_types() { $blocks_dirs = array( __DIR__ . '/../build/block-library/blocks/' => array( 'block_folders' => array( + 'archives', 'audio', 'button', 'buttons', @@ -45,7 +46,6 @@ function gutenberg_reregister_core_block_types() { 'embed', ), 'block_names' => array( - 'archives.php' => 'core/archives', 'avatar.php' => 'core/avatar', 'block.php' => 'core/block', 'calendar.php' => 'core/calendar', diff --git a/lib/compat/wordpress-6.1/blocks.php b/lib/compat/wordpress-6.1/blocks.php index ed41c5aa8cd09..bed61799d05d2 100644 --- a/lib/compat/wordpress-6.1/blocks.php +++ b/lib/compat/wordpress-6.1/blocks.php @@ -178,3 +178,44 @@ function gutenberg_block_type_metadata_multiple_view_scripts( $metadata ) { return $metadata; } add_filter( 'block_type_metadata', 'gutenberg_block_type_metadata_multiple_view_scripts' ); + +/** + * Register render template for core blocks if handling is missing in WordPress core. + * + * @since 6.1.0 + * + * @param array $settings Array of determined settings for registering a block type. + * @param array $metadata Metadata provided for registering a block type. + * + * @return array Array of settings for registering a block type. + */ +function gutenberg_block_type_metadata_render_template( $settings, $metadata ) { + if ( empty( $metadata['renderTemplate'] ) || isset( $settings['render_callback'] ) ) { + return $settings; + } + + $template_path = wp_normalize_path( + realpath( + dirname( $metadata['file'] ) . '/' . + remove_block_asset_path_prefix( $metadata['renderTemplate'] ) + ) + ); + + /** + * Renders the block on the server. + * + * @param array $attributes Block attributes. + * @param string $content Block default content. + * @param WP_Block $block Block instance. + * + * @return string Returns the block content. + */ + $settings['render_callback'] = function( $attributes, $content, $block ) use ( $template_path ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable + ob_start(); + require $template_path; + return ob_get_clean(); + }; + + return $settings; +} +add_filter( 'block_type_metadata_settings', 'gutenberg_block_type_metadata_render_template', 10, 2 ); diff --git a/packages/block-library/src/archives/block.json b/packages/block-library/src/archives/block.json index e75aafbbe0327..c622b2641ba5b 100644 --- a/packages/block-library/src/archives/block.json +++ b/packages/block-library/src/archives/block.json @@ -24,5 +24,6 @@ "align": true, "html": false }, - "editorStyle": "wp-block-archives-editor" + "editorStyle": "wp-block-archives-editor", + "renderTemplate": "file:../archives.php" } diff --git a/packages/block-library/src/archives/index.php b/packages/block-library/src/archives/index.php index 220f0f3229b1b..04927fabe6197 100644 --- a/packages/block-library/src/archives/index.php +++ b/packages/block-library/src/archives/index.php @@ -5,115 +5,93 @@ * @package WordPress */ -/** - * Renders the `core/archives` block on server. - * - * @see WP_Widget_Archives - * - * @param array $attributes The block attributes. - * - * @return string Returns the post content with archives added. - */ -function render_block_core_archives( $attributes ) { - $show_post_count = ! empty( $attributes['showPostCounts'] ); - $type = isset( $attributes['type'] ) ? $attributes['type'] : 'monthly'; - $class = ''; - - if ( ! empty( $attributes['displayAsDropdown'] ) ) { - - $class .= ' wp-block-archives-dropdown'; - - $dropdown_id = wp_unique_id( 'wp-block-archives-' ); - $title = __( 'Archives' ); - - /** This filter is documented in wp-includes/widgets/class-wp-widget-archives.php */ - $dropdown_args = apply_filters( - 'widget_archives_dropdown_args', - array( - 'type' => $type, - 'format' => 'option', - 'show_post_count' => $show_post_count, - ) - ); - - $dropdown_args['echo'] = 0; - - $archives = wp_get_archives( $dropdown_args ); - - $classnames = esc_attr( $class ); - - $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) ); - - switch ( $dropdown_args['type'] ) { - case 'yearly': - $label = __( 'Select Year' ); - break; - case 'monthly': - $label = __( 'Select Month' ); - break; - case 'daily': - $label = __( 'Select Day' ); - break; - case 'weekly': - $label = __( 'Select Week' ); - break; - default: - $label = __( 'Select Post' ); - break; - } - - $block_content = ' - '; - - return sprintf( - '
%2$s
', - $wrapper_attributes, - $block_content - ); - } +$show_post_count = ! empty( $attributes['showPostCounts'] ); +$type = isset( $attributes['type'] ) ? $attributes['type'] : 'monthly'; +$class = ''; - $class .= ' wp-block-archives-list'; +if ( ! empty( $attributes['displayAsDropdown'] ) ) { + + $class .= ' wp-block-archives-dropdown'; + + $dropdown_id = wp_unique_id( 'wp-block-archives-' ); + $title = __( 'Archives' ); /** This filter is documented in wp-includes/widgets/class-wp-widget-archives.php */ - $archives_args = apply_filters( - 'widget_archives_args', + $dropdown_args = apply_filters( + 'widget_archives_dropdown_args', array( 'type' => $type, + 'format' => 'option', 'show_post_count' => $show_post_count, ) ); - $archives_args['echo'] = 0; - - $archives = wp_get_archives( $archives_args ); - - $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $class ) ); - - if ( empty( $archives ) ) { - return sprintf( - '
%2$s
', - $wrapper_attributes, - __( 'No archives to show.' ) - ); + $dropdown_args['echo'] = 0; + + $archives = wp_get_archives( $dropdown_args ); + + $classnames = esc_attr( $class ); + + $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) ); + + switch ( $dropdown_args['type'] ) { + case 'yearly': + $label = __( 'Select Year' ); + break; + case 'monthly': + $label = __( 'Select Month' ); + break; + case 'daily': + $label = __( 'Select Day' ); + break; + case 'weekly': + $label = __( 'Select Week' ); + break; + default: + $label = __( 'Select Post' ); + break; } - return sprintf( - '', + $block_content = ' +'; + + echo sprintf( + '
%2$s
', $wrapper_attributes, - $archives + $block_content ); + return; } -/** - * Register archives block. - */ -function register_block_core_archives() { - register_block_type_from_metadata( - __DIR__ . '/archives', - array( - 'render_callback' => 'render_block_core_archives', - ) +$class .= ' wp-block-archives-list'; + +/** This filter is documented in wp-includes/widgets/class-wp-widget-archives.php */ +$archives_args = apply_filters( + 'widget_archives_args', + array( + 'type' => $type, + 'show_post_count' => $show_post_count, + ) +); + +$archives_args['echo'] = 0; + +$archives = wp_get_archives( $archives_args ); + +$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $class ) ); + +if ( empty( $archives ) ) { + echo sprintf( + '
%2$s
', + $wrapper_attributes, + __( 'No archives to show.' ) ); + return; } -add_action( 'init', 'register_block_core_archives' ); + +echo sprintf( + '', + $wrapper_attributes, + $archives +); diff --git a/schemas/json/block.json b/schemas/json/block.json index fb1aa5ce49ff0..80fff0da9f903 100644 --- a/schemas/json/block.json +++ b/schemas/json/block.json @@ -482,6 +482,10 @@ } } ] + }, + "renderTemplate": { + "type": "string", + "description": "Template file loaded on the server when rendering a block." } }, "required": [ "name", "title" ],