[ Index ]

PHP Cross Reference of WordPress

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   * @param array $attributes The block attributes.
  12   *
  13   * @return string Returns the categories list/dropdown markup.
  14   */
  15  function render_block_core_categories( $attributes ) {
  16      static $block_id = 0;
  17      $block_id++;
  18  
  19      $args = array(
  20          'echo'         => false,
  21          'hierarchical' => ! empty( $attributes['showHierarchy'] ),
  22          'orderby'      => 'name',
  23          'show_count'   => ! empty( $attributes['showPostCounts'] ),
  24          'title_li'     => '',
  25      );
  26      if ( ! empty( $attributes['showOnlyTopLevel'] ) && $attributes['showOnlyTopLevel'] ) {
  27          $args['parent'] = 0;
  28      }
  29  
  30      if ( ! empty( $attributes['displayAsDropdown'] ) ) {
  31          $id                       = 'wp-block-categories-' . $block_id;
  32          $args['id']               = $id;
  33          $args['show_option_none'] = __( 'Select Category' );
  34          $wrapper_markup           = '<div %1$s><label class="screen-reader-text" for="' . esc_attr( $id ) . '">' . __( 'Categories' ) . '</label>%2$s</div>';
  35          $items_markup             = wp_dropdown_categories( $args );
  36          $type                     = 'dropdown';
  37  
  38          if ( ! is_admin() ) {
  39              // Inject the dropdown script immediately after the select dropdown.
  40              $items_markup = preg_replace(
  41                  '#(?<=</select>)#',
  42                  build_dropdown_script_block_core_categories( $id ),
  43                  $items_markup,
  44                  1
  45              );
  46          }
  47      } else {
  48          $wrapper_markup = '<ul %1$s>%2$s</ul>';
  49          $items_markup   = wp_list_categories( $args );
  50          $type           = 'list';
  51      }
  52  
  53      $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => "wp-block-categories-{$type}" ) );
  54  
  55      return sprintf(
  56          $wrapper_markup,
  57          $wrapper_attributes,
  58          $items_markup
  59      );
  60  }
  61  
  62  /**
  63   * Generates the inline script for a categories dropdown field.
  64   *
  65   * @param string $dropdown_id ID of the dropdown field.
  66   *
  67   * @return string Returns the dropdown onChange redirection script.
  68   */
  69  function build_dropdown_script_block_core_categories( $dropdown_id ) {
  70      ob_start();
  71      ?>
  72      <script type='text/javascript'>
  73      /* <![CDATA[ */
  74      ( function() {
  75          var dropdown = document.getElementById( '<?php echo esc_js( $dropdown_id ); ?>' );
  76  		function onCatChange() {
  77              if ( dropdown.options[ dropdown.selectedIndex ].value > 0 ) {
  78                  location.href = "<?php echo home_url(); ?>/?cat=" + dropdown.options[ dropdown.selectedIndex ].value;
  79              }
  80          }
  81          dropdown.onchange = onCatChange;
  82      })();
  83      /* ]]> */
  84      </script>
  85      <?php
  86      return ob_get_clean();
  87  }
  88  
  89  /**
  90   * Registers the `core/categories` block on server.
  91   */
  92  function register_block_core_categories() {
  93      register_block_type_from_metadata(
  94          __DIR__ . '/categories',
  95          array(
  96              'render_callback' => 'render_block_core_categories',
  97          )
  98      );
  99  }
 100  add_action( 'init', 'register_block_core_categories' );


Generated: Fri Apr 26 01:00:03 2024 Cross-referenced by PHPXref 0.7.1