[ 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 * @since 5.0.0 12 * @since 6.7.0 Enable client-side rendering if enhancedPagination context is true. 13 * 14 * @param array $attributes The block attributes. 15 * @param string $content Block default content. 16 * @param WP_Block $block Block instance. 17 * 18 * @return string Returns the categories list/dropdown markup. 19 */ 20 function render_block_core_categories( $attributes, $content, $block ) { 21 static $block_id = 0; 22 ++$block_id; 23 24 $taxonomy = get_taxonomy( $attributes['taxonomy'] ); 25 26 $args = array( 27 'echo' => false, 28 'hierarchical' => ! empty( $attributes['showHierarchy'] ), 29 'orderby' => 'name', 30 'show_count' => ! empty( $attributes['showPostCounts'] ), 31 'taxonomy' => $attributes['taxonomy'], 32 'title_li' => '', 33 'hide_empty' => empty( $attributes['showEmpty'] ), 34 ); 35 if ( ! empty( $attributes['showOnlyTopLevel'] ) && $attributes['showOnlyTopLevel'] ) { 36 $args['parent'] = 0; 37 } 38 39 if ( ! empty( $attributes['displayAsDropdown'] ) ) { 40 $id = 'wp-block-categories-' . $block_id; 41 $args['id'] = $id; 42 $args['name'] = $taxonomy->query_var; 43 $args['value_field'] = 'slug'; 44 $args['show_option_none'] = sprintf( 45 /* translators: %s: taxonomy's singular name */ 46 __( 'Select %s' ), 47 $taxonomy->labels->singular_name 48 ); 49 50 $show_label = empty( $attributes['showLabel'] ) ? ' screen-reader-text' : ''; 51 $default_label = $taxonomy->label; 52 $label_text = ! empty( $attributes['label'] ) ? wp_kses_post( $attributes['label'] ) : $default_label; 53 $wrapper_markup = '<div %1$s><label class="wp-block-categories__label' . $show_label . '" for="' . esc_attr( $id ) . '">' . $label_text . '</label>%2$s</div>'; 54 $items_markup = wp_dropdown_categories( $args ); 55 $type = 'dropdown'; 56 57 if ( ! is_admin() ) { 58 // Inject the dropdown script immediately after the select dropdown. 59 $items_markup = preg_replace( 60 '#(?<=</select>)#', 61 build_dropdown_script_block_core_categories( $id ), 62 $items_markup, 63 1 64 ); 65 } 66 } else { 67 $args['show_option_none'] = $taxonomy->labels->no_terms; 68 69 $wrapper_markup = '<ul %1$s>%2$s</ul>'; 70 $items_markup = wp_list_categories( $args ); 71 $type = 'list'; 72 73 if ( ! empty( $block->context['enhancedPagination'] ) ) { 74 $p = new WP_HTML_Tag_Processor( $items_markup ); 75 while ( $p->next_tag( 'a' ) ) { 76 $p->set_attribute( 'data-wp-on--click', 'core/query::actions.navigate' ); 77 } 78 $items_markup = $p->get_updated_html(); 79 } 80 } 81 82 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => "wp-block-categories-{$type}" ) ); 83 84 return sprintf( 85 $wrapper_markup, 86 $wrapper_attributes, 87 $items_markup 88 ); 89 } 90 91 /** 92 * Generates the inline script for a categories dropdown field. 93 * 94 * @since 5.0.0 95 * 96 * @param string $dropdown_id ID of the dropdown field. 97 * 98 * @return string Returns the dropdown onChange redirection script. 99 */ 100 function build_dropdown_script_block_core_categories( $dropdown_id ) { 101 ob_start(); 102 ?> 103 <script> 104 ( function() { 105 var dropdown = document.getElementById( '<?php echo esc_js( $dropdown_id ); ?>' ); 106 function onCatChange() { 107 if ( dropdown.options[ dropdown.selectedIndex ].value !== -1 ) { 108 location.href = "<?php echo esc_url( home_url() ); ?>/?" + dropdown.name + '=' + dropdown.options[ dropdown.selectedIndex ].value; 109 } 110 } 111 dropdown.onchange = onCatChange; 112 })(); 113 </script> 114 <?php 115 return wp_get_inline_script_tag( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) ); 116 } 117 118 /** 119 * Registers the `core/categories` block on server. 120 * 121 * @since 5.0.0 122 */ 123 function register_block_core_categories() { 124 register_block_type_from_metadata( 125 __DIR__ . '/categories', 126 array( 127 'render_callback' => 'render_block_core_categories', 128 ) 129 ); 130 } 131 add_action( 'init', 'register_block_core_categories' );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Sat Dec 21 08:20:01 2024 | Cross-referenced by PHPXref |