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