From 9b6b0c9a7131610a2a2f0e75cdc78ac330945926 Mon Sep 17 00:00:00 2001 From: Ajay D'Souza Date: Thu, 10 Oct 2024 23:24:42 +0100 Subject: [PATCH] Remove duplication of filters being called --- includes/class-better-search-core-query.php | 35 ++++++++---------- includes/class-better-search-query.php | 2 +- includes/class-main.php | 41 ++++++++++++++------- 3 files changed, 44 insertions(+), 34 deletions(-) diff --git a/includes/class-better-search-core-query.php b/includes/class-better-search-core-query.php index e56da96..4d8bc42 100644 --- a/includes/class-better-search-core-query.php +++ b/includes/class-better-search-core-query.php @@ -102,14 +102,6 @@ class Better_Search_Core_Query { */ public $query_args = array(); - /** - * Holds the top score. - * - * @since 4.0.0 - * @var bool - */ - public $is_better_search_query = true; - /** * Holds the array of stopwords. * @@ -136,8 +128,9 @@ class Better_Search_Core_Query { public function __construct( $args = array() ) { $this->prepare_query_args( $args ); - if ( ( $this->is_seamless_mode && is_main_query() ) || - ( isset( $args['better_search_query'] ) && true === $args['better_search_query'] && ! $this->is_better_search_query ) ) { + if ( ( $this->is_seamless_mode && is_main_query() && ! wp_is_serving_rest_request() ) || + ! empty( $args['better_search_query'] ) + ) { $this->hooks(); } } @@ -149,6 +142,10 @@ public function __construct( $args = array() ) { */ public function hooks() { + if ( isset( $this->query_args['is_better_search_loaded'] ) && $this->query_args['is_better_search_loaded'] ) { + return; + } + add_filter( 'pre_get_posts', array( $this, 'pre_get_posts' ), 10 ); add_filter( 'posts_fields', array( $this, 'posts_fields' ), 10, 2 ); add_filter( 'posts_join', array( $this, 'posts_join' ), 10, 2 ); @@ -199,11 +196,9 @@ public function prepare_query_args( $args = array() ) { $args['better_search_query'] = true; $args['suppress_filters'] = false; $args['ignore_sticky_posts'] = true; - $args['no_found_rows'] = true; // Store query args before we manipulate them. - $this->input_query_args = $args; - $this->is_better_search_query = isset( $this->input_query_args['is_better_search_query'] ) ? $this->input_query_args['is_better_search_query'] : false; + $this->input_query_args = $args; /** * Applies filters to the query arguments before executing the query. @@ -489,6 +484,9 @@ public function pre_get_posts( $query ) { 'post_status', 'posts_per_page', 'author', + 'no_found_rows', + 'suppress_filters', + 'ignore_sticky_posts', ); foreach ( $fields as $field ) { @@ -497,9 +495,6 @@ public function pre_get_posts( $query ) { } } - $query->set( 'suppress_filters', false ); - $query->set( 'no_found_rows', true ); - $query->set( 'ignore_sticky_posts', true ); remove_filter( 'pre_get_posts', array( $this, 'pre_get_posts' ) ); } } @@ -963,14 +958,14 @@ public function posts_groupby( $groupby, $query ) { } /** - * Filter posts_clauses in WP_Query + * Filter posts_clauses in WP_Query. * * @since 4.0.0 * * @param string[] $clauses Array of clauses. * @param \WP_Query $query The WP_Query instance. * - * @return array Updated Array of clauses. + * @return array Updated array of clauses. */ public function posts_clauses( $clauses, $query ) { @@ -983,8 +978,8 @@ public function posts_clauses( $clauses, $query ) { * * @since 4.0.0 * - * @param string[] $clauses Array of clauses. - * @param Better_Search_Core_Query $query The Better_Search instance (passed by reference). + * @param string[] $clauses Array of clauses. + * @param Better_Search_Core_Query $query The Better_Search instance (passed by reference). */ $clauses = apply_filters_ref_array( 'better_search_query_posts_clauses', array( $clauses, &$this ) ); diff --git a/includes/class-better-search-query.php b/includes/class-better-search-query.php index be9a5b1..7782fb4 100644 --- a/includes/class-better-search-query.php +++ b/includes/class-better-search-query.php @@ -28,7 +28,7 @@ class Better_Search_Query extends WP_Query { * @param array|string $args The Query variables. Accepts an array or a query string. */ public function __construct( $args = array() ) { - $args = wp_parse_args( $args, array( 'better_search_query' => true ) ); + $args = wp_parse_args( $args, array( 'is_better_search_loaded' => true ) ); $core_query = new Better_Search_Core_Query( $args ); add_filter( 'pre_get_posts', array( $core_query, 'pre_get_posts' ), 10 ); diff --git a/includes/class-main.php b/includes/class-main.php index 6fde76c..8b8e1ed 100644 --- a/includes/class-main.php +++ b/includes/class-main.php @@ -90,6 +90,15 @@ final class Main { */ public $live_search; + /** + * Pro. + * + * @since 4.0.0 + * + * @var object Pro. + */ + public $pro; + /** * Gets the instance of the class. * @@ -127,6 +136,7 @@ private function init() { $this->shortcodes = new Frontend\Shortcodes(); $this->display = new Frontend\Display(); $this->live_search = new Frontend\Live_Search(); + $this->pro = new Pro\Pro(); $this->hooks(); @@ -158,7 +168,7 @@ public function initiate_plugin() { } /** - * Initialise the Top 10 widgets. + * Initialise the Better Search widgets. * * @since 3.3.0 */ @@ -289,21 +299,26 @@ public static function document_title( $title ) { return $title; } - /** - * Hook into WP_Query to check if bsearch_query is set and is true. If so, we load the Top 10 query. - * - * @since 3.5.0 - * - * @param \WP_Query $query The WP_Query object. - */ + /** + * Hook into WP_Query to check if better_search_query is set and true. + * If so, load the Better Search query. + * + * @since 3.5.0 + * + * @param \WP_Query $query The WP_Query object. + */ public function parse_query( $query ) { if ( true === $query->get( 'better_search_query' ) ) { - new Better_Search_Core_Query( $query->query_vars ); + // Load the Better Search query only if it's not already initialized. + if ( ! isset( $query->query_vars['is_better_search_loaded'] ) || ! $query->query_vars['is_better_search_loaded'] ) { + $query->set( 'is_better_search_loaded', true ); + new Better_Search_Core_Query( $query->query_vars ); + } } } /** - * Checks if another version of Top 10/Top 10 Pro is active and deactivates it. + * Checks if another version of Better Search/Better Search Pro is active and deactivates it. * Hooked on `activated_plugin` so other plugin is deactivated when current plugin is activated. * * @since 3.5.0 @@ -344,7 +359,7 @@ public function activated_plugin( $plugin, $network_wide ) { } /** - * Displays a notice when either Top 10 or Top 10 Pro is automatically deactivated. + * Displays a notice when either Better Search or Better Search Pro is automatically deactivated. * * @since 3.5.0 */ @@ -354,9 +369,9 @@ public function plugin_deactivated_notice() { return; } - $message = __( "Top 10 and Top 10 Pro should not be active at the same time. We've automatically deactivated Top 10.", 'better-search' ); + $message = __( "Better Search and Better Search Pro should not be active at the same time. We've automatically deactivated Better Search.", 'better-search' ); if ( 2 === $deactivated_notice_id ) { - $message = __( "Top 10 and Top 10 Pro should not be active at the same time. We've automatically deactivated Top 10 Pro.", 'better-search' ); + $message = __( "Better Search and Better Search Pro should not be active at the same time. We've automatically deactivated Better Search Pro.", 'better-search' ); } ?>