tag-cloud.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/tag-cloud` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/tag-cloud` block on server.
  9. *
  10. * @param array $attributes The block attributes.
  11. *
  12. * @return string Returns the tag cloud for selected taxonomy.
  13. */
  14. function render_block_core_tag_cloud( $attributes ) {
  15. $class = isset( $attributes['align'] ) ?
  16. "wp-block-tag-cloud align{$attributes['align']}" :
  17. 'wp-block-tag-cloud';
  18. if ( isset( $attributes['className'] ) ) {
  19. $class .= ' ' . $attributes['className'];
  20. }
  21. $args = array(
  22. 'echo' => false,
  23. 'taxonomy' => $attributes['taxonomy'],
  24. 'show_count' => $attributes['showTagCounts'],
  25. );
  26. $tag_cloud = wp_tag_cloud( $args );
  27. if ( ! $tag_cloud ) {
  28. $labels = get_taxonomy_labels( get_taxonomy( $attributes['taxonomy'] ) );
  29. $tag_cloud = esc_html(
  30. sprintf(
  31. /* translators: %s: taxonomy name */
  32. __( 'Your site doesn&#8217;t have any %s, so there&#8217;s nothing to display here at the moment.' ),
  33. strtolower( $labels->name )
  34. )
  35. );
  36. }
  37. return sprintf(
  38. '<p class="%1$s">%2$s</p>',
  39. esc_attr( $class ),
  40. $tag_cloud
  41. );
  42. }
  43. /**
  44. * Registers the `core/tag-cloud` block on server.
  45. */
  46. function register_block_core_tag_cloud() {
  47. register_block_type(
  48. 'core/tag-cloud',
  49. array(
  50. 'attributes' => array(
  51. 'align' => array(
  52. 'type' => 'string',
  53. 'enum' => array( 'left', 'center', 'right', 'wide', 'full' ),
  54. ),
  55. 'className' => array(
  56. 'type' => 'string',
  57. ),
  58. 'taxonomy' => array(
  59. 'type' => 'string',
  60. 'default' => 'post_tag',
  61. ),
  62. 'showTagCounts' => array(
  63. 'type' => 'boolean',
  64. 'default' => false,
  65. ),
  66. ),
  67. 'render_callback' => 'render_block_core_tag_cloud',
  68. )
  69. );
  70. }
  71. add_action( 'init', 'register_block_core_tag_cloud' );