Skip to content

Commit 202d9e7

Browse files
committed
Refactor seamless mode handling and improve query validation
- Replace bypass_posts_pre_query() with inline closure in load_seamless_mode() - Add WP_Query instance validation before processing - Extract seamless mode logic into clearer conditional variables - Use Better_Search_Query to populate main query results via posts_pre_query filter - Add WP_Post_Type validation in get_bsearch_form() post type loop - Simplify aria_label assignment using ternary operator - Update translation file line number
1 parent 9b6640a commit 202d9e7

4 files changed

Lines changed: 79 additions & 77 deletions

File tree

includes/frontend/class-template-handler.php

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -50,47 +50,52 @@ public function __construct() {
5050
* @param \WP_Query $query Query object.
5151
*/
5252
public function load_seamless_mode( $query ) {
53-
if (
54-
$query->get( 'better_search_query' ) ||
53+
if ( ! $query instanceof \WP_Query ) {
54+
return;
55+
}
56+
57+
$is_seamless = (bool) bsearch_get_option( 'seamless' );
58+
$should_force_seamless = $query->get( 'better_search_query' ) ||
5559
( wp_is_block_theme() && $query->is_search() ) ||
56-
( $query->is_search() && bsearch_get_option( 'seamless' ) )
57-
) {
60+
( $query->is_search() && $is_seamless );
61+
62+
if ( $should_force_seamless ) {
5863
if ( ! isset( $query->query_vars['is_better_search_loaded'] ) || ! $query->query_vars['is_better_search_loaded'] ) {
5964
new \Better_Search_Core_Query( $query->query_vars );
6065
$query->set( 'is_better_search_loaded', true );
6166
}
67+
return;
6268
}
6369

64-
// For non-seamless mode search queries, add a filter to bypass the main query.
65-
if ( $query->is_search() && ! bsearch_get_option( 'seamless' ) ) {
66-
static $bypass_filter_added = false;
67-
if ( ! $bypass_filter_added ) {
68-
add_filter( 'posts_pre_query', array( $this, 'bypass_posts_pre_query' ), 10, 2 );
69-
$bypass_filter_added = true;
70-
}
70+
if ( $is_seamless || ! $query->is_main_query() || ! $query->is_search() || is_admin() ) {
71+
return;
7172
}
72-
}
7373

74-
/**
75-
* Bypass the main query for non-seamless search requests by returning a fake post.
76-
*
77-
* @since 4.2.2
78-
*
79-
* @param \WP_Post[]|null $posts Array of post data.
80-
* @param \WP_Query $query The WP_Query instance.
81-
* @return \WP_Post[]|null Updated array of post objects.
82-
*/
83-
public function bypass_posts_pre_query( ?array $posts, \WP_Query $query ): ?array {
84-
if ( ! is_admin() && $query->is_main_query() && isset( $query->query_vars['s'] ) && ! empty( $query->query_vars['s'] ) ) {
85-
// Set essential query properties to prevent further processing.
86-
$query->found_posts = 0;
87-
$query->max_num_pages = 0;
88-
$query->post_count = 0;
89-
90-
// Return empty array instead of fake post.
91-
return array();
92-
}
93-
return $posts;
74+
$search_args = $query->query_vars;
75+
$search_args['better_search_query'] = true;
76+
$search_args['is_better_search_loaded'] = true;
77+
78+
$search_results = new \Better_Search_Query( $search_args );
79+
80+
$populate_main_query = null;
81+
$populate_main_query = static function ( $posts, $current_query ) use ( &$populate_main_query, $search_results, $query ) {
82+
if ( ! $current_query instanceof \WP_Query || $current_query !== $query ) {
83+
return $posts;
84+
}
85+
86+
if ( ! $current_query->is_main_query() || ! $current_query->is_search() ) {
87+
return $posts;
88+
}
89+
90+
remove_filter( 'posts_pre_query', $populate_main_query );
91+
92+
$current_query->found_posts = $search_results->found_posts;
93+
$current_query->max_num_pages = $search_results->max_num_pages;
94+
95+
return $search_results->posts;
96+
};
97+
98+
add_filter( 'posts_pre_query', $populate_main_query, 10, 2 );
9499
}
95100

96101
/**

includes/general-template.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,7 @@ function get_bsearch_form( $search_query = '', $args = array() ) {
209209
$args = array_merge( $defaults, $args );
210210

211211
// Build a string containing an aria-label to use for the search form.
212-
if ( $args['aria_label'] ) {
213-
$aria_label = 'aria-label="' . esc_attr( $args['aria_label'] ) . '" ';
214-
} else {
215-
/*
216-
* If there's no custom aria-label, we can set a default here. At the
217-
* moment it's empty as there's uncertainty about what the default should be.
218-
*/
219-
$aria_label = '';
220-
}
212+
$aria_label = $args['aria_label'] ? 'aria-label="' . esc_attr( $args['aria_label'] ) . '" ' : '';
221213

222214
// Parse post_types.
223215
$post_types = wp_parse_slug_list( $args['post_types'] );
@@ -234,7 +226,12 @@ function get_bsearch_form( $search_query = '', $args = array() ) {
234226

235227
foreach ( $post_types as $post_type ) {
236228
$post_type = get_post_type_object( $post_type );
237-
$select .= sprintf(
229+
230+
if ( ! $post_type instanceof WP_Post_Type ) {
231+
continue;
232+
}
233+
234+
$select .= sprintf(
238235
'<option value="%1$s" %3$s>%2$s</option>',
239236
$post_type->name,
240237
$post_type->labels->singular_name,

languages/better-search-en_US.po

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ msgstr ""
120120
#: better-search.php
121121
#: includes/admin/class-dashboard.php:182
122122
#: includes/admin/network/class-admin.php:92
123-
#: includes/frontend/class-template-handler.php:161
123+
#: includes/frontend/class-template-handler.php:166
124124
msgid "Better Search"
125125
msgstr ""
126126

@@ -1640,71 +1640,71 @@ msgstr ""
16401640
msgid "There is no excerpt because this is a protected post."
16411641
msgstr ""
16421642

1643-
#: includes/general-template.php:228
1643+
#: includes/general-template.php:220
16441644
msgid "Any Post Type"
16451645
msgstr ""
16461646

1647-
#: includes/general-template.php:231
1647+
#: includes/general-template.php:223
16481648
msgctxt "label"
16491649
msgid "Post types:"
16501650
msgstr ""
16511651

1652-
#: includes/general-template.php:252
1652+
#: includes/general-template.php:249
16531653
msgctxt "label"
16541654
msgid "Search for:"
16551655
msgstr ""
16561656

1657-
#: includes/general-template.php:253
1657+
#: includes/general-template.php:250
16581658
msgctxt "placeholder"
16591659
msgid "Search &hellip;"
16601660
msgstr ""
16611661

1662-
#: includes/general-template.php:256
1662+
#: includes/general-template.php:253
16631663
msgctxt "submit button"
16641664
msgid "Search"
16651665
msgstr ""
16661666

16671667
#. translators: 1: First, 2: Last, 3: Number of rows
1668-
#: includes/general-template.php:410
1668+
#: includes/general-template.php:407
16691669
#, php-format
16701670
msgid "Results <strong>%1$s</strong> - <strong>%2$s</strong> of <strong>%3$s</strong>"
16711671
msgstr ""
16721672

16731673
#. translators: 1: Current page number, 2: Total pages
1674-
#: includes/general-template.php:416
1674+
#: includes/general-template.php:413
16751675
#, php-format
16761676
msgid "Page <strong>%1$s</strong> of <strong>%2$s</strong>"
16771677
msgstr ""
16781678

1679-
#: includes/general-template.php:470
1679+
#: includes/general-template.php:467
16801680
msgid "All"
16811681
msgstr ""
16821682

1683-
#: includes/general-template.php:491
1684-
#: includes/general-template.php:495
1683+
#: includes/general-template.php:488
1684+
#: includes/general-template.php:492
16851685
msgid "Date"
16861686
msgstr ""
16871687

1688-
#: includes/general-template.php:492
1689-
#: includes/general-template.php:494
1688+
#: includes/general-template.php:489
1689+
#: includes/general-template.php:491
16901690
msgid "Relevance"
16911691
msgstr ""
16921692

1693-
#: includes/general-template.php:498
1693+
#: includes/general-template.php:495
16941694
msgid "Sorted by"
16951695
msgstr ""
16961696

1697-
#: includes/general-template.php:499
1697+
#: includes/general-template.php:496
16981698
msgid "Sort by"
16991699
msgstr ""
17001700

17011701
#. translators: 1: Results per page.
1702-
#: includes/general-template.php:506
1702+
#: includes/general-template.php:503
17031703
#, php-format
17041704
msgid "Results per-page: %s "
17051705
msgstr ""
17061706

1707-
#: includes/general-template.php:598
1707+
#: includes/general-template.php:595
17081708
msgid "Relevance:"
17091709
msgstr ""
17101710

languages/better-search-en_US.pot

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ msgstr ""
99
"MIME-Version: 1.0\n"
1010
"Content-Type: text/plain; charset=UTF-8\n"
1111
"Content-Transfer-Encoding: 8bit\n"
12-
"POT-Creation-Date: 2026-01-18T11:31:23+00:00\n"
12+
"POT-Creation-Date: 2026-01-24T09:27:52+00:00\n"
1313
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1414
"X-Generator: WP-CLI 2.12.0\n"
1515
"X-Domain: better-search\n"
@@ -18,7 +18,7 @@ msgstr ""
1818
#: better-search.php
1919
#: includes/admin/class-dashboard.php:182
2020
#: includes/admin/network/class-admin.php:92
21-
#: includes/frontend/class-template-handler.php:161
21+
#: includes/frontend/class-template-handler.php:166
2222
msgid "Better Search"
2323
msgstr ""
2424

@@ -1691,71 +1691,71 @@ msgstr ""
16911691
msgid "There is no excerpt because this is a protected post."
16921692
msgstr ""
16931693

1694-
#: includes/general-template.php:228
1694+
#: includes/general-template.php:220
16951695
msgid "Any Post Type"
16961696
msgstr ""
16971697

1698-
#: includes/general-template.php:231
1698+
#: includes/general-template.php:223
16991699
msgctxt "label"
17001700
msgid "Post types:"
17011701
msgstr ""
17021702

1703-
#: includes/general-template.php:252
1703+
#: includes/general-template.php:249
17041704
msgctxt "label"
17051705
msgid "Search for:"
17061706
msgstr ""
17071707

1708-
#: includes/general-template.php:253
1708+
#: includes/general-template.php:250
17091709
msgctxt "placeholder"
17101710
msgid "Search &hellip;"
17111711
msgstr ""
17121712

1713-
#: includes/general-template.php:256
1713+
#: includes/general-template.php:253
17141714
msgctxt "submit button"
17151715
msgid "Search"
17161716
msgstr ""
17171717

17181718
#. translators: 1: First, 2: Last, 3: Number of rows
1719-
#: includes/general-template.php:410
1719+
#: includes/general-template.php:407
17201720
#, php-format
17211721
msgid "Results <strong>%1$s</strong> - <strong>%2$s</strong> of <strong>%3$s</strong>"
17221722
msgstr ""
17231723

17241724
#. translators: 1: Current page number, 2: Total pages
1725-
#: includes/general-template.php:416
1725+
#: includes/general-template.php:413
17261726
#, php-format
17271727
msgid "Page <strong>%1$s</strong> of <strong>%2$s</strong>"
17281728
msgstr ""
17291729

1730-
#: includes/general-template.php:470
1730+
#: includes/general-template.php:467
17311731
msgid "All"
17321732
msgstr ""
17331733

1734-
#: includes/general-template.php:491
1735-
#: includes/general-template.php:495
1734+
#: includes/general-template.php:488
1735+
#: includes/general-template.php:492
17361736
msgid "Date"
17371737
msgstr ""
17381738

1739-
#: includes/general-template.php:492
1740-
#: includes/general-template.php:494
1739+
#: includes/general-template.php:489
1740+
#: includes/general-template.php:491
17411741
msgid "Relevance"
17421742
msgstr ""
17431743

1744-
#: includes/general-template.php:498
1744+
#: includes/general-template.php:495
17451745
msgid "Sorted by"
17461746
msgstr ""
17471747

1748-
#: includes/general-template.php:499
1748+
#: includes/general-template.php:496
17491749
msgid "Sort by"
17501750
msgstr ""
17511751

17521752
#. translators: 1: Results per page.
1753-
#: includes/general-template.php:506
1753+
#: includes/general-template.php:503
17541754
#, php-format
17551755
msgid "Results per-page: %s "
17561756
msgstr ""
17571757

1758-
#: includes/general-template.php:598
1758+
#: includes/general-template.php:595
17591759
msgid "Relevance:"
17601760
msgstr ""
17611761

0 commit comments

Comments
 (0)