Changeset 57377 for trunk/src/wp-includes/blocks/search.php
- Timestamp:
- 01/29/2024 09:04:18 PM (9 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks/search.php
r56849 r57377 17 17 * @return string The search block markup. 18 18 */ 19 function render_block_core_search( $attributes , $content, $block) {19 function render_block_core_search( $attributes ) { 20 20 // Older versions of the Search block defaulted the label and buttonText 21 21 // attributes to `__( 'Search' )` meaning that many posts contain `<!-- … … 37 37 $button_position = $show_button ? $attributes['buttonPosition'] : null; 38 38 $query_params = ( ! empty( $attributes['query'] ) ) ? $attributes['query'] : array(); 39 $button_behavior = ( ! empty( $attributes['buttonBehavior'] ) ) ? $attributes['buttonBehavior'] : 'default';40 39 $button = ''; 41 40 $query_params_markup = ''; … … 79 78 $input->set_attribute( 'placeholder', $attributes['placeholder'] ); 80 79 81 $is_expandable_searchfield = 'button-only' === $button_position && 'expand-searchfield' === $button_behavior; 80 // If it's interactive, enqueue the script module and add the directives. 81 $is_expandable_searchfield = 'button-only' === $button_position; 82 82 if ( $is_expandable_searchfield ) { 83 $input->set_attribute( 'data-wp-bind--aria-hidden', '!context.core.search.isSearchInputVisible' ); 84 $input->set_attribute( 'data-wp-bind--tabindex', 'selectors.core.search.tabindex' ); 85 // Adding these attributes manually is needed until the Interactivity API SSR logic is added to core. 83 wp_enqueue_script_module( '@wordpress/block-library/search' ); 84 85 $input->set_attribute( 'data-wp-bind--aria-hidden', '!context.isSearchInputVisible' ); 86 $input->set_attribute( 'data-wp-bind--tabindex', 'state.tabindex' ); 87 88 // Adding these attributes manually is needed until the Interactivity API 89 // SSR logic is added to core. 86 90 $input->set_attribute( 'aria-hidden', 'true' ); 87 91 $input->set_attribute( 'tabindex', '-1' ); 88 }89 90 // If the script already exists, there is no point in removing it from viewScript.91 $view_js_file = 'wp-block-search-view';92 if ( ! wp_script_is( $view_js_file ) ) {93 $script_handles = $block->block_type->view_script_handles;94 95 // If the script is not needed, and it is still in the `view_script_handles`, remove it.96 if ( ! $is_expandable_searchfield && in_array( $view_js_file, $script_handles, true ) ) {97 $block->block_type->view_script_handles = array_diff( $script_handles, array( $view_js_file ) );98 }99 // If the script is needed, but it was previously removed, add it again.100 if ( $is_expandable_searchfield && ! in_array( $view_js_file, $script_handles, true ) ) {101 $block->block_type->view_script_handles = array_merge( $script_handles, array( $view_js_file ) );102 }103 92 } 104 93 } … … 145 134 if ( $button->next_tag() ) { 146 135 $button->add_class( implode( ' ', $button_classes ) ); 147 if ( 'expand-searchfield' === $attributes['buttonBehavior'] && 'button-only' === $attributes['buttonPosition'] ) { 148 $button->set_attribute( 'data-wp-bind--aria-label', 'selectors.core.search.ariaLabel' ); 149 $button->set_attribute( 'data-wp-bind--aria-controls', 'selectors.core.search.ariaControls' ); 150 $button->set_attribute( 'data-wp-bind--aria-expanded', 'context.core.search.isSearchInputVisible' ); 151 $button->set_attribute( 'data-wp-bind--type', 'selectors.core.search.type' ); 152 $button->set_attribute( 'data-wp-on--click', 'actions.core.search.openSearchInput' ); 153 // Adding these attributes manually is needed until the Interactivity API SSR logic is added to core. 136 if ( 'button-only' === $attributes['buttonPosition'] ) { 137 $button->set_attribute( 'data-wp-bind--aria-label', 'state.ariaLabel' ); 138 $button->set_attribute( 'data-wp-bind--aria-controls', 'state.ariaControls' ); 139 $button->set_attribute( 'data-wp-bind--aria-expanded', 'context.isSearchInputVisible' ); 140 $button->set_attribute( 'data-wp-bind--type', 'state.type' ); 141 $button->set_attribute( 'data-wp-on--click', 'actions.openSearchInput' ); 142 143 // Adding these attributes manually is needed until the Interactivity 144 // API SSR logic is added to core. 154 145 $button->set_attribute( 'aria-label', __( 'Expand search field' ) ); 155 146 $button->set_attribute( 'aria-controls', 'wp-block-search__input-' . $input_id ); … … 173 164 ); 174 165 $form_directives = ''; 166 167 // If it's interactive, add the directives. 175 168 if ( $is_expandable_searchfield ) { 176 169 $aria_label_expanded = __( 'Submit Search' ); 177 170 $aria_label_collapsed = __( 'Expand search field' ); 178 171 $form_directives = ' 179 data-wp-interactive 180 data-wp-context=\'{ " core": { "search": { "isSearchInputVisible": ' . $open_by_default . ', "inputId": "' . $input_id . '", "ariaLabelExpanded": "' . $aria_label_expanded . '", "ariaLabelCollapsed": "' . $aria_label_collapsed . '" } }}\'181 data-wp-class--wp-block-search__searchfield-hidden="!context. core.search.isSearchInputVisible"182 data-wp-on--keydown="actions. core.search.handleSearchKeydown"183 data-wp-on--focusout="actions. core.search.handleSearchFocusout"172 data-wp-interactive=\'{ "namespace": "core/search" }\' 173 data-wp-context=\'{ "isSearchInputVisible": ' . $open_by_default . ', "inputId": "' . $input_id . '", "ariaLabelExpanded": "' . $aria_label_expanded . '", "ariaLabelCollapsed": "' . $aria_label_collapsed . '" }\' 174 data-wp-class--wp-block-search__searchfield-hidden="!context.isSearchInputVisible" 175 data-wp-on--keydown="actions.handleSearchKeydown" 176 data-wp-on--focusout="actions.handleSearchFocusout" 184 177 '; 185 178 } … … 204 197 ) 205 198 ); 199 200 wp_register_script_module( 201 '@wordpress/block-library/search', 202 defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ? gutenberg_url( '/build/interactivity/search.min.js' ) : includes_url( 'blocks/search/view.min.js' ), 203 array( '@wordpress/interactivity' ), 204 defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : get_bloginfo( 'version' ) 205 ); 206 206 } 207 207 add_action( 'init', 'register_block_core_search' ); 208 209 /**210 * Ensure that the view script has the `wp-interactivity` dependency.211 *212 * @since 6.4.0213 *214 * @global WP_Scripts $wp_scripts215 */216 function block_core_search_ensure_interactivity_dependency() {217 global $wp_scripts;218 if (219 isset( $wp_scripts->registered['wp-block-search-view'] ) &&220 ! in_array( 'wp-interactivity', $wp_scripts->registered['wp-block-search-view']->deps, true )221 ) {222 $wp_scripts->registered['wp-block-search-view']->deps[] = 'wp-interactivity';223 }224 }225 226 add_action( 'wp_print_scripts', 'block_core_search_ensure_interactivity_dependency' );227 208 228 209 /** … … 250 231 251 232 if ( 'button-only' === $attributes['buttonPosition'] ) { 252 $classnames[] = 'wp-block-search__button-only'; 253 if ( ! empty( $attributes['buttonBehavior'] ) && 'expand-searchfield' === $attributes['buttonBehavior'] ) { 254 $classnames[] = 'wp-block-search__button-behavior-expand wp-block-search__searchfield-hidden'; 255 } 233 $classnames[] = 'wp-block-search__button-only wp-block-search__searchfield-hidden'; 256 234 } 257 235 }
Note: See TracChangeset
for help on using the changeset viewer.