categories.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/categories` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/categories` block on server.
  9. *
  10. * @param array $attributes The block attributes.
  11. *
  12. * @return string Returns the categories list/dropdown markup.
  13. */
  14. function render_block_core_categories( $attributes ) {
  15. static $block_id = 0;
  16. $block_id++;
  17. $args = array(
  18. 'echo' => false,
  19. 'hierarchical' => ! empty( $attributes['showHierarchy'] ),
  20. 'orderby' => 'name',
  21. 'show_count' => ! empty( $attributes['showPostCounts'] ),
  22. 'title_li' => '',
  23. );
  24. if ( ! empty( $attributes['displayAsDropdown'] ) ) {
  25. $id = 'wp-block-categories-' . $block_id;
  26. $args['id'] = $id;
  27. $args['show_option_none'] = __( 'Select Category' );
  28. $wrapper_markup = '<div class="%1$s">%2$s</div>';
  29. $items_markup = wp_dropdown_categories( $args );
  30. $type = 'dropdown';
  31. if ( ! is_admin() ) {
  32. $wrapper_markup .= build_dropdown_script_block_core_categories( $id );
  33. }
  34. } else {
  35. $wrapper_markup = '<ul class="%1$s">%2$s</ul>';
  36. $items_markup = wp_list_categories( $args );
  37. $type = 'list';
  38. }
  39. $class = "wp-block-categories wp-block-categories-{$type}";
  40. if ( isset( $attributes['align'] ) ) {
  41. $class .= " align{$attributes['align']}";
  42. }
  43. if ( isset( $attributes['className'] ) ) {
  44. $class .= " {$attributes['className']}";
  45. }
  46. return sprintf(
  47. $wrapper_markup,
  48. esc_attr( $class ),
  49. $items_markup
  50. );
  51. }
  52. /**
  53. * Generates the inline script for a categories dropdown field.
  54. *
  55. * @param string $dropdown_id ID of the dropdown field.
  56. *
  57. * @return string Returns the dropdown onChange redirection script.
  58. */
  59. function build_dropdown_script_block_core_categories( $dropdown_id ) {
  60. ob_start();
  61. ?>
  62. <script type='text/javascript'>
  63. /* <![CDATA[ */
  64. ( function() {
  65. var dropdown = document.getElementById( '<?php echo esc_js( $dropdown_id ); ?>' );
  66. function onCatChange() {
  67. if ( dropdown.options[ dropdown.selectedIndex ].value > 0 ) {
  68. location.href = "<?php echo home_url(); ?>/?cat=" + dropdown.options[ dropdown.selectedIndex ].value;
  69. }
  70. }
  71. dropdown.onchange = onCatChange;
  72. })();
  73. /* ]]> */
  74. </script>
  75. <?php
  76. return ob_get_clean();
  77. }
  78. /**
  79. * Registers the `core/categories` block on server.
  80. */
  81. function register_block_core_categories() {
  82. register_block_type(
  83. 'core/categories',
  84. array(
  85. 'attributes' => array(
  86. 'align' => array(
  87. 'type' => 'string',
  88. 'enum' => array( 'left', 'center', 'right', 'wide', 'full' ),
  89. ),
  90. 'className' => array(
  91. 'type' => 'string',
  92. ),
  93. 'displayAsDropdown' => array(
  94. 'type' => 'boolean',
  95. 'default' => false,
  96. ),
  97. 'showHierarchy' => array(
  98. 'type' => 'boolean',
  99. 'default' => false,
  100. ),
  101. 'showPostCounts' => array(
  102. 'type' => 'boolean',
  103. 'default' => false,
  104. ),
  105. ),
  106. 'render_callback' => 'render_block_core_categories',
  107. )
  108. );
  109. }
  110. add_action( 'init', 'register_block_core_categories' );