Skip to content

Commit

Permalink
Block Library: Add Post Content and Title Blocks. (#18461)
Browse files Browse the repository at this point in the history
* Block Library: Add Post Content and Title Blocks.

* Block Library: Factor out common post loop handling code.

* Block Library: Make post object handling explicit.

* Fixtures
  • Loading branch information
epiqueras authored Nov 14, 2019
1 parent 8fcef7e commit 3ab776b
Show file tree
Hide file tree
Showing 22 changed files with 230 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ function gutenberg_reregister_core_block_types() {
'social-link.php' => gutenberg_get_registered_social_link_blocks(),
'tag-cloud.php' => 'core/tag-cloud',
'site-title.php' => 'core/site-title',
'post-title.php' => 'core/post-title',
'post-content.php' => 'core/post-content',
);

$registry = WP_Block_Type_Registry::get_instance();
Expand Down
18 changes: 18 additions & 0 deletions lib/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,21 @@ function gutenberg_provide_render_callback_with_block_object( $pre_render, $bloc
return apply_filters( 'render_block', $block_content, $block );
}
add_filter( 'pre_render_block', 'gutenberg_provide_render_callback_with_block_object', 10, 2 );

/**
* Sets the current post for usage in template blocks.
*
* @return WP_Post|null The post if any, or null otherwise.
*/
function gutenberg_get_post_from_context() {
// TODO: Without this temporary fix, an infinite loop can occur where
// posts with post content blocks render themselves recursively.
if ( is_admin() || defined( 'REST_REQUEST' ) ) {
return null;
}
if ( ! in_the_loop() ) {
rewind_posts();
the_post();
}
return get_post();
}
6 changes: 5 additions & 1 deletion packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ import * as socialLink from './social-link';

// Full Site Editing Blocks
import * as siteTitle from './site-title';
import * as postTitle from './post-title';
import * as postContent from './post-content';

/**
* Function to register an individual block.
Expand Down Expand Up @@ -182,7 +184,9 @@ export const __experimentalRegisterExperimentalCoreBlocks =
...socialLink.sites,

// Register Full Site Editing Blocks.
...( __experimentalEnableFullSiteEditing ? [ siteTitle ] : [] ),
...( __experimentalEnableFullSiteEditing ?
[ siteTitle, postTitle, postContent ] :
[] ),
].forEach( registerBlock );
} :
undefined;
4 changes: 4 additions & 0 deletions packages/block-library/src/post-content/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "core/post-content",
"category": "layout"
}
3 changes: 3 additions & 0 deletions packages/block-library/src/post-content/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function PostContentEdit() {
return 'Post Content Placeholder';
}
11 changes: 11 additions & 0 deletions packages/block-library/src/post-content/icon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* WordPress dependencies
*/
import { SVG, Path } from '@wordpress/components';

export default (
<SVG xmlns="https://www.w3.org/2000/svg" viewBox="0 0 24 24">
<Path fill="none" d="M0 0h24v24H0V0z" />
<Path d="M3 15h18v-2H3v2zm0 4h18v-2H3v2zm0-8h18V9H3v2zm0-6v2h18V5H3z" />
</SVG>
);
20 changes: 20 additions & 0 deletions packages/block-library/src/post-content/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import metadata from './block.json';
import icon from './icon';
import edit from './edit';

const { name } = metadata;
export { metadata, name };

export const settings = {
title: __( 'Post Content' ),
icon,
edit,
};
36 changes: 36 additions & 0 deletions packages/block-library/src/post-content/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Server-side rendering of the `core/post-content` block.
*
* @package WordPress
*/

/**
* Renders the `core/post-content` block on the server.
*
* @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 ) {
return '';
}
return (
'<div class="entry-content">' .
apply_filters( 'the_content', str_replace( ']]>', ']]&gt;', get_the_content( $post ) ) ) .
'</div>'
);
}

/**
* Registers the `core/post-content` block on the server.
*/
function register_block_core_post_content() {
register_block_type(
'core/post-content',
array(
'render_callback' => 'render_block_core_post_content',
)
);
}
add_action( 'init', 'register_block_core_post_content' );
4 changes: 4 additions & 0 deletions packages/block-library/src/post-title/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "core/post-title",
"category": "layout"
}
3 changes: 3 additions & 0 deletions packages/block-library/src/post-title/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function PostTitleEdit() {
return 'Post Title Placeholder';
}
11 changes: 11 additions & 0 deletions packages/block-library/src/post-title/icon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* WordPress dependencies
*/
import { SVG, Path } from '@wordpress/components';

export default (
<SVG xmlns="https://www.w3.org/2000/svg" viewBox="0 0 24 24">
<Path fill="none" d="M0 0h24v24H0V0z" />
<Path d="M5 4v3h5.5v12h3V7H19V4H5z" />
</SVG>
);
20 changes: 20 additions & 0 deletions packages/block-library/src/post-title/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import metadata from './block.json';
import icon from './icon';
import edit from './edit';

const { name } = metadata;
export { metadata, name };

export const settings = {
title: __( 'Post Title' ),
icon,
edit,
};
32 changes: 32 additions & 0 deletions packages/block-library/src/post-title/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Server-side rendering of the `core/post-title` block.
*
* @package WordPress
*/

/**
* Renders the `core/post-title` block on the server.
*
* @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();
if ( ! $post ) {
return '';
}
return '<h1>' . get_the_title( $post ) . '</h1>';
}

/**
* Registers the `core/post-title` block on the server.
*/
function register_block_core_post_title() {
register_block_type(
'core/post-title',
array(
'render_callback' => 'render_block_core_post_title',
)
);
}
add_action( 'init', 'register_block_core_post_title' );
1 change: 1 addition & 0 deletions packages/e2e-tests/fixtures/blocks/core__post-content.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:post-content /-->
10 changes: 10 additions & 0 deletions packages/e2e-tests/fixtures/blocks/core__post-content.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"clientId": "_clientId_0",
"name": "core/post-content",
"isValid": true,
"attributes": {},
"innerBlocks": [],
"originalContent": ""
}
]
18 changes: 18 additions & 0 deletions packages/e2e-tests/fixtures/blocks/core__post-content.parsed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"blockName": "core/post-content",
"attrs": {},
"innerBlocks": [],
"innerHTML": "",
"innerContent": []
},
{
"blockName": null,
"attrs": {},
"innerBlocks": [],
"innerHTML": "\n",
"innerContent": [
"\n"
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:post-content /-->
1 change: 1 addition & 0 deletions packages/e2e-tests/fixtures/blocks/core__post-title.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:post-title /-->
10 changes: 10 additions & 0 deletions packages/e2e-tests/fixtures/blocks/core__post-title.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"clientId": "_clientId_0",
"name": "core/post-title",
"isValid": true,
"attributes": {},
"innerBlocks": [],
"originalContent": ""
}
]
18 changes: 18 additions & 0 deletions packages/e2e-tests/fixtures/blocks/core__post-title.parsed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"blockName": "core/post-title",
"attrs": {},
"innerBlocks": [],
"innerHTML": "",
"innerContent": []
},
{
"blockName": null,
"attrs": {},
"innerBlocks": [],
"innerHTML": "\n",
"innerContent": [
"\n"
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:post-title /-->
Loading

0 comments on commit 3ab776b

Please sign in to comment.