From 6bfa16d2266ee1f696ae6e584c39794c2701c69e Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 22 Jul 2025 14:54:15 +0200 Subject: [PATCH 01/10] Block Bindings: Allow Date block's datetime attribute to be bound --- src/wp-includes/class-wp-block.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index fcbc749cecb0e..085931beb42e7 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -285,6 +285,7 @@ private function process_block_bindings() { 'core/heading' => array( 'content' ), 'core/image' => array( 'id', 'url', 'title', 'alt' ), 'core/button' => array( 'url', 'text', 'linkTarget', 'rel' ), + 'core/post-date' => array( 'datetime' ), ); // If the block doesn't have the bindings property, isn't one of the supported From 9b1fba1fc34def2d2532cd7bb0adcd5ad447d220 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 23 Jul 2025 13:12:45 +0200 Subject: [PATCH 02/10] Block Bindings: Add core/post-data source --- src/wp-includes/block-bindings/post-data.php | 70 ++++++++++++++++++++ src/wp-settings.php | 1 + 2 files changed, 71 insertions(+) create mode 100644 src/wp-includes/block-bindings/post-data.php diff --git a/src/wp-includes/block-bindings/post-data.php b/src/wp-includes/block-bindings/post-data.php new file mode 100644 index 0000000000000..7357c5aeedd55 --- /dev/null +++ b/src/wp-includes/block-bindings/post-data.php @@ -0,0 +1,70 @@ + "foo" ). + * @param WP_Block $block_instance The block instance. + * @return mixed The value computed for the source. + */ +function _block_bindings_post_data_get_value( array $source_args, $block_instance ) { + if ( empty( $source_args['key'] ) ) { + return null; + } + + if ( empty( $block_instance->context['postId'] ) ) { + return null; + } + $post_id = $block_instance->context['postId']; + + // If a post isn't public, we need to prevent unauthorized users from accessing the post data. + $post = get_post( $post_id ); + if ( ( ! is_post_publicly_viewable( $post ) && ! current_user_can( 'read_post', $post_id ) ) || post_password_required( $post ) ) { + return null; + } + + if ( 'date' === $source_args['key'] ) { + return esc_attr( get_the_date( 'c', $post_id ) ); + } + + if ( 'modified' === $source_args['key'] ) { + /* + * Only return the modified date if it is later than the publishing date. + */ + if ( get_the_modified_date( 'Ymdhi', $post_id ) > get_the_date( 'Ymdhi', $post_id ) ) { + return esc_attr( get_the_modified_date( 'c', $post_id ) ); + } else { + return ''; + } + } +} + +/** + * Registers Post Data source in the block bindings registry. + * + * @since 6.9.0 + * @access private + */ +function _register_block_bindings_post_data_source() { + register_block_bindings_source( + 'core/post-data', + array( + 'label' => _x( 'Post Data', 'block bindings source' ), + 'get_value_callback' => '_block_bindings_post_data_get_value', + 'uses_context' => array( 'postId', 'postType' ), + ) + ); +} + +add_action( 'init', '_register_block_bindings_post_data_source' ); diff --git a/src/wp-settings.php b/src/wp-settings.php index 60ffc307c5f6e..3892b8cd33f91 100644 --- a/src/wp-settings.php +++ b/src/wp-settings.php @@ -364,6 +364,7 @@ require ABSPATH . WPINC . '/class-wp-navigation-fallback.php'; require ABSPATH . WPINC . '/block-bindings.php'; require ABSPATH . WPINC . '/block-bindings/pattern-overrides.php'; +require ABSPATH . WPINC . '/block-bindings/post-data.php'; require ABSPATH . WPINC . '/block-bindings/post-meta.php'; require ABSPATH . WPINC . '/blocks.php'; require ABSPATH . WPINC . '/blocks/index.php'; From c35a900b56f1584e67b1fa5503e4cae0c95a8a24 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 23 Jul 2025 13:28:39 +0200 Subject: [PATCH 03/10] Update tests --- tests/phpunit/tests/block-bindings/register.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/phpunit/tests/block-bindings/register.php b/tests/phpunit/tests/block-bindings/register.php index 49dd203d30ca6..cff3c0251cd6e 100644 --- a/tests/phpunit/tests/block-bindings/register.php +++ b/tests/phpunit/tests/block-bindings/register.php @@ -72,6 +72,7 @@ public function test_get_all_registered() { $source_one_name => new WP_Block_Bindings_Source( $source_one_name, $source_one_properties ), $source_two_name => new WP_Block_Bindings_Source( $source_two_name, $source_two_properties ), $source_three_name => new WP_Block_Bindings_Source( $source_three_name, $source_three_properties ), + 'core/post-data' => get_block_bindings_source( 'core/post-data' ), 'core/post-meta' => get_block_bindings_source( 'core/post-meta' ), 'core/pattern-overrides' => get_block_bindings_source( 'core/pattern-overrides' ), ); From 9c4d6ab7bf96a73210c5b164a94cd7f04ccac3dd Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 23 Jul 2025 15:58:15 +0200 Subject: [PATCH 04/10] Unregister core/post-data source during tests --- tests/phpunit/includes/functions.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/phpunit/includes/functions.php b/tests/phpunit/includes/functions.php index c2976dba9fe41..516727380c109 100644 --- a/tests/phpunit/includes/functions.php +++ b/tests/phpunit/includes/functions.php @@ -348,6 +348,7 @@ function _unhook_block_registration() { // Block binding sources. remove_action( 'init', '_register_block_bindings_pattern_overrides_source' ); + remove_action( 'init', '_register_block_bindings_post_data_source' ); remove_action( 'init', '_register_block_bindings_post_meta_source' ); } tests_add_filter( 'init', '_unhook_block_registration', 1000 ); From c65ed45c12ce74ddc8dcd7e3082eb3c35e0fe42c Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 23 Jul 2025 15:58:53 +0200 Subject: [PATCH 05/10] Remove unnecessary postType context dependency --- src/wp-includes/block-bindings/post-data.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/block-bindings/post-data.php b/src/wp-includes/block-bindings/post-data.php index 7357c5aeedd55..2f353b790b1ea 100644 --- a/src/wp-includes/block-bindings/post-data.php +++ b/src/wp-includes/block-bindings/post-data.php @@ -62,7 +62,7 @@ function _register_block_bindings_post_data_source() { array( 'label' => _x( 'Post Data', 'block bindings source' ), 'get_value_callback' => '_block_bindings_post_data_get_value', - 'uses_context' => array( 'postId', 'postType' ), + 'uses_context' => array( 'postId', ), ) ); } From c29d031d1f5d8770d9e5dcec91044c05be38b808 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 23 Jul 2025 16:07:49 +0200 Subject: [PATCH 06/10] Use single line comment format --- src/wp-includes/block-bindings/post-data.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/wp-includes/block-bindings/post-data.php b/src/wp-includes/block-bindings/post-data.php index 2f353b790b1ea..ea5eae10c7bcc 100644 --- a/src/wp-includes/block-bindings/post-data.php +++ b/src/wp-includes/block-bindings/post-data.php @@ -39,9 +39,7 @@ function _block_bindings_post_data_get_value( array $source_args, $block_instanc } if ( 'modified' === $source_args['key'] ) { - /* - * Only return the modified date if it is later than the publishing date. - */ + // Only return the modified date if it is later than the publishing date. if ( get_the_modified_date( 'Ymdhi', $post_id ) > get_the_date( 'Ymdhi', $post_id ) ) { return esc_attr( get_the_modified_date( 'c', $post_id ) ); } else { From da8a02e27e97112f298d623503e27f820d5e981d Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 23 Jul 2025 16:14:01 +0200 Subject: [PATCH 07/10] Remove stray trailing comma --- src/wp-includes/block-bindings/post-data.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/block-bindings/post-data.php b/src/wp-includes/block-bindings/post-data.php index ea5eae10c7bcc..9990f7326d42c 100644 --- a/src/wp-includes/block-bindings/post-data.php +++ b/src/wp-includes/block-bindings/post-data.php @@ -60,7 +60,7 @@ function _register_block_bindings_post_data_source() { array( 'label' => _x( 'Post Data', 'block bindings source' ), 'get_value_callback' => '_block_bindings_post_data_get_value', - 'uses_context' => array( 'postId', ), + 'uses_context' => array( 'postId' ), ) ); } From 18c9c92a3c1eef82087768e6225f65ed4c8e9b04 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 30 Jul 2025 14:51:13 +0200 Subject: [PATCH 08/10] Use epoch to compare dates --- src/wp-includes/block-bindings/post-data.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/block-bindings/post-data.php b/src/wp-includes/block-bindings/post-data.php index 9990f7326d42c..d5a9eac1927d6 100644 --- a/src/wp-includes/block-bindings/post-data.php +++ b/src/wp-includes/block-bindings/post-data.php @@ -40,7 +40,7 @@ function _block_bindings_post_data_get_value( array $source_args, $block_instanc if ( 'modified' === $source_args['key'] ) { // Only return the modified date if it is later than the publishing date. - if ( get_the_modified_date( 'Ymdhi', $post_id ) > get_the_date( 'Ymdhi', $post_id ) ) { + if ( get_the_modified_date( 'U', $post_id ) > get_the_date( 'U', $post_id ) ) { return esc_attr( get_the_modified_date( 'c', $post_id ) ); } else { return ''; From aaf8c6dae52482457c63090aceb597348473bae3 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 5 Aug 2025 14:55:31 +0200 Subject: [PATCH 09/10] Tweak PHPDoc --- src/wp-includes/block-bindings/post-data.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/block-bindings/post-data.php b/src/wp-includes/block-bindings/post-data.php index d5a9eac1927d6..58f0041a30cfe 100644 --- a/src/wp-includes/block-bindings/post-data.php +++ b/src/wp-includes/block-bindings/post-data.php @@ -1,6 +1,6 @@ Date: Tue, 5 Aug 2025 15:22:34 +0200 Subject: [PATCH 10/10] More PHPDoc tweaks --- src/wp-includes/block-bindings/post-data.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/block-bindings/post-data.php b/src/wp-includes/block-bindings/post-data.php index 58f0041a30cfe..37699a3faf0c4 100644 --- a/src/wp-includes/block-bindings/post-data.php +++ b/src/wp-includes/block-bindings/post-data.php @@ -13,7 +13,7 @@ * @since 6.9.0 * @access private * - * @param array $source_args Array containing source arguments used to look up the override value. + * @param array $source_args Array containing arguments used to look up the source value. * Example: array( "key" => "foo" ). * @param WP_Block $block_instance The block instance. * @return mixed The value computed for the source.