[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
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 'hide_empty' => empty( $attributes['showEmpty'] ), 26 ); 27 if ( ! empty( $attributes['showOnlyTopLevel'] ) && $attributes['showOnlyTopLevel'] ) { 28 $args['parent'] = 0; 29 } 30 31 if ( ! empty( $attributes['displayAsDropdown'] ) ) { 32 $id = 'wp-block-categories-' . $block_id; 33 $args['id'] = $id; 34 $args['show_option_none'] = __( 'Select Category' ); 35 $wrapper_markup = '<div %1$s><label class="screen-reader-text" for="' . esc_attr( $id ) . '">' . __( 'Categories' ) . '</label>%2$s</div>'; 36 $items_markup = wp_dropdown_categories( $args ); 37 $type = 'dropdown'; 38 39 if ( ! is_admin() ) { 40 // Inject the dropdown script immediately after the select dropdown. 41 $items_markup = preg_replace( 42 '#(?<=</select>)#', 43 build_dropdown_script_block_core_categories( $id ), 44 $items_markup, 45 1 46 ); 47 } 48 } else { 49 $wrapper_markup = '<ul %1$s>%2$s</ul>'; 50 $items_markup = wp_list_categories( $args ); 51 $type = 'list'; 52 } 53 54 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => "wp-block-categories-{$type}" ) ); 55 56 return sprintf( 57 $wrapper_markup, 58 $wrapper_attributes, 59 $items_markup 60 ); 61 } 62 63 /** 64 * Generates the inline script for a categories dropdown field. 65 * 66 * @param string $dropdown_id ID of the dropdown field. 67 * 68 * @return string Returns the dropdown onChange redirection script. 69 */ 70 function build_dropdown_script_block_core_categories( $dropdown_id ) { 71 ob_start(); 72 ?> 73 <script> 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 esc_url( home_url() ); ?>/?cat=" + dropdown.options[ dropdown.selectedIndex ].value; 79 } 80 } 81 dropdown.onchange = onCatChange; 82 })(); 83 </script> 84 <?php 85 return wp_get_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', 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_from_metadata( 93 __DIR__ . '/categories', 94 array( 95 'render_callback' => 'render_block_core_categories', 96 ) 97 ); 98 } 99 add_action( 'init', 'register_block_core_categories' );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Fri Apr 19 08:20:01 2024 | Cross-referenced by PHPXref |