Нема описа

categories.php 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 %1$s><label class="screen-reader-text" for="' . $id . '">' . __( 'Categories' ) . '</label>%2$s</div>';
  29. $items_markup = wp_dropdown_categories( $args );
  30. $type = 'dropdown';
  31. if ( ! is_admin() ) {
  32. // Inject the dropdown script immediately after the select dropdown.
  33. $items_markup = preg_replace(
  34. '#(?<=</select>)#',
  35. build_dropdown_script_block_core_categories( $id ),
  36. $items_markup,
  37. 1
  38. );
  39. }
  40. } else {
  41. $wrapper_markup = '<ul %1$s>%2$s</ul>';
  42. $items_markup = wp_list_categories( $args );
  43. $type = 'list';
  44. }
  45. $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => "wp-block-categories-{$type}" ) );
  46. return sprintf(
  47. $wrapper_markup,
  48. $wrapper_attributes,
  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_from_metadata(
  83. __DIR__ . '/categories',
  84. array(
  85. 'render_callback' => 'render_block_core_categories',
  86. )
  87. );
  88. }
  89. add_action( 'init', 'register_block_core_categories' );