Skip to content

Commit

Permalink
Block Library: Add a Post Excerpt block. (#19579)
Browse files Browse the repository at this point in the history
* Block Library: Add a Post Excerpt block.

* Block Library: Make Post Excerpt block editable.

* E2E Tests: Add Post Excerpt block fixtures.
  • Loading branch information
epiqueras authored Jan 13, 2020
1 parent 06782e1 commit 5e2ac97
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ function gutenberg_reregister_core_block_types() {
'template-part.php' => 'core/template-part',
'post-title.php' => 'core/post-title',
'post-content.php' => 'core/post-content',
'post-excerpt.php' => 'core/post-excerpt',
);

$registry = WP_Block_Type_Registry::get_instance();
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import * as siteTitle from './site-title';
import * as templatePart from './template-part';
import * as postTitle from './post-title';
import * as postContent from './post-content';
import * as postExcerpt from './post-excerpt';

/**
* Function to register an individual block.
Expand Down Expand Up @@ -187,7 +188,7 @@ export const __experimentalRegisterExperimentalCoreBlocks =

// Register Full Site Editing Blocks.
...( __experimentalEnableFullSiteEditing ?
[ siteTitle, templatePart, postTitle, postContent ] :
[ siteTitle, templatePart, postTitle, postContent, postExcerpt ] :
[] ),
].forEach( registerBlock );
} :
Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/post-excerpt/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "core/post-excerpt",
"category": "layout"
}
17 changes: 17 additions & 0 deletions packages/block-library/src/post-excerpt/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* WordPress dependencies
*/
import { useEntityProp, useEntityId } from '@wordpress/core-data';
import { PlainText } from '@wordpress/block-editor';

function PostExcerptDisplay() {
const [ excerpt, setExcerpt ] = useEntityProp( 'postType', 'post', 'excerpt' );
return <PlainText value={ excerpt } onChange={ setExcerpt } />;
}

export default function PostExcerptEdit() {
if ( ! useEntityId( 'postType', 'post' ) ) {
return 'Post Excerpt Placeholder';
}
return <PostExcerptDisplay />;
}
18 changes: 18 additions & 0 deletions packages/block-library/src/post-excerpt/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

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

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

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

/**
* Renders the `core/post-excerpt` block on the server.
*
* @return string Returns the filtered post excerpt for the current post wrapped inside "p" tags.
*/
function render_block_core_post_excerpt() {
$post = gutenberg_get_post_from_context();
if ( ! $post ) {
return '';
}
return '<p>' . get_the_excerpt( $post ) . '</p>';
}

/**
* Registers the `core/post-excerpt` block on the server.
*/
function register_block_core_post_excerpt() {
register_block_type(
'core/post-excerpt',
array(
'render_callback' => 'render_block_core_post_excerpt',
)
);
}
add_action( 'init', 'register_block_core_post_excerpt' );
1 change: 1 addition & 0 deletions packages/e2e-tests/fixtures/blocks/core__post-excerpt.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:post-excerpt /-->
10 changes: 10 additions & 0 deletions packages/e2e-tests/fixtures/blocks/core__post-excerpt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"clientId": "_clientId_0",
"name": "core/post-excerpt",
"isValid": true,
"attributes": {},
"innerBlocks": [],
"originalContent": ""
}
]
18 changes: 18 additions & 0 deletions packages/e2e-tests/fixtures/blocks/core__post-excerpt.parsed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"blockName": "core/post-excerpt",
"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-excerpt /-->

0 comments on commit 5e2ac97

Please sign in to comment.