9
|
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 |
* @param array $attributes The block attributes. |
|
12 |
* |
|
13 |
* @return string Returns the tag cloud for selected taxonomy. |
|
14 |
*/ |
|
15 |
function render_block_core_tag_cloud( $attributes ) { |
19
|
16 |
$smallest_font_size = $attributes['smallestFontSize']; |
|
17 |
$unit = ( preg_match( '/^[0-9.]+(?P<unit>[a-z%]+)$/i', $smallest_font_size, $m ) ? $m['unit'] : 'pt' ); |
|
18 |
|
18
|
19 |
$args = array( |
9
|
20 |
'echo' => false, |
19
|
21 |
'unit' => $unit, |
9
|
22 |
'taxonomy' => $attributes['taxonomy'], |
|
23 |
'show_count' => $attributes['showTagCounts'], |
19
|
24 |
'number' => $attributes['numberOfTags'], |
|
25 |
'smallest' => floatVal( $attributes['smallestFontSize'] ), |
|
26 |
'largest' => floatVal( $attributes['largestFontSize'] ), |
9
|
27 |
); |
|
28 |
$tag_cloud = wp_tag_cloud( $args ); |
|
29 |
|
|
30 |
if ( ! $tag_cloud ) { |
16
|
31 |
$labels = get_taxonomy_labels( get_taxonomy( $attributes['taxonomy'] ) ); |
|
32 |
$tag_cloud = esc_html( |
|
33 |
sprintf( |
|
34 |
/* translators: %s: taxonomy name */ |
|
35 |
__( 'Your site doesn’t have any %s, so there’s nothing to display here at the moment.' ), |
|
36 |
strtolower( $labels->name ) |
|
37 |
) |
|
38 |
); |
9
|
39 |
} |
|
40 |
|
18
|
41 |
$wrapper_attributes = get_block_wrapper_attributes(); |
|
42 |
|
9
|
43 |
return sprintf( |
18
|
44 |
'<p %1$s>%2$s</p>', |
|
45 |
$wrapper_attributes, |
9
|
46 |
$tag_cloud |
|
47 |
); |
|
48 |
} |
|
49 |
|
|
50 |
/** |
|
51 |
* Registers the `core/tag-cloud` block on server. |
|
52 |
*/ |
|
53 |
function register_block_core_tag_cloud() { |
16
|
54 |
register_block_type_from_metadata( |
|
55 |
__DIR__ . '/tag-cloud', |
9
|
56 |
array( |
|
57 |
'render_callback' => 'render_block_core_tag_cloud', |
|
58 |
) |
|
59 |
); |
|
60 |
} |
|
61 |
add_action( 'init', 'register_block_core_tag_cloud' ); |