| 1 |
<?php |
| 2 |
/** |
| 3 |
* Server-side rendering of the `core/term-name` block. |
| 4 |
* |
| 5 |
* @package WordPress |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Renders the `core/term-name` block on the server. |
| 10 |
* |
| 11 |
* @since 6.9.0 |
| 12 |
* |
| 13 |
* @param array $attributes Block attributes. |
| 14 |
* @param string $content Block default content. |
| 15 |
* @param WP_Block $block Block instance. |
| 16 |
* |
| 17 |
* @return string Returns the name of the current taxonomy term wrapped inside a heading tag. |
| 18 |
*/ |
| 19 |
function gutenberg_render_block_core_term_name( $attributes, $content, $block ) { |
| 20 |
$term_name = ''; |
| 21 |
|
| 22 |
// Get term from context or from the current query. |
| 23 |
if ( isset( $block->context['termId'] ) && isset( $block->context['taxonomy'] ) ) { |
| 24 |
$term = get_term( $block->context['termId'], $block->context['taxonomy'] ); |
| 25 |
} else { |
| 26 |
$term = get_queried_object(); |
| 27 |
if ( ! $term instanceof WP_Term ) { |
| 28 |
$term = null; |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
if ( ! $term || is_wp_error( $term ) ) { |
| 33 |
return ''; |
| 34 |
} |
| 35 |
|
| 36 |
$term_name = $term->name; |
| 37 |
$level = $attributes['level'] ?? 0; |
| 38 |
$tag_name = 0 === $level ? 'p' : 'h' . (int) $level; |
| 39 |
|
| 40 |
if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) { |
| 41 |
$term_link = get_term_link( $term ); |
| 42 |
if ( ! is_wp_error( $term_link ) ) { |
| 43 |
$term_name = sprintf( |
| 44 |
'<a href="%1$s">%2$s</a>', |
| 45 |
esc_url( $term_link ), |
| 46 |
$term_name |
| 47 |
); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
$classes = array(); |
| 52 |
if ( isset( $attributes['textAlign'] ) ) { |
| 53 |
$classes[] = 'has-text-align-' . $attributes['textAlign']; |
| 54 |
} |
| 55 |
if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) { |
| 56 |
$classes[] = 'has-link-color'; |
| 57 |
} |
| 58 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) ); |
| 59 |
|
| 60 |
return sprintf( |
| 61 |
'<%1$s %2$s>%3$s</%1$s>', |
| 62 |
$tag_name, |
| 63 |
$wrapper_attributes, |
| 64 |
$term_name |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Registers the `core/term-name` block on the server. |
| 70 |
* |
| 71 |
* @since 6.9.0 |
| 72 |
*/ |
| 73 |
function gutenberg_register_block_core_term_name() { |
| 74 |
register_block_type_from_metadata( |
| 75 |
__DIR__ . '/term-name', |
| 76 |
array( |
| 77 |
'render_callback' => 'gutenberg_render_block_core_term_name', |
| 78 |
) |
| 79 |
); |
| 80 |
} |
| 81 |
add_action( 'init', 'gutenberg_register_block_core_term_name', 20 ); |
| 82 |
|