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..37699a3faf0c4 --- /dev/null +++ b/src/wp-includes/block-bindings/post-data.php @@ -0,0 +1,68 @@ + "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( 'U', $post_id ) > get_the_date( 'U', $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' ), + ) + ); +} + +add_action( 'init', '_register_block_bindings_post_data_source' ); 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 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'; 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 ); 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' ), );