Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor how we pass the query_id and add a comment on the filter
  • Loading branch information
michalczaplinski committed Dec 18, 2024
commit 2f50ac9e049e974d1c9f5e5fec5651b3eac8500e
6 changes: 6 additions & 0 deletions lib/experimental/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ function gutenberg_block_core_query_add_url_filtering( $query, $block ) {
}
add_filter( 'query_loop_block_query_vars', 'gutenberg_block_core_query_add_url_filtering', 10, 2 );

/**
* Adds the search query to Query blocks for the inherited queries if the instant search experiment is enabled.
*
* @param WP_Query $query The query object.
* @return void
*/
function gutenberg_block_core_query_add_search_query_filtering( $query ) {

// if the query is not the main query, return
Expand Down
16 changes: 9 additions & 7 deletions packages/block-library/src/search/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,26 +224,28 @@ function render_block_core_search( $attributes, $content, $block ) {

wp_interactivity_config( 'core/search', array( 'canonicalURL' => $canonical_url_no_pagination ) );

$is_inherited = isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] && ! empty( $block->context['queryId'] );
$query_id = $block->context['queryId'];
$search = '';

// If the query is defined in the block context, use it
if ( isset( $block->context['query']['search'] ) && '' !== $block->context['query']['search'] ) {
$search = $block->context['query']['search'];
}

$is_inherited = isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] && ! empty( $query_id );

// Inherited query: `instant-search=`
// Custom query: `instant-search-<query_id>=`
$search_key = $is_inherited ? 'instant-search' : 'instant-search-' . $query_id;

// If the query is defined in the URL, it overrides the block context value.
if ( $is_inherited ) {
$search = empty( $_GET['instant-search'] ) ? $search : sanitize_text_field( $_GET['instant-search'] );
} else {
$search = empty( $_GET[ 'instant-search-' . $block->context['queryId'] ] ) ? $search : sanitize_text_field( $_GET[ 'instant-search-' . $block->context['queryId'] ] );
}
$search = empty( $_GET[ $search_key ] ) ? $search : sanitize_text_field( $_GET[ $search_key ] );

$form_context = array_merge(
$form_context,
array(
'search' => $search,
'queryId' => $block->context['queryId'],
'queryId' => $query_id,
'isInherited' => $is_inherited,
)
);
Expand Down