-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Block Library: Add a Post Excerpt block. (#19579)
* Block Library: Add a Post Excerpt block. * Block Library: Make Post Excerpt block editable. * E2E Tests: Add Post Excerpt block fixtures.
- Loading branch information
Showing
10 changed files
with
104 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "core/post-excerpt", | ||
"category": "layout" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<!-- wp:post-excerpt /--> |
10 changes: 10 additions & 0 deletions
10
packages/e2e-tests/fixtures/blocks/core__post-excerpt.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
packages/e2e-tests/fixtures/blocks/core__post-excerpt.parsed.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
] |
1 change: 1 addition & 0 deletions
1
packages/e2e-tests/fixtures/blocks/core__post-excerpt.serialized.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<!-- wp:post-excerpt /--> |