[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/blocks/ -> post-terms.php (source)

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


Generated : Thu Mar 28 08:20:01 2024 Cross-referenced by PHPXref