Skip to content

Commit

Permalink
Blocks: Add new renderTemplate field to block.json
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Jul 14, 2022
1 parent d3d3ed4 commit f1f1b37
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 97 deletions.
2 changes: 1 addition & 1 deletion lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
41 changes: 41 additions & 0 deletions lib/compat/wordpress-6.1/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
3 changes: 2 additions & 1 deletion packages/block-library/src/archives/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
"align": true,
"html": false
},
"editorStyle": "wp-block-archives-editor"
"editorStyle": "wp-block-archives-editor",
"renderTemplate": "file:../archives.php"
}
168 changes: 73 additions & 95 deletions packages/block-library/src/archives/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<label for="' . esc_attr( $dropdown_id ) . '">' . esc_html( $title ) . '</label>
<select id="' . esc_attr( $dropdown_id ) . '" name="archive-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
<option value="">' . esc_html( $label ) . '</option>' . $archives . '</select>';

return sprintf(
'<div %1$s>%2$s</div>',
$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(
'<div %1$s>%2$s</div>',
$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(
'<ul %1$s>%2$s</ul>',
$block_content = '<label for="' . esc_attr( $dropdown_id ) . '">' . esc_html( $title ) . '</label>
<select id="' . esc_attr( $dropdown_id ) . '" name="archive-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
<option value="">' . esc_html( $label ) . '</option>' . $archives . '</select>';

echo sprintf(
'<div %1$s>%2$s</div>',
$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(
'<div %1$s>%2$s</div>',
$wrapper_attributes,
__( 'No archives to show.' )
);
return;
}
add_action( 'init', 'register_block_core_archives' );

echo sprintf(
'<ul %1$s>%2$s</ul>',
$wrapper_attributes,
$archives
);
4 changes: 4 additions & 0 deletions schemas/json/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,10 @@
}
}
]
},
"renderTemplate": {
"type": "string",
"description": "Template file loaded on the server when rendering a block."
}
},
"required": [ "name", "title" ],
Expand Down

0 comments on commit f1f1b37

Please sign in to comment.