PluginProbe
Gutenberg / 16.6.0
Gutenberg v16.6.0
24.0.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 All 403 releases
gutenberg / build / block-library / blocks / post-terms.php

post-terms.php in Gutenberg 16.6.0, at build/block-library/blocks/post-terms.php

112 lines 3.3 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 * @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 = array( 'taxonomy-' . $attributes['term'] );
31 if ( isset( $attributes['textAlign'] ) ) {
32 $classes[] = 'has-text-align-' . $attributes['textAlign'];
33 }
34 if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
35 $classes[] = 'has-link-color';
36 }
37
38 $separator = empty( $attributes['separator'] ) ? ' ' : $attributes['separator'];
39
40 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) );
41
42 $prefix = "<div $wrapper_attributes>";
43 if ( isset( $attributes['prefix'] ) && $attributes['prefix'] ) {
44 $prefix .= '<span class="wp-block-post-terms__prefix">' . $attributes['prefix'] . '</span>';
45 }
46
47 $suffix = '</div>';
48 if ( isset( $attributes['suffix'] ) && $attributes['suffix'] ) {
49 $suffix = '<span class="wp-block-post-terms__suffix">' . $attributes['suffix'] . '</span>' . $suffix;
50 }
51
52 return get_the_term_list(
53 $block->context['postId'],
54 $attributes['term'],
55 wp_kses_post( $prefix ),
56 '<span class="wp-block-post-terms__separator">' . esc_html( $separator ) . '</span>',
57 wp_kses_post( $suffix )
58 );
59 }
60
61 /**
62 * Registers the `core/post-terms` block on the server.
63 */
64 function gutenberg_register_block_core_post_terms() {
65 $taxonomies = get_taxonomies(
66 array(
67 'publicly_queryable' => true,
68 'show_in_rest' => true,
69 ),
70 'objects'
71 );
72
73 // Split the available taxonomies to `built_in` and custom ones,
74 // in order to prioritize the `built_in` taxonomies at the
75 // search results.
76 $built_ins = array();
77 $custom_variations = array();
78
79 // Create and register the eligible taxonomies variations.
80 foreach ( $taxonomies as $taxonomy ) {
81 $variation = array(
82 'name' => $taxonomy->name,
83 'title' => $taxonomy->label,
84 /* translators: %s: taxonomy's label */
85 'description' => sprintf( __( 'Display the assigned taxonomy: %s' ), $taxonomy->label ),
86 'attributes' => array(
87 'term' => $taxonomy->name,
88 ),
89 'isActive' => array( 'term' ),
90 'scope' => array( 'inserter', 'transform' ),
91 );
92 // Set the category variation as the default one.
93 if ( 'category' === $taxonomy->name ) {
94 $variation['isDefault'] = true;
95 }
96 if ( $taxonomy->_builtin ) {
97 $built_ins[] = $variation;
98 } else {
99 $custom_variations[] = $variation;
100 }
101 }
102
103 register_block_type_from_metadata(
104 __DIR__ . '/post-terms',
105 array(
106 'render_callback' => 'gutenberg_render_block_core_post_terms',
107 'variations' => array_merge( $built_ins, $custom_variations ),
108 )
109 );
110 }
111 add_action( 'init', 'gutenberg_register_block_core_post_terms', 20 );
112