| 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 gutenberg_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 |
// Pre-select the current term using query var. |
| 51 |
$args['selected'] = get_query_var( $taxonomy->query_var ); |
| 52 |
|
| 53 |
$show_label = empty( $attributes['showLabel'] ) ? ' screen-reader-text' : ''; |
| 54 |
$default_label = $taxonomy->label; |
| 55 |
$label_text = ! empty( $attributes['label'] ) ? wp_kses_post( $attributes['label'] ) : $default_label; |
| 56 |
$wrapper_markup = '<div %1$s><label class="wp-block-categories__label' . $show_label . '" for="' . esc_attr( $id ) . '">' . $label_text . '</label>%2$s</div>'; |
| 57 |
$items_markup = wp_dropdown_categories( $args ); |
| 58 |
$type = 'dropdown'; |
| 59 |
|
| 60 |
if ( ! is_admin() ) { |
| 61 |
// Inject the dropdown script immediately after the select dropdown. |
| 62 |
$items_markup = preg_replace( |
| 63 |
'#(?<=</select>)#', |
| 64 |
gutenberg_build_dropdown_script_block_core_categories( $id ), |
| 65 |
$items_markup, |
| 66 |
1 |
| 67 |
); |
| 68 |
} |
| 69 |
} else { |
| 70 |
$args['show_option_none'] = $taxonomy->labels->no_terms; |
| 71 |
|
| 72 |
$wrapper_markup = '<ul %1$s>%2$s</ul>'; |
| 73 |
$items_markup = wp_list_categories( $args ); |
| 74 |
$type = 'list'; |
| 75 |
|
| 76 |
if ( ! empty( $block->context['enhancedPagination'] ) ) { |
| 77 |
$p = new WP_HTML_Tag_Processor( $items_markup ); |
| 78 |
while ( $p->next_tag( 'a' ) ) { |
| 79 |
$p->set_attribute( 'data-wp-on--click', 'core/query::actions.navigate' ); |
| 80 |
} |
| 81 |
$items_markup = $p->get_updated_html(); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => "wp-block-categories-{$type} wp-block-categories-taxonomy-{$attributes['taxonomy']}" ) ); |
| 86 |
|
| 87 |
return sprintf( |
| 88 |
$wrapper_markup, |
| 89 |
$wrapper_attributes, |
| 90 |
$items_markup |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Generates the inline script for a categories dropdown field. |
| 96 |
* |
| 97 |
* @since 5.0.0 |
| 98 |
* |
| 99 |
* @param string $dropdown_id ID of the dropdown field. |
| 100 |
* |
| 101 |
* @return string Returns the dropdown onChange redirection script. |
| 102 |
*/ |
| 103 |
function gutenberg_build_dropdown_script_block_core_categories( $dropdown_id ) { |
| 104 |
ob_start(); |
| 105 |
|
| 106 |
$exports = array( $dropdown_id, home_url() ); |
| 107 |
?> |
| 108 |
<script> |
| 109 |
( ( [ dropdownId, homeUrl ] ) => { |
| 110 |
const dropdown = document.getElementById( dropdownId ); |
| 111 |
function gutenberg_onSelectChange() { |
| 112 |
setTimeout( () => { |
| 113 |
if ( 'escape' === dropdown.dataset.lastkey ) { |
| 114 |
return; |
| 115 |
} |
| 116 |
// Only navigate if a valid term is selected (not the default "Select [taxonomy]" option) |
| 117 |
if ( dropdown.value && dropdown.value !== '-1' && dropdown instanceof HTMLSelectElement ) { |
| 118 |
const url = new URL( homeUrl ); |
| 119 |
url.searchParams.set( dropdown.name, dropdown.value ); |
| 120 |
location.href = url.href; |
| 121 |
} |
| 122 |
}, 250 ); |
| 123 |
} |
| 124 |
function gutenberg_onKeyUp( event ) { |
| 125 |
if ( 'Escape' === event.key ) { |
| 126 |
dropdown.dataset.lastkey = 'escape'; |
| 127 |
} else { |
| 128 |
delete dropdown.dataset.lastkey; |
| 129 |
} |
| 130 |
} |
| 131 |
function gutenberg_onClick() { |
| 132 |
delete dropdown.dataset.lastkey; |
| 133 |
} |
| 134 |
dropdown.addEventListener( 'keyup', gutenberg_onKeyUp ); |
| 135 |
dropdown.addEventListener( 'click', gutenberg_onClick ); |
| 136 |
dropdown.addEventListener( 'change', gutenberg_onSelectChange ); |
| 137 |
} )( <?php echo wp_json_encode( $exports, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ); ?> ); |
| 138 |
</script> |
| 139 |
<?php |
| 140 |
return wp_get_inline_script_tag( |
| 141 |
trim( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) ) . |
| 142 |
"\n//# sourceURL=" . rawurlencode( __FUNCTION__ ) |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Registers the `core/categories` block on server. |
| 148 |
* |
| 149 |
* @since 5.0.0 |
| 150 |
*/ |
| 151 |
function gutenberg_register_block_core_categories() { |
| 152 |
register_block_type_from_metadata( |
| 153 |
__DIR__ . '/categories', |
| 154 |
array( |
| 155 |
'render_callback' => 'gutenberg_render_block_core_categories', |
| 156 |
) |
| 157 |
); |
| 158 |
} |
| 159 |
add_action( 'init', 'gutenberg_register_block_core_categories', 20 ); |
| 160 |
|