Skip to content

Commit

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

* Block Library: Use time tags in Post Date block.

* E2E Tests: Generate Post Date block fixtures.
  • Loading branch information
epiqueras authored Jan 14, 2020
1 parent c411b37 commit eeb13f4
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 0 deletions.
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,
};
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 /-->

0 comments on commit eeb13f4

Please sign in to comment.