Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Block Library: Implement Post blocks context server-side.
  • Loading branch information
epiqueras authored and aduth committed Apr 6, 2020
commit 2cf7ce444112facc602c33241b32c2c8e815c6ba
1 change: 1 addition & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function gutenberg_reregister_core_block_types() {
'tag-cloud.php' => 'core/tag-cloud',
'site-title.php' => 'core/site-title',
'template-part.php' => 'core/template-part',
'post.php' => 'core/post',
'post-title.php' => 'core/post-title',
'post-content.php' => 'core/post-content',
'post-author.php' => 'core/post-author',
Expand Down
14 changes: 11 additions & 3 deletions packages/block-library/src/post-content/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@
/**
* Renders the `core/post-content` block on the server.
*
* @param array $attributes The block attributes.
* @param array $content The saved content.
* @param array $block The parsed block.
*
* @return string Returns the filtered post content of the current post.
*/
function render_block_core_post_content() {
$post = gutenberg_get_post_from_context();
function render_block_core_post_content( $attributes, $content, $block ) {
$post = isset( $block['context']['postId'] ) ? $block['context']['postId'] : gutenberg_get_post_from_context();
if ( ! $post ) {
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, $post ) ) ) .
'</div>'
);
}
Expand All @@ -34,6 +38,10 @@ function register_block_core_post_content() {
array_merge(
$metadata,
array(
'context' => array(
'postType' => 'core/post',
'postId' => 'core/post',
),
'render_callback' => 'render_block_core_post_content',
)
)
Expand Down
12 changes: 10 additions & 2 deletions packages/block-library/src/post-title/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
/**
* Renders the `core/post-title` block on the server.
*
* @param array $attributes The block attributes.
* @param array $content The saved content.
* @param array $block The parsed block.
*
* @return string Returns the filtered post title for the current post wrapped inside "h1" tags.
*/
function render_block_core_post_title() {
$post = gutenberg_get_post_from_context();
function render_block_core_post_title( $attributes, $content, $block ) {
$post = isset( $block['context']['postId'] ) ? $block['context']['postId'] : gutenberg_get_post_from_context();
if ( ! $post ) {
return '';
}
Expand All @@ -30,6 +34,10 @@ function register_block_core_post_title() {
array_merge(
$metadata,
array(
'context' => array(
'postType' => 'core/post',
'postId' => 'core/post',
),
'render_callback' => 'render_block_core_post_title',
)
)
Expand Down
28 changes: 28 additions & 0 deletions packages/block-library/src/post/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Server-side registration of the `core/post` block.
*
* @package WordPress
*/

/**
* Registers the `core/post` block on the server.
*/
function register_block_core_post() {
register_block_type(
'core/post',
array(
'attributes' => array(
'postType' => array(
'type' => 'string',
'context' => true,
),
'postId' => array(
'type' => 'string',
'context' => true,
),
),
)
);
}
add_action( 'init', 'register_block_core_post' );