PluginProbe
Gutenberg / 22.9.0
Gutenberg v22.9.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / build / scripts / block-library / term-name.php

term-name.php in Gutenberg 22.9.0, at build/scripts/block-library/term-name.php

82 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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