Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query & Search block #65950

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c3fd536
adding the necessary directives
Jul 4, 2024
793e330
add filter for query loop, rest necessarily things
Jul 5, 2024
0159a4a
Switch to isSearchInputInitiallyVisible so it works with navigation
luisherranz Jul 8, 2024
29b2399
Remove redundant await
michalczaplinski Oct 8, 2024
5952a03
Update the filter that adds the search param to query block
michalczaplinski Oct 8, 2024
f0944db
Refactor the view.js of search block
michalczaplinski Oct 8, 2024
5a85f98
Move changes from the `query_loop_block_query_vars` filter to inside …
michalczaplinski Oct 9, 2024
520629d
Add a comment in search/view.js
michalczaplinski Oct 9, 2024
b3331d5
Merge remote-tracking branch 'origin/trunk' into feature/instant-sear…
michalczaplinski Oct 9, 2024
f4776db
I messed up the merge commit earlier
michalczaplinski Oct 9, 2024
ce27856
First stab at making search work for "inherited" queries
michalczaplinski Oct 10, 2024
be75444
Forgot to check if $enhanced_pagination was on
michalczaplinski Oct 10, 2024
6fbf19c
remove the error_log()
michalczaplinski Oct 10, 2024
7627a35
reorder variables for easier review
michalczaplinski Oct 10, 2024
288603a
remove linebreak
michalczaplinski Oct 10, 2024
03d1be0
Add the missing space before parens
michalczaplinski Oct 10, 2024
1495a42
Add the experimental setting
michalczaplinski Oct 10, 2024
f249ed3
Add the experimental setting of for search and query block
michalczaplinski Oct 10, 2024
ca0e697
Fix the phpcs spaces
michalczaplinski Oct 10, 2024
d2fb6e6
Actually fix the phpcs lint 🤦‍♂️
michalczaplinski Oct 10, 2024
ed9b908
Rename `search` to `instant-search`
michalczaplinski Oct 15, 2024
c63840a
remove the `name`
michalczaplinski Oct 16, 2024
a55194f
Add support for inherited queries
michalczaplinski Oct 16, 2024
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
4 changes: 4 additions & 0 deletions lib/experimental/editor-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down
12 changes: 12 additions & 0 deletions lib/experiments-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
25 changes: 24 additions & 1 deletion packages/block-library/src/post-template/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
$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['instant-search'] ) ? '' : sanitize_text_field( $_GET['instant-search'] );

// 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.
$use_global_query = ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] );
Expand All @@ -67,9 +72,27 @@
} else {
$query = $wp_query;
}

/*
* 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 ) );

Check warning on line 84 in packages/block-library/src/post-template/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space
$query = new WP_Query( $args );
}
} 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 && $instant_search_enabled && ! empty( $search_query ) ) {
$query_args['s'] = $search_query;
}

$query = new WP_Query( $query_args );
}

if ( ! $query->have_posts() ) {
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/search/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"default": false
}
},
"usesContext": [ "enhancedPagination" ],
"supports": {
"align": [ "left", "center", "right" ],
"color": {
Expand Down
39 changes: 30 additions & 9 deletions packages/block-library/src/search/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<!--
// wp:search /-->`. Support these by defaulting an undefined label and
Expand Down Expand Up @@ -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( '<label %1$s>%2$s</label>', $inline_styles['label'], $label_inner_html ) );
Expand Down Expand Up @@ -79,9 +81,11 @@ 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 ) {
wp_enqueue_script_module( '@wordpress/block-library/search/view' );

}
if ( $is_expandable_searchfield ) {
$input->set_attribute( 'data-wp-bind--aria-hidden', '!context.isSearchInputVisible' );
$input->set_attribute( 'data-wp-bind--tabindex', 'state.tabindex' );

Expand All @@ -90,6 +94,11 @@ function render_block_core_search( $attributes ) {
$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 ) {
Expand Down Expand Up @@ -165,20 +174,32 @@ 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"';
}

// Adding wp_interactivity_state for the search block.
if ( $enhanced_pagination ) {
wp_interactivity_state(
'core/search',
array(
'search' => isset( $_GET['instant-search'] ) ? $_GET['instant-search'] : '',
)
);
}

if ( $is_expandable_searchfield ) {
$aria_label_expanded = __( 'Submit Search' );
$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 = '
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"
Expand Down
49 changes: 45 additions & 4 deletions packages/block-library/src/search/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
*/
import { store, getContext, getElement } from '@wordpress/interactivity';

const { actions } = store(
const isEmpty = ( obj ) =>
[ Object, Array ].includes( ( obj || {} ).constructor ) &&
! Object.entries( obj || {} ).length;

const { state, actions } = store(
'core/search',
{
state: {
Expand All @@ -29,14 +33,25 @@ const { actions } = store(
const { isSearchInputVisible } = getContext();
return isSearchInputVisible ? '0' : '-1';
},
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;
}
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();
}
},
Expand Down Expand Up @@ -66,6 +81,32 @@ const { actions } = store(
actions.closeSearchInput();
}
},
*updateSearch() {
const { ref } = getElement();
const { value } = ref;

// Don't navigate if the search didn't really change.
if ( value === state.search ) {
return;
}

const url = new URL( window.location );

if ( ! isEmpty( value ) ) {
state.search = value;
url.searchParams.set( 'instant-search', value );
} else {
url.searchParams.delete( 'instant-search' );
}

const { actions: routerActions } = yield import(
'@wordpress/interactivity-router'
);

routerActions.navigate(
`${ window.location.pathname }${ url.search }`
);
},
},
},
{ lock: true }
Expand Down
Loading