[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

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  
  27      if ( ! empty( $attributes['displayAsDropdown'] ) ) {
  28          $id                       = 'wp-block-categories-' . $block_id;
  29          $args['id']               = $id;
  30          $args['show_option_none'] = __( 'Select Category' );
  31          $wrapper_markup           = '<div class="%1$s">%2$s</div>';
  32          $items_markup             = wp_dropdown_categories( $args );
  33          $type                     = 'dropdown';
  34  
  35          if ( ! is_admin() ) {
  36              $wrapper_markup .= build_dropdown_script_block_core_categories( $id );
  37          }
  38      } else {
  39          $wrapper_markup = '<ul class="%1$s">%2$s</ul>';
  40          $items_markup   = wp_list_categories( $args );
  41          $type           = 'list';
  42      }
  43  
  44      $class = "wp-block-categories wp-block-categories-{$type}";
  45  
  46      if ( isset( $attributes['align'] ) ) {
  47          $class .= " align{$attributes['align']}";
  48      }
  49  
  50      if ( isset( $attributes['className'] ) ) {
  51          $class .= " {$attributes['className']}";
  52      }
  53  
  54      return sprintf(
  55          $wrapper_markup,
  56          esc_attr( $class ),
  57          $items_markup
  58      );
  59  }
  60  
  61  /**
  62   * Generates the inline script for a categories dropdown field.
  63   *
  64   * @param string $dropdown_id ID of the dropdown field.
  65   *
  66   * @return string Returns the dropdown onChange redirection script.
  67   */
  68  function build_dropdown_script_block_core_categories( $dropdown_id ) {
  69      ob_start();
  70      ?>
  71      <script type='text/javascript'>
  72      /* <![CDATA[ */
  73      ( function() {
  74          var dropdown = document.getElementById( '<?php echo esc_js( $dropdown_id ); ?>' );
  75  		function onCatChange() {
  76              if ( dropdown.options[ dropdown.selectedIndex ].value > 0 ) {
  77                  location.href = "<?php echo home_url(); ?>/?cat=" + dropdown.options[ dropdown.selectedIndex ].value;
  78              }
  79          }
  80          dropdown.onchange = onCatChange;
  81      })();
  82      /* ]]> */
  83      </script>
  84      <?php
  85      return ob_get_clean();
  86  }
  87  
  88  /**
  89   * Registers the `core/categories` block on server.
  90   */
  91  function register_block_core_categories() {
  92      register_block_type(
  93          'core/categories',
  94          array(
  95              'attributes'      => array(
  96                  'align'             => array(
  97                      'type' => 'string',
  98                      'enum' => array( 'left', 'center', 'right', 'wide', 'full' ),
  99                  ),
 100                  'className'         => array(
 101                      'type' => 'string',
 102                  ),
 103                  'displayAsDropdown' => array(
 104                      'type'    => 'boolean',
 105                      'default' => false,
 106                  ),
 107                  'showHierarchy'     => array(
 108                      'type'    => 'boolean',
 109                      'default' => false,
 110                  ),
 111                  'showPostCounts'    => array(
 112                      'type'    => 'boolean',
 113                      'default' => false,
 114                  ),
 115              ),
 116              'render_callback' => 'render_block_core_categories',
 117          )
 118      );
 119  }
 120  add_action( 'init', 'register_block_core_categories' );


Generated: Tue Oct 22 08:20:01 2019 Cross-referenced by PHPXref 0.7