Skip to content

Commit

Permalink
Create instant search using render_block_context filter.
Browse files Browse the repository at this point in the history
  • Loading branch information
michalczaplinski committed Dec 5, 2024
1 parent 0b814e4 commit e47be64
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
29 changes: 29 additions & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,3 +509,32 @@ function _gutenberg_footnotes_force_filtered_html_on_import_filter( $arg ) {
add_action( 'init', '_gutenberg_footnotes_kses_init' );
add_action( 'set_current_user', '_gutenberg_footnotes_kses_init' );
add_filter( 'force_filtered_html_on_import', '_gutenberg_footnotes_force_filtered_html_on_import_filter', 999 );


function gutenberg_block_core_query_add_url_filtering( $context ) {

// Make sure it only runs for blocks with a queryId
if ( empty( $context['queryId'] ) ) {
return $context;
}

// Check if the instant search gutenberg experiment is enabled
$gutenberg_experiments = get_option( 'gutenberg-experiments' );
$instant_search_enabled = $gutenberg_experiments && array_key_exists( 'gutenberg-search-query-block', $gutenberg_experiments );
if ( ! $instant_search_enabled ) {
return $context;
}

// Get the search key from the URL
$search_key = 'instant-search-' . $context['queryId'];
if ( ! isset( $_GET[ $search_key ] ) ) {
return $context;
}

// Add the search query to the context, it will be picked up by all the blocks that
// use the `query` context like `post-template` or `query-pagination`.
$context['query']['search'] = sanitize_text_field( $_GET[ $search_key ] );

return $context;
}
add_filter( 'render_block_context', 'gutenberg_block_core_query_add_url_filtering', 10, 2 );
11 changes: 9 additions & 2 deletions lib/experiments-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,16 @@ function gutenberg_initialize_experiments_settings() {
)
);

register_setting(
add_settings_field(
'gutenberg-search-query-block',
__( 'Instant Search and Query Block', 'gutenberg' ),
'gutenberg_display_experiment_field',
'gutenberg-experiments',
'gutenberg-experiments'
'gutenberg_experiments_section',
array(
'label' => __( 'Enable instant search functionality of the Search + Query blocks.', 'gutenberg' ),
'id' => 'gutenberg-search-query-block',
)
);
}

Expand Down

0 comments on commit e47be64

Please sign in to comment.