From c3fd536976b5a717f2f09260ce65c6071f306001 Mon Sep 17 00:00:00 2001 From: rchrzan Date: Thu, 4 Jul 2024 22:47:10 +0200 Subject: [PATCH 01/22] adding the necessary directives --- packages/block-library/src/search/block.json | 1 + packages/block-library/src/search/index.php | 27 +++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/packages/block-library/src/search/block.json b/packages/block-library/src/search/block.json index 8d5e208045068d..f3d9780e9e834a 100644 --- a/packages/block-library/src/search/block.json +++ b/packages/block-library/src/search/block.json @@ -48,6 +48,7 @@ "default": false } }, + "usesContext": [ "enhancedPagination" ], "supports": { "align": [ "left", "center", "right" ], "color": { diff --git a/packages/block-library/src/search/index.php b/packages/block-library/src/search/index.php index 39b8591c86600f..129e153539bd99 100644 --- a/packages/block-library/src/search/index.php +++ b/packages/block-library/src/search/index.php @@ -16,7 +16,7 @@ * * @return string The search block markup. */ -function render_block_core_search( $attributes ) { +function render_block_core_search( $attributes, $content, $block ) { // 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 @@ -48,6 +48,8 @@ function render_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; + // Check if the block is using the enhanced pagination. + $enhanced_pagination = isset( $block->context['enhancedPagination'] ) && $block->context['enhancedPagination']; $label_inner_html = empty( $attributes['label'] ) ? __( 'Search' ) : wp_kses_post( $attributes['label'] ); $label = new WP_HTML_Tag_Processor( sprintf( '', $inline_styles['label'], $label_inner_html ) ); @@ -79,7 +81,8 @@ function render_block_core_search( $attributes ) { // If it's interactive, enqueue the script module and add the directives. $is_expandable_searchfield = 'button-only' === $button_position; - if ( $is_expandable_searchfield ) { + + if ( $is_expandable_searchfield || $enhanced_pagination ) { $suffix = wp_scripts_get_suffix(); if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ) { $module_url = gutenberg_url( '/build/interactivity/search.min.js' ); @@ -93,14 +96,20 @@ function render_block_core_search( $attributes ) { ); wp_enqueue_script_module( '@wordpress/block-library/search' ); + } + if ( $is_expandable_searchfield ) { $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' ); } + // Instant search is only available when using the enhanced pagination. + if ( $enhanced_pagination ) { + $input->set_attribute( 'data-wp-bind--value', 'state.search' ); + $input->set_attribute( 'data-wp-on--input', 'actions.updateSearch' ); + } } if ( count( $query_params ) > 0 ) { @@ -176,6 +185,14 @@ function render_block_core_search( $attributes ) { $form_directives = ''; // If it's interactive, add the directives. + if ( $is_expandable_searchfield || $enhanced_pagination ) { + $form_directives = 'data-wp-interactive="core/search"'; + } + + if ( $enhanced_pagination ) { + // TODO: add wp_interactivity_state() + } + if ( $is_expandable_searchfield ) { $aria_label_expanded = __( 'Submit Search' ); $aria_label_collapsed = __( 'Expand search field' ); @@ -187,9 +204,7 @@ function render_block_core_search( $attributes ) { 'ariaLabelCollapsed' => $aria_label_collapsed, ) ); - $form_directives = ' - data-wp-interactive="core/search"' - . $form_context . + $form_directives .= $form_context . 'data-wp-class--wp-block-search__searchfield-hidden="!context.isSearchInputVisible" data-wp-on-async--keydown="actions.handleSearchKeydown" data-wp-on-async--focusout="actions.handleSearchFocusout" From 793e3308e020b1eed9d36944ea26dfa4238d4aed Mon Sep 17 00:00:00 2001 From: rchrzan Date: Fri, 5 Jul 2024 22:50:54 +0200 Subject: [PATCH 02/22] add filter for query loop, rest necessarily things --- packages/block-library/src/query/index.php | 32 ++++++++++++++++++ packages/block-library/src/search/index.php | 8 ++++- packages/block-library/src/search/view.js | 37 ++++++++++++++++++++- 3 files changed, 75 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/query/index.php b/packages/block-library/src/query/index.php index 6cc57dc08388c6..0c390448948470 100644 --- a/packages/block-library/src/query/index.php +++ b/packages/block-library/src/query/index.php @@ -170,3 +170,35 @@ function block_core_query_disable_enhanced_pagination( $parsed_block ) { } add_filter( 'render_block_data', 'block_core_query_disable_enhanced_pagination', 10, 1 ); + +/** + * Modifies the static `core/query` block on the server for instant search. + * + * @since 6.5.0 + * + * @param array $attributes Block attributes. + * @param string $content Block default content. + * @param WP_Block $block The block instance. + * + * @return string Returns the modified output of the query block. + */ + +function block_core_query_instant_search_filter( $pre_render, $parsed_block ) { + if ( 'core/query' === $parsed_block['blockName'] && array_key_exists( 'enhancedPagination', $parsed_block['attrs'] ) && true === $parsed_block['attrs']['enhancedPagination'] ) { + add_filter( + 'query_loop_block_query_vars', + function ( $query ) { + + if ( isset( $_GET['search'] ) && ! empty( $_GET['search'] ) ) { + $query['s'] = $_GET['search']; + } + + return $query; + } + ); + } + + return $pre_render; +} + +add_filter( 'pre_render_block', 'block_core_query_instant_search_filter', 10, 3 ); diff --git a/packages/block-library/src/search/index.php b/packages/block-library/src/search/index.php index 129e153539bd99..c514b0f1ff7810 100644 --- a/packages/block-library/src/search/index.php +++ b/packages/block-library/src/search/index.php @@ -189,8 +189,14 @@ function render_block_core_search( $attributes, $content, $block ) { $form_directives = 'data-wp-interactive="core/search"'; } + // Adding wp_interactivity_state for the search block. if ( $enhanced_pagination ) { - // TODO: add wp_interactivity_state() + wp_interactivity_state( + 'core/search', + array( + 'search' => isset( $_GET['search'] ) ? $_GET['search'] : '', + ) + ); } if ( $is_expandable_searchfield ) { diff --git a/packages/block-library/src/search/view.js b/packages/block-library/src/search/view.js index 0e4c462a2e3213..baba35f41d6f31 100644 --- a/packages/block-library/src/search/view.js +++ b/packages/block-library/src/search/view.js @@ -3,7 +3,26 @@ */ import { store, getContext, getElement } from '@wordpress/interactivity'; -const { actions } = store( +const isEmpty = ( obj ) => + [ Object, Array ].includes( ( obj || {} ).constructor ) && + ! Object.entries( obj || {} ).length; + +const updateURL = async ( value, name ) => { + const url = new URL( window.location ); + const { actions } = await import( '@wordpress/interactivity-router' ); + + if ( 's' === name ) { + if ( ! isEmpty( value ) ) { + url.searchParams.set( 'search', value ); + } else { + url.searchParams.delete( 'search' ); + } + } + + await actions.navigate( `${ window.location.pathname }${ url.search }` ); +}; + +const { state, actions } = store( 'core/search', { state: { @@ -66,6 +85,22 @@ const { actions } = store( actions.closeSearchInput(); } }, + *updateSearch() { + const { ref } = getElement(); + const { value, name } = ref; + + // Don't navigate if the search didn't really change. + if ( 's' === name && value === state.search ) { + return; + } + + if ( 's' === name ) { + state.search = value; + } + + // If not, navigate to the new URL. + yield updateURL( value, name ); + }, }, }, { lock: true } From 0159a4a7273e0b4c185e856ad461838583a398ac Mon Sep 17 00:00:00 2001 From: Luis Herranz Date: Mon, 8 Jul 2024 17:31:27 +0200 Subject: [PATCH 03/22] Switch to isSearchInputInitiallyVisible so it works with navigation --- packages/block-library/src/search/index.php | 8 ++++---- packages/block-library/src/search/view.js | 13 ++++++++++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/packages/block-library/src/search/index.php b/packages/block-library/src/search/index.php index c514b0f1ff7810..656fc7598802d7 100644 --- a/packages/block-library/src/search/index.php +++ b/packages/block-library/src/search/index.php @@ -204,10 +204,10 @@ function render_block_core_search( $attributes, $content, $block ) { $aria_label_collapsed = __( 'Expand search field' ); $form_context = wp_interactivity_data_wp_context( array( - 'isSearchInputVisible' => $open_by_default, - 'inputId' => $input_id, - 'ariaLabelExpanded' => $aria_label_expanded, - 'ariaLabelCollapsed' => $aria_label_collapsed, + 'isSearchInputInitiallyVisible' => $open_by_default, + 'inputId' => $input_id, + 'ariaLabelExpanded' => $aria_label_expanded, + 'ariaLabelCollapsed' => $aria_label_collapsed, ) ); $form_directives .= $form_context . diff --git a/packages/block-library/src/search/view.js b/packages/block-library/src/search/view.js index baba35f41d6f31..c13f66cb99043b 100644 --- a/packages/block-library/src/search/view.js +++ b/packages/block-library/src/search/view.js @@ -48,14 +48,21 @@ const { state, actions } = store( const { isSearchInputVisible } = getContext(); return isSearchInputVisible ? '0' : '-1'; }, + get isSearchInputVisible() { + const ctx = getContext(); + if ( typeof ctx.isSearchInputVisible === 'undefined' ) { + return ctx.isSearchInputInitiallyVisible; + } + return ctx.isSearchInputVisible; + }, }, actions: { openSearchInput( event ) { - const ctx = getContext(); - const { ref } = getElement(); - if ( ! ctx.isSearchInputVisible ) { + if ( ! state.isSearchInputVisible ) { event.preventDefault(); + const ctx = getContext(); ctx.isSearchInputVisible = true; + const { ref } = getElement(); ref.parentElement.querySelector( 'input' ).focus(); } }, From 29b239962a4f3d9411f089add9d8381d35da50db Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Tue, 8 Oct 2024 17:08:26 +0100 Subject: [PATCH 04/22] Remove redundant await --- packages/block-library/src/search/view.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/search/view.js b/packages/block-library/src/search/view.js index c13f66cb99043b..a2929b9c719e87 100644 --- a/packages/block-library/src/search/view.js +++ b/packages/block-library/src/search/view.js @@ -19,7 +19,7 @@ const updateURL = async ( value, name ) => { } } - await actions.navigate( `${ window.location.pathname }${ url.search }` ); + actions.navigate( `${ window.location.pathname }${ url.search }` ); }; const { state, actions } = store( From 5952a03ee29ce8adfd8c930f64b61d9602d90e66 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Tue, 8 Oct 2024 17:14:19 +0100 Subject: [PATCH 05/22] Update the filter that adds the search param to query block --- packages/block-library/src/query/index.php | 39 +++++++++++----------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/packages/block-library/src/query/index.php b/packages/block-library/src/query/index.php index 0c390448948470..25846aff12ca40 100644 --- a/packages/block-library/src/query/index.php +++ b/packages/block-library/src/query/index.php @@ -172,33 +172,32 @@ function block_core_query_disable_enhanced_pagination( $parsed_block ) { add_filter( 'render_block_data', 'block_core_query_disable_enhanced_pagination', 10, 1 ); /** - * Modifies the static `core/query` block on the server for instant search. + * Filters the query arguments for the Query Loop block to support instant search functionality. * - * @since 6.5.0 + * This function modifies the query arguments if enhanced pagination is enabled and a search query + * parameter is present in the GET request. It adds the search term to the query, enabling + * instant search within the Query Loop block. * - * @param array $attributes Block attributes. - * @param string $content Block default content. - * @param WP_Block $block The block instance. + * @since 6.8.0 * - * @return string Returns the modified output of the query block. + * @param array $query The original query arguments. + * @param WP_Block $block The block instance. + * @return array The modified query arguments. */ +function block_core_query_instant_search_filter( $query, $block ) { -function block_core_query_instant_search_filter( $pre_render, $parsed_block ) { - if ( 'core/query' === $parsed_block['blockName'] && array_key_exists( 'enhancedPagination', $parsed_block['attrs'] ) && true === $parsed_block['attrs']['enhancedPagination'] ) { - add_filter( - 'query_loop_block_query_vars', - function ( $query ) { - - if ( isset( $_GET['search'] ) && ! empty( $_GET['search'] ) ) { - $query['s'] = $_GET['search']; - } + // if the enhancedPagination is false or not set, return the query as is + if ( empty( $block->context['enhancedPagination'] ) ) { + return $query; + } - return $query; - } - ); + // if the search query param is set, add it to the query + if ( isset( $_GET['search'] ) && ! empty( $_GET['search'] ) ) { + $query['s'] = $_GET['search']; } - return $pre_render; + return $query; } -add_filter( 'pre_render_block', 'block_core_query_instant_search_filter', 10, 3 ); + +add_filter( 'query_loop_block_query_vars', 'block_core_query_instant_search_filter', 10, 3 ); From f0944db3f7d2838bdffc2a9ca9e1b397253e4db6 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Tue, 8 Oct 2024 20:06:22 +0100 Subject: [PATCH 06/22] Refactor the view.js of search block --- packages/block-library/src/search/view.js | 31 ++++++++++------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/packages/block-library/src/search/view.js b/packages/block-library/src/search/view.js index a2929b9c719e87..db0aa486e1f070 100644 --- a/packages/block-library/src/search/view.js +++ b/packages/block-library/src/search/view.js @@ -7,21 +7,6 @@ const isEmpty = ( obj ) => [ Object, Array ].includes( ( obj || {} ).constructor ) && ! Object.entries( obj || {} ).length; -const updateURL = async ( value, name ) => { - const url = new URL( window.location ); - const { actions } = await import( '@wordpress/interactivity-router' ); - - if ( 's' === name ) { - if ( ! isEmpty( value ) ) { - url.searchParams.set( 'search', value ); - } else { - url.searchParams.delete( 'search' ); - } - } - - actions.navigate( `${ window.location.pathname }${ url.search }` ); -}; - const { state, actions } = store( 'core/search', { @@ -101,12 +86,24 @@ const { state, actions } = store( return; } + const url = new URL( window.location ); + if ( 's' === name ) { state.search = value; + if ( ! isEmpty( value ) ) { + url.searchParams.set( 'search', value ); + } else { + url.searchParams.delete( 'search' ); + } } - // If not, navigate to the new URL. - yield updateURL( value, name ); + const { actions: routerActions } = yield import( + '@wordpress/interactivity-router' + ); + + routerActions.navigate( + `${ window.location.pathname }${ url.search }` + ); }, }, }, From 5a85f98d7e18f281a66bb32f9cd4d3ef5e983b0a Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Wed, 9 Oct 2024 13:41:09 +0100 Subject: [PATCH 07/22] Move changes from the `query_loop_block_query_vars` filter to inside of post-template/index.php --- .../block-library/src/post-template/index.php | 18 ++++++++++- packages/block-library/src/query/index.php | 31 ------------------- 2 files changed, 17 insertions(+), 32 deletions(-) diff --git a/packages/block-library/src/post-template/index.php b/packages/block-library/src/post-template/index.php index 9126355c096a57..dfb79b7435dabb 100644 --- a/packages/block-library/src/post-template/index.php +++ b/packages/block-library/src/post-template/index.php @@ -67,9 +67,25 @@ function render_block_core_post_template( $attributes, $content, $block ) { } else { $query = $wp_query; } + + // Add search parameter if enhanced pagination is on and search query exists + if ( $enhanced_pagination && isset( $_GET['search'] ) && ! empty( $_GET['search'] ) ) { + $query->set( 's', $_GET['search'] ); + } + + error_log( 'inherited query' ); } else { + $query_args = build_query_vars_from_query_block( $block, $page ); - $query = new WP_Query( $query_args ); + + // Add search parameter if enhanced pagination is on and search query exists + if ( $enhanced_pagination && isset( $_GET['search'] ) && ! empty( $_GET['search'] ) ) { + $query_args['s'] = $_GET['search']; + } + + error_log( 'regular query' ); + + $query = new WP_Query( $query_args ); } if ( ! $query->have_posts() ) { diff --git a/packages/block-library/src/query/index.php b/packages/block-library/src/query/index.php index 25846aff12ca40..6cc57dc08388c6 100644 --- a/packages/block-library/src/query/index.php +++ b/packages/block-library/src/query/index.php @@ -170,34 +170,3 @@ function block_core_query_disable_enhanced_pagination( $parsed_block ) { } add_filter( 'render_block_data', 'block_core_query_disable_enhanced_pagination', 10, 1 ); - -/** - * Filters the query arguments for the Query Loop block to support instant search functionality. - * - * This function modifies the query arguments if enhanced pagination is enabled and a search query - * parameter is present in the GET request. It adds the search term to the query, enabling - * instant search within the Query Loop block. - * - * @since 6.8.0 - * - * @param array $query The original query arguments. - * @param WP_Block $block The block instance. - * @return array The modified query arguments. - */ -function block_core_query_instant_search_filter( $query, $block ) { - - // if the enhancedPagination is false or not set, return the query as is - if ( empty( $block->context['enhancedPagination'] ) ) { - return $query; - } - - // if the search query param is set, add it to the query - if ( isset( $_GET['search'] ) && ! empty( $_GET['search'] ) ) { - $query['s'] = $_GET['search']; - } - - return $query; -} - - -add_filter( 'query_loop_block_query_vars', 'block_core_query_instant_search_filter', 10, 3 ); From 520629df58bb94390df6f30151a6354def1e19fa Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Wed, 9 Oct 2024 13:41:39 +0100 Subject: [PATCH 08/22] Add a comment in search/view.js --- packages/block-library/src/search/view.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/block-library/src/search/view.js b/packages/block-library/src/search/view.js index db0aa486e1f070..dff0c5608a09fb 100644 --- a/packages/block-library/src/search/view.js +++ b/packages/block-library/src/search/view.js @@ -35,6 +35,10 @@ const { state, actions } = store( }, get isSearchInputVisible() { const ctx = getContext(); + + // `ctx.isSearchInputVisible` is a client-side-only context value, so + // if it's not set, it means that it's an initial page load, so we need + // to return the value of `ctx.isSearchInputInitiallyVisible`. if ( typeof ctx.isSearchInputVisible === 'undefined' ) { return ctx.isSearchInputInitiallyVisible; } From f4776dbfbb1d30a9b212e0b4a32c357ec5119ca3 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Wed, 9 Oct 2024 17:12:12 +0100 Subject: [PATCH 09/22] I messed up the merge commit earlier --- packages/block-library/src/search/index.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/search/index.php b/packages/block-library/src/search/index.php index 6219ae629a7b85..39347a0a2ca95f 100644 --- a/packages/block-library/src/search/index.php +++ b/packages/block-library/src/search/index.php @@ -81,13 +81,14 @@ function render_block_core_search( $attributes, $content, $block ) { // If it's interactive, enqueue the script module and add the directives. $is_expandable_searchfield = 'button-only' === $button_position; - if ( $is_expandable_searchfield ) { + if ( $is_expandable_searchfield || $enhanced_pagination) { wp_enqueue_script_module( '@wordpress/block-library/search/view' ); } - if ( $is_expandable_searchfield || $enhanced_pagination ) { + if ( $is_expandable_searchfield ) { $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' ); From ce278566740aa84fdcaa442b5833173ee2634e88 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Thu, 10 Oct 2024 15:03:50 +0100 Subject: [PATCH 10/22] First stab at making search work for "inherited" queries --- .../block-library/src/post-template/index.php | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/packages/block-library/src/post-template/index.php b/packages/block-library/src/post-template/index.php index dfb79b7435dabb..c59d423d50bd63 100644 --- a/packages/block-library/src/post-template/index.php +++ b/packages/block-library/src/post-template/index.php @@ -50,6 +50,7 @@ function render_block_core_post_template( $attributes, $content, $block ) { $page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page'; $enhanced_pagination = isset( $block->context['enhancedPagination'] ) && $block->context['enhancedPagination']; $page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ]; + $search_query = empty( $_GET['search'] ) ? '' : sanitize_text_field( $_GET['search'] ); // Use global query if needed. $use_global_query = ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ); @@ -63,24 +64,29 @@ function render_block_core_post_template( $attributes, $content, $block ) { */ if ( in_the_loop() ) { $query = clone $wp_query; + $query_args = $wp_query->query_vars; $query->rewind_posts(); + + // Add search parameter if it exists. + if ( ! empty( $search_query ) ) { + $query_args['s'] = $search_query; + } + $query->query( $query_args ); + } else { + // The query has not been run yet, modify the global query. + if ( ! empty( $search_query ) ) { + $wp_query->set( 's', $search_query ); + } $query = $wp_query; } - - // Add search parameter if enhanced pagination is on and search query exists - if ( $enhanced_pagination && isset( $_GET['search'] ) && ! empty( $_GET['search'] ) ) { - $query->set( 's', $_GET['search'] ); - } - - error_log( 'inherited query' ); } else { $query_args = build_query_vars_from_query_block( $block, $page ); // Add search parameter if enhanced pagination is on and search query exists - if ( $enhanced_pagination && isset( $_GET['search'] ) && ! empty( $_GET['search'] ) ) { - $query_args['s'] = $_GET['search']; + if ( $enhanced_pagination && ! empty( $search_query ) ) { + $query_args['s'] = $search_query; } error_log( 'regular query' ); From be7544492837e71769e3399238efdbd41a43f314 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Thu, 10 Oct 2024 15:07:43 +0100 Subject: [PATCH 11/22] Forgot to check if $enhanced_pagination was on --- packages/block-library/src/post-template/index.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/post-template/index.php b/packages/block-library/src/post-template/index.php index c59d423d50bd63..d3bc478b3e1601 100644 --- a/packages/block-library/src/post-template/index.php +++ b/packages/block-library/src/post-template/index.php @@ -68,14 +68,14 @@ function render_block_core_post_template( $attributes, $content, $block ) { $query->rewind_posts(); // Add search parameter if it exists. - if ( ! empty( $search_query ) ) { + if ( $enhanced_pagination && ! empty( $search_query ) ) { $query_args['s'] = $search_query; } $query->query( $query_args ); } else { // The query has not been run yet, modify the global query. - if ( ! empty( $search_query ) ) { + if ( $enhanced_pagination && ! empty( $search_query ) ) { $wp_query->set( 's', $search_query ); } $query = $wp_query; From 6fbf19c1bc00b9d7fe968be775c839788cef0376 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Thu, 10 Oct 2024 15:19:55 +0100 Subject: [PATCH 12/22] remove the error_log() --- packages/block-library/src/post-template/index.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/block-library/src/post-template/index.php b/packages/block-library/src/post-template/index.php index d3bc478b3e1601..92be174f7e5ef1 100644 --- a/packages/block-library/src/post-template/index.php +++ b/packages/block-library/src/post-template/index.php @@ -89,8 +89,6 @@ function render_block_core_post_template( $attributes, $content, $block ) { $query_args['s'] = $search_query; } - error_log( 'regular query' ); - $query = new WP_Query( $query_args ); } From 7627a350a88710051625aec1465c9f0183e36348 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Thu, 10 Oct 2024 15:20:32 +0100 Subject: [PATCH 13/22] reorder variables for easier review --- packages/block-library/src/post-template/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/post-template/index.php b/packages/block-library/src/post-template/index.php index 92be174f7e5ef1..e12ee3119c066c 100644 --- a/packages/block-library/src/post-template/index.php +++ b/packages/block-library/src/post-template/index.php @@ -64,8 +64,8 @@ function render_block_core_post_template( $attributes, $content, $block ) { */ if ( in_the_loop() ) { $query = clone $wp_query; - $query_args = $wp_query->query_vars; $query->rewind_posts(); + $query_args = $wp_query->query_vars; // Add search parameter if it exists. if ( $enhanced_pagination && ! empty( $search_query ) ) { From 288603ad1f63eff600e4196d0415b77f5f627b39 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Thu, 10 Oct 2024 15:21:04 +0100 Subject: [PATCH 14/22] remove linebreak --- packages/block-library/src/post-template/index.php | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/block-library/src/post-template/index.php b/packages/block-library/src/post-template/index.php index e12ee3119c066c..8a1dcab03502fe 100644 --- a/packages/block-library/src/post-template/index.php +++ b/packages/block-library/src/post-template/index.php @@ -72,7 +72,6 @@ function render_block_core_post_template( $attributes, $content, $block ) { $query_args['s'] = $search_query; } $query->query( $query_args ); - } else { // The query has not been run yet, modify the global query. if ( $enhanced_pagination && ! empty( $search_query ) ) { From 03d1be096e57595ce5b21cb7549bc9fcca6d274f Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Thu, 10 Oct 2024 16:08:59 +0100 Subject: [PATCH 15/22] Add the missing space before parens --- packages/block-library/src/search/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/search/index.php b/packages/block-library/src/search/index.php index 39347a0a2ca95f..e2e2444c2bd9a7 100644 --- a/packages/block-library/src/search/index.php +++ b/packages/block-library/src/search/index.php @@ -81,7 +81,7 @@ function render_block_core_search( $attributes, $content, $block ) { // If it's interactive, enqueue the script module and add the directives. $is_expandable_searchfield = 'button-only' === $button_position; - if ( $is_expandable_searchfield || $enhanced_pagination) { + if ( $is_expandable_searchfield || $enhanced_pagination ) { wp_enqueue_script_module( '@wordpress/block-library/search/view' ); } From 1495a42baa8470d8ada4ac5d2450006a4d28af5c Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Thu, 10 Oct 2024 18:55:02 +0100 Subject: [PATCH 16/22] Add the experimental setting --- lib/experimental/editor-settings.php | 4 ++++ lib/experiments-page.php | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/experimental/editor-settings.php b/lib/experimental/editor-settings.php index 126382f85a513e..5b9764c8377426 100644 --- a/lib/experimental/editor-settings.php +++ b/lib/experimental/editor-settings.php @@ -34,6 +34,10 @@ function gutenberg_enable_experiments() { if ( $gutenberg_experiments && array_key_exists( 'gutenberg-media-processing', $gutenberg_experiments ) ) { wp_add_inline_script( 'wp-block-editor', 'window.__experimentalMediaProcessing = true', 'before' ); } + + if ( $gutenberg_experiments && array_key_exists( 'gutenberg-search-query-block', $gutenberg_experiments ) ) { + wp_add_inline_script( 'wp-block-editor', 'window.__experimentalSearchQueryBlock = true', 'before' ); + } } add_action( 'admin_init', 'gutenberg_enable_experiments' ); diff --git a/lib/experiments-page.php b/lib/experiments-page.php index 27a54b920f4d52..9de707fb0d2db0 100644 --- a/lib/experiments-page.php +++ b/lib/experiments-page.php @@ -175,6 +175,18 @@ function gutenberg_initialize_experiments_settings() { ) ); + add_settings_field( + 'gutenberg-search-query-block', + __( 'Instant Search and Query Block', 'gutenberg' ), + 'gutenberg_display_experiment_field', + 'gutenberg-experiments', + 'gutenberg_experiments_section', + array( + 'label' => __( 'Enable instant search functionality of the Search + Query blocks.', 'gutenberg' ), + 'id' => 'gutenberg-search-query-block', + ) + ); + register_setting( 'gutenberg-experiments', 'gutenberg-experiments' From f249ed317f42dfdc2287d8d36eab9905665761ed Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Thu, 10 Oct 2024 18:57:21 +0100 Subject: [PATCH 17/22] Add the experimental setting of for search and query block --- packages/block-library/src/post-template/index.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/post-template/index.php b/packages/block-library/src/post-template/index.php index 8a1dcab03502fe..49911bc4c11864 100644 --- a/packages/block-library/src/post-template/index.php +++ b/packages/block-library/src/post-template/index.php @@ -52,6 +52,9 @@ function render_block_core_post_template( $attributes, $content, $block ) { $page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ]; $search_query = empty( $_GET['search'] ) ? '' : sanitize_text_field( $_GET['search'] ); + $gutenberg_experiments = get_option( 'gutenberg-experiments' ); + $instant_search_enabled = isset( $gutenberg_experiments['gutenberg-search-query-block'] ) && $gutenberg_experiments['gutenberg-search-query-block']; + // Use global query if needed. $use_global_query = ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ); if ( $use_global_query ) { @@ -68,13 +71,13 @@ function render_block_core_post_template( $attributes, $content, $block ) { $query_args = $wp_query->query_vars; // Add search parameter if it exists. - if ( $enhanced_pagination && ! empty( $search_query ) ) { + if ( $enhanced_pagination && $instant_search_enabled && ! empty( $search_query ) ) { $query_args['s'] = $search_query; } $query->query( $query_args ); } else { // The query has not been run yet, modify the global query. - if ( $enhanced_pagination && ! empty( $search_query ) ) { + if ( $enhanced_pagination && $instant_search_enabled && ! empty( $search_query ) ) { $wp_query->set( 's', $search_query ); } $query = $wp_query; @@ -84,7 +87,7 @@ function render_block_core_post_template( $attributes, $content, $block ) { $query_args = build_query_vars_from_query_block( $block, $page ); // Add search parameter if enhanced pagination is on and search query exists - if ( $enhanced_pagination && ! empty( $search_query ) ) { + if ( $enhanced_pagination && $instant_search_enabled && ! empty( $search_query ) ) { $query_args['s'] = $search_query; } From ca0e697036db0d0c61b5cb0536c14cc2ca962aec Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Thu, 10 Oct 2024 19:01:47 +0100 Subject: [PATCH 18/22] Fix the phpcs spaces --- packages/block-library/src/post-template/index.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/post-template/index.php b/packages/block-library/src/post-template/index.php index 49911bc4c11864..0ae5c5d2741fde 100644 --- a/packages/block-library/src/post-template/index.php +++ b/packages/block-library/src/post-template/index.php @@ -52,7 +52,8 @@ function render_block_core_post_template( $attributes, $content, $block ) { $page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ]; $search_query = empty( $_GET['search'] ) ? '' : sanitize_text_field( $_GET['search'] ); - $gutenberg_experiments = get_option( 'gutenberg-experiments' ); + // Check if the Instant Search experiment is enabled. + $gutenberg_experiments = get_option( 'gutenberg-experiments' ); $instant_search_enabled = isset( $gutenberg_experiments['gutenberg-search-query-block'] ) && $gutenberg_experiments['gutenberg-search-query-block']; // Use global query if needed. From d2fb6e6a3d72f08acfcd131048943a2d06edc0d9 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Thu, 10 Oct 2024 19:03:48 +0100 Subject: [PATCH 19/22] =?UTF-8?q?Actually=20fix=20the=20phpcs=20lint=20?= =?UTF-8?q?=F0=9F=A4=A6=E2=80=8D=E2=99=82=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/block-library/src/post-template/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/post-template/index.php b/packages/block-library/src/post-template/index.php index 0ae5c5d2741fde..b08722759f0370 100644 --- a/packages/block-library/src/post-template/index.php +++ b/packages/block-library/src/post-template/index.php @@ -53,7 +53,7 @@ function render_block_core_post_template( $attributes, $content, $block ) { $search_query = empty( $_GET['search'] ) ? '' : sanitize_text_field( $_GET['search'] ); // Check if the Instant Search experiment is enabled. - $gutenberg_experiments = get_option( 'gutenberg-experiments' ); + $gutenberg_experiments = get_option( 'gutenberg-experiments' ); $instant_search_enabled = isset( $gutenberg_experiments['gutenberg-search-query-block'] ) && $gutenberg_experiments['gutenberg-search-query-block']; // Use global query if needed. From ed9b908d030fc6d3e390aa2ae8004bbedc19bfe5 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Tue, 15 Oct 2024 17:54:13 +0100 Subject: [PATCH 20/22] Rename `search` to `instant-search` --- packages/block-library/src/post-template/index.php | 2 +- packages/block-library/src/search/index.php | 2 +- packages/block-library/src/search/view.js | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/block-library/src/post-template/index.php b/packages/block-library/src/post-template/index.php index b08722759f0370..b01eb0ba1f15df 100644 --- a/packages/block-library/src/post-template/index.php +++ b/packages/block-library/src/post-template/index.php @@ -50,7 +50,7 @@ function render_block_core_post_template( $attributes, $content, $block ) { $page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page'; $enhanced_pagination = isset( $block->context['enhancedPagination'] ) && $block->context['enhancedPagination']; $page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ]; - $search_query = empty( $_GET['search'] ) ? '' : sanitize_text_field( $_GET['search'] ); + $search_query = empty( $_GET['instant-search'] ) ? '' : sanitize_text_field( $_GET['instant-search'] ); // Check if the Instant Search experiment is enabled. $gutenberg_experiments = get_option( 'gutenberg-experiments' ); diff --git a/packages/block-library/src/search/index.php b/packages/block-library/src/search/index.php index e2e2444c2bd9a7..b21e3632ceef9b 100644 --- a/packages/block-library/src/search/index.php +++ b/packages/block-library/src/search/index.php @@ -183,7 +183,7 @@ function render_block_core_search( $attributes, $content, $block ) { wp_interactivity_state( 'core/search', array( - 'search' => isset( $_GET['search'] ) ? $_GET['search'] : '', + 'search' => isset( $_GET['instant-search'] ) ? $_GET['instant-search'] : '', ) ); } diff --git a/packages/block-library/src/search/view.js b/packages/block-library/src/search/view.js index dff0c5608a09fb..559bf84b5b0f1a 100644 --- a/packages/block-library/src/search/view.js +++ b/packages/block-library/src/search/view.js @@ -95,9 +95,9 @@ const { state, actions } = store( if ( 's' === name ) { state.search = value; if ( ! isEmpty( value ) ) { - url.searchParams.set( 'search', value ); + url.searchParams.set( 'instant-search', value ); } else { - url.searchParams.delete( 'search' ); + url.searchParams.delete( 'instant-search' ); } } From c63840a8e55d66c5370e622ea805ced6ee7f6fea Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Wed, 16 Oct 2024 12:22:25 +0100 Subject: [PATCH 21/22] remove the `name` --- packages/block-library/src/search/view.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/packages/block-library/src/search/view.js b/packages/block-library/src/search/view.js index 559bf84b5b0f1a..91b31e771e65a7 100644 --- a/packages/block-library/src/search/view.js +++ b/packages/block-library/src/search/view.js @@ -83,22 +83,20 @@ const { state, actions } = store( }, *updateSearch() { const { ref } = getElement(); - const { value, name } = ref; + const { value } = ref; // Don't navigate if the search didn't really change. - if ( 's' === name && value === state.search ) { + if ( value === state.search ) { return; } const url = new URL( window.location ); - if ( 's' === name ) { + if ( ! isEmpty( value ) ) { state.search = value; - if ( ! isEmpty( value ) ) { - url.searchParams.set( 'instant-search', value ); - } else { - url.searchParams.delete( 'instant-search' ); - } + url.searchParams.set( 'instant-search', value ); + } else { + url.searchParams.delete( 'instant-search' ); } const { actions: routerActions } = yield import( From a55194fe44a0b1b241735345282113e282420177 Mon Sep 17 00:00:00 2001 From: Michal Czaplinski Date: Wed, 16 Oct 2024 12:35:19 +0100 Subject: [PATCH 22/22] Add support for inherited queries --- .../block-library/src/post-template/index.php | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/block-library/src/post-template/index.php b/packages/block-library/src/post-template/index.php index b01eb0ba1f15df..feb8926118c94f 100644 --- a/packages/block-library/src/post-template/index.php +++ b/packages/block-library/src/post-template/index.php @@ -69,22 +69,22 @@ function render_block_core_post_template( $attributes, $content, $block ) { if ( in_the_loop() ) { $query = clone $wp_query; $query->rewind_posts(); - $query_args = $wp_query->query_vars; - - // Add search parameter if it exists. - if ( $enhanced_pagination && $instant_search_enabled && ! empty( $search_query ) ) { - $query_args['s'] = $search_query; - } - $query->query( $query_args ); } else { - // The query has not been run yet, modify the global query. - if ( $enhanced_pagination && $instant_search_enabled && ! empty( $search_query ) ) { - $wp_query->set( 's', $search_query ); - } $query = $wp_query; } - } else { + /* + * If the following conditions are met, run a new query with the search query: + * 1. Enhanced pagination is on. + * 2. Instant search is enabled. + * 3. The search query is not empty. + * 4. The query already has posts. + */ + if ( $enhanced_pagination && $instant_search_enabled && ! empty( $search_query ) && $query->have_posts() ) { + $args = array_merge( $query->query_vars, array( 's' => $search_query ) ); + $query = new WP_Query( $args ); + } + } else { $query_args = build_query_vars_from_query_block( $block, $page ); // Add search parameter if enhanced pagination is on and search query exists