Skip to content

Commit

Permalink
Remove duplication of filters being called
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaydsouza committed Oct 10, 2024
1 parent 2d5c706 commit 9b6b0c9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 34 deletions.
35 changes: 15 additions & 20 deletions includes/class-better-search-core-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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();
}
}
Expand All @@ -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 );
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 ) {
Expand All @@ -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' ) );
}
}
Expand Down Expand Up @@ -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 ) {

Expand All @@ -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 ) );

Expand Down
2 changes: 1 addition & 1 deletion includes/class-better-search-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
41 changes: 28 additions & 13 deletions includes/class-main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -158,7 +168,7 @@ public function initiate_plugin() {
}

/**
* Initialise the Top 10 widgets.
* Initialise the Better Search widgets.
*
* @since 3.3.0
*/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
*/
Expand All @@ -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' );
}

?>
Expand Down

0 comments on commit 9b6b0c9

Please sign in to comment.