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


Generated : Thu Nov 21 08:20:01 2024 Cross-referenced by PHPXref