No Description

categories.php 2.7KB

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