Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Library: Update FSE blocks to use block context #21696

Merged
merged 8 commits into from
May 7, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

For features included in the Gutenberg plugin, the deprecation policy is intended to support backward compatibility for two minor plugin releases, when possible. Features and code included in a stable release of WordPress are not included in this deprecation timeline, and are instead subject to the [versioning policies of the WordPress project](https://make.wordpress.org/core/handbook/about/release-cycle/version-numbering/). The current deprecations are listed below and are grouped by _the version at which they will be removed completely_. If your plugin depends on these behaviors, you must update to the recommended alternative before the noted version.

## 8.3.0

- The PHP function `gutenberg_get_post_from_context` has been removed. Use [Block Context](https://github.com/WordPress/gutenberg/blob/master/docs/designers-developers/developers/block-api/block-context.md) instead.

## 5.5.0

- The PHP function `gutenberg_init` has been removed.
Expand Down
17 changes: 15 additions & 2 deletions lib/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ function gutenberg_get_post_from_context() {
if ( is_admin() || defined( 'REST_REQUEST' ) ) {
return null;
}

_deprecated_function( __FUNCTION__, '8.1.0' );

if ( ! in_the_loop() ) {
rewind_posts();
the_post();
Expand Down Expand Up @@ -204,7 +207,8 @@ function gutenberg_render_block_with_assigned_block_context( $pre_render, $parse

/** This filter is documented in src/wp-includes/blocks.php */
$parsed_block = apply_filters( 'render_block_data', $parsed_block, $source_block );
$context = array(

$context = array(
'postId' => $post->ID,

/*
Expand All @@ -215,7 +219,16 @@ function gutenberg_render_block_with_assigned_block_context( $pre_render, $parse
*/
'postType' => $post->post_type,
);
$block = new WP_Block( $parsed_block, $context );

/**
* Filters the default context provided to a rendered block.
*
* @param array $context Default context.
* @param array $parsed_block Block being rendered, filtered by `render_block_data`.
*/
$context = apply_filters( 'render_block_context', $context, $parsed_block );

$block = new WP_Block( $parsed_block, $context );

return $block->render();
}
Expand Down
25 changes: 25 additions & 0 deletions lib/template-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,28 @@ function gutenberg_template_loader_filter_block_editor_settings( $settings ) {
return $settings;
}
add_filter( 'block_editor_settings', 'gutenberg_template_loader_filter_block_editor_settings' );

/**
* Removes post details from block context when rendering a block template.
*
* @param array $context Default context.
*
* @return array Filtered context.
*/
function gutenberg_template_render_without_post_block_context( $context ) {
/*
* When loading a template or template part directly and not through a page
* that resolves it, the top-level post ID and type context get set to that
* of the template part. Templates are just the structure of a site, and
* they should not be available as post context because blocks like Post
* Content would recurse infinitely.
*/
if ( isset( $context['postType'] ) &&
( 'wp_template' === $context['postType'] || 'wp_template_part' === $context['postType'] ) ) {
unset( $context['postId'] );
unset( $context['postType'] );
}

return $context;
}
add_filter( 'render_block_context', 'gutenberg_template_render_without_post_block_context' );
3 changes: 2 additions & 1 deletion packages/block-library/src/post-author/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
"customTextColor": {
"type": "string"
}
}
},
"context": [ "postId" ]
}
20 changes: 12 additions & 8 deletions packages/block-library/src/post-author/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,27 @@ function post_author_build_css_font_sizes( $attributes ) {
/**
* Renders the `core/post-author` block on the server.
*
* @param array $attributes Block attributes.
*
* @param array $attributes Block attributes.
* @param string $content Block default content.
* @param WP_Block $block Block instance.
* @return string Returns the rendered author block.
*/
function render_block_core_post_author( $attributes ) {
function render_block_core_post_author( $attributes, $content, $block ) {
if ( empty( $attributes ) ) {
return '';
}

$post = gutenberg_get_post_from_context();
if ( ! isset( $block->context['postId'] ) ) {
return '';
}

if ( ! $post ) {
$author_id = get_post_field( 'post_author', $block->context['postId'] );
if ( empty( $author_id ) ) {
return '';
}

$avatar = ! empty( $attributes['avatarSize'] ) ? get_avatar(
$post->post_author,
$author_id,
$attributes['avatarSize']
) : null;

Expand All @@ -129,8 +133,8 @@ function render_block_core_post_author( $attributes ) {
( $attributes['showAvatar'] ? '<div class="wp-block-post-author__avatar">' . $avatar . '</div>' : '' ) .
'<div class="wp-block-post-author__content">' .
( ! empty( $byline ) ? '<p class="wp-block-post-author__byline">' . $byline . '</p>' : '' ) .
'<p class="wp-block-post-author__name">' . get_the_author_meta( 'display_name' ) . '</p>' .
( ! empty( $attributes['showBio'] ) ? '<p class="wp-block-post-author__bio">' . get_the_author_meta( 'user_description' ) . '</p>' : '' ) .
'<p class="wp-block-post-author__name">' . get_the_author_meta( 'display_name', $author_id ) . '</p>' .
( ! empty( $attributes['showBio'] ) ? '<p class="wp-block-post-author__bio">' . get_the_author_meta( 'user_description', $author_id ) . '</p>' : '' ) .
'</div>' .
'</div>';
}
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/post-comments-count/block.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "core/post-comments-count",
"category": "layout"
"category": "layout",
"context": [ "postId" ]
}
14 changes: 8 additions & 6 deletions packages/block-library/src/post-comments-count/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,25 @@
/**
* Renders the `core/post-comments-count` block on the server.
*
* @param array $attributes The block attributes.
*
* @param array $attributes Block attributes.
* @param string $content Block default content.
* @param WP_Block $block Block instance.
* @return string Returns the filtered post comments count for the current post.
*/
function render_block_core_post_comments_count( $attributes ) {
$post = gutenberg_get_post_from_context();
if ( ! $post ) {
function render_block_core_post_comments_count( $attributes, $content, $block ) {
if ( ! isset( $block->context['postId'] ) ) {
return '';
}

$class = 'wp-block-post-comments-count';
if ( isset( $attributes['className'] ) ) {
$class .= ' ' . $attributes['className'];
}

return sprintf(
'<span class="%1$s">%2$s</span>',
esc_attr( $class ),
get_comments_number( $post )
get_comments_number( $block->context['postId'] )
);
}

Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/post-comments-form/block.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "core/post-comments-form",
"category": "layout"
"category": "layout",
"context": [ "postId" ]
}
11 changes: 7 additions & 4 deletions packages/block-library/src/post-comments-form/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
/**
* Renders the `core/post-comments-form` 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 filtered post comments form for the current post.
*/
function render_block_core_post_comments_form() {
$post = gutenberg_get_post_from_context();
if ( ! $post ) {
function render_block_core_post_comments_form( $attributes, $content, $block ) {
if ( ! isset( $block->context['postId'] ) ) {
return '';
}

ob_start();
comment_form( array(), $post->ID );
comment_form( array(), $block->context['postId'] );
$form = ob_get_clean();

return $form;
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/post-comments/block.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "core/post-comments",
"category": "layout"
"category": "layout",
"context": [ "postId" ]
}
16 changes: 13 additions & 3 deletions packages/block-library/src/post-comments/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,28 @@
/**
* Renders the `core/post-comments` 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 filtered post comments for the current post wrapped inside "p" tags.
*/
function render_block_core_post_comments() {
$post = gutenberg_get_post_from_context();
if ( ! $post ) {
function render_block_core_post_comments( $attributes, $content, $block ) {
global $post;

if ( ! isset( $block->context['postId'] ) ) {
return '';
}

$post_before = $post;

$post = get_post( $block->context['postId'] );
setup_postdata( $post );

// This generates a deprecate message.
// Ideally this deprecation is removed.
ob_start();
comments_template();
$post = $post_before;
return ob_get_clean();
}

Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/post-content/block.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "core/post-content",
"category": "layout"
"category": "layout",
"context": [ "postId" ]
}
11 changes: 7 additions & 4 deletions packages/block-library/src/post-content/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@
/**
* Renders the `core/post-content` 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 filtered post content of the current post.
*/
function render_block_core_post_content() {
$post = gutenberg_get_post_from_context();
if ( ! $post ) {
function render_block_core_post_content( $attributes, $content, $block ) {
if ( ! isset( $block->context['postId'] ) ) {
return '';
}

return (
'<div class="entry-content">' .
apply_filters( 'the_content', str_replace( ']]>', ']]&gt;', get_the_content( $post ) ) ) .
apply_filters( 'the_content', str_replace( ']]>', ']]&gt;', get_the_content( null, false, $block->context['postId'] ) ) ) .
ZebulanStanphill marked this conversation as resolved.
Show resolved Hide resolved
'</div>'
);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/post-date/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"format": {
"type": "string"
}
}
},
"context": [ "postId" ]
}
15 changes: 8 additions & 7 deletions packages/block-library/src/post-date/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@
/**
* Renders the `core/post-date` block on the server.
*
* @param array $attributes The block attributes.
*
* @param array $attributes Block attributes.
* @param string $content Block default content.
* @param WP_Block $block Block instance.
* @return string Returns the filtered post date for the current post wrapped inside "time" tags.
*/
function render_block_core_post_date( $attributes ) {
$post = gutenberg_get_post_from_context();
if ( ! $post ) {
function render_block_core_post_date( $attributes, $content, $block ) {
if ( ! isset( $block->context['postId'] ) ) {
return '';
}

return '<time datetime="'
. get_the_date( 'c', $post ) . '">'
. get_the_date( isset( $attributes['format'] ) ? $attributes['format'] : '', $post )
. get_the_date( 'c', $block->context['postId'] ) . '">'
. get_the_date( isset( $attributes['format'] ) ? $attributes['format'] : '', $block->context['postId'] )
. '</time>';
}

Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/post-excerpt/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
"type": "boolean",
"default": true
}
}
},
"context": [ "postId" ]
}
14 changes: 7 additions & 7 deletions packages/block-library/src/post-excerpt/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
/**
* Renders the `core/post-excerpt` block on the server.
*
* @param array $attributes The block attributes.
*
* @param array $attributes Block attributes.
* @param string $content Block default content.
* @param WP_Block $block Block instance.
* @return string Returns the filtered post excerpt for the current post wrapped inside "p" tags.
*/
function render_block_core_post_excerpt( $attributes ) {
$post = gutenberg_get_post_from_context();
if ( ! $post ) {
function render_block_core_post_excerpt( $attributes, $content, $block ) {
if ( ! isset( $block->context['postId'] ) ) {
return '';
}

$more_text = isset( $attributes['moreText'] ) ? '<a href="' . esc_url( get_the_permalink( $post ) ) . '">' . $attributes['moreText'] . '</a>' : '';
$more_text = isset( $attributes['moreText'] ) ? '<a href="' . esc_url( get_the_permalink( $block->context['postId'] ) ) . '">' . $attributes['moreText'] . '</a>' : '';

$filter_excerpt_length = function() use ( $attributes ) {
return isset( $attributes['wordCount'] ) ? $attributes['wordCount'] : 55;
Expand All @@ -28,7 +28,7 @@ function render_block_core_post_excerpt( $attributes ) {
$filter_excerpt_length
);

$output = '<p>' . get_the_excerpt( $post );
$output = '<p>' . get_the_excerpt( $block->context['postId'] );
if ( ! isset( $attributes['showMoreOnNewLine'] ) || $attributes['showMoreOnNewLine'] ) {
$output .= '</p>' . '<p>' . $more_text . '</p>';
} else {
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/post-featured-image/block.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "core/post-featured-image",
"category": "layout"
"category": "layout",
"context": [ "postId" ]
}
11 changes: 7 additions & 4 deletions packages/block-library/src/post-featured-image/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
/**
* Renders the `core/post-featured-image` 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 featured image for the current post.
*/
function render_block_core_post_featured_image() {
$post = gutenberg_get_post_from_context();
if ( ! $post ) {
function render_block_core_post_featured_image( $attributes, $content, $block ) {
if ( ! isset( $block->context['postId'] ) ) {
return '';
}
return '<p>' . get_the_post_thumbnail( $post ) . '</p>';

return '<p>' . get_the_post_thumbnail( $block->context['postId'] ) . '</p>';
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/post-tags/block.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "core/post-tags",
"category": "layout"
"category": "layout",
"context": [ "postId" ]
}
Loading