diff -r 7b1b88e27a20 -r 48c4eec2b7e6 wp/wp-includes/blocks/search.php
--- a/wp/wp-includes/blocks/search.php Thu Sep 29 08:06:27 2022 +0200
+++ b/wp/wp-includes/blocks/search.php Fri Sep 05 18:40:08 2025 +0200
@@ -8,13 +8,15 @@
/**
* Dynamically renders the `core/search` block.
*
- * @param array $attributes The block attributes.
+ * @since 6.3.0 Using block.json `viewScript` to register script, and update `view_script_handles()` only when needed.
+ *
+ * @param array $attributes The block attributes.
+ * @param string $content The saved content.
+ * @param WP_Block $block The parsed block.
*
* @return string The search block markup.
*/
function render_block_core_search( $attributes ) {
- static $instance_id = 0;
-
// Older versions of the Search block defaulted the label and buttonText
// attributes to `__( 'Search' )` meaning that many posts contain ``. Support these by defaulting an undefined label and
@@ -27,77 +29,138 @@
)
);
- $input_id = 'wp-block-search__input-' . ++$instance_id;
- $classnames = classnames_for_block_core_search( $attributes );
- $show_label = ( ! empty( $attributes['showLabel'] ) ) ? true : false;
- $use_icon_button = ( ! empty( $attributes['buttonUseIcon'] ) ) ? true : false;
- $show_input = ( ! empty( $attributes['buttonPosition'] ) && 'button-only' === $attributes['buttonPosition'] ) ? false : true;
- $show_button = ( ! empty( $attributes['buttonPosition'] ) && 'no-button' === $attributes['buttonPosition'] ) ? false : true;
- $input_markup = '';
- $button_markup = '';
- $inline_styles = styles_for_block_core_search( $attributes );
- $color_classes = get_color_classes_for_block_core_search( $attributes );
- $is_button_inside = ! empty( $attributes['buttonPosition'] ) &&
+ $input_id = wp_unique_id( 'wp-block-search__input-' );
+ $classnames = classnames_for_block_core_search( $attributes );
+ $show_label = ( ! empty( $attributes['showLabel'] ) ) ? true : false;
+ $use_icon_button = ( ! empty( $attributes['buttonUseIcon'] ) ) ? true : false;
+ $show_button = ( ! empty( $attributes['buttonPosition'] ) && 'no-button' === $attributes['buttonPosition'] ) ? false : true;
+ $button_position = $show_button ? $attributes['buttonPosition'] : null;
+ $query_params = ( ! empty( $attributes['query'] ) ) ? $attributes['query'] : array();
+ $button = '';
+ $query_params_markup = '';
+ $inline_styles = styles_for_block_core_search( $attributes );
+ $color_classes = get_color_classes_for_block_core_search( $attributes );
+ $typography_classes = get_typography_classes_for_block_core_search( $attributes );
+ $is_button_inside = ! empty( $attributes['buttonPosition'] ) &&
'button-inside' === $attributes['buttonPosition'];
// Border color classes need to be applied to the elements that have a border color.
$border_color_classes = get_border_color_classes_for_block_core_search( $attributes );
+ // This variable is a constant and its value is always false at this moment.
+ // It is defined this way because some values depend on it, in case it changes in the future.
+ $open_by_default = false;
$label_inner_html = empty( $attributes['label'] ) ? __( 'Search' ) : wp_kses_post( $attributes['label'] );
-
- $label_markup = sprintf(
- '',
- esc_attr( $input_id ),
- $label_inner_html
- );
- if ( $show_label && ! empty( $attributes['label'] ) ) {
- $label_markup = sprintf(
- '',
- $input_id,
- $label_inner_html
- );
+ $label = new WP_HTML_Tag_Processor( sprintf( '', $inline_styles['label'], $label_inner_html ) );
+ if ( $label->next_tag() ) {
+ $label->set_attribute( 'for', $input_id );
+ $label->add_class( 'wp-block-search__label' );
+ if ( $show_label && ! empty( $attributes['label'] ) ) {
+ if ( ! empty( $typography_classes ) ) {
+ $label->add_class( $typography_classes );
+ }
+ } else {
+ $label->add_class( 'screen-reader-text' );
+ }
}
- if ( $show_input ) {
- $input_classes = ! $is_button_inside ? $border_color_classes : '';
- $input_markup = sprintf(
- '',
- $input_id,
- esc_attr( $input_classes ),
- esc_attr( get_search_query() ),
- esc_attr( $attributes['placeholder'] ),
- $inline_styles['input']
- );
+ $input = new WP_HTML_Tag_Processor( sprintf( '', $inline_styles['input'] ) );
+ $input_classes = array( 'wp-block-search__input' );
+ if ( ! $is_button_inside && ! empty( $border_color_classes ) ) {
+ $input_classes[] = $border_color_classes;
+ }
+ if ( ! empty( $typography_classes ) ) {
+ $input_classes[] = $typography_classes;
+ }
+ if ( $input->next_tag() ) {
+ $input->add_class( implode( ' ', $input_classes ) );
+ $input->set_attribute( 'id', $input_id );
+ $input->set_attribute( 'value', get_search_query() );
+ $input->set_attribute( 'placeholder', $attributes['placeholder'] );
+
+ // If it's interactive, enqueue the script module and add the directives.
+ $is_expandable_searchfield = 'button-only' === $button_position;
+ if ( $is_expandable_searchfield ) {
+ $suffix = wp_scripts_get_suffix();
+ if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ) {
+ $module_url = gutenberg_url( '/build/interactivity/search.min.js' );
+ }
+
+ wp_register_script_module(
+ '@wordpress/block-library/search',
+ isset( $module_url ) ? $module_url : includes_url( "blocks/search/view{$suffix}.js" ),
+ array( '@wordpress/interactivity' ),
+ defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : get_bloginfo( 'version' )
+ );
+ wp_enqueue_script_module( '@wordpress/block-library/search' );
+
+ $input->set_attribute( 'data-wp-bind--aria-hidden', '!context.isSearchInputVisible' );
+ $input->set_attribute( 'data-wp-bind--tabindex', 'state.tabindex' );
+
+ // Adding these attributes manually is needed until the Interactivity API
+ // SSR logic is added to core.
+ $input->set_attribute( 'aria-hidden', 'true' );
+ $input->set_attribute( 'tabindex', '-1' );
+ }
+ }
+
+ if ( count( $query_params ) > 0 ) {
+ foreach ( $query_params as $param => $value ) {
+ $query_params_markup .= sprintf(
+ '',
+ esc_attr( $param ),
+ esc_attr( $value )
+ );
+ }
}
if ( $show_button ) {
+ $button_classes = array( 'wp-block-search__button' );
$button_internal_markup = '';
- $button_classes = $color_classes;
- $aria_label = '';
+ if ( ! empty( $color_classes ) ) {
+ $button_classes[] = $color_classes;
+ }
+ if ( ! empty( $typography_classes ) ) {
+ $button_classes[] = $typography_classes;
+ }
- if ( ! $is_button_inside ) {
- $button_classes .= ' ' . $border_color_classes;
+ if ( ! $is_button_inside && ! empty( $border_color_classes ) ) {
+ $button_classes[] = $border_color_classes;
}
if ( ! $use_icon_button ) {
if ( ! empty( $attributes['buttonText'] ) ) {
$button_internal_markup = wp_kses_post( $attributes['buttonText'] );
}
} else {
- $aria_label = sprintf( 'aria-label="%s"', esc_attr( wp_strip_all_tags( $attributes['buttonText'] ) ) );
- $button_classes .= ' has-icon';
-
+ $button_classes[] = 'has-icon';
$button_internal_markup =
- '