kollapsminoriteten/wp-includes/blocks/categories.php

156 lines
4.5 KiB
PHP
Raw Normal View History

2019-11-02 10:38:58 +01:00
<?php
/**
* Server-side rendering of the `core/categories` block.
*
* @package WordPress
*/
/**
* Renders the `core/categories` block on server.
*
2025-02-28 08:42:11 +01:00
* @since 5.0.0
* @since 6.7.0 Enable client-side rendering if enhancedPagination context is true.
*
* @param array $attributes The block attributes.
* @param string $content Block default content.
* @param WP_Block $block Block instance.
2019-11-02 10:38:58 +01:00
*
* @return string Returns the categories list/dropdown markup.
*/
2025-02-28 08:42:11 +01:00
function render_block_core_categories( $attributes, $content, $block ) {
2019-11-02 10:38:58 +01:00
static $block_id = 0;
2023-04-26 17:39:43 +02:00
++$block_id;
2019-11-02 10:38:58 +01:00
2025-02-28 08:42:11 +01:00
$taxonomy = get_taxonomy( $attributes['taxonomy'] );
2019-11-02 10:38:58 +01:00
$args = array(
'echo' => false,
'hierarchical' => ! empty( $attributes['showHierarchy'] ),
'orderby' => 'name',
'show_count' => ! empty( $attributes['showPostCounts'] ),
2025-02-28 08:42:11 +01:00
'taxonomy' => $attributes['taxonomy'],
2019-11-02 10:38:58 +01:00
'title_li' => '',
2022-12-15 17:47:31 +01:00
'hide_empty' => empty( $attributes['showEmpty'] ),
2019-11-02 10:38:58 +01:00
);
2022-04-02 10:26:41 +02:00
if ( ! empty( $attributes['showOnlyTopLevel'] ) && $attributes['showOnlyTopLevel'] ) {
$args['parent'] = 0;
}
2019-11-02 10:38:58 +01:00
if ( ! empty( $attributes['displayAsDropdown'] ) ) {
$id = 'wp-block-categories-' . $block_id;
$args['id'] = $id;
2025-02-28 08:42:11 +01:00
$args['name'] = $taxonomy->query_var;
$args['value_field'] = 'slug';
$args['show_option_none'] = sprintf(
/* translators: %s: taxonomy's singular name */
__( 'Select %s' ),
$taxonomy->labels->singular_name
);
$show_label = empty( $attributes['showLabel'] ) ? ' screen-reader-text' : '';
$default_label = $taxonomy->label;
$label_text = ! empty( $attributes['label'] ) ? wp_kses_post( $attributes['label'] ) : $default_label;
$wrapper_markup = '<div %1$s><label class="wp-block-categories__label' . $show_label . '" for="' . esc_attr( $id ) . '">' . $label_text . '</label>%2$s</div>';
$items_markup = wp_dropdown_categories( $args );
$type = 'dropdown';
2019-11-02 10:38:58 +01:00
if ( ! is_admin() ) {
2020-12-10 14:06:04 +01:00
// Inject the dropdown script immediately after the select dropdown.
$items_markup = preg_replace(
'#(?<=</select>)#',
build_dropdown_script_block_core_categories( $id ),
$items_markup,
1
);
2019-11-02 10:38:58 +01:00
}
} else {
2025-02-28 08:42:11 +01:00
$args['show_option_none'] = $taxonomy->labels->no_terms;
2020-12-10 14:06:04 +01:00
$wrapper_markup = '<ul %1$s>%2$s</ul>';
2019-11-02 10:38:58 +01:00
$items_markup = wp_list_categories( $args );
$type = 'list';
2025-02-28 08:42:11 +01:00
if ( ! empty( $block->context['enhancedPagination'] ) ) {
$p = new WP_HTML_Tag_Processor( $items_markup );
while ( $p->next_tag( 'a' ) ) {
$p->set_attribute( 'data-wp-on--click', 'core/query::actions.navigate' );
}
$items_markup = $p->get_updated_html();
}
2019-11-02 10:38:58 +01:00
}
2020-12-10 14:06:04 +01:00
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => "wp-block-categories-{$type}" ) );
2019-11-02 10:38:58 +01:00
2019-11-15 22:59:44 +01:00
return sprintf(
2019-11-02 10:38:58 +01:00
$wrapper_markup,
2020-12-10 14:06:04 +01:00
$wrapper_attributes,
2019-11-02 10:38:58 +01:00
$items_markup
);
}
/**
* Generates the inline script for a categories dropdown field.
*
2025-02-28 08:42:11 +01:00
* @since 5.0.0
*
2019-11-02 10:38:58 +01:00
* @param string $dropdown_id ID of the dropdown field.
*
* @return string Returns the dropdown onChange redirection script.
*/
function build_dropdown_script_block_core_categories( $dropdown_id ) {
ob_start();
2025-12-12 13:15:55 +01:00
$exports = array( $dropdown_id, home_url() );
2019-11-02 10:38:58 +01:00
?>
2024-04-17 11:32:24 +02:00
<script>
2025-12-12 13:15:55 +01:00
( ( [ dropdownId, homeUrl ] ) => {
const dropdown = document.getElementById( dropdownId );
function onSelectChange() {
setTimeout( () => {
if ( 'escape' === dropdown.dataset.lastkey ) {
return;
}
if ( dropdown.value && dropdown instanceof HTMLSelectElement ) {
const url = new URL( homeUrl );
url.searchParams.set( dropdown.name, dropdown.value );
location.href = url.href;
}
}, 250 );
}
function onKeyUp( event ) {
if ( 'Escape' === event.key ) {
dropdown.dataset.lastkey = 'escape';
} else {
delete dropdown.dataset.lastkey;
2019-11-02 10:38:58 +01:00
}
}
2025-12-12 13:15:55 +01:00
function onClick() {
delete dropdown.dataset.lastkey;
}
dropdown.addEventListener( 'keyup', onKeyUp );
dropdown.addEventListener( 'click', onClick );
dropdown.addEventListener( 'change', onSelectChange );
} )( <?php echo wp_json_encode( $exports, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ); ?> );
2019-11-02 10:38:58 +01:00
</script>
<?php
2025-12-12 13:15:55 +01:00
return wp_get_inline_script_tag(
trim( str_replace( array( '<script>', '</script>' ), '', ob_get_clean() ) ) .
"\n//# sourceURL=" . rawurlencode( __FUNCTION__ )
);
2019-11-02 10:38:58 +01:00
}
/**
* Registers the `core/categories` block on server.
2025-02-28 08:42:11 +01:00
*
* @since 5.0.0
2019-11-02 10:38:58 +01:00
*/
function register_block_core_categories() {
2020-09-15 14:29:22 +02:00
register_block_type_from_metadata(
__DIR__ . '/categories',
2019-11-02 10:38:58 +01:00
array(
'render_callback' => 'render_block_core_categories',
)
);
}
add_action( 'init', 'register_block_core_categories' );