[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/blocks/ -> term-template.php (source)

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/term-template` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Renders the `core/term-template` block on the server.
  10   *
  11   * @since 6.9.0
  12   *
  13   * @param array    $attributes Block attributes.
  14   * @param string   $content    Block default content.
  15   * @param WP_Block $block      Block instance.
  16   *
  17   * @return string Returns the output of the term template.
  18   */
  19  function render_block_core_term_template( $attributes, $content, $block ) {
  20      if ( ! isset( $block->context ) || empty( $block->context['termQuery'] ) ) {
  21          return '';
  22      }
  23  
  24      $query = $block->context['termQuery'];
  25  
  26      $query_args = array(
  27          'number'     => $query['perPage'],
  28          'order'      => $query['order'],
  29          'orderby'    => $query['orderBy'],
  30          'hide_empty' => $query['hideEmpty'],
  31      );
  32  
  33      $inherit_query = isset( $query['inherit'] )
  34          && $query['inherit']
  35          && ( is_tax() || is_category() || is_tag() );
  36  
  37      if ( $inherit_query ) {
  38          // Get the current term and taxonomy from the queried object.
  39          $queried_object = get_queried_object();
  40  
  41          // For hierarchical taxonomies, show direct children of the current term.
  42          // For non-hierarchical taxonomies, show all terms (don't set parent).
  43          if ( is_taxonomy_hierarchical( $queried_object->taxonomy ) ) {
  44              $query_args['parent'] = $queried_object->term_id;
  45          }
  46          $query_args['taxonomy'] = $queried_object->taxonomy;
  47      } else {
  48          // If not inheriting set `taxonomy` from the block attribute.
  49          $query_args['taxonomy'] = $query['taxonomy'];
  50  
  51          // If we are including specific terms we ignore `showNested` argument.
  52          if ( ! empty( $query['include'] ) ) {
  53              $query_args['include'] = array_unique( array_map( 'intval', $query['include'] ) );
  54              $query_args['orderby'] = 'include';
  55              $query_args['order']   = 'asc';
  56          } elseif ( empty( $query['showNested'] ) ) {
  57              // We set parent only when inheriting from the taxonomy archive context or not
  58              // showing nested terms, otherwise nested terms are not displayed.
  59              $query_args['parent'] = 0;
  60          }
  61      }
  62  
  63      $terms_query = new WP_Term_Query( $query_args );
  64      $terms       = $terms_query->get_terms();
  65  
  66      if ( ! $terms || is_wp_error( $terms ) ) {
  67          return '';
  68      }
  69  
  70      $content = '';
  71      foreach ( $terms as $term ) {
  72          // Get an instance of the current Term Template block.
  73          $block_instance = $block->parsed_block;
  74  
  75          // Set the block name to one that does not correspond to an existing registered block.
  76          // This ensures that for the inner instances of the Term Template block, we do not render any block supports.
  77          $block_instance['blockName'] = 'core/null';
  78  
  79          $term_id  = $term->term_id;
  80          $taxonomy = $term->taxonomy;
  81  
  82          $filter_block_context = static function ( $context ) use ( $term_id, $taxonomy ) {
  83              $context['termId']   = $term_id;
  84              $context['taxonomy'] = $taxonomy;
  85              return $context;
  86          };
  87  
  88          // Use an early priority to so that other 'render_block_context' filters have access to the values.
  89          add_filter( 'render_block_context', $filter_block_context, 1 );
  90  
  91          // Render the inner blocks of the Term Template block with `dynamic` set to `false` to prevent calling
  92          // `render_callback` and ensure that no wrapper markup is included.
  93          $block_content = ( new WP_Block( $block_instance ) )->render( array( 'dynamic' => false ) );
  94  
  95          remove_filter( 'render_block_context', $filter_block_context, 1 );
  96  
  97          // Wrap the render inner blocks in a `li` element with the appropriate term classes.
  98          $term_classes = "wp-block-term term-{$term->term_id} {$term->taxonomy} taxonomy-{$term->taxonomy}";
  99  
 100          $content .= '<li class="' . esc_attr( $term_classes ) . '">' . $block_content . '</li>';
 101      }
 102  
 103      $classnames = '';
 104  
 105      if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
 106          $classnames .= 'has-link-color';
 107      }
 108  
 109      $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => trim( $classnames ) ) );
 110  
 111      return sprintf(
 112          '<ul %s>%s</ul>',
 113          $wrapper_attributes,
 114          $content
 115      );
 116  }
 117  
 118  /**
 119   * Registers the `core/term-template` block on the server.
 120   *
 121   * @since 6.9.0
 122   */
 123  function register_block_core_term_template() {
 124      register_block_type_from_metadata(
 125          __DIR__ . '/term-template',
 126          array(
 127              'render_callback' => 'render_block_core_term_template',
 128          )
 129      );
 130  }
 131  add_action( 'init', 'register_block_core_term_template' );


Generated : Wed Oct 22 08:20:04 2025 Cross-referenced by PHPXref