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 a Post Excerpt block. #19579

Merged
merged 3 commits into from
Jan 13, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
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';
Copy link
Contributor

Choose a reason for hiding this comment

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

i18n

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 don't think we should use it here.

This is a temporary placeholder while #19256 is worked on.

}
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' );