Skip to content
Closed
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
68 changes: 68 additions & 0 deletions src/wp-includes/block-bindings/post-data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Post Data source for Block Bindings.
*
* @since 6.9.0
* @package WordPress
* @subpackage Block Bindings
*/

/**
* Gets value for Post Data source.
*
* @since 6.9.0
* @access private
*
* @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.
*/
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' );
1 change: 1 addition & 0 deletions src/wp-includes/class-wp-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/wp-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/tests/block-bindings/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ),
);
Expand Down
Loading