[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/blocks/ -> categories.php (source)

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/categories` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Renders the `core/categories` block on server.
  10   *
  11   * @since 5.0.0
  12   * @since 6.7.0 Enable client-side rendering if enhancedPagination context is true.
  13   *
  14   * @param array    $attributes The block attributes.
  15   * @param string   $content    Block default content.
  16   * @param WP_Block $block      Block instance.
  17   *
  18   * @return string Returns the categories list/dropdown markup.
  19   */
  20  function render_block_core_categories( $attributes, $content, $block ) {
  21      static $block_id = 0;
  22      ++$block_id;
  23  
  24      $taxonomy = get_taxonomy( $attributes['taxonomy'] );
  25  
  26      if ( ! $taxonomy ) {
  27          return '';
  28      }
  29  
  30      $args = array(
  31          'echo'         => false,
  32          'hierarchical' => ! empty( $attributes['showHierarchy'] ),
  33          'orderby'      => 'name',
  34          'show_count'   => ! empty( $attributes['showPostCounts'] ),
  35          'taxonomy'     => $attributes['taxonomy'],
  36          'title_li'     => '',
  37          'hide_empty'   => empty( $attributes['showEmpty'] ),
  38      );
  39      if ( ! empty( $attributes['showOnlyTopLevel'] ) && $attributes['showOnlyTopLevel'] ) {
  40          $args['parent'] = 0;
  41      }
  42  
  43      if ( ! empty( $attributes['displayAsDropdown'] ) ) {
  44          $id                       = 'wp-block-categories-' . $block_id;
  45          $args['id']               = $id;
  46          $args['name']             = $taxonomy->query_var;
  47          $args['value_field']      = 'slug';
  48          $args['show_option_none'] = sprintf(
  49              /* translators: %s: taxonomy's singular name */
  50              __( 'Select %s' ),
  51              $taxonomy->labels->singular_name
  52          );
  53  
  54          // Pre-select the current term using query var.
  55          $args['selected'] = get_query_var( $taxonomy->query_var );
  56  
  57          $show_label     = empty( $attributes['showLabel'] ) ? ' screen-reader-text' : '';
  58          $default_label  = $taxonomy->label;
  59          $label_text     = ! empty( $attributes['label'] ) ? wp_kses_post( $attributes['label'] ) : $default_label;
  60          $wrapper_markup = '<div %1$s><label class="wp-block-categories__label' . $show_label . '" for="' . esc_attr( $id ) . '">%3$s</label>%2$s</div>';
  61          $items_markup   = wp_dropdown_categories( $args );
  62          $type           = 'dropdown';
  63  
  64          if ( ! is_admin() ) {
  65              // Inject the dropdown script immediately after the select dropdown.
  66              $items_markup = preg_replace(
  67                  '#(?<=</select>)#',
  68                  build_dropdown_script_block_core_categories( $id ),
  69                  $items_markup,
  70                  1
  71              );
  72          }
  73      } else {
  74          $args['show_option_none'] = $taxonomy->labels->no_terms;
  75  
  76          $label_text     = '';
  77          $wrapper_markup = '<ul %1$s>%2$s</ul>';
  78          $items_markup   = wp_list_categories( $args );
  79          $type           = 'list';
  80  
  81          if ( ! empty( $block->context['enhancedPagination'] ) ) {
  82              $p = new WP_HTML_Tag_Processor( $items_markup );
  83              while ( $p->next_tag( 'a' ) ) {
  84                  $p->set_attribute( 'data-wp-on--click', 'core/query::actions.navigate' );
  85              }
  86              $items_markup = $p->get_updated_html();
  87          }
  88      }
  89  
  90      $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => "wp-block-categories-{$type} wp-block-categories-taxonomy-{$attributes['taxonomy']}" ) );
  91  
  92      return sprintf(
  93          $wrapper_markup,
  94          $wrapper_attributes,
  95          $items_markup,
  96          $label_text
  97      );
  98  }
  99  
 100  /**
 101   * Generates the inline script for a categories dropdown field.
 102   *
 103   * @since 5.0.0
 104   *
 105   * @param string $dropdown_id ID of the dropdown field.
 106   *
 107   * @return string Returns the dropdown onChange redirection script.
 108   */
 109  function build_dropdown_script_block_core_categories( $dropdown_id ) {
 110      ob_start();
 111  
 112      $exports = array( $dropdown_id, home_url() );
 113      ?>
 114      <script>
 115      ( ( [ dropdownId, homeUrl ] ) => {
 116          const dropdown = document.getElementById( dropdownId );
 117  		function onSelectChange() {
 118              setTimeout( () => {
 119                  if ( 'escape' === dropdown.dataset.lastkey ) {
 120                      return;
 121                  }
 122                  // Only navigate if a valid term is selected (not the default "Select [taxonomy]" option)
 123                  if ( dropdown.value && dropdown.value !== '-1' && dropdown instanceof HTMLSelectElement ) {
 124                      const url = new URL( homeUrl );
 125                      url.searchParams.set( dropdown.name, dropdown.value );
 126                      location.href = url.href;
 127                  }
 128              }, 250 );
 129          }
 130  		function onKeyUp( event ) {
 131              if ( 'Escape' === event.key ) {
 132                  dropdown.dataset.lastkey = 'escape';
 133              } else {
 134                  delete dropdown.dataset.lastkey;
 135              }
 136          }
 137  		function onClick() {
 138              delete dropdown.dataset.lastkey;
 139          }
 140          dropdown.addEventListener( 'keyup', onKeyUp );
 141          dropdown.addEventListener( 'click', onClick );
 142          dropdown.addEventListener( 'change', onSelectChange );
 143      } )( <?php echo wp_json_encode( $exports, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ); ?> );
 144      </script>
 145      <?php
 146      return wp_get_inline_script_tag(
 147          trim( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) ) .
 148          "\n//# sourceURL=" . rawurlencode( __FUNCTION__ )
 149      );
 150  }
 151  
 152  /**
 153   * Registers the `core/categories` block on server.
 154   *
 155   * @since 5.0.0
 156   */
 157  function register_block_core_categories() {
 158      register_block_type_from_metadata(
 159          __DIR__ . '/categories',
 160          array(
 161              'render_callback' => 'render_block_core_categories',
 162          )
 163      );
 164  }
 165  add_action( 'init', 'register_block_core_categories' );


Generated : Tue Nov 3 08:20:36 2026 Cross-referenced by PHPXref