Changeset 61009 for trunk/src/wp-includes/blocks/post-date.php
- Timestamp:
- 10/21/2025 07:11:53 AM (3 months ago)
- File:
-
- 1 edited
-
trunk/src/wp-includes/blocks/post-date.php (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks/post-date.php
r59072 r61009 10 10 * 11 11 * @since 5.8.0 12 * @since 6.9.0 Added `datetime` attribute and Block Bindings support. 12 13 * 13 14 * @param array $attributes Block attributes. … … 17 18 */ 18 19 function render_block_core_post_date( $attributes, $content, $block ) { 19 if ( ! isset( $block->context['postId'] ) ) { 20 $classes = array(); 21 22 if ( 23 ! isset( $attributes['datetime'] ) && ! ( 24 isset( $attributes['metadata']['bindings']['datetime']['source'] ) && 25 isset( $attributes['metadata']['bindings']['datetime']['args'] ) 26 ) 27 ) { 28 /* 29 * This is the legacy version of the block that didn't have the `datetime` attribute. 30 * This branch needs to be kept for backward compatibility. 31 */ 32 $source = get_block_bindings_source( 'core/post-data' ); 33 if ( isset( $attributes['displayType'] ) && 'modified' === $attributes['displayType'] ) { 34 $source_args = array( 35 'field' => 'modified', 36 ); 37 } else { 38 $source_args = array( 39 'field' => 'date', 40 ); 41 } 42 $attributes['datetime'] = $source->get_value( $source_args, $block, 'datetime' ); 43 } 44 45 if ( isset( $source_args['field'] ) && 'modified' === $source_args['field'] ) { 46 $classes[] = 'wp-block-post-date__modified-date'; 47 } 48 49 if ( empty( $attributes['datetime'] ) ) { 50 // If the `datetime` attribute is set but empty, it could be because Block Bindings 51 // set it that way. This can happen e.g. if the block is bound to the 52 // post's last modified date, and the latter lies before the publish date. 53 // (See https://github.com/WordPress/gutenberg/pull/46839 where this logic was originally 54 // implemented.) 55 // In this case, we have to respect and return the empty value. 20 56 return ''; 21 57 } 22 58 23 $post_ID = $block->context['postId']; 59 $unformatted_date = $attributes['datetime']; 60 $post_timestamp = strtotime( $unformatted_date ); 24 61 25 62 if ( isset( $attributes['format'] ) && 'human-diff' === $attributes['format'] ) { 26 $post_timestamp = get_post_timestamp( $post_ID );27 63 if ( $post_timestamp > time() ) { 28 64 // translators: %s: human-readable time difference. … … 33 69 } 34 70 } else { 35 $formatted_date = get_the_date( empty( $attributes['format'] ) ? '' : $attributes['format'], $post_ID ); 71 $format = empty( $attributes['format'] ) ? get_option( 'date_format' ) : $attributes['format']; 72 $formatted_date = wp_date( $format, $post_timestamp ); 36 73 } 37 $unformatted_date = esc_attr( get_the_date( 'c', $post_ID ) );38 $classes = array();39 74 40 75 if ( isset( $attributes['textAlign'] ) ) { … … 45 80 } 46 81 47 /*48 * If the "Display last modified date" setting is enabled,49 * only display the modified date if it is later than the publishing date.50 */51 if ( isset( $attributes['displayType'] ) && 'modified' === $attributes['displayType'] ) {52 if ( get_the_modified_date( 'Ymdhi', $post_ID ) > get_the_date( 'Ymdhi', $post_ID ) ) {53 if ( isset( $attributes['format'] ) && 'human-diff' === $attributes['format'] ) {54 // translators: %s: human-readable time difference.55 $formatted_date = sprintf( __( '%s ago' ), human_time_diff( get_post_timestamp( $post_ID, 'modified' ) ) );56 } else {57 $formatted_date = get_the_modified_date( empty( $attributes['format'] ) ? '' : $attributes['format'], $post_ID );58 }59 $unformatted_date = esc_attr( get_the_modified_date( 'c', $post_ID ) );60 $classes[] = 'wp-block-post-date__modified-date';61 } else {62 return '';63 }64 }65 66 82 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) ); 67 83 68 if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) {69 $formatted_date = sprintf( '<a href="%1s">%2s</a>', get_the_permalink( $ post_ID), $formatted_date );84 if ( isset( $attributes['isLink'] ) && $attributes['isLink'] && isset( $block->context['postId'] ) ) { 85 $formatted_date = sprintf( '<a href="%1s">%2s</a>', get_the_permalink( $block->context['postId'] ), $formatted_date ); 70 86 } 71 87
Note: See TracChangeset
for help on using the changeset viewer.