Relevanssi – A Better Search - Version 4.18.1

Version Description

  • New feature: New filter hook relevanssi_add_highlight_and_tracking can be used to force Relevanssi to add the highlight and tracking parameters to permalinks.
  • Changed behaviour: The 'relevanssi_wpml_filter' filter function now runs on priority 9 instead of 10 to avoid problems with custom filters on relevanssi_hits_filter.
  • Minor fix: Handle cases of missing posts better; relevanssi_get_post() now returns a WP_Error if no post is found.
  • Minor fix: Search queries that contain apostrophes and quotes can now be deleted from the log.
  • Minor fix: Avoid a slow query on the searching tab when the throttle is not enabled.
Download this release

Release Info

Developer msaari
Plugin Icon 128x128 Relevanssi – A Better Search
Version 4.18.1
Comparing to
See all releases

Code changes from version 2.20.0 to 4.18.1

lib/common.php CHANGED
@@ -202,7 +202,7 @@ function relevanssi_default_post_ok( $post_ok, $post_id ) {
202
  $current_user = wp_get_current_user();
203
  if ( ! $post_ok && $current_user->ID > 0 ) {
204
  $post = relevanssi_get_post( $post_id );
205
- if ( $current_user->ID === (int) $post->post_author ) {
206
  // Allow authors to see their own private posts.
207
  $post_ok = true;
208
  }
@@ -1041,7 +1041,7 @@ function relevanssi_permalink( $link, $link_post = null ) {
1041
  global $post;
1042
  $link_post = $post;
1043
  } elseif ( is_int( $link_post ) ) {
1044
- $link_post = get_post( $link_post );
1045
  }
1046
  // Using property_exists() to avoid troubles from magic variables.
1047
  if ( is_object( $link_post ) && property_exists( $link_post, 'relevanssi_link' ) ) {
@@ -1063,11 +1063,28 @@ function relevanssi_permalink( $link, $link_post = null ) {
1063
  $add_highlight_and_tracking = true;
1064
  }
1065
 
1066
- if ( $add_highlight_and_tracking && is_object( $link_post ) && property_exists( $link_post, 'relevance_score' ) ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1067
  $link = relevanssi_add_highlight( $link, $link_post );
1068
  }
1069
 
1070
- if ( $add_highlight_and_tracking && function_exists( 'relevanssi_add_tracking' ) && property_exists( $link_post, 'relevance_score' ) ) {
1071
  $link = relevanssi_add_tracking( $link, $link_post );
1072
  }
1073
 
202
  $current_user = wp_get_current_user();
203
  if ( ! $post_ok && $current_user->ID > 0 ) {
204
  $post = relevanssi_get_post( $post_id );
205
+ if ( ! is_wp_error( $post ) && $current_user->ID === (int) $post->post_author ) {
206
  // Allow authors to see their own private posts.
207
  $post_ok = true;
208
  }
1041
  global $post;
1042
  $link_post = $post;
1043
  } elseif ( is_int( $link_post ) ) {
1044
+ $link_post = relevanssi_get_post( $link_post );
1045
  }
1046
  // Using property_exists() to avoid troubles from magic variables.
1047
  if ( is_object( $link_post ) && property_exists( $link_post, 'relevanssi_link' ) ) {
1063
  $add_highlight_and_tracking = true;
1064
  }
1065
 
1066
+ if ( is_object( $link_post ) && ! property_exists( $link_post, 'relevance_score' ) ) {
1067
+ $add_highlight_and_tracking = false;
1068
+ }
1069
+
1070
+ /**
1071
+ * Filters whether to add the highlight and tracking parameters to the link.
1072
+ *
1073
+ * @param boolean $add_highlight_and_tracking Whether to add the highlight
1074
+ * and tracking parameters to the link.
1075
+ * @param object $link_post The post object.
1076
+ */
1077
+ $add_highlight_and_tracking = apply_filters(
1078
+ 'relevanssi_add_highlight_and_tracking',
1079
+ $add_highlight_and_tracking,
1080
+ $link_post
1081
+ );
1082
+
1083
+ if ( $add_highlight_and_tracking ) {
1084
  $link = relevanssi_add_highlight( $link, $link_post );
1085
  }
1086
 
1087
+ if ( $add_highlight_and_tracking && function_exists( 'relevanssi_add_tracking' ) ) {
1088
  $link = relevanssi_add_tracking( $link, $link_post );
1089
  }
1090
 
lib/compatibility/wpml.php CHANGED
@@ -10,7 +10,7 @@
10
  * @see https://www.relevanssi.com/
11
  */
12
 
13
- add_filter( 'relevanssi_hits_filter', 'relevanssi_wpml_filter' );
14
  add_filter( 'relevanssi_tag_before_tokenize', 'relevanssi_wpml_term_fix', 10, 4 );
15
 
16
  /**
10
  * @see https://www.relevanssi.com/
11
  */
12
 
13
+ add_filter( 'relevanssi_hits_filter', 'relevanssi_wpml_filter', 9 );
14
  add_filter( 'relevanssi_tag_before_tokenize', 'relevanssi_wpml_term_fix', 10, 4 );
15
 
16
  /**
lib/log.php CHANGED
@@ -334,7 +334,7 @@ function relevanssi_delete_query_from_log( string $query ) {
334
  $deleted = $wpdb->query(
335
  $wpdb->prepare(
336
  "DELETE FROM {$relevanssi_variables['log_table']} WHERE query = %s", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQL.NotPrepared
337
- $query
338
  )
339
  );
340
 
334
  $deleted = $wpdb->query(
335
  $wpdb->prepare(
336
  "DELETE FROM {$relevanssi_variables['log_table']} WHERE query = %s", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQL.NotPrepared
337
+ stripslashes( $query )
338
  )
339
  );
340
 
lib/search.php CHANGED
@@ -437,8 +437,11 @@ function relevanssi_search( $args ) {
437
  $hits[ intval( $i ) ] = relevanssi_generate_id_type( $doc );
438
  }
439
  } else {
440
- $hits[ intval( $i ) ] = relevanssi_get_post( $doc );
441
- $hits[ intval( $i ) ]->relevance_score = round( $weight, 2 );
 
 
 
442
 
443
  if ( isset( $missing_terms[ $doc ] ) ) {
444
  $hits[ intval( $i ) ]->missing_terms = $missing_terms[ $doc ];
@@ -1381,7 +1384,7 @@ function relevanssi_calculate_weight( $match, $idf, $post_type_weights, $query )
1381
  $recency_cutoff_date = $recency_details['cutoff'];
1382
  if ( $recency_bonus ) {
1383
  $post = relevanssi_get_post( $match->doc );
1384
- if ( strtotime( $post->post_date ) > $recency_cutoff_date ) {
1385
  $weight = $weight * $recency_bonus;
1386
  }
1387
  }
@@ -1404,10 +1407,10 @@ function relevanssi_calculate_weight( $match, $idf, $post_type_weights, $query )
1404
 
1405
  $post = relevanssi_get_post( $match->doc );
1406
  $clean_query = str_replace( '"', '', $query );
1407
- if ( stristr( $post->post_title, $clean_query ) !== false ) {
1408
  $weight *= $exact_match_boost['title'];
1409
  }
1410
- if ( stristr( $post->post_content, $clean_query ) !== false ) {
1411
  $weight *= $exact_match_boost['content'];
1412
  }
1413
  }
437
  $hits[ intval( $i ) ] = relevanssi_generate_id_type( $doc );
438
  }
439
  } else {
440
+ $post_object = relevanssi_get_post( $doc );
441
+ if ( ! is_wp_error( $post_object ) ) {
442
+ $hits[ intval( $i ) ] = $post_object;
443
+ $hits[ intval( $i ) ]->relevance_score = round( $weight, 2 );
444
+ }
445
 
446
  if ( isset( $missing_terms[ $doc ] ) ) {
447
  $hits[ intval( $i ) ]->missing_terms = $missing_terms[ $doc ];
1384
  $recency_cutoff_date = $recency_details['cutoff'];
1385
  if ( $recency_bonus ) {
1386
  $post = relevanssi_get_post( $match->doc );
1387
+ if ( ! is_wp_error( $post ) && strtotime( $post->post_date ) > $recency_cutoff_date ) {
1388
  $weight = $weight * $recency_bonus;
1389
  }
1390
  }
1407
 
1408
  $post = relevanssi_get_post( $match->doc );
1409
  $clean_query = str_replace( '"', '', $query );
1410
+ if ( ! is_wp_error( $post ) && stristr( $post->post_title, $clean_query ) !== false ) {
1411
  $weight *= $exact_match_boost['title'];
1412
  }
1413
+ if ( ! is_wp_error( $post ) && stristr( $post->post_content, $clean_query ) !== false ) {
1414
  $weight *= $exact_match_boost['content'];
1415
  }
1416
  }
lib/tabs/searching-tab.php CHANGED
@@ -60,7 +60,11 @@ function relevanssi_searching_tab() {
60
  }
61
 
62
  if ( ! $throttle ) {
63
- $docs_count = $wpdb->get_var( 'SELECT COUNT(DISTINCT doc) FROM ' . $relevanssi_variables['relevanssi_table'] . ' WHERE doc != -1' ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQL.NotPrepared
 
 
 
 
64
  } else {
65
  $docs_count = null;
66
  }
60
  }
61
 
62
  if ( ! $throttle ) {
63
+ $docs_count = get_transient( 'relevanssi_docs_count' );
64
+ if ( ! $docs_count ) {
65
+ $docs_count = $wpdb->get_var( 'SELECT COUNT(DISTINCT doc) FROM ' . $relevanssi_variables['relevanssi_table'] . ' WHERE doc != -1' ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQL.NotPrepared
66
+ set_transient( 'relevanssi_docs_count', $docs_count, WEEK_IN_SECONDS );
67
+ }
68
  } else {
69
  $docs_count = null;
70
  }
lib/utils.php CHANGED
@@ -389,7 +389,7 @@ function relevanssi_get_an_object( $source ) {
389
  function relevanssi_get_attachment_suffix( $post ) : string {
390
  if ( ! is_object( $post ) ) {
391
  $post = relevanssi_get_post( $post );
392
- if ( ! $post ) {
393
  return '';
394
  }
395
  }
@@ -514,7 +514,8 @@ function relevanssi_get_permalink( $post = 0 ) {
514
  * @param int $blog_id The blog ID, default -1. If -1, will be replaced
515
  * with the actual current blog ID from get_current_blog_id().
516
  *
517
- * @return object The post object.
 
518
  */
519
  function relevanssi_get_post( $post_id, int $blog_id = -1 ) {
520
  if ( -1 === $blog_id ) {
@@ -535,6 +536,9 @@ function relevanssi_get_post( $post_id, int $blog_id = -1 ) {
535
 
536
  $relevanssi_post_array[ $post_id ] = $post;
537
  }
 
 
 
538
  return $post;
539
  }
540
 
@@ -587,8 +591,9 @@ function relevanssi_get_post_meta_for_all_posts( array $post_ids, string $field
587
  * @param int|string $post_id An ID, either an integer post ID or a
588
  * **type**id string for terms and users.
589
  *
590
- * @return WP_Post|WP_Term|WP_User An object, type of which depends on the
591
- * target object.
 
592
  */
593
  function relevanssi_get_post_object( $post_id ) {
594
  $object = null;
@@ -678,14 +683,15 @@ function relevanssi_get_the_tags( string $before = '', string $separator = ', ',
678
  *
679
  * @param int|WP_Post $post The post ID or a post object.
680
  *
681
- * @return string The post title with highlights.
 
682
  */
683
  function relevanssi_get_the_title( $post ) {
684
  if ( is_numeric( $post ) ) {
685
  $post = relevanssi_get_post( $post );
686
  }
687
- if ( ! is_object( $post ) ) {
688
- return null;
689
  }
690
  if ( empty( $post->post_highlighted_title ) ) {
691
  $post->post_highlighted_title = $post->post_title;
@@ -1510,7 +1516,7 @@ function relevanssi_turn_off_options( array &$request, array $options ) {
1510
  * @param boolean $positive If true, replace negative values and zeroes with
1511
  * $default.
1512
  */
1513
- function relevanssi_update_floatval( array $request, string $option, bool $autoload = true, int $default = 0, bool $positive = false ) {
1514
  if ( isset( $request[ $option ] ) ) {
1515
  $value = floatval( $request[ $option ] );
1516
  if ( ! $value ) {
389
  function relevanssi_get_attachment_suffix( $post ) : string {
390
  if ( ! is_object( $post ) ) {
391
  $post = relevanssi_get_post( $post );
392
+ if ( is_wp_error( $post ) ) {
393
  return '';
394
  }
395
  }
514
  * @param int $blog_id The blog ID, default -1. If -1, will be replaced
515
  * with the actual current blog ID from get_current_blog_id().
516
  *
517
+ * @return object|WP_Error The post object or a WP_Error object if the post
518
+ * doesn't exist.
519
  */
520
  function relevanssi_get_post( $post_id, int $blog_id = -1 ) {
521
  if ( -1 === $blog_id ) {
536
 
537
  $relevanssi_post_array[ $post_id ] = $post;
538
  }
539
+ if ( ! $post ) {
540
+ $post = new WP_Error( 'post_not_found', __( 'The requested post does not exist.' ) );
541
+ }
542
  return $post;
543
  }
544
 
591
  * @param int|string $post_id An ID, either an integer post ID or a
592
  * **type**id string for terms and users.
593
  *
594
+ * @return WP_Post|WP_Term|WP_User|WP_Error An object, type of which depends on
595
+ * the target object. If relevanssi_get_post() doesn't find the post, this
596
+ * returns a WP_Error.
597
  */
598
  function relevanssi_get_post_object( $post_id ) {
599
  $object = null;
683
  *
684
  * @param int|WP_Post $post The post ID or a post object.
685
  *
686
+ * @return string The post title with highlights and an empty string, if the
687
+ * post cannot be found.
688
  */
689
  function relevanssi_get_the_title( $post ) {
690
  if ( is_numeric( $post ) ) {
691
  $post = relevanssi_get_post( $post );
692
  }
693
+ if ( is_wp_error( $post ) ) {
694
+ return '';
695
  }
696
  if ( empty( $post->post_highlighted_title ) ) {
697
  $post->post_highlighted_title = $post->post_title;
1516
  * @param boolean $positive If true, replace negative values and zeroes with
1517
  * $default.
1518
  */
1519
+ function relevanssi_update_floatval( array $request, string $option, bool $autoload = true, float $default = 0, bool $positive = false ) {
1520
  if ( isset( $request[ $option ] ) ) {
1521
  $value = floatval( $request[ $option ] );
1522
  if ( ! $value ) {
readme.txt CHANGED
@@ -5,7 +5,7 @@ Tags: search, relevance, better search, product search, woocommerce search
5
  Requires at least: 4.9
6
  Tested up to: 6.1
7
  Requires PHP: 7.0
8
- Stable tag: 4.18.0
9
  License: GPLv2 or later
10
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
 
@@ -128,6 +128,13 @@ Each document database is full of useless words. All the little words that appea
128
  * John Calahan for extensive 4.0 beta testing.
129
 
130
  == Changelog ==
 
 
 
 
 
 
 
131
  = 4.18.0 =
132
  * New feature: Relevanssi now shows the MySQL `max_allowed_packet` size on the debug tab.
133
  * New feature: Relevanssi now shows the indexing query on the debug tab.
@@ -173,6 +180,9 @@ Each document database is full of useless words. All the little words that appea
173
  * Minor fix: Prevents fatal errors from `relevanssi_strip_all_tags()`.
174
 
175
  == Upgrade notice ==
 
 
 
176
  = 4.18.0 =
177
  * Debugging features, improved ACF support and bug fixes.
178
 
5
  Requires at least: 4.9
6
  Tested up to: 6.1
7
  Requires PHP: 7.0
8
+ Stable tag: 4.18.1
9
  License: GPLv2 or later
10
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
 
128
  * John Calahan for extensive 4.0 beta testing.
129
 
130
  == Changelog ==
131
+ = 4.18.1 =
132
+ * New feature: New filter hook `relevanssi_add_highlight_and_tracking` can be used to force Relevanssi to add the `highlight` and tracking parameters to permalinks.
133
+ * Changed behaviour: The 'relevanssi_wpml_filter' filter function now runs on priority 9 instead of 10 to avoid problems with custom filters on relevanssi_hits_filter.
134
+ * Minor fix: Handle cases of missing posts better; relevanssi_get_post() now returns a WP_Error if no post is found.
135
+ * Minor fix: Search queries that contain apostrophes and quotes can now be deleted from the log.
136
+ * Minor fix: Avoid a slow query on the searching tab when the throttle is not enabled.
137
+
138
  = 4.18.0 =
139
  * New feature: Relevanssi now shows the MySQL `max_allowed_packet` size on the debug tab.
140
  * New feature: Relevanssi now shows the indexing query on the debug tab.
180
  * Minor fix: Prevents fatal errors from `relevanssi_strip_all_tags()`.
181
 
182
  == Upgrade notice ==
183
+ = 4.18.1 =
184
+ * Small bug fixes.
185
+
186
  = 4.18.0 =
187
  * Debugging features, improved ACF support and bug fixes.
188
 
relevanssi.php CHANGED
@@ -13,7 +13,7 @@
13
  * Plugin Name: Relevanssi
14
  * Plugin URI: https://www.relevanssi.com/
15
  * Description: This plugin replaces WordPress search with a relevance-sorting search.
16
- * Version: 4.18.0
17
  * Author: Mikko Saari
18
  * Author URI: http://www.mikkosaari.fi/
19
  * Text Domain: relevanssi
@@ -67,7 +67,7 @@ $relevanssi_variables['database_version'] = 6;
67
  $relevanssi_variables['file'] = __FILE__;
68
  $relevanssi_variables['plugin_dir'] = plugin_dir_path( __FILE__ );
69
  $relevanssi_variables['plugin_basename'] = plugin_basename( __FILE__ );
70
- $relevanssi_variables['plugin_version'] = '4.18.0';
71
 
72
  require_once 'lib/admin-ajax.php';
73
  require_once 'lib/common.php';
13
  * Plugin Name: Relevanssi
14
  * Plugin URI: https://www.relevanssi.com/
15
  * Description: This plugin replaces WordPress search with a relevance-sorting search.
16
+ * Version: 4.18.1
17
  * Author: Mikko Saari
18
  * Author URI: http://www.mikkosaari.fi/
19
  * Text Domain: relevanssi
67
  $relevanssi_variables['file'] = __FILE__;
68
  $relevanssi_variables['plugin_dir'] = plugin_dir_path( __FILE__ );
69
  $relevanssi_variables['plugin_basename'] = plugin_basename( __FILE__ );
70
+ $relevanssi_variables['plugin_version'] = '4.18.1';
71
 
72
  require_once 'lib/admin-ajax.php';
73
  require_once 'lib/common.php';