Advanced Woo Search - Version 1.20

Version Description

  • Add WPML, WooCommerce Multilingual support
Download this release

Release Info

Developer Mihail Barinov
Plugin Icon 128x128 Advanced Woo Search
Version 1.20
Comparing to
See all releases

Code changes from version 1.19 to 1.20

advanced-woo-search.php CHANGED
@@ -3,7 +3,7 @@
3
  /*
4
  Plugin Name: Advanced Woo Search
5
  Description: Advance ajax WooCommerce product search.
6
- Version: 1.19
7
  Author: ILLID
8
  Text Domain: aws
9
  */
@@ -13,7 +13,7 @@ if ( ! defined( 'ABSPATH' ) ) {
13
  exit;
14
  }
15
 
16
- define( 'AWS_VERSION', '1.19' );
17
 
18
 
19
  define( 'AWS_DIR', dirname( __FILE__ ) );
3
  /*
4
  Plugin Name: Advanced Woo Search
5
  Description: Advance ajax WooCommerce product search.
6
+ Version: 1.20
7
  Author: ILLID
8
  Text Domain: aws
9
  */
13
  exit;
14
  }
15
 
16
+ define( 'AWS_VERSION', '1.20' );
17
 
18
 
19
  define( 'AWS_DIR', dirname( __FILE__ ) );
includes/class-aws-search.php CHANGED
@@ -49,6 +49,24 @@ if ( ! class_exists( 'AWS_Search' ) ) :
49
 
50
  }
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  public function search( $keyword = '' ) {
53
 
54
  global $wpdb;
@@ -59,20 +77,18 @@ if ( ! class_exists( 'AWS_Search' ) ) :
59
  $s = stripslashes( $s );
60
  $s = str_replace( array( "\r", "\n" ), '', $s );
61
 
 
62
 
63
  if ( $cache === 'true' ) {
64
 
65
- $cache_option_name = 'aws_search_term_' . $s;
66
-
67
  // Check if value was already cached
68
- if ($cached_value = get_option($cache_option_name)) {
69
  $cached_value['cache'] = 'cached';
70
  return $cached_value;
71
  }
72
 
73
  }
74
 
75
-
76
  $show_cats = AWS()->get_settings( 'show_cats' );
77
  $show_tags = AWS()->get_settings( 'show_tags' );
78
  $results_num = $keyword ? 100 : AWS()->get_settings( 'results_num' );
@@ -160,6 +176,7 @@ if ( ! class_exists( 'AWS_Search' ) ) :
160
  $query['relevance'] = '';
161
  $query['stock'] = '';
162
  $query['visibility'] = '';
 
163
 
164
  $search_array = array();
165
  $source_array = array();
@@ -247,6 +264,16 @@ if ( ! class_exists( 'AWS_Search' ) ) :
247
  }
248
 
249
 
 
 
 
 
 
 
 
 
 
 
250
  $sql = "SELECT
251
  distinct ID,
252
  {$query['relevance']} as relevance
@@ -258,6 +285,7 @@ if ( ! class_exists( 'AWS_Search' ) ) :
258
  {$query['search']}
259
  {$query['stock']}
260
  {$query['visibility']}
 
261
  GROUP BY ID
262
  ORDER BY
263
  relevance DESC
@@ -271,8 +299,8 @@ if ( ! class_exists( 'AWS_Search' ) ) :
271
  }
272
 
273
  /*
274
- * Get array of included to search result posts ids
275
- */
276
  private function get_posts_ids( $sql ) {
277
 
278
  global $wpdb;
@@ -529,12 +557,19 @@ if ( ! class_exists( 'AWS_Search' ) ) :
529
 
530
  foreach ( $search_results as $result ) {
531
 
532
- $term = get_term( $result->term_id, $result->taxonomy );
 
 
 
 
 
 
 
533
 
534
  if ( $term != null && !is_wp_error( $term ) ) {
535
  $term_link = get_term_link( $term );
536
  } else {
537
- $term_link = '';
538
  }
539
 
540
  $new_result = array(
49
 
50
  }
51
 
52
+ /**
53
+ * Get caching option name
54
+ */
55
+ private function get_cache_name( $s ) {
56
+
57
+ $cache_option_name = 'aws_search_term_' . $s;
58
+
59
+ if ( has_filter('wpml_current_language') ) {
60
+ $current_lang = apply_filters('wpml_current_language', NULL);
61
+ if ( $current_lang ) {
62
+ $cache_option_name = $cache_option_name . '_' . $current_lang;
63
+ }
64
+ }
65
+
66
+ return $cache_option_name;
67
+
68
+ }
69
+
70
  public function search( $keyword = '' ) {
71
 
72
  global $wpdb;
77
  $s = stripslashes( $s );
78
  $s = str_replace( array( "\r", "\n" ), '', $s );
79
 
80
+ $cache_option_name = $this->get_cache_name( $s );
81
 
82
  if ( $cache === 'true' ) {
83
 
 
 
84
  // Check if value was already cached
85
+ if ( $cached_value = get_option( $cache_option_name ) ) {
86
  $cached_value['cache'] = 'cached';
87
  return $cached_value;
88
  }
89
 
90
  }
91
 
 
92
  $show_cats = AWS()->get_settings( 'show_cats' );
93
  $show_tags = AWS()->get_settings( 'show_tags' );
94
  $results_num = $keyword ? 100 : AWS()->get_settings( 'results_num' );
176
  $query['relevance'] = '';
177
  $query['stock'] = '';
178
  $query['visibility'] = '';
179
+ $query['lang'] = '';
180
 
181
  $search_array = array();
182
  $source_array = array();
264
  }
265
 
266
 
267
+ if ( $reindex_version && version_compare( $reindex_version, '1.20', '>=' ) ) {
268
+ if ( has_filter('wpml_current_language') ) {
269
+ $current_lang = apply_filters( 'wpml_current_language', NULL );
270
+ if ( $current_lang ) {
271
+ $query['lang'] .= $wpdb->prepare( " AND lang LIKE %s", $current_lang );
272
+ }
273
+ }
274
+ }
275
+
276
+
277
  $sql = "SELECT
278
  distinct ID,
279
  {$query['relevance']} as relevance
285
  {$query['search']}
286
  {$query['stock']}
287
  {$query['visibility']}
288
+ {$query['lang']}
289
  GROUP BY ID
290
  ORDER BY
291
  relevance DESC
299
  }
300
 
301
  /*
302
+ * Get array of included to search result posts ids
303
+ */
304
  private function get_posts_ids( $sql ) {
305
 
306
  global $wpdb;
557
 
558
  foreach ( $search_results as $result ) {
559
 
560
+ if ( function_exists( 'wpml_object_id_filter' ) ) {
561
+ $term = wpml_object_id_filter( $result->term_id, $result->taxonomy );
562
+ if ( $term != $result->term_id ) {
563
+ continue;
564
+ }
565
+ } else {
566
+ $term = get_term( $result->term_id, $result->taxonomy );
567
+ }
568
 
569
  if ( $term != null && !is_wp_error( $term ) ) {
570
  $term_link = get_term_link( $term );
571
  } else {
572
+ continue;
573
  }
574
 
575
  $new_result = array(
includes/class-aws-table.php CHANGED
@@ -87,27 +87,26 @@ if ( ! class_exists( 'AWS_Table' ) ) :
87
  'post_status' => 'publish',
88
  'offset' => $index_meta['offset'],
89
  'ignore_sticky_posts' => true,
 
 
90
  'orderby' => 'ID',
91
  'order' => 'DESC',
92
  );
93
 
94
- $query = new WP_Query( $args );
95
 
96
- $index_meta['found_posts'] = $query->found_posts;
 
 
97
 
98
  if ( $status !== 'start' ) {
99
 
100
- if ( $query->have_posts() ) {
101
  $queued_posts = array();
102
 
103
- while ( $query->have_posts() ) {
104
- $query->the_post();
105
-
106
- $queued_posts[] = absint( get_the_ID() );
107
-
108
  }
109
 
110
-
111
  $this->fill_table( $queued_posts );
112
 
113
  $index_meta['offset'] = absint( $index_meta['offset'] + $posts_per_page );
@@ -121,7 +120,7 @@ if ( ! class_exists( 'AWS_Table' ) ) :
121
  } else {
122
  // We are done (with this site)
123
 
124
- $index_meta['offset'] = (int) $query->found_posts;
125
 
126
  delete_option( 'aws_index_meta' );
127
 
@@ -168,7 +167,8 @@ if ( ! class_exists( 'AWS_Table' ) ) :
168
  type VARCHAR(50) NOT NULL DEFAULT 0,
169
  count BIGINT(20) UNSIGNED NOT NULL DEFAULT 0,
170
  in_stock INT(11) NOT NULL DEFAULT 0,
171
- visibility VARCHAR(20) NOT NULL DEFAULT 0
 
172
  ) $charset_collate;";
173
 
174
  require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
@@ -181,27 +181,27 @@ if ( ! class_exists( 'AWS_Table' ) ) :
181
  */
182
  private function fill_table( $posts ) {
183
 
184
- global $wpdb;
185
-
186
  foreach ( $posts as $found_post_id ) {
187
 
188
- $values = array();
189
 
190
- $terms = array();
191
- $id = $found_post_id;
192
 
193
- $product = wc_get_product( $id );
 
194
 
195
- $in_stock = ( $product->get_stock_status() === 'outofstock' ) ? 0 : 1;
196
- $visibility = $product->get_catalog_visibility() ? $product->get_catalog_visibility() : ( $product->get_visibility() ? $product->get_visibility() : 'visible' );
 
197
 
198
  $sku = $product->get_sku();
199
 
200
- $title = apply_filters( 'the_title', get_the_title( $id ) );
201
- $content = apply_filters( 'the_content', get_post_field( 'post_content', $id ) );
202
- $excerpt = get_post_field( 'post_excerpt', $id );
203
- $cat_names = $this->get_terms_names_list( $id, 'product_cat' );
204
- $tag_names = $this->get_terms_names_list( $id, 'product_tag' );
205
 
206
 
207
  // Get all child products if exists
@@ -234,7 +234,6 @@ if ( ! class_exists( 'AWS_Table' ) ) :
234
 
235
  }
236
 
237
-
238
  // WP 4.2 emoji strip
239
  if ( function_exists( 'wp_encode_emoji' ) ) {
240
  $content = wp_encode_emoji( $content );
@@ -243,47 +242,105 @@ if ( ! class_exists( 'AWS_Table' ) ) :
243
  $content = strip_shortcodes( $content );
244
 
245
 
246
- $terms['title'] = $this->extract_terms( $title );
247
- $terms['content'] = $this->extract_terms( $content );
248
- $terms['excerpt'] = $this->extract_terms( $excerpt );
249
- $terms['sku'] = $this->extract_terms( $sku );
250
- $terms['category'] = $this->extract_terms( $cat_names );
251
- $terms['tag'] = $this->extract_terms( $tag_names );
252
 
253
 
254
- foreach( $terms as $source => $all_terms ) {
 
255
 
256
- foreach ( $all_terms as $term => $count ) {
257
 
258
- if ( ! $term ) {
259
- continue;
260
- }
 
 
 
 
 
 
 
 
 
 
 
 
261
 
262
- $value = $wpdb->prepare(
263
- "(%d, %s, %s, %s, %d, %d, %s)",
264
- $id, $term, $source, 'product', $count, $in_stock, $visibility
265
- );
266
 
267
- $values[] = $value;
 
 
 
 
 
 
 
 
 
 
 
 
268
 
269
  }
270
 
271
  }
272
 
273
 
274
- if ( count( $values ) > 0 ) {
 
275
 
276
- $values = implode( ', ', $values );
277
 
278
- $query = "INSERT IGNORE INTO {$this->table_name}
279
- (`id`, `term`, `term_source`, `type`, `count`, `in_stock`, `visibility`)
280
- VALUES $values
281
- ";
282
 
283
- $wpdb->query( $query );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
284
 
285
  }
286
 
 
 
 
 
 
 
 
 
 
 
 
 
 
287
 
288
  }
289
 
@@ -309,12 +366,13 @@ if ( ! class_exists( 'AWS_Table' ) ) :
309
  $wpdb->delete( $this->table_name, array( 'id' => $post_id ) );
310
 
311
  $posts = get_posts( array(
312
- 'posts_per_page' => -1,
313
- 'fields' => 'ids',
314
- 'post_type' => 'product',
315
- 'post_status' => 'publish',
316
- 'no_found_rows' => 1,
317
- 'include' => $post_id
 
318
  ) );
319
 
320
  $this->fill_table( $posts );
@@ -348,12 +406,13 @@ if ( ! class_exists( 'AWS_Table' ) ) :
348
  $wpdb->delete( $this->table_name, array( 'id' => $product_id ) );
349
 
350
  $posts = get_posts( array(
351
- 'posts_per_page' => -1,
352
- 'fields' => 'ids',
353
- 'post_type' => 'product',
354
- 'post_status' => 'publish',
355
- 'no_found_rows' => 1,
356
- 'include' => $product_id
 
357
  ) );
358
 
359
  $this->fill_table( $posts );
87
  'post_status' => 'publish',
88
  'offset' => $index_meta['offset'],
89
  'ignore_sticky_posts' => true,
90
+ 'suppress_filters' => true,
91
+ 'no_found_rows' => 1,
92
  'orderby' => 'ID',
93
  'order' => 'DESC',
94
  );
95
 
 
96
 
97
+ $posts = get_posts( $args );
98
+
99
+ $index_meta['found_posts'] = count( $posts );
100
 
101
  if ( $status !== 'start' ) {
102
 
103
+ if ( $posts && count( $posts ) > 0 ) {
104
  $queued_posts = array();
105
 
106
+ foreach( $posts as $post_id ) {
107
+ $queued_posts[] = absint( $post_id );
 
 
 
108
  }
109
 
 
110
  $this->fill_table( $queued_posts );
111
 
112
  $index_meta['offset'] = absint( $index_meta['offset'] + $posts_per_page );
120
  } else {
121
  // We are done (with this site)
122
 
123
+ $index_meta['offset'] = (int) count( $posts );
124
 
125
  delete_option( 'aws_index_meta' );
126
 
167
  type VARCHAR(50) NOT NULL DEFAULT 0,
168
  count BIGINT(20) UNSIGNED NOT NULL DEFAULT 0,
169
  in_stock INT(11) NOT NULL DEFAULT 0,
170
+ visibility VARCHAR(20) NOT NULL DEFAULT 0,
171
+ lang VARCHAR(20) NOT NULL DEFAULT 0
172
  ) $charset_collate;";
173
 
174
  require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
181
  */
182
  private function fill_table( $posts ) {
183
 
 
 
184
  foreach ( $posts as $found_post_id ) {
185
 
186
+ $data = array();
187
 
188
+ $data['terms'] = array();
189
+ $data['id'] = $found_post_id;
190
 
191
+ $product = wc_get_product( $data['id'] );
192
+ $lang = has_filter( 'wpml_post_language_details' ) ? apply_filters( 'wpml_post_language_details', NULL, $data['id'] ) : '';
193
 
194
+ $data['in_stock'] = ( $product->get_stock_status() === 'outofstock' ) ? 0 : 1;
195
+ $data['visibility'] = $product->get_catalog_visibility() ? $product->get_catalog_visibility() : ( $product->get_visibility() ? $product->get_visibility() : 'visible' );
196
+ $data['lang'] = $lang ? $lang['language_code'] : '';
197
 
198
  $sku = $product->get_sku();
199
 
200
+ $title = apply_filters( 'the_title', get_the_title( $data['id'] ) );
201
+ $content = apply_filters( 'the_content', get_post_field( 'post_content', $data['id'] ) );
202
+ $excerpt = get_post_field( 'post_excerpt', $data['id'] );
203
+ $cat_names = $this->get_terms_names_list( $data['id'], 'product_cat' );
204
+ $tag_names = $this->get_terms_names_list( $data['id'], 'product_tag' );
205
 
206
 
207
  // Get all child products if exists
234
 
235
  }
236
 
 
237
  // WP 4.2 emoji strip
238
  if ( function_exists( 'wp_encode_emoji' ) ) {
239
  $content = wp_encode_emoji( $content );
242
  $content = strip_shortcodes( $content );
243
 
244
 
245
+ $data['terms']['title'] = $this->extract_terms( $title );
246
+ $data['terms']['content'] = $this->extract_terms( $content );
247
+ $data['terms']['excerpt'] = $this->extract_terms( $excerpt );
248
+ $data['terms']['sku'] = $this->extract_terms( $sku );
249
+ $data['terms']['category'] = $this->extract_terms( $cat_names );
250
+ $data['terms']['tag'] = $this->extract_terms( $tag_names );
251
 
252
 
253
+ // Get translations if exists
254
+ if ( has_filter('wpml_element_has_translations') && has_filter('wpml_get_element_translations') ) {
255
 
256
+ $is_translated = apply_filters( 'wpml_element_has_translations', NULL, $data['id'], 'post_product' );
257
 
258
+ if ( $is_translated ) {
259
+
260
+ $translations = apply_filters( 'wpml_get_element_translations', NULL, $data['id'], 'post_product');
261
+
262
+ foreach( $translations as $language => $lang_obj ) {
263
+ if ( ! $lang_obj->original && $lang_obj->post_status === 'publish' ) {
264
+ $translated_post = get_post( $lang_obj->element_id );
265
+ if ( $translated_post && !empty( $translated_post ) ) {
266
+
267
+ $translated_post_data = array();
268
+ $translated_post_data['id'] = $translated_post->ID;
269
+ $translated_post_data['in_stock'] = $data['in_stock'];
270
+ $translated_post_data['visibility'] = $data['visibility'];
271
+ $translated_post_data['lang'] = $lang_obj->language_code;
272
+ $translated_post_data['terms'] = array();
273
 
274
+ $translated_title = apply_filters( 'the_title', get_the_title( $translated_post->ID ) );
275
+ $translated_content = apply_filters( 'the_content', get_post_field( 'post_content', $translated_post->ID ) );
276
+ $translated_excerpt = get_post_field( 'post_excerpt', $translated_post->ID );
 
277
 
278
+
279
+ $translated_post_data['terms']['title'] = $this->extract_terms( $translated_title );
280
+ $translated_post_data['terms']['content'] = $this->extract_terms( $translated_content );
281
+ $translated_post_data['terms']['excerpt'] = $this->extract_terms( $translated_excerpt );
282
+ $translated_post_data['terms']['sku'] = $this->extract_terms( $sku );
283
+
284
+
285
+ //Insert translated product data into table
286
+ $this->insert_into_table( $translated_post_data );
287
+
288
+ }
289
+ }
290
+ }
291
 
292
  }
293
 
294
  }
295
 
296
 
297
+ //Insert data into table
298
+ $this->insert_into_table( $data );
299
 
 
300
 
301
+ }
 
 
 
302
 
303
+ }
304
+
305
+ /*
306
+ * Scrap all product data and insert to table
307
+ */
308
+ private function insert_into_table( $data ) {
309
+
310
+ global $wpdb;
311
+
312
+ $values = array();
313
+
314
+ foreach( $data['terms'] as $source => $all_terms ) {
315
+
316
+ foreach ( $all_terms as $term => $count ) {
317
+
318
+ if ( ! $term ) {
319
+ continue;
320
+ }
321
+
322
+ $value = $wpdb->prepare(
323
+ "(%d, %s, %s, %s, %d, %d, %s, %s)",
324
+ $data['id'], $term, $source, 'product', $count, $data['in_stock'], $data['visibility'], $data['lang']
325
+ );
326
+
327
+ $values[] = $value;
328
 
329
  }
330
 
331
+ }
332
+
333
+
334
+ if ( count( $values ) > 0 ) {
335
+
336
+ $values = implode( ', ', $values );
337
+
338
+ $query = "INSERT IGNORE INTO {$this->table_name}
339
+ (`id`, `term`, `term_source`, `type`, `count`, `in_stock`, `visibility`, `lang`)
340
+ VALUES $values
341
+ ";
342
+
343
+ $wpdb->query( $query );
344
 
345
  }
346
 
366
  $wpdb->delete( $this->table_name, array( 'id' => $post_id ) );
367
 
368
  $posts = get_posts( array(
369
+ 'posts_per_page' => -1,
370
+ 'fields' => 'ids',
371
+ 'post_type' => 'product',
372
+ 'post_status' => 'publish',
373
+ 'suppress_filters' => false,
374
+ 'no_found_rows' => 1,
375
+ 'include' => $post_id
376
  ) );
377
 
378
  $this->fill_table( $posts );
406
  $wpdb->delete( $this->table_name, array( 'id' => $product_id ) );
407
 
408
  $posts = get_posts( array(
409
+ 'posts_per_page' => -1,
410
+ 'fields' => 'ids',
411
+ 'post_type' => 'product',
412
+ 'post_status' => 'publish',
413
+ 'suppress_filters' => false,
414
+ 'no_found_rows' => 1,
415
+ 'include' => $product_id
416
  ) );
417
 
418
  $this->fill_table( $posts );
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_i
4
  Tags: widget, plugin, woocommerce, search, product search, woocommerce search, ajax search, live search, custom search, ajax, shortcode, better search, relevance search, relevant search, search by sku, search plugin, shop, store, wordpress search, wp ajax search, wp search, wp search plugin, sidebar, ecommerce, merketing, products, category search, instant-search, search highlight, woocommerce advanced search, woocommerce live search, WooCommerce Plugin, woocommerce product search
5
  Requires at least: 4.0
6
  Tested up to: 4.8
7
- Stable tag: 1.19
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -25,6 +25,7 @@ Advanced Woo Search - powerful live search plugin for WooCommerce. Just start ty
25
  * **Terms search** - Search for product categories and tags
26
  * **Smart ordering** - Search results ordered by the priority of source where they were found
27
  * **Fast** - Nothing extra. Just what you need for proper work
 
28
  * Supports variable products
29
  * Google Analytics support
30
 
@@ -73,6 +74,9 @@ Yep. This plugin is always compatible with the latest version of Woocommerce?
73
 
74
  == Changelog ==
75
 
 
 
 
76
  = 1.19 =
77
  * Fix bugs
78
 
4
  Tags: widget, plugin, woocommerce, search, product search, woocommerce search, ajax search, live search, custom search, ajax, shortcode, better search, relevance search, relevant search, search by sku, search plugin, shop, store, wordpress search, wp ajax search, wp search, wp search plugin, sidebar, ecommerce, merketing, products, category search, instant-search, search highlight, woocommerce advanced search, woocommerce live search, WooCommerce Plugin, woocommerce product search
5
  Requires at least: 4.0
6
  Tested up to: 4.8
7
+ Stable tag: 1.20
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
25
  * **Terms search** - Search for product categories and tags
26
  * **Smart ordering** - Search results ordered by the priority of source where they were found
27
  * **Fast** - Nothing extra. Just what you need for proper work
28
+ * **WPML**, **WooCommerce Multilingual** support
29
  * Supports variable products
30
  * Google Analytics support
31
 
74
 
75
  == Changelog ==
76
 
77
+ = 1.20 =
78
+ * Add WPML, WooCommerce Multilingual support
79
+
80
  = 1.19 =
81
  * Fix bugs
82