post-terms.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/post-terms` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/post-terms` block on the server.
  9. *
  10. * @param array $attributes Block attributes.
  11. * @param string $content Block default content.
  12. * @param WP_Block $block Block instance.
  13. * @return string Returns the filtered post terms for the current post wrapped inside "a" tags.
  14. */
  15. function render_block_core_post_terms( $attributes, $content, $block ) {
  16. if ( ! isset( $block->context['postId'] ) || ! isset( $attributes['term'] ) ) {
  17. return '';
  18. }
  19. if ( ! is_taxonomy_viewable( $attributes['term'] ) ) {
  20. return '';
  21. }
  22. $post_terms = get_the_terms( $block->context['postId'], $attributes['term'] );
  23. if ( is_wp_error( $post_terms ) || empty( $post_terms ) ) {
  24. return '';
  25. }
  26. $classes = 'taxonomy-' . $attributes['term'];
  27. if ( isset( $attributes['textAlign'] ) ) {
  28. $classes .= ' has-text-align-' . $attributes['textAlign'];
  29. }
  30. $separator = empty( $attributes['separator'] ) ? ' ' : $attributes['separator'];
  31. $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );
  32. $prefix = "<div $wrapper_attributes>";
  33. if ( isset( $attributes['prefix'] ) && $attributes['prefix'] ) {
  34. $prefix .= '<span class="wp-block-post-terms__prefix">' . $attributes['prefix'] . '</span>';
  35. }
  36. $suffix = '</div>';
  37. if ( isset( $attributes['suffix'] ) && $attributes['suffix'] ) {
  38. $suffix = '<span class="wp-block-post-terms__suffix">' . $attributes['suffix'] . '</span>' . $suffix;
  39. }
  40. return get_the_term_list(
  41. $block->context['postId'],
  42. $attributes['term'],
  43. wp_kses_post( $prefix ),
  44. '<span class="wp-block-post-terms__separator">' . esc_html( $separator ) . '</span>',
  45. wp_kses_post( $suffix )
  46. );
  47. }
  48. /**
  49. * Registers the `core/post-terms` block on the server.
  50. */
  51. function register_block_core_post_terms() {
  52. $taxonomies = get_taxonomies(
  53. array(
  54. 'public' => true,
  55. 'show_in_rest' => true,
  56. ),
  57. 'objects'
  58. );
  59. // Split the available taxonomies to `built_in` and custom ones,
  60. // in order to prioritize the `built_in` taxonomies at the
  61. // search results.
  62. $built_ins = array();
  63. $custom_variations = array();
  64. // Create and register the eligible taxonomies variations.
  65. foreach ( $taxonomies as $taxonomy ) {
  66. $variation = array(
  67. 'name' => $taxonomy->name,
  68. 'title' => $taxonomy->label,
  69. /* translators: %s: taxonomy's label */
  70. 'description' => sprintf( __( 'Display the assigned taxonomy: %s' ), $taxonomy->label ),
  71. 'attributes' => array(
  72. 'term' => $taxonomy->name,
  73. ),
  74. 'isActive' => array( 'term' ),
  75. );
  76. // Set the category variation as the default one.
  77. if ( 'category' === $taxonomy->name ) {
  78. $variation['isDefault'] = true;
  79. }
  80. if ( $taxonomy->_builtin ) {
  81. $built_ins[] = $variation;
  82. } else {
  83. $custom_variations[] = $variation;
  84. }
  85. }
  86. register_block_type_from_metadata(
  87. __DIR__ . '/post-terms',
  88. array(
  89. 'render_callback' => 'render_block_core_post_terms',
  90. 'variations' => array_merge( $built_ins, $custom_variations ),
  91. )
  92. );
  93. }
  94. add_action( 'init', 'register_block_core_post_terms' );