| 1 |
<?php |
| 2 |
/** |
| 3 |
* Server-side rendering of the `core/post-terms` block. |
| 4 |
* |
| 5 |
* @package WordPress |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Renders the `core/post-terms` block on the server. |
| 10 |
* |
| 11 |
* @param array $attributes Block attributes. |
| 12 |
* @param string $content Block default content. |
| 13 |
* @param WP_Block $block Block instance. |
| 14 |
* @return string Returns the filtered post terms for the current post wrapped inside "a" tags. |
| 15 |
*/ |
| 16 |
function gutenberg_render_block_core_post_terms( $attributes, $content, $block ) { |
| 17 |
if ( ! isset( $block->context['postId'] ) || ! isset( $attributes['term'] ) ) { |
| 18 |
return ''; |
| 19 |
} |
| 20 |
|
| 21 |
if ( ! is_taxonomy_viewable( $attributes['term'] ) ) { |
| 22 |
return ''; |
| 23 |
} |
| 24 |
|
| 25 |
$post_terms = get_the_terms( $block->context['postId'], $attributes['term'] ); |
| 26 |
if ( is_wp_error( $post_terms ) || empty( $post_terms ) ) { |
| 27 |
return ''; |
| 28 |
} |
| 29 |
|
| 30 |
$classes = 'taxonomy-' . $attributes['term']; |
| 31 |
if ( isset( $attributes['textAlign'] ) ) { |
| 32 |
$classes .= ' has-text-align-' . $attributes['textAlign']; |
| 33 |
} |
| 34 |
|
| 35 |
$separator = empty( $attributes['separator'] ) ? ' ' : $attributes['separator']; |
| 36 |
|
| 37 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) ); |
| 38 |
|
| 39 |
return get_the_term_list( |
| 40 |
$block->context['postId'], |
| 41 |
$attributes['term'], |
| 42 |
"<div $wrapper_attributes>", |
| 43 |
'<span class="wp-block-post-terms__separator">' . $separator . '</span>', |
| 44 |
'</div>' |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Registers the `core/post-terms` block on the server. |
| 50 |
*/ |
| 51 |
function gutenberg_register_block_core_post_terms() { |
| 52 |
register_block_type_from_metadata( |
| 53 |
__DIR__ . '/post-terms', |
| 54 |
array( |
| 55 |
'render_callback' => 'gutenberg_render_block_core_post_terms', |
| 56 |
) |
| 57 |
); |
| 58 |
} |
| 59 |
add_action( 'init', 'gutenberg_register_block_core_post_terms', 20 ); |
| 60 |
|