Skip to content

Commit 1db55a9

Browse files
committed
Fixed relevance score issues
Fixes #124 - Relevance score was always 0 - Relevance query returned more than 1 result.
1 parent 009891d commit 1db55a9

18 files changed

Lines changed: 165 additions & 99 deletions

better-search.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Plugin Name: Better Search
1414
* Plugin URI: https://webberzone.com/plugins/better-search/
1515
* Description: Replace the default WordPress search with a contextual search. Search results are sorted by relevancy ensuring a better visitor search experience.
16-
* Version: 4.1.1
16+
* Version: 4.1.2-beta1
1717
* Author: WebberZone
1818
* Author URI: https://webberzone.com/
1919
* Text Domain: better-search

includes/class-better-search-core-query.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ class Better_Search_Core_Query extends \WP_Query {
126126
*/
127127
public $match_sql = '';
128128

129+
/**
130+
* Holds the array of post scores.
131+
*
132+
* @since 4.1.2
133+
* @var array
134+
*/
135+
public $post_scores = array();
136+
129137
/**
130138
* Main constructor.
131139
*
@@ -165,7 +173,7 @@ public function hooks() {
165173
add_filter( 'posts_groupby', array( $this, 'posts_groupby' ), 10, 2 );
166174
add_filter( 'posts_clauses', array( $this, 'posts_clauses' ), 10, 2 );
167175
add_filter( 'posts_request', array( $this, 'posts_request' ), 10, 2 );
168-
add_filter( 'better_search_query_posts_request', array( $this, 'set_topscore' ), 999, 2 );
176+
add_filter( 'better_search_query_posts_request', array( $this, 'set_topscore' ), PHP_INT_MAX, 2 );
169177
add_filter( 'posts_pre_query', array( $this, 'posts_pre_query' ), 10, 2 );
170178
add_filter( 'the_posts', array( $this, 'the_posts' ), 10, 2 );
171179
}
@@ -1138,6 +1146,14 @@ public function the_posts( $posts, $query ) {
11381146
return $posts;
11391147
}
11401148

1149+
// Store scores in the query object for template access.
1150+
$query->post_scores = array();
1151+
foreach ( $posts as $post ) {
1152+
if ( isset( $post->score ) ) {
1153+
$query->post_scores[ $post->ID ] = floatval( $post->score );
1154+
}
1155+
}
1156+
11411157
// Support caching to speed up retrieval.
11421158
if ( ! empty( $posts ) && ! empty( $this->query_args['cache'] ) && ! $this->in_cache && ! ( $query->is_preview() || is_admin() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) ) {
11431159

@@ -1230,7 +1246,7 @@ public function set_topscore( $request, $query ) {
12301246

12311247
// Take $request. Check if there is a LIMIT clause. If so, make sure it's limited to a single entry only. If there is no LIMIT then add it to extract a single entry only. Also, check if there is an ORDER BY clause. If so, make sure it's ordered by the score column. If there is no ORDER BY clause then add one.
12321248
if ( strpos( $request, 'LIMIT' ) !== false ) {
1233-
$score_query = preg_replace( '/LIMIT\s+\\d+(,\\d+)?/', 'LIMIT 1', $request );
1249+
$score_query = preg_replace( '/LIMIT\s+(?:\d+\s*,\s*)?\d+/', 'LIMIT 1', $request );
12341250
} else {
12351251
$score_query = $request . ' LIMIT 1';
12361252
}
@@ -1253,8 +1269,9 @@ public function set_topscore( $request, $query ) {
12531269
$score_query = $score_query . ' ORDER BY score DESC';
12541270
}
12551271

1256-
$this->topscore = (float) $wpdb->get_var( $score_query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
1272+
$topscore = $wpdb->get_results( $score_query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
12571273

1274+
$this->topscore = ! empty( $topscore ) ? (float) wp_list_pluck( $topscore, 'score' )[0] : 0;
12581275
$query->topscore = $this->topscore;
12591276

12601277
if ( ! empty( $this->query_args['cache'] ) ) {

includes/class-better-search-query.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function __construct( $args = array() ) {
4141
add_filter( 'posts_groupby', array( $core_query, 'posts_groupby' ), 10, 2 );
4242
add_filter( 'posts_clauses', array( $core_query, 'posts_clauses' ), 10, 2 );
4343
add_filter( 'posts_request', array( $core_query, 'posts_request' ), 10, 2 );
44-
add_filter( 'better_search_query_posts_request', array( $core_query, 'set_topscore' ), 10, 2 );
44+
add_filter( 'better_search_query_posts_request', array( $core_query, 'set_topscore' ), PHP_INT_MAX, 2 );
4545
add_filter( 'posts_pre_query', array( $core_query, 'posts_pre_query' ), 10, 2 );
4646
add_filter( 'the_posts', array( $core_query, 'the_posts' ), 10, 2 );
4747

@@ -58,7 +58,7 @@ public function __construct( $args = array() ) {
5858
remove_filter( 'posts_groupby', array( $core_query, 'posts_groupby' ) );
5959
remove_filter( 'posts_clauses', array( $core_query, 'posts_clauses' ) );
6060
remove_filter( 'posts_request', array( $core_query, 'posts_request' ) );
61-
remove_filter( 'better_search_query_posts_request', array( $core_query, 'set_topscore' ) );
61+
remove_filter( 'better_search_query_posts_request', array( $core_query, 'set_topscore' ), PHP_INT_MAX );
6262
remove_filter( 'posts_pre_query', array( $core_query, 'posts_pre_query' ) );
6363
remove_filter( 'the_posts', array( $core_query, 'the_posts' ) );
6464
}

includes/class-main.php

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
namespace WebberZone\Better_Search;
99

1010
use WebberZone\Better_Search\Admin\Activator;
11+
1112
if ( ! defined( 'WPINC' ) ) {
1213
exit;
1314
}
15+
1416
/**
1517
* Main plugin class.
1618
*
@@ -117,6 +119,7 @@ public static function get_instance() {
117119
self::$instance = new self();
118120
self::$instance->init();
119121
}
122+
120123
return self::$instance;
121124
}
122125

@@ -142,10 +145,19 @@ private function init() {
142145
$this->display = new Frontend\Display();
143146
$this->live_search = new Frontend\Live_Search();
144147
$this->template_handler = new Frontend\Template_Handler();
148+
145149
$this->hooks();
150+
146151
if ( ! function_exists( 'bsearch_freemius' ) ) {
147152
require_once __DIR__ . '/load-freemius.php';
148153
}
154+
155+
if ( bsearch_freemius()->is__premium_only() ) {
156+
if ( bsearch_freemius()->can_use_premium_code() ) {
157+
$this->pro = new Pro\Pro();
158+
}
159+
}
160+
149161
if ( is_admin() ) {
150162
$this->admin = new Admin\Admin();
151163
if ( is_multisite() ) {
@@ -182,8 +194,8 @@ public function initiate_plugin() {
182194
* @since 3.3.0
183195
*/
184196
public function register_widgets() {
185-
register_widget( '\\WebberZone\\Better_Search\\Frontend\\Widgets\\Search_Box' );
186-
register_widget( '\\WebberZone\\Better_Search\\Frontend\\Widgets\\Search_Heatmap' );
197+
register_widget( '\WebberZone\Better_Search\Frontend\Widgets\Search_Box' );
198+
register_widget( '\WebberZone\Better_Search\Frontend\Widgets\Search_Heatmap' );
187199
}
188200

189201
/**
@@ -199,20 +211,25 @@ public function activated_plugin( $plugin, $network_wide ) {
199211
if ( ! in_array( $plugin, array( 'better-search/better-search.php', 'better-search-pro/better-search.php' ), true ) ) {
200212
return;
201213
}
214+
202215
Activator::activation_hook( $network_wide );
216+
203217
$plugin_to_deactivate = 'better-search/better-search.php';
204218
$deactivated_notice_id = '1';
219+
205220
// If we just activated the free version, deactivate the pro version.
206221
if ( $plugin === $plugin_to_deactivate ) {
207222
$plugin_to_deactivate = 'better-search-pro/better-search.php';
208223
$deactivated_notice_id = '2';
209224
}
225+
210226
if ( is_multisite() && is_network_admin() ) {
211227
$active_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
212228
$active_plugins = array_keys( $active_plugins );
213229
} else {
214230
$active_plugins = (array) get_option( 'active_plugins', array() );
215231
}
232+
216233
foreach ( $active_plugins as $plugin_basename ) {
217234
if ( $plugin_to_deactivate === $plugin_basename ) {
218235
set_transient( 'bsearch_deactivated_notice_id', $deactivated_notice_id, 1 * HOUR_IN_SECONDS );
@@ -232,10 +249,12 @@ public function plugin_deactivated_notice() {
232249
if ( ! in_array( $deactivated_notice_id, array( 1, 2 ), true ) ) {
233250
return;
234251
}
252+
235253
$message = __( "Better Search and Better Search Pro should not be active at the same time. We've automatically deactivated Better Search.", 'better-search' );
236254
if ( 2 === $deactivated_notice_id ) {
237255
$message = __( "Better Search and Better Search Pro should not be active at the same time. We've automatically deactivated Better Search Pro.", 'better-search' );
238256
}
257+
239258
?>
240259
<div class="updated" style="border-left: 4px solid #ffba00;">
241260
<p>
@@ -245,6 +264,7 @@ public function plugin_deactivated_notice() {
245264
</p>
246265
</div>
247266
<?php
267+
248268
delete_transient( 'bsearch_deactivated_notice_id' );
249269
}
250270

@@ -260,37 +280,12 @@ public static function pro_upgrade_banner( $donate = true ) {
260280
?>
261281
<div id="pro-upgrade-banner">
262282
<div class="inside">
263-
<p><a href="https://webberzone.com/plugins/better-search/pro/" target="_blank"><img src="
264-
<?php
265-
echo esc_url( BETTER_SEARCH_PLUGIN_URL . 'includes/admin/images/better-search-pro-banner.png' );
266-
?>
267-
" alt="
268-
<?php
269-
esc_html_e( 'Better Search Pro - Coming soon. Sign up to find out more', 'better-search' );
270-
?>
271-
" width="300" height="300" style="max-width: 100%;" /></a></p>
272-
273-
<?php
274-
if ( $donate ) {
275-
?>
276-
277-
<p style="text-align:center;">
278-
<?php
279-
esc_html_e( 'OR' );
280-
?>
281-
</p>
282-
<p><a href="https://wzn.io/donate-bs" target="_blank"><img src="
283-
<?php
284-
echo esc_url( BETTER_SEARCH_PLUGIN_URL . 'includes/admin/images/support.webp' );
285-
?>
286-
" alt="
287-
<?php
288-
esc_html_e( 'Support the development - Send us a donation today.', 'better-search' );
289-
?>
290-
" width="300" height="169" style="max-width: 100%;" /></a></p>
291-
<?php
292-
}
293-
?>
283+
<p><a href="https://webberzone.com/plugins/better-search/pro/" target="_blank"><img src="<?php echo esc_url( BETTER_SEARCH_PLUGIN_URL . 'includes/admin/images/better-search-pro-banner.png' ); ?>" alt="<?php esc_html_e( 'Better Search Pro - Coming soon. Sign up to find out more', 'better-search' ); ?>" width="300" height="300" style="max-width: 100%;" /></a></p>
284+
285+
<?php if ( $donate ) : ?>
286+
<p style="text-align:center;"><?php esc_html_e( 'OR' ); ?></p>
287+
<p><a href="https://wzn.io/donate-bs" target="_blank"><img src="<?php echo esc_url( BETTER_SEARCH_PLUGIN_URL . 'includes/admin/images/support.webp' ); ?>" alt="<?php esc_html_e( 'Support the development - Send us a donation today.', 'better-search' ); ?>" width="300" height="169" style="max-width: 100%;" /></a></p>
288+
<?php endif; ?>
294289
</div>
295290
</div>
296291
<?php

includes/general-template.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,28 @@ function the_bsearch_score( $args = array() ) {
548548
echo apply_filters( 'the_bsearch_score', get_bsearch_score( $args ), $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
549549
}
550550

551+
/**
552+
* Get the relevance score for a post.
553+
*
554+
* @since 4.1.2
555+
*
556+
* @param int $post_id Post ID. Default is current post.
557+
* @return float Post score or 0 if not available.
558+
*/
559+
function bsearch_get_post_score( $post_id = 0 ) {
560+
global $wp_query;
561+
562+
if ( ! $post_id ) {
563+
$post_id = get_the_ID();
564+
}
565+
566+
if ( isset( $wp_query->post_scores ) && isset( $wp_query->post_scores[ $post_id ] ) ) {
567+
return floatval( $wp_query->post_scores[ $post_id ] );
568+
}
569+
570+
return 0;
571+
}
572+
551573
/**
552574
* Retrieve the relevance score for the post in the search results.
553575
*

includes/load-freemius.php

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,48 @@
11
<?php
2+
23
/**
34
* Loads Freemius SDK.
45
*
56
* @package WebberZone\Better_Search
67
*/
7-
88
namespace WebberZone\Better_Search;
99

10-
if ( ! defined( 'ABSPATH' ) ) {
11-
exit;
10+
if ( !defined( 'ABSPATH' ) ) {
11+
exit;
1212
}
1313
/**
1414
* Initialize Freemius SDK.
1515
*/
1616
function bsearch_freemius() {
17-
global $bsearch_freemius;
18-
if ( ! isset( $bsearch_freemius ) ) {
19-
if ( ! defined( 'WP_FS__PRODUCT_17020_MULTISITE' ) ) {
20-
define( 'WP_FS__PRODUCT_17020_MULTISITE', true );
21-
}
22-
// Include Freemius SDK.
23-
require_once plugin_dir_path( __DIR__ ) . 'freemius/start.php';
24-
$bsearch_freemius = \fs_dynamic_init(
25-
array(
26-
'id' => '17020',
27-
'slug' => 'better-search',
28-
'premium_slug' => 'better-search-pro',
29-
'type' => 'plugin',
30-
'public_key' => 'pk_40525301bca835d9836ec4d946693',
31-
'is_premium' => false,
32-
'premium_suffix' => 'Pro',
33-
'has_addons' => false,
34-
'has_paid_plans' => true,
35-
'menu' => array(
36-
'slug' => 'bsearch_dashboard',
37-
'contact' => false,
38-
'support' => false,
39-
'network' => true,
40-
),
41-
'is_live' => true,
42-
)
43-
);
44-
}
45-
$bsearch_freemius->add_filter( 'plugin_icon', __NAMESPACE__ . '\\bsearch_freemius_get_plugin_icon' );
46-
$bsearch_freemius->add_filter( 'after_uninstall', __NAMESPACE__ . '\\bsearch_freemius_uninstall' );
47-
return $bsearch_freemius;
17+
global $bsearch_freemius;
18+
if ( !isset( $bsearch_freemius ) ) {
19+
if ( !defined( 'WP_FS__PRODUCT_17020_MULTISITE' ) ) {
20+
define( 'WP_FS__PRODUCT_17020_MULTISITE', true );
21+
}
22+
// Include Freemius SDK.
23+
require_once plugin_dir_path( __DIR__ ) . 'freemius/start.php';
24+
$bsearch_freemius = \fs_dynamic_init( array(
25+
'id' => '17020',
26+
'slug' => 'better-search',
27+
'premium_slug' => 'better-search-pro',
28+
'type' => 'plugin',
29+
'public_key' => 'pk_40525301bca835d9836ec4d946693',
30+
'is_premium' => true,
31+
'premium_suffix' => 'Pro',
32+
'has_addons' => false,
33+
'has_paid_plans' => true,
34+
'menu' => array(
35+
'slug' => 'bsearch_dashboard',
36+
'contact' => false,
37+
'support' => false,
38+
'network' => true,
39+
),
40+
'is_live' => true,
41+
) );
42+
}
43+
$bsearch_freemius->add_filter( 'plugin_icon', __NAMESPACE__ . '\\bsearch_freemius_get_plugin_icon' );
44+
$bsearch_freemius->add_filter( 'after_uninstall', __NAMESPACE__ . '\\bsearch_freemius_uninstall' );
45+
return $bsearch_freemius;
4846
}
4947

5048
/**
@@ -53,17 +51,20 @@ function bsearch_freemius() {
5351
* @return string
5452
*/
5553
function bsearch_freemius_get_plugin_icon() {
56-
return __DIR__ . '/admin/images/bsearch-icon.png';
54+
return __DIR__ . '/admin/images/bsearch-icon.png';
5755
}
5856

5957
/**
6058
* Uninstall the plugin.
6159
*/
6260
function bsearch_freemius_uninstall() {
63-
require_once dirname( __DIR__ ) . '/uninstaller.php';
61+
require_once dirname( __DIR__ ) . '/uninstaller.php';
62+
if ( bsearch_freemius()->can_use_premium_code__premium_only() ) {
63+
\WebberZone\Better_Search\Pro\Pro::uninstall_pro();
64+
}
6465
}
6566

6667
// Init Freemius.
6768
bsearch_freemius();
6869
// Signal that SDK was initiated.
69-
do_action( 'bsearch_freemius_loaded' );
70+
do_action( 'bsearch_freemius_loaded' );

includes/util/class-helpers.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,6 @@ public static function highlight( $input, $keys ) {
452452
* @return string Score converted to percentage
453453
*/
454454
public static function score2percent( $score, $topscore ) {
455-
456455
$output = '';
457456

458457
if ( $score > 0 && $topscore > 0 ) {

0 commit comments

Comments
 (0)