Skip to content

Commit ccfd74c

Browse files
committed
Block Bindings: Add core/post-data source.
Add a new Block Bindings source, `core/post-data`, which exposes `date` and `modified` fields for now -- reflecting the publish date and the last modified date of the post, respectively. The source could be subsequently extended to include other fields associated with a post object, such as title, featured image, etc. Props bernhard-reiter. Closes #63741. git-svn-id: https://develop.svn.wordpress.org/trunk@60539 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 76cfc04 commit ccfd74c

File tree

5 files changed

+72
-0
lines changed

5 files changed

+72
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Post Data source for Block Bindings.
4+
*
5+
* @since 6.9.0
6+
* @package WordPress
7+
* @subpackage Block Bindings
8+
*/
9+
10+
/**
11+
* Gets value for Post Data source.
12+
*
13+
* @since 6.9.0
14+
* @access private
15+
*
16+
* @param array $source_args Array containing arguments used to look up the source value.
17+
* Example: array( "key" => "foo" ).
18+
* @param WP_Block $block_instance The block instance.
19+
* @return mixed The value computed for the source.
20+
*/
21+
function _block_bindings_post_data_get_value( array $source_args, $block_instance ) {
22+
if ( empty( $source_args['key'] ) ) {
23+
return null;
24+
}
25+
26+
if ( empty( $block_instance->context['postId'] ) ) {
27+
return null;
28+
}
29+
$post_id = $block_instance->context['postId'];
30+
31+
// If a post isn't public, we need to prevent unauthorized users from accessing the post data.
32+
$post = get_post( $post_id );
33+
if ( ( ! is_post_publicly_viewable( $post ) && ! current_user_can( 'read_post', $post_id ) ) || post_password_required( $post ) ) {
34+
return null;
35+
}
36+
37+
if ( 'date' === $source_args['key'] ) {
38+
return esc_attr( get_the_date( 'c', $post_id ) );
39+
}
40+
41+
if ( 'modified' === $source_args['key'] ) {
42+
// Only return the modified date if it is later than the publishing date.
43+
if ( get_the_modified_date( 'U', $post_id ) > get_the_date( 'U', $post_id ) ) {
44+
return esc_attr( get_the_modified_date( 'c', $post_id ) );
45+
} else {
46+
return '';
47+
}
48+
}
49+
}
50+
51+
/**
52+
* Registers Post Data source in the block bindings registry.
53+
*
54+
* @since 6.9.0
55+
* @access private
56+
*/
57+
function _register_block_bindings_post_data_source() {
58+
register_block_bindings_source(
59+
'core/post-data',
60+
array(
61+
'label' => _x( 'Post Data', 'block bindings source' ),
62+
'get_value_callback' => '_block_bindings_post_data_get_value',
63+
'uses_context' => array( 'postId' ),
64+
)
65+
);
66+
}
67+
68+
add_action( 'init', '_register_block_bindings_post_data_source' );

src/wp-includes/class-wp-block.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ private function process_block_bindings() {
285285
'core/heading' => array( 'content' ),
286286
'core/image' => array( 'id', 'url', 'title', 'alt' ),
287287
'core/button' => array( 'url', 'text', 'linkTarget', 'rel' ),
288+
'core/post-date' => array( 'datetime' ),
288289
);
289290

290291
// If the block doesn't have the bindings property, isn't one of the supported

src/wp-settings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@
364364
require ABSPATH . WPINC . '/class-wp-navigation-fallback.php';
365365
require ABSPATH . WPINC . '/block-bindings.php';
366366
require ABSPATH . WPINC . '/block-bindings/pattern-overrides.php';
367+
require ABSPATH . WPINC . '/block-bindings/post-data.php';
367368
require ABSPATH . WPINC . '/block-bindings/post-meta.php';
368369
require ABSPATH . WPINC . '/blocks.php';
369370
require ABSPATH . WPINC . '/blocks/index.php';

tests/phpunit/includes/functions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ function _unhook_block_registration() {
348348

349349
// Block binding sources.
350350
remove_action( 'init', '_register_block_bindings_pattern_overrides_source' );
351+
remove_action( 'init', '_register_block_bindings_post_data_source' );
351352
remove_action( 'init', '_register_block_bindings_post_meta_source' );
352353
}
353354
tests_add_filter( 'init', '_unhook_block_registration', 1000 );

tests/phpunit/tests/block-bindings/register.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public function test_get_all_registered() {
7272
$source_one_name => new WP_Block_Bindings_Source( $source_one_name, $source_one_properties ),
7373
$source_two_name => new WP_Block_Bindings_Source( $source_two_name, $source_two_properties ),
7474
$source_three_name => new WP_Block_Bindings_Source( $source_three_name, $source_three_properties ),
75+
'core/post-data' => get_block_bindings_source( 'core/post-data' ),
7576
'core/post-meta' => get_block_bindings_source( 'core/post-meta' ),
7677
'core/pattern-overrides' => get_block_bindings_source( 'core/pattern-overrides' ),
7778
);

0 commit comments

Comments
 (0)