Advanced Woo Search - Version 1.05

Version Description

  • Improve search speed
Download this release

Release Info

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

Code changes from version 1.04 to 1.05

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.04
7
  Author: ILLID
8
  Text Domain: aws
9
  */
@@ -13,6 +13,8 @@ if ( ! defined( 'ABSPATH' ) ) {
13
  exit;
14
  }
15
 
 
 
16
 
17
  define( 'AWS_DIR', dirname( __FILE__ ) );
18
  define( 'AWS_URL', plugins_url( '', __FILE__ ) );
@@ -68,7 +70,7 @@ final class AWS_Main {
68
  add_action( 'wp_ajax_aws_action', array( $this, 'action_callback' ) );
69
  add_action('wp_ajax_nopriv_aws_action', array( $this, 'action_callback' ) );
70
 
71
- add_filter( 'plugin_action_links', array( $this, 'add_settings_link' ), 10, 2 );
72
 
73
  //load_plugin_textdomain( 'aws', false, dirname( plugin_basename( __FILE__ ) ). '/languages/' );
74
 
@@ -81,6 +83,7 @@ final class AWS_Main {
81
  */
82
  public function includes() {
83
  include_once( 'includes/class-aws-admin.php' );
 
84
  include_once( 'includes/widget.php' );
85
  }
86
 
@@ -123,8 +126,8 @@ final class AWS_Main {
123
  * Load assets for search form
124
  */
125
  public function load_scripts() {
126
- wp_enqueue_style( 'aws-style', AWS_URL . '/assets/css/common.css' );
127
- wp_enqueue_script( 'aws-script', AWS_URL . '/assets/js/common.js', array('jquery'), '1.0', true );
128
  }
129
 
130
  /*
@@ -163,7 +166,6 @@ final class AWS_Main {
163
 
164
  $show_cats = $this->get_settings( 'show_cats' );
165
  $show_tags = $this->get_settings( 'show_tags' );
166
- $exact_match = $this->get_settings( 'exact_match' );
167
  $results_num = $this->get_settings( 'results_num' );
168
  $search_in = $this->get_settings( 'search_in' );
169
 
@@ -176,150 +178,142 @@ final class AWS_Main {
176
 
177
  $categories_array = array();
178
  $tags_array = array();
179
- $query = array();
180
-
181
 
182
  $s = esc_attr( $_POST['keyword'] );
183
  $s = stripslashes( $s );
184
  $s = str_replace( array( "\r", "\n" ), '', $s );
185
 
186
  $this->data['s'] = $s;
 
187
  $this->data['search_terms'] = array();
188
  $this->data['search_in'] = $search_in_arr;
189
 
190
- if ( $exact_match === 'true' ) {
191
- $this->data['search_terms'] = array( $s );
192
- } else {
193
- $this->data['search_terms'] = array_unique( explode( ' ', $s ) );
194
 
195
- if ( count( $this->data['search_terms'] ) > 0 ) {
196
- if ( count( $this->data['search_terms'] ) > 9 ) {
197
- $this->data['search_terms'] = array( $s );
198
- }
199
- } else {
200
- $this->data['search_terms'] = array( $s );
201
- }
202
 
203
- }
 
 
204
 
205
 
206
- // Generate search query
 
207
 
208
- $query['search'] = '';
209
- $query['relevance'] = '';
210
- $query['join'] = '';
211
 
212
- $temp_search_array = array();
213
- $relevance_array = array();
214
- $new_relevance_array = array();
215
 
216
- foreach ( $this->data['search_terms'] as $search_term ) {
 
 
217
 
218
- $like = '%' . $wpdb->esc_like( $search_term ) . '%';
 
 
 
 
219
 
220
- $search_in_array = array();
221
 
222
- foreach ( $search_in_arr as $search_in_term ) {
223
 
224
- switch ( $search_in_term ) {
 
 
 
225
 
226
- case 'title':
227
- $search_in_array[] = $wpdb->prepare( '( posts.post_title LIKE %s )', $like );
228
- $relevance_array['title'][] = $wpdb->prepare( "( case when ( post_title LIKE %s ) then 10 else 0 end )", $like );
229
- break;
230
 
231
- case 'content':
232
- $search_in_array[] = $wpdb->prepare( '( posts.post_content LIKE %s )', $like );
233
- $relevance_array['content'][] = $wpdb->prepare( "( case when ( post_content LIKE %s ) then 7 else 0 end )", $like );
234
- break;
235
 
236
- case 'excerpt':
237
- $search_in_array[] = $wpdb->prepare( '( posts.post_excerpt LIKE %s )', $like );
238
- $relevance_array['content'][] = $wpdb->prepare( "( case when ( post_excerpt LIKE %s ) then 7 else 0 end )", $like );
239
- break;
240
 
241
- case 'category':
242
- $search_in_array[] = $wpdb->prepare( "( $wpdb->terms.name LIKE %s AND $wpdb->term_taxonomy.taxonomy = 'product_cat' )", $like );
243
- $relevance_array['category'][] = $wpdb->prepare( "( case when ( $wpdb->terms.name LIKE %s AND $wpdb->term_taxonomy.taxonomy = 'product_cat' ) then 5 else 0 end )", $like );
244
 
245
- $query['join'] = "LEFT JOIN $wpdb->term_relationships ON posts.ID = $wpdb->term_relationships.object_id
246
- LEFT JOIN $wpdb->term_taxonomy ON $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id
247
- LEFT JOIN $wpdb->terms ON $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id";
248
- break;
249
 
250
- case 'tag':
251
- $search_in_array[] = $wpdb->prepare( "( $wpdb->terms.name LIKE %s AND $wpdb->term_taxonomy.taxonomy = 'product_tag' )", $like );
252
- $relevance_array['tag'][] = $wpdb->prepare( "( case when ( $wpdb->terms.name LIKE %s AND $wpdb->term_taxonomy.taxonomy = 'product_tag' ) then 5 else 0 end )", $like );
 
253
 
254
- $query['join'] = "LEFT JOIN $wpdb->term_relationships ON posts.ID = $wpdb->term_relationships.object_id
255
- LEFT JOIN $wpdb->term_taxonomy ON $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id
256
- LEFT JOIN $wpdb->terms ON $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id";
257
- break;
258
 
259
- case 'sku':
260
- $search_in_array[] = $wpdb->prepare( "( posts.ID = postmeta.post_id AND postmeta.meta_key = '_sku' AND postmeta.meta_value LIKE %s )", $like );
261
- break;
262
 
263
- }
264
 
265
- }
266
 
267
- $temp_search_array[] = sprintf( ' ( %s ) ', implode( ' OR ', $search_in_array ) );
268
 
269
- }
270
 
271
- $query['search'] .= sprintf( ' AND ( %s )', implode( ' OR ', $temp_search_array ) );
 
 
272
 
273
- // Sort 'relevance' queries in the array by search priority
274
- foreach ( $search_in_arr as $search_in_item ) {
275
- if ( isset( $relevance_array[$search_in_item] ) ) {
276
- $new_relevance_array[$search_in_item] = implode( ' + ', $relevance_array[$search_in_item] );
277
- }
278
- }
279
 
280
- $query['relevance'] .= sprintf( ' ( %s ) ', implode( ' + ', $new_relevance_array ) );
 
 
281
 
 
 
 
282
 
283
- $sql = "SELECT
284
- distinct ID,
285
- {$query['relevance']} as relevance
286
- FROM
287
- $wpdb->postmeta AS postmeta,
288
- $wpdb->posts AS posts
289
- {$query['join']}
290
- WHERE
291
- posts.post_type = 'product'
292
- AND posts.post_status = 'publish'
293
- {$query['search']}
294
- ORDER BY
295
- relevance DESC,
296
- posts.post_date DESC
297
- LIMIT 0, {$results_num}
298
- ";
299
 
300
- $posts_ids = $this->get_posts_ids( $sql );
301
 
302
- $products_array = $this->get_products( $posts_ids );
303
 
 
304
 
305
- if ( $show_cats === 'true' ) {
306
- $categories_array = $this->get_taxonomies( $this->data['s'], 'product_cat' );
 
 
 
307
  }
308
 
309
- if ( $show_tags === 'true' ) {
310
- $tags_array = $this->get_taxonomies( $this->data['s'], 'product_tag' );
311
  }
312
 
313
- echo json_encode( array(
314
- //'sql' => $wpdb->get_results( $sql ),
315
- 'cats' => $categories_array,
316
- 'tags' => $tags_array,
317
- 'products' => $products_array
318
- ) );
319
 
320
- die;
321
 
322
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
323
 
324
  /*
325
  * Get products info
@@ -489,12 +483,22 @@ final class AWS_Main {
489
  /*
490
  * Query product taxonomies
491
  */
492
- private function get_taxonomies( $s, $taxonomy ) {
493
 
494
  global $wpdb;
495
 
496
  $result_array = array();
 
497
  $excludes = '';
 
 
 
 
 
 
 
 
 
498
 
499
  $sql = "
500
  SELECT
@@ -505,8 +509,8 @@ final class AWS_Main {
505
  FROM
506
  $wpdb->terms
507
  , $wpdb->term_taxonomy
508
- WHERE
509
- name LIKE '%{$s}%'
510
  AND $wpdb->term_taxonomy.taxonomy = '{$taxonomy}'
511
  AND $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id
512
  $excludes
@@ -518,13 +522,13 @@ final class AWS_Main {
518
 
519
  foreach ( $search_results as $result ) {
520
 
521
- $term = get_term( $result->term_id, $result->taxonomy );
522
 
523
- if ( $term != null && !is_wp_error( $term ) ) {
524
- $term_link = get_term_link( $term );
525
- } else {
526
- $term_link = '';
527
- }
528
 
529
  $new_result = array(
530
  'name' => $result->name,
@@ -553,12 +557,15 @@ final class AWS_Main {
553
  /*
554
  * Add settings link to plugins
555
  */
556
- public function add_settings_link( $links, $file ) {
557
  $plugin_base = plugin_basename( __FILE__ );
558
 
559
  if ( $file == $plugin_base ) {
560
  $setting_link = '<a href="' . admin_url('admin.php?page=aws-options') . '">'.__( 'Settings', 'aws' ).'</a>';
561
  array_unshift( $links, $setting_link );
 
 
 
562
  }
563
 
564
  return $links;
3
  /*
4
  Plugin Name: Advanced Woo Search
5
  Description: Advance ajax WooCommerce product search.
6
+ Version: 1.05
7
  Author: ILLID
8
  Text Domain: aws
9
  */
13
  exit;
14
  }
15
 
16
+ define( 'AWS_VERSION', '1.05' );
17
+
18
 
19
  define( 'AWS_DIR', dirname( __FILE__ ) );
20
  define( 'AWS_URL', plugins_url( '', __FILE__ ) );
70
  add_action( 'wp_ajax_aws_action', array( $this, 'action_callback' ) );
71
  add_action('wp_ajax_nopriv_aws_action', array( $this, 'action_callback' ) );
72
 
73
+ add_filter( 'plugin_action_links', array( $this, 'add_action_link' ), 10, 2 );
74
 
75
  //load_plugin_textdomain( 'aws', false, dirname( plugin_basename( __FILE__ ) ). '/languages/' );
76
 
83
  */
84
  public function includes() {
85
  include_once( 'includes/class-aws-admin.php' );
86
+ include_once( 'includes/class-aws-table.php' );
87
  include_once( 'includes/widget.php' );
88
  }
89
 
126
  * Load assets for search form
127
  */
128
  public function load_scripts() {
129
+ wp_enqueue_style( 'aws-style', AWS_URL . '/assets/css/common.css', array(), AWS_VERSION );
130
+ wp_enqueue_script( 'aws-script', AWS_URL . '/assets/js/common.js', array('jquery'), AWS_VERSION, true );
131
  }
132
 
133
  /*
166
 
167
  $show_cats = $this->get_settings( 'show_cats' );
168
  $show_tags = $this->get_settings( 'show_tags' );
 
169
  $results_num = $this->get_settings( 'results_num' );
170
  $search_in = $this->get_settings( 'search_in' );
171
 
178
 
179
  $categories_array = array();
180
  $tags_array = array();
 
 
181
 
182
  $s = esc_attr( $_POST['keyword'] );
183
  $s = stripslashes( $s );
184
  $s = str_replace( array( "\r", "\n" ), '', $s );
185
 
186
  $this->data['s'] = $s;
187
+ $this->data['results_num'] = $results_num;
188
  $this->data['search_terms'] = array();
189
  $this->data['search_in'] = $search_in_arr;
190
 
 
 
 
 
191
 
192
+ $this->data['search_terms'] = array_unique( explode( ' ', $s ) );
 
 
 
 
 
 
193
 
194
+ if ( ! count( $this->data['search_terms'] ) > 0 ) {
195
+ $this->data['search_terms'] = array( $s );
196
+ }
197
 
198
 
199
+ $posts_ids = $this->query_index_table();
200
+ $products_array = $this->get_products( $posts_ids );
201
 
 
 
 
202
 
203
+ if ( $show_cats === 'true' ) {
204
+ $categories_array = $this->get_taxonomies( 'product_cat' );
205
+ }
206
 
207
+ if ( $show_tags === 'true' ) {
208
+ $tags_array = $this->get_taxonomies( 'product_tag' );
209
+ }
210
 
211
+ echo json_encode( array(
212
+ 'cats' => $categories_array,
213
+ 'tags' => $tags_array,
214
+ 'products' => $products_array
215
+ ) );
216
 
217
+ die;
218
 
219
+ }
220
 
221
+ /*
222
+ * Query in index table
223
+ */
224
+ private function query_index_table() {
225
 
226
+ global $wpdb;
 
 
 
227
 
228
+ $table_name = $wpdb->prefix . 'aws_index';
 
 
 
229
 
230
+ $search_in_arr = $this->data['search_in'];
231
+ $results_num = $this->data['results_num'];
 
 
232
 
233
+ $query = array();
 
 
234
 
235
+ $query['search'] = '';
236
+ $query['source'] = '';
237
+ $query['relevance'] = '';
 
238
 
239
+ $search_array = array();
240
+ $source_array = array();
241
+ $relevance_array = array();
242
+ $new_relevance_array = array();
243
 
 
 
 
 
244
 
245
+ foreach ( $this->data['search_terms'] as $search_term ) {
 
 
246
 
247
+ $like = '%' . $wpdb->esc_like( $search_term ) . '%';
248
 
249
+ $search_array[] = $wpdb->prepare( '( term LIKE %s )', $like );
250
 
251
+ foreach ( $search_in_arr as $search_in_term ) {
252
 
253
+ switch ( $search_in_term ) {
254
 
255
+ case 'title':
256
+ $relevance_array['title'][] = $wpdb->prepare( "( case when ( term_source = 'title' AND term LIKE %s ) then 10 else 0 end )", $like );
257
+ break;
258
 
259
+ case 'content':
260
+ $relevance_array['content'][] = $wpdb->prepare( "( case when ( term_source = 'content' AND term LIKE %s ) then 7 else 0 end )", $like );
261
+ break;
 
 
 
262
 
263
+ case 'excerpt':
264
+ $relevance_array['content'][] = $wpdb->prepare( "( case when ( term_source = 'excerpt' AND term LIKE %s ) then 7 else 0 end )", $like );
265
+ break;
266
 
267
+ case 'category':
268
+ $relevance_array['category'][] = $wpdb->prepare( "( case when ( term_source = 'category' AND term LIKE %s ) then 5 else 0 end )", $like );
269
+ break;
270
 
271
+ case 'tag':
272
+ $relevance_array['tag'][] = $wpdb->prepare( "( case when ( term_source = 'tag' AND term LIKE %s ) then 5 else 0 end )", $like );
273
+ break;
 
 
 
 
 
 
 
 
 
 
 
 
 
274
 
275
+ }
276
 
277
+ }
278
 
279
+ }
280
 
281
+ // Sort 'relevance' queries in the array by search priority
282
+ foreach ( $search_in_arr as $search_in_item ) {
283
+ if ( isset( $relevance_array[$search_in_item] ) ) {
284
+ $new_relevance_array[$search_in_item] = implode( ' + ', $relevance_array[$search_in_item] );
285
+ }
286
  }
287
 
288
+ foreach ( $search_in_arr as $search_in_term ) {
289
+ $source_array[] = "term_source = '{$search_in_term}'";
290
  }
291
 
292
+ $query['relevance'] .= sprintf( ' (SUM( %s )) ', implode( ' + ', $new_relevance_array ) );
293
+ $query['search'] .= sprintf( ' AND ( %s )', implode( ' OR ', $search_array ) );
294
+ $query['source'] .= sprintf( ' AND ( %s )', implode( ' OR ', $source_array ) );
 
 
 
295
 
 
296
 
297
+ $sql = "SELECT
298
+ distinct ID,
299
+ {$query['relevance']} as relevance
300
+ FROM
301
+ {$table_name}
302
+ WHERE
303
+ type = 'product'
304
+ {$query['source']}
305
+ {$query['search']}
306
+ GROUP BY ID
307
+ ORDER BY
308
+ relevance DESC
309
+ LIMIT 0, {$results_num}
310
+ ";
311
+
312
+ $posts_ids = $this->get_posts_ids( $sql );
313
+
314
+ return $posts_ids;
315
+
316
+ }
317
 
318
  /*
319
  * Get products info
483
  /*
484
  * Query product taxonomies
485
  */
486
+ private function get_taxonomies( $taxonomy ) {
487
 
488
  global $wpdb;
489
 
490
  $result_array = array();
491
+ $search_array = array();
492
  $excludes = '';
493
+ $search_query = '';
494
+
495
+ foreach ( $this->data['search_terms'] as $search_term ) {
496
+ $like = '%' . $wpdb->esc_like($search_term) . '%';
497
+ $search_array[] = $wpdb->prepare('( name LIKE %s )', $like);
498
+ }
499
+
500
+ $search_query .= sprintf( ' AND ( %s )', implode( ' OR ', $search_array ) );
501
+
502
 
503
  $sql = "
504
  SELECT
509
  FROM
510
  $wpdb->terms
511
  , $wpdb->term_taxonomy
512
+ WHERE 1 = 1
513
+ {$search_query}
514
  AND $wpdb->term_taxonomy.taxonomy = '{$taxonomy}'
515
  AND $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id
516
  $excludes
522
 
523
  foreach ( $search_results as $result ) {
524
 
525
+ $term = get_term( $result->term_id, $result->taxonomy );
526
 
527
+ if ( $term != null && !is_wp_error( $term ) ) {
528
+ $term_link = get_term_link( $term );
529
+ } else {
530
+ $term_link = '';
531
+ }
532
 
533
  $new_result = array(
534
  'name' => $result->name,
557
  /*
558
  * Add settings link to plugins
559
  */
560
+ public function add_action_link( $links, $file ) {
561
  $plugin_base = plugin_basename( __FILE__ );
562
 
563
  if ( $file == $plugin_base ) {
564
  $setting_link = '<a href="' . admin_url('admin.php?page=aws-options') . '">'.__( 'Settings', 'aws' ).'</a>';
565
  array_unshift( $links, $setting_link );
566
+
567
+ $premium_link = '<a href="https://advanced-woo-search.com/" target="_blank">'.__( 'Get Premium', 'aws' ).'</a>';
568
+ array_unshift( $links, $premium_link );
569
  }
570
 
571
  return $links;
assets/css/admin.css CHANGED
@@ -42,4 +42,42 @@
42
  height: 36px;
43
  padding: 0;
44
  border: 1px dotted #ccc;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  }
42
  height: 36px;
43
  padding: 0;
44
  border: 1px dotted #ccc;
45
+ }
46
+
47
+ .nav-tab.premium-tab {
48
+ background: #FFF7C7;
49
+ }
50
+
51
+ .nav-tab.premium-tab:hover {
52
+ background: #fff;
53
+ }
54
+
55
+ /* Reindex button */
56
+ #aws-reindex {
57
+ display: inline-block;
58
+ }
59
+
60
+ #aws-reindex .loader {
61
+ display: none;
62
+ width: 20px;
63
+ height: 20px;
64
+ background: url('../img/loader-1.GIF') no-repeat 50% 50%;
65
+ margin-left: 15px;
66
+ margin-top: 4px;
67
+ }
68
+
69
+ #aws-reindex.loading .loader {
70
+ display: inline-block;
71
+ }
72
+
73
+ /* Premium tab */
74
+ #aws_form .b-compare .column {
75
+ width: 50%;
76
+ float: left;
77
+ }
78
+
79
+ #aws_form .b-compare .column p {
80
+ font-size: 14px;
81
+ border-bottom: 1px solid #ccc;
82
+ padding-bottom: 4px;
83
  }
assets/js/admin.js ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ jQuery(document).ready(function ($) {
2
+ 'use strict';
3
+
4
+
5
+ // Reindex table
6
+ $('#aws-reindex .button').on( 'click', function(e) {
7
+
8
+ e.preventDefault();
9
+
10
+ var $reindexBlock = $(this).closest('#aws-reindex');
11
+
12
+ $reindexBlock.addClass('loading');
13
+
14
+ $.ajax({
15
+ type: 'POST',
16
+ url: aws_vars.ajaxurl,
17
+ data: {
18
+ action: 'aws-reindex'
19
+ },
20
+ dataType: "json",
21
+ success: function (data) {
22
+ console.log('Reindex complete!');
23
+ $reindexBlock.removeClass('loading');
24
+ }
25
+ });
26
+
27
+ });
28
+
29
+
30
+ });
includes/class-aws-admin.php CHANGED
@@ -58,8 +58,11 @@ class AWS_Admin {
58
 
59
  foreach ( $tabs as $name => $label ) {
60
  $tabs_html .= '<a href="' . admin_url( 'admin.php?page=aws-options&tab=' . $name ) . '" class="nav-tab ' . ( $current_tab == $name ? 'nav-tab-active' : '' ) . '">' . $label . '</a>';
 
61
  }
62
 
 
 
63
  $tabs_html = '<h2 class="nav-tab-wrapper woo-nav-tab-wrapper">'.$tabs_html.'</h2>';
64
 
65
  if( isset( $_POST["Submit"] ) ) {
@@ -114,6 +117,7 @@ class AWS_Admin {
114
  $this->generate_options( $options['view'] );
115
  break;
116
  default:
 
117
  $this->generate_options( $options['general'] );
118
  }
119
 
@@ -333,6 +337,23 @@ class AWS_Admin {
333
 
334
  }
335
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
336
  /*
337
  * Options array that generate settings page
338
  */
@@ -389,9 +410,11 @@ class AWS_Admin {
389
  public function admin_enqueue_scripts() {
390
 
391
  if ( isset( $_GET['page'] ) && $_GET['page'] == 'aws-options' ) {
392
- wp_enqueue_style( 'plugin-admin-style', AWS_URL . '/assets/css/admin.css' );
393
  wp_enqueue_script( 'jquery' );
394
  wp_enqueue_script( 'jquery-ui-sortable' );
 
 
395
  }
396
 
397
  }
58
 
59
  foreach ( $tabs as $name => $label ) {
60
  $tabs_html .= '<a href="' . admin_url( 'admin.php?page=aws-options&tab=' . $name ) . '" class="nav-tab ' . ( $current_tab == $name ? 'nav-tab-active' : '' ) . '">' . $label . '</a>';
61
+
62
  }
63
 
64
+ $tabs_html .= '<a href="https://advanced-woo-search.com/" class="nav-tab premium-tab" target="_blank">Get Premium</a>';
65
+
66
  $tabs_html = '<h2 class="nav-tab-wrapper woo-nav-tab-wrapper">'.$tabs_html.'</h2>';
67
 
68
  if( isset( $_POST["Submit"] ) ) {
117
  $this->generate_options( $options['view'] );
118
  break;
119
  default:
120
+ $this->update_table();
121
  $this->generate_options( $options['general'] );
122
  }
123
 
337
 
338
  }
339
 
340
+ /*
341
+ * Reindex table
342
+ */
343
+ private function update_table() {
344
+
345
+ echo '<tr>';
346
+
347
+ echo '<th>Reindex table</th>';
348
+ echo '<td>';
349
+ echo '<div id="aws-reindex"><input class="button" type="button" value="Reindex table"><span class="loader"></span></div><br><br>';
350
+ echo '<span class="description">Update all data in plugins index table. Index table - table with products data where plugin is searching all typed terms.<br>Use this button if you think that plugin not shows last actual data in its search results.<br><strong>CAUTION:</strong> this can take large amount of time.</span>';
351
+ echo '</td>';
352
+
353
+ echo '<tr>';
354
+
355
+ }
356
+
357
  /*
358
  * Options array that generate settings page
359
  */
410
  public function admin_enqueue_scripts() {
411
 
412
  if ( isset( $_GET['page'] ) && $_GET['page'] == 'aws-options' ) {
413
+ wp_enqueue_style( 'plugin-admin-style', AWS_URL . '/assets/css/admin.css', array(), AWS_VERSION );
414
  wp_enqueue_script( 'jquery' );
415
  wp_enqueue_script( 'jquery-ui-sortable' );
416
+ wp_enqueue_script( 'plugin-admin-scripts', AWS_URL . '/assets/js/admin.js', array('jquery'), AWS_VERSION );
417
+ wp_localize_script( 'plugin-admin-scripts', 'aws_vars', array( 'ajaxurl' => admin_url('admin-ajax.php' ) ) );
418
  }
419
 
420
  }
includes/class-aws-table.php ADDED
@@ -0,0 +1,338 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ if ( ! defined( 'ABSPATH' ) ) {
4
+ exit;
5
+ }
6
+
7
+ if ( ! class_exists( 'AWS_Table' ) ) :
8
+
9
+ /**
10
+ * Class for plugin index table
11
+ */
12
+ class AWS_Table {
13
+
14
+ /**
15
+ * @var AWS_Search ID of current filter $filter_id
16
+ */
17
+ private $table_name;
18
+
19
+ /**
20
+ * Constructor
21
+ */
22
+ public function __construct() {
23
+
24
+ global $wpdb;
25
+
26
+ $this->table_name = $wpdb->prefix . 'aws_index';
27
+
28
+ add_action( 'wp_loaded', array( $this, 'check_table' ) );
29
+
30
+ add_action( 'save_post', array( $this, 'update_table' ), 10, 3 );
31
+
32
+ add_action( 'wp_ajax_aws-reindex', array( $this, 'reindex_table' ) );
33
+ add_action( 'wp_ajax_nopriv_aws-reindex', array( $this, 'reindex_table' ) );
34
+
35
+ }
36
+
37
+ /*
38
+ * Reindex plugin table
39
+ */
40
+ public function reindex_table() {
41
+
42
+ global $wpdb;
43
+
44
+ $wpdb->query("DROP TABLE IF EXISTS {$this->table_name}");
45
+
46
+ $this->check_table();
47
+
48
+ }
49
+
50
+ /*
51
+ * Generate table for search terms
52
+ */
53
+ public function check_table() {
54
+
55
+ global $wpdb;
56
+
57
+ if ( $wpdb->get_var( "SHOW TABLES LIKE '{$this->table_name}'" ) != $this->table_name ) {
58
+
59
+ $charset_collate = $wpdb->get_charset_collate();
60
+
61
+ $sql = "CREATE TABLE {$this->table_name} (
62
+ id MEDIUMINT(20) NOT NULL DEFAULT 0,
63
+ term VARCHAR(50) NOT NULL DEFAULT 0,
64
+ term_source VARCHAR(20) NOT NULL DEFAULT 0,
65
+ type VARCHAR(50) NOT NULL DEFAULT 0,
66
+ count MEDIUMINT(20) NOT NULL DEFAULT 0
67
+ ) $charset_collate;";
68
+
69
+ require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
70
+ dbDelta( $sql );
71
+
72
+ $this->fill_table();
73
+
74
+ }
75
+
76
+ }
77
+
78
+ /*
79
+ * Insert data into the index table
80
+ */
81
+ private function fill_table( $post_id = 0 ) {
82
+
83
+ global $wpdb;
84
+
85
+ $posts = get_posts( array(
86
+ 'posts_per_page' => -1,
87
+ 'post_type' => 'product',
88
+ 'include' => $post_id
89
+ ) );
90
+
91
+
92
+ foreach ( $posts as $post ) {
93
+
94
+ $terms = array();
95
+
96
+ $id = $post->ID;
97
+
98
+ $product = new WC_product( $id );
99
+
100
+ $sku = $product->get_sku();
101
+
102
+ $title = apply_filters( 'the_title', get_the_title( $id ) );
103
+ $content = apply_filters( 'the_content', get_post_field( 'post_content', $id ) );
104
+ $excerpt = apply_filters( 'get_the_excerpt', get_post_field( 'post_excerpt', $id ) );
105
+ $cat_names = $this->get_terms_names_list( $id, 'product_cat' );
106
+ $tag_names = $this->get_terms_names_list( $id, 'product_tag' );
107
+
108
+
109
+ // WP 4.2 emoji strip
110
+ if ( function_exists( 'wp_encode_emoji' ) ) {
111
+ $content = wp_encode_emoji( $content );
112
+ }
113
+
114
+ $content = strip_shortcodes( $content );
115
+
116
+
117
+ $terms['title'] = $this->extract_terms( $title );
118
+ $terms['content'] = $this->extract_terms( $content );
119
+ $terms['excerpt'] = $this->extract_terms( $excerpt );
120
+ $terms['sku'] = $this->extract_terms( $sku );
121
+ $terms['category'] = $this->extract_terms( $cat_names );
122
+ $terms['tag'] = $this->extract_terms( $tag_names );
123
+
124
+
125
+ foreach( $terms as $source => $all_terms ) {
126
+
127
+ foreach ( $all_terms as $term => $count ) {
128
+
129
+ if ( ! $term ) {
130
+ continue;
131
+ }
132
+
133
+ $wpdb->insert(
134
+ $this->table_name,
135
+ array(
136
+ 'id' => $id,
137
+ 'term' => $term,
138
+ 'term_source' => $source,
139
+ 'type' => 'product',
140
+ 'count' => $count
141
+ )
142
+ );
143
+
144
+ }
145
+
146
+ }
147
+
148
+ // break;
149
+
150
+ }
151
+
152
+ }
153
+
154
+ /*
155
+ * Update index table
156
+ */
157
+ public function update_table( $post_id, $post, $update ) {
158
+
159
+ global $wpdb;
160
+
161
+ $this->check_table();
162
+
163
+ $slug = 'product';
164
+
165
+ if ( $slug != $post->post_type ) {
166
+ return;
167
+ }
168
+
169
+ $wpdb->delete( $this->table_name, array( 'id' => $post_id ) );
170
+
171
+ $this->fill_table( $post_id );
172
+
173
+ }
174
+
175
+ /*
176
+ * Extract terms from content
177
+ */
178
+ private function extract_terms( $str ) {
179
+
180
+ $str = $this->html2txt( $str );
181
+
182
+ $str = esc_attr( $str );
183
+ $str = stripslashes( $str );
184
+ $str = str_replace( array( "\r", "\n" ), '', $str );
185
+
186
+ // Avoid single A-Z.
187
+ $str = preg_replace( '/\b\w{1}\b/i', " ", $str );
188
+
189
+ $str = str_replace( array(
190
+ "·",
191
+ "…",
192
+ "€",
193
+ "&shy;"
194
+ ), "", $str );
195
+
196
+ $str = str_replace( array(
197
+ ".",
198
+ ",",
199
+ "$",
200
+ "\\",
201
+ "/",
202
+ "{",
203
+ "^",
204
+ "}",
205
+ "?",
206
+ "!",
207
+ ";",
208
+ "(",
209
+ ")",
210
+ ":",
211
+ "[",
212
+ "]",
213
+ "'",
214
+ "+",
215
+ "ˇ",
216
+ "°",
217
+ "~",
218
+ '"',
219
+ "Ë›",
220
+ "Ă‹ĹĄ",
221
+ "¸",
222
+ "§",
223
+ "%",
224
+ "=",
225
+ "¨",
226
+ "`",
227
+ "’",
228
+ "‘",
229
+ "”",
230
+ "“",
231
+ "„",
232
+ "´",
233
+ "—",
234
+ "–",
235
+ "Ă—",
236
+ '&#8217;',
237
+ "&nbsp;",
238
+ chr( 194 ) . chr( 160 )
239
+ ), " ", $str );
240
+
241
+ $str = str_replace( 'Ăź', 'ss', $str );
242
+
243
+ //$str = preg_replace( '/[[:punct:]]+/u', ' ', $str );
244
+ $str = preg_replace( '/[[:space:]]+/', ' ', $str );
245
+
246
+ // Most objects except unicode characters
247
+ $str = preg_replace( '/[\x00-\x08\x0B\x0C\x0E-\x1F\x80-\x9F]/u', '', $str );
248
+
249
+ // Line feeds, carriage returns, tabs
250
+ $str = preg_replace( '/[\x00-\x1F\x80-\x9F]/u', '', $str );
251
+
252
+ $str = strtolower( $str );
253
+
254
+ $str = preg_replace( '/^[a-z]$/i', "", $str );
255
+
256
+ $str = trim( preg_replace( '/\s+/', ' ', $str ) );
257
+
258
+ $str_array = array_count_values( explode( ' ', $str ) );
259
+
260
+ return $str_array;
261
+
262
+ }
263
+
264
+ /*
265
+ * Removes scripts, styles, html tags
266
+ */
267
+ private function html2txt( $str ) {
268
+ $search = array(
269
+ '@<script[^>]*?>.*?</script>@si',
270
+ '@<[\/\!]*?[^<>]*?>@si',
271
+ '@<style[^>]*?>.*?</style>@siU',
272
+ '@<![\s\S]*?--[ \t\n\r]*>@'
273
+ );
274
+ $text = preg_replace( $search, '', $str );
275
+
276
+ return $text;
277
+ }
278
+
279
+ /*
280
+ * Get string with current product terms ids
281
+ *
282
+ * @return string List of terms ids
283
+ */
284
+ private function get_terms_list( $id, $taxonomy ) {
285
+
286
+ $terms = get_the_terms( $id, $taxonomy );
287
+
288
+ if ( is_wp_error( $terms ) ) {
289
+ return '';
290
+ }
291
+
292
+ if ( empty( $terms ) ) {
293
+ return '';
294
+ }
295
+
296
+ $cats_array_temp = array();
297
+
298
+ foreach ( $terms as $term ) {
299
+ $cats_array_temp[] = $term->term_id;
300
+ }
301
+
302
+ return implode( ', ', $cats_array_temp );
303
+
304
+ }
305
+
306
+ /*
307
+ * Get string with current product terms names
308
+ *
309
+ * @return string List of terms names
310
+ */
311
+ private function get_terms_names_list( $id, $taxonomy ) {
312
+
313
+ $terms = get_the_terms( $id, $taxonomy );
314
+
315
+ if ( is_wp_error( $terms ) ) {
316
+ return '';
317
+ }
318
+
319
+ if ( empty( $terms ) ) {
320
+ return '';
321
+ }
322
+
323
+ $cats_array_temp = array();
324
+
325
+ foreach ( $terms as $term ) {
326
+ $cats_array_temp[] = $term->name;
327
+ }
328
+
329
+ return implode( ', ', $cats_array_temp );
330
+
331
+ }
332
+
333
+ }
334
+
335
+ endif;
336
+
337
+
338
+ new AWS_Table();
includes/options.php CHANGED
@@ -159,18 +159,6 @@ $options['general'][] = array(
159
  "type" => "number"
160
  );
161
 
162
- $options['general'][] = array(
163
- "name" => __( "Exact match", "aws" ),
164
- "desc" => __( "Search only for fully matching string. Not divide string into words.", "aws" ),
165
- "id" => "exact_match",
166
- "value" => 'false',
167
- "type" => "radio",
168
- 'choices' => array(
169
- 'true' => 'On',
170
- 'false' => 'Off'
171
- )
172
- );
173
-
174
  $options['general'][] = array(
175
  "name" => __( "Search in", "aws" ),
176
  "desc" => __( "Source of searching. Set the source of searching by drag&drop needed fields to the right area.", "aws" ),
159
  "type" => "number"
160
  );
161
 
 
 
 
 
 
 
 
 
 
 
 
 
162
  $options['general'][] = array(
163
  "name" => __( "Search in", "aws" ),
164
  "desc" => __( "Source of searching. Set the source of searching by drag&drop needed fields to the right area.", "aws" ),
readme.txt CHANGED
@@ -4,15 +4,15 @@ 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.5
7
- Stable tag: 1.04
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
11
- Advanced AJAX search plugin for WooCommerce.
12
 
13
  == Description ==
14
 
15
- Advanced Woo Search - powerful live search plugin for WooCommerce. Just start typing and you will immediately see the products that you search
16
 
17
  = Main Features =
18
 
@@ -26,6 +26,17 @@ Advanced Woo Search - powerful live search plugin for WooCommerce. Just start ty
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
 
 
 
 
 
 
 
 
 
 
 
 
29
  == Installation ==
30
 
31
  1. Upload advanced-woo-search to the /wp-content/plugins/ directory
@@ -53,6 +64,9 @@ Or insert this function inside php file ( often it used to insert form inside pa
53
 
54
  == Changelog ==
55
 
 
 
 
56
  = 1.04 =
57
  * Fix issue with SKU search
58
  * Add option to display product SKU in search results
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.5
7
+ Stable tag: 1.05
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
11
+ Advanced AJAX search plugin for WooCommerce
12
 
13
  == Description ==
14
 
15
+ Advanced Woo Search - powerful live search plugin for WooCommerce. Just start typing and you will immediately see the products that you search.
16
 
17
  = Main Features =
18
 
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
 
29
+ = Premium Features =
30
+
31
+ [Premium Version Demo](https://advanced-woo-search.com/)
32
+
33
+ * Search **results layouts**
34
+ * **Filters**. Switch between tabs to show different search results
35
+ * **Unlimited** amount of search form instances
36
+ * **Anvanced settings page** with lot of options
37
+ * **Exclude** spicific products by its categories or tags from search results
38
+ * Ability to specify **source of image** for search results: featured image, gallery, product content, product short description or set default image if there is no other images
39
+
40
  == Installation ==
41
 
42
  1. Upload advanced-woo-search to the /wp-content/plugins/ directory
64
 
65
  == Changelog ==
66
 
67
+ = 1.05 =
68
+ * Improve search speed
69
+
70
  = 1.04 =
71
  * Fix issue with SKU search
72
  * Add option to display product SKU in search results