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 / post-terms.php

post-terms.php in Gutenberg 22.9.0, at build/scripts/block-library/post-terms.php

131 lines 3.6 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/post-terms` block.
4 *
5 * @package WordPress
6 */
7
8 /**
9 * Renders the `core/post-terms` block on the server.
10 *
11 * @since 5.8.0
12 *
13 * @param array $attributes Block attributes.
14 * @param string $content Block default content.
15 * @param WP_Block $block Block instance.
16 * @return string Returns the filtered post terms for the current post wrapped inside "a" tags.
17 */
18 function gutenberg_render_block_core_post_terms( $attributes, $content, $block ) {
19 if ( ! isset( $block->context['postId'] ) || ! isset( $attributes['term'] ) ) {
20 return '';
21 }
22
23 if ( ! is_taxonomy_viewable( $attributes['term'] ) ) {
24 return '';
25 }
26
27 $classes = array( 'taxonomy-' . $attributes['term'] );
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 $separator = empty( $attributes['separator'] ) ? ' ' : $attributes['separator'];
36
37 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) );
38
39 $prefix = "<div $wrapper_attributes>";
40 if ( isset( $attributes['prefix'] ) && $attributes['prefix'] ) {
41 $prefix .= '<span class="wp-block-post-terms__prefix">' . $attributes['prefix'] . '</span>';
42 }
43
44 $suffix = '</div>';
45 if ( isset( $attributes['suffix'] ) && $attributes['suffix'] ) {
46 $suffix = '<span class="wp-block-post-terms__suffix">' . $attributes['suffix'] . '</span>' . $suffix;
47 }
48
49 $post_terms = get_the_term_list(
50 $block->context['postId'],
51 $attributes['term'],
52 wp_kses_post( $prefix ),
53 '<span class="wp-block-post-terms__separator">' . esc_html( $separator ) . '</span>',
54 wp_kses_post( $suffix )
55 );
56
57 if ( is_wp_error( $post_terms ) || empty( $post_terms ) ) {
58 return '';
59 }
60
61 return $post_terms;
62 }
63
64 /**
65 * Returns the available variations for the `core/post-terms` block.
66 *
67 * @since 6.5.0
68 *
69 * @return array The available variations for the block.
70 */
71 function gutenberg_block_core_post_terms_build_variations() {
72 $taxonomies = get_taxonomies(
73 array(
74 'publicly_queryable' => true,
75 'show_in_rest' => true,
76 ),
77 'objects'
78 );
79
80 // Split the available taxonomies to `built_in` and custom ones,
81 // in order to prioritize the `built_in` taxonomies at the
82 // search results.
83 $built_ins = array();
84 $custom_variations = array();
85
86 // Create and register the eligible taxonomies variations.
87 foreach ( $taxonomies as $taxonomy ) {
88 $variation = array(
89 'name' => $taxonomy->name,
90 'title' => $taxonomy->label,
91 'description' => sprintf(
92 /* translators: %s: taxonomy's label */
93 __( 'Display a list of assigned terms from the taxonomy: %s' ),
94 $taxonomy->label
95 ),
96 'attributes' => array(
97 'term' => $taxonomy->name,
98 ),
99 'isActive' => array( 'term' ),
100 'scope' => array( 'inserter', 'transform' ),
101 );
102 // Set the category variation as the default one.
103 if ( 'category' === $taxonomy->name ) {
104 $variation['isDefault'] = true;
105 }
106 if ( $taxonomy->_builtin ) {
107 $built_ins[] = $variation;
108 } else {
109 $custom_variations[] = $variation;
110 }
111 }
112
113 return array_merge( $built_ins, $custom_variations );
114 }
115
116 /**
117 * Registers the `core/post-terms` block on the server.
118 *
119 * @since 5.8.0
120 */
121 function gutenberg_register_block_core_post_terms() {
122 register_block_type_from_metadata(
123 __DIR__ . '/post-terms',
124 array(
125 'render_callback' => 'gutenberg_render_block_core_post_terms',
126 'variation_callback' => 'gutenberg_block_core_post_terms_build_variations',
127 )
128 );
129 }
130 add_action( 'init', 'gutenberg_register_block_core_post_terms', 20 );
131