No Description

post-terms.php 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. return get_the_term_list(
  33. $block->context['postId'],
  34. $attributes['term'],
  35. "<div $wrapper_attributes>",
  36. '<span class="wp-block-post-terms__separator">' . esc_html( $separator ) . '</span>',
  37. '</div>'
  38. );
  39. }
  40. /**
  41. * Registers the `core/post-terms` block on the server.
  42. */
  43. function register_block_core_post_terms() {
  44. register_block_type_from_metadata(
  45. __DIR__ . '/post-terms',
  46. array(
  47. 'render_callback' => 'render_block_core_post_terms',
  48. )
  49. );
  50. }
  51. add_action( 'init', 'register_block_core_post_terms' );