1<?php
2/**
3 * Server-side rendering of the `core/tag-cloud` block.
4 *
5 * @package WordPress
6 */
7
8/**
9 * Renders the `core/tag-cloud` block on server.
10 *
11 * @since 5.2.0
12 *
13 * @param array $attributes The block attributes.
14 *
15 * @return string Returns the tag cloud for selected taxonomy.
16 */
17function render_block_core_tag_cloud( $attributes ) {
18 $smallest_font_size = $attributes['smallestFontSize'];
19 $unit = ( preg_match( '/^[0-9.]+(?P<unit>[a-z%]+)$/i', $smallest_font_size, $m ) ? $m['unit'] : 'pt' );
20
21 $args = array(
22 'echo' => false,
23 'unit' => $unit,
24 'taxonomy' => $attributes['taxonomy'],
25 'show_count' => $attributes['showTagCounts'],
26 'number' => $attributes['numberOfTags'],
27 'smallest' => floatVal( $attributes['smallestFontSize'] ),
28 'largest' => floatVal( $attributes['largestFontSize'] ),
29 );
30 $tag_cloud = wp_tag_cloud( $args );
31
32 if ( empty( $tag_cloud ) ) {
33 // Display placeholder content when there are no tags only in editor.
34 if ( wp_is_serving_rest_request() ) {
35 $tag_cloud = __( 'There’s no content to show here yet.' );
36 } else {
37 return '';
38 }
39 }
40
41 $wrapper_attributes = get_block_wrapper_attributes();
42
43 return sprintf(
44 '<p %1$s>%2$s</p>',
45 $wrapper_attributes,
46 $tag_cloud
47 );
48}
49
50/**
51 * Registers the `core/tag-cloud` block on server.
52 *
53 * @since 5.2.0
54 */
55function register_block_core_tag_cloud() {
56 register_block_type_from_metadata(
57 __DIR__ . '/tag-cloud',
58 array(
59 'render_callback' => 'render_block_core_tag_cloud',
60 )
61 );
62}
63add_action( 'init', 'register_block_core_tag_cloud' );
64