| 1 |
<?php |
| 2 |
/** |
| 3 |
* Server-side rendering of the `core/post-date` block. |
| 4 |
* |
| 5 |
* @package WordPress |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Renders the `core/post-date` block on the server. |
| 10 |
* |
| 11 |
* @since 5.8.0 |
| 12 |
* @since 6.9.0 Added `datetime` attribute and Block Bindings support. |
| 13 |
* |
| 14 |
* @param array $attributes Block attributes. |
| 15 |
* @param string $content Block default content. |
| 16 |
* @param WP_Block $block Block instance. |
| 17 |
* @return string Returns the filtered post date for the current post wrapped inside "time" tags. |
| 18 |
*/ |
| 19 |
function gutenberg_render_block_core_post_date( $attributes, $content, $block ) { |
| 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. |
| 56 |
return ''; |
| 57 |
} |
| 58 |
|
| 59 |
$unformatted_date = $attributes['datetime']; |
| 60 |
$post_timestamp = strtotime( $unformatted_date ); |
| 61 |
|
| 62 |
if ( isset( $attributes['format'] ) && 'human-diff' === $attributes['format'] ) { |
| 63 |
if ( $post_timestamp > time() ) { |
| 64 |
// translators: %s: human-readable time difference. |
| 65 |
$formatted_date = sprintf( __( '%s from now' ), human_time_diff( $post_timestamp ) ); |
| 66 |
} else { |
| 67 |
// translators: %s: human-readable time difference. |
| 68 |
$formatted_date = sprintf( __( '%s ago' ), human_time_diff( $post_timestamp ) ); |
| 69 |
} |
| 70 |
} else { |
| 71 |
$format = empty( $attributes['format'] ) ? get_option( 'date_format' ) : $attributes['format']; |
| 72 |
$formatted_date = wp_date( $format, $post_timestamp ); |
| 73 |
} |
| 74 |
|
| 75 |
if ( isset( $attributes['textAlign'] ) ) { |
| 76 |
$classes[] = 'has-text-align-' . $attributes['textAlign']; |
| 77 |
} |
| 78 |
if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) { |
| 79 |
$classes[] = 'has-link-color'; |
| 80 |
} |
| 81 |
|
| 82 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) ); |
| 83 |
|
| 84 |
$time_tag = sprintf( '<time datetime="%1$s">%2$s</time>', $unformatted_date, $formatted_date ); |
| 85 |
|
| 86 |
if ( isset( $attributes['isLink'] ) && $attributes['isLink'] && isset( $block->context['postId'] ) ) { |
| 87 |
$time_tag = sprintf( '<a href="%1s">%2s</a>', get_the_permalink( $block->context['postId'] ), $time_tag ); |
| 88 |
} |
| 89 |
|
| 90 |
return sprintf( '<div %1$s>%2$s</div>', $wrapper_attributes, $time_tag ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Registers the `core/post-date` block on the server. |
| 95 |
* |
| 96 |
* @since 5.8.0 |
| 97 |
*/ |
| 98 |
function gutenberg_register_block_core_post_date() { |
| 99 |
register_block_type_from_metadata( |
| 100 |
__DIR__ . '/post-date', |
| 101 |
array( |
| 102 |
'render_callback' => 'gutenberg_render_block_core_post_date', |
| 103 |
) |
| 104 |
); |
| 105 |
} |
| 106 |
add_action( 'init', 'gutenberg_register_block_core_post_date', 20 ); |
| 107 |
|