1<?php
2/**
3 * Server-side rendering of the `core/term-description` block.
4 *
5 * @package WordPress
6 */
7
8/**
9 * Renders the `core/term-description` block on the server.
10 *
11 * @since 5.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 description of the current taxonomy term, if available
18 */
19function render_block_core_term_description( $attributes, $content, $block ) {
20 $term_description = '';
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 if ( $term && ! is_wp_error( $term ) ) {
26 $term_description = $term->description;
27 }
28 } elseif ( is_category() || is_tag() || is_tax() ) {
29 $term_description = term_description();
30 }
31
32 if ( empty( $term_description ) ) {
33 return '';
34 }
35
36 $classes = array();
37 if ( isset( $attributes['textAlign'] ) ) {
38 $classes[] = 'has-text-align-' . $attributes['textAlign'];
39 }
40 if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
41 $classes[] = 'has-link-color';
42 }
43 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) );
44
45 return '<div ' . $wrapper_attributes . '>' . $term_description . '</div>';
46}
47
48/**
49 * Registers the `core/term-description` block on the server.
50 *
51 * @since 5.9.0
52 */
53function register_block_core_term_description() {
54 register_block_type_from_metadata(
55 __DIR__ . '/term-description',
56 array(
57 'render_callback' => 'render_block_core_term_description',
58 )
59 );
60}
61add_action( 'init', 'register_block_core_term_description' );
62