| 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 gutenberg_render_block_core_tag_cloud( $attributes ) { |
| 16 |
$class = isset( $attributes['align'] ) ? |
| 17 |
"wp-block-tag-cloud align{$attributes['align']}" : |
| 18 |
'wp-block-tag-cloud'; |
| 19 |
|
| 20 |
if ( isset( $attributes['className'] ) ) { |
| 21 |
$class .= ' ' . $attributes['className']; |
| 22 |
} |
| 23 |
|
| 24 |
$args = array( |
| 25 |
'echo' => false, |
| 26 |
'taxonomy' => $attributes['taxonomy'], |
| 27 |
'show_count' => $attributes['showTagCounts'], |
| 28 |
); |
| 29 |
|
| 30 |
$tag_cloud = wp_tag_cloud( $args ); |
| 31 |
|
| 32 |
if ( ! $tag_cloud ) { |
| 33 |
$labels = get_taxonomy_labels( get_taxonomy( $attributes['taxonomy'] ) ); |
| 34 |
$tag_cloud = esc_html( |
| 35 |
sprintf( |
| 36 |
/* translators: %s: taxonomy name */ |
| 37 |
__( 'Your site doesn’t have any %s, so there’s nothing to display here at the moment.' ), |
| 38 |
strtolower( $labels->name ) |
| 39 |
) |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
return sprintf( |
| 44 |
'<p class="%1$s">%2$s</p>', |
| 45 |
esc_attr( $class ), |
| 46 |
$tag_cloud |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Registers the `core/tag-cloud` block on server. |
| 52 |
*/ |
| 53 |
function gutenberg_register_block_core_tag_cloud() { |
| 54 |
register_block_type( |
| 55 |
'core/tag-cloud', |
| 56 |
array( |
| 57 |
'attributes' => array( |
| 58 |
'align' => array( |
| 59 |
'type' => 'string', |
| 60 |
'enum' => array( 'left', 'center', 'right', 'wide', 'full' ), |
| 61 |
), |
| 62 |
'className' => array( |
| 63 |
'type' => 'string', |
| 64 |
), |
| 65 |
'taxonomy' => array( |
| 66 |
'type' => 'string', |
| 67 |
'default' => 'post_tag', |
| 68 |
), |
| 69 |
'showTagCounts' => array( |
| 70 |
'type' => 'boolean', |
| 71 |
'default' => false, |
| 72 |
), |
| 73 |
), |
| 74 |
'render_callback' => 'gutenberg_render_block_core_tag_cloud', |
| 75 |
) |
| 76 |
); |
| 77 |
} |
| 78 |
add_action( 'init', 'gutenberg_register_block_core_tag_cloud', 20 ); |
| 79 |
|