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 Date block. #19578

Merged
merged 3 commits into from
Jan 14, 2020
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
1 change: 1 addition & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ function gutenberg_reregister_core_block_types() {
'post-title.php' => 'core/post-title',
'post-content.php' => 'core/post-content',
'post-author.php' => 'core/post-author',
'post-date.php' => 'core/post-date',
'post-excerpt.php' => 'core/post-excerpt',
);

Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import * as templatePart from './template-part';
import * as postTitle from './post-title';
import * as postContent from './post-content';
import * as postAuthor from './post-author';
import * as postDate from './post-date';
import * as postExcerpt from './post-excerpt';

/**
Expand Down Expand Up @@ -195,6 +196,7 @@ export const __experimentalRegisterExperimentalCoreBlocks =
postTitle,
postContent,
postAuthor,
postDate,
postExcerpt,
] :
[] ),
Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/post-date/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "core/post-date",
"category": "layout"
}
26 changes: 26 additions & 0 deletions packages/block-library/src/post-date/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* WordPress dependencies
*/
import { useEntityProp, useEntityId } from '@wordpress/core-data';
import { __experimentalGetSettings, dateI18n } from '@wordpress/date';
import { __ } from '@wordpress/i18n';

function PostDateDisplay() {
const [ date ] = useEntityProp( 'postType', 'post', 'date' );
const settings = __experimentalGetSettings();

return date ? (
<time dateTime={ dateI18n( 'c', date ) }>
{ dateI18n( settings.formats.date, date ) }
</time>
) : (
__( 'No Date' )
);
}

export default function PostDateEdit() {
if ( ! useEntityId( 'postType', 'post' ) ) {
return 'Post Date Placeholder';
}
return <PostDateDisplay />;
}
18 changes: 18 additions & 0 deletions packages/block-library/src/post-date/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 Date' ),
edit,
Copy link
Contributor

Choose a reason for hiding this comment

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

a format attribute is definitely needed for this block.

};
32 changes: 32 additions & 0 deletions packages/block-library/src/post-date/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Server-side rendering of the `core/post-date` block.
*
* @package WordPress
*/

/**
* Renders the `core/post-date` block on the server.
*
* @return string Returns the filtered post date for the current post wrapped inside "time" tags.
*/
function render_block_core_post_date() {
$post = gutenberg_get_post_from_context();
if ( ! $post ) {
return '';
}
return '<time datetime="' . get_the_date( 'c', $post ) . '">' . get_the_date( '', $post ) . '</time>';
}

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