5 * @package WordPress |
5 * @package WordPress |
6 */ |
6 */ |
7 |
7 |
8 /** |
8 /** |
9 * Renders the `core/post-date` block on the server. |
9 * Renders the `core/post-date` block on the server. |
|
10 * |
|
11 * @since 5.8.0 |
10 * |
12 * |
11 * @param array $attributes Block attributes. |
13 * @param array $attributes Block attributes. |
12 * @param string $content Block default content. |
14 * @param string $content Block default content. |
13 * @param WP_Block $block Block instance. |
15 * @param WP_Block $block Block instance. |
14 * @return string Returns the filtered post date for the current post wrapped inside "time" tags. |
16 * @return string Returns the filtered post date for the current post wrapped inside "time" tags. |
16 function render_block_core_post_date( $attributes, $content, $block ) { |
18 function render_block_core_post_date( $attributes, $content, $block ) { |
17 if ( ! isset( $block->context['postId'] ) ) { |
19 if ( ! isset( $block->context['postId'] ) ) { |
18 return ''; |
20 return ''; |
19 } |
21 } |
20 |
22 |
21 $post_ID = $block->context['postId']; |
23 $post_ID = $block->context['postId']; |
22 $align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}"; |
24 $formatted_date = get_the_date( empty( $attributes['format'] ) ? '' : $attributes['format'], $post_ID ); |
23 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) ); |
25 $unformatted_date = esc_attr( get_the_date( 'c', $post_ID ) ); |
24 $formatted_date = get_the_date( empty( $attributes['format'] ) ? '' : $attributes['format'], $post_ID ); |
26 $classes = array(); |
|
27 |
|
28 if ( isset( $attributes['textAlign'] ) ) { |
|
29 $classes[] = 'has-text-align-' . $attributes['textAlign']; |
|
30 } |
|
31 if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) { |
|
32 $classes[] = 'has-link-color'; |
|
33 } |
|
34 |
|
35 /* |
|
36 * If the "Display last modified date" setting is enabled, |
|
37 * only display the modified date if it is later than the publishing date. |
|
38 */ |
|
39 if ( isset( $attributes['displayType'] ) && 'modified' === $attributes['displayType'] ) { |
|
40 if ( get_the_modified_date( 'Ymdhi', $post_ID ) > get_the_date( 'Ymdhi', $post_ID ) ) { |
|
41 $formatted_date = get_the_modified_date( empty( $attributes['format'] ) ? '' : $attributes['format'], $post_ID ); |
|
42 $unformatted_date = esc_attr( get_the_modified_date( 'c', $post_ID ) ); |
|
43 $classes[] = 'wp-block-post-date__modified-date'; |
|
44 } else { |
|
45 return ''; |
|
46 } |
|
47 } |
|
48 |
|
49 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) ); |
|
50 |
25 if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) { |
51 if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) { |
26 $formatted_date = sprintf( '<a href="%1s">%2s</a>', get_the_permalink( $post_ID ), $formatted_date ); |
52 $formatted_date = sprintf( '<a href="%1s">%2s</a>', get_the_permalink( $post_ID ), $formatted_date ); |
27 } |
53 } |
28 |
54 |
29 return sprintf( |
55 return sprintf( |
30 '<div %1$s><time datetime="%2$s">%3$s</time></div>', |
56 '<div %1$s><time datetime="%2$s">%3$s</time></div>', |
31 $wrapper_attributes, |
57 $wrapper_attributes, |
32 esc_attr( get_the_date( 'c', $post_ID ) ), |
58 $unformatted_date, |
33 $formatted_date |
59 $formatted_date |
34 ); |
60 ); |
35 } |
61 } |
36 |
62 |
37 /** |
63 /** |
38 * Registers the `core/post-date` block on the server. |
64 * Registers the `core/post-date` block on the server. |
|
65 * |
|
66 * @since 5.8.0 |
39 */ |
67 */ |
40 function register_block_core_post_date() { |
68 function register_block_core_post_date() { |
41 register_block_type_from_metadata( |
69 register_block_type_from_metadata( |
42 __DIR__ . '/post-date', |
70 __DIR__ . '/post-date', |
43 array( |
71 array( |