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: Add Post Content and Title Blocks. #18461

Merged
merged 4 commits into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
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 );

/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have a separate file for template-related PHP logic? This compat.php file is at high risk for becoming a dumping ground of various utility functions, so should be limited to functions which can be very closely associated with a specific Trac ticket to incorporate a given change. It doesn't seem like we have such a Trac ticket we could link to with this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that would be good. Maybe even template-utils.php.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that would be good. Maybe even template-utils.php.

Would template.php be sufficient to describe the grouping of this related functionality?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be confused with templates.php. Maybe a templates directory makes sense at this point? Then, this file could just be called utils.php, templates.php would become create-cpt.php and so on.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I missed that there was a file already. In what way does this function differ that makes it not suitable to be placed in templates.php ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

templates.php just sets up the CPT.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a utility function for server rendered block implementations that use the post loop.

You could even argue it's not template specific and belongs in compat.php.

* 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