WP 404 Auto Redirect to Similar Post - Version 0.4

Version Description

  • Revamped Code
  • Improved Speed
  • Better Post Type / Category / Taxonomy matching
Download this release

Release Info

Developer hwk-fr
Plugin Icon 128x128 WP 404 Auto Redirect to Similar Post
Version 0.4
Comparing to
See all releases

Code changes from version 0.3.2 to 0.4

Files changed (2) hide show
  1. readme.txt +8 -4
  2. wp-404-auto-redirect-similar-post.php +216 -339
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: http://hwk.fr/
4
  Tags: 404, Redirect, 301, Similar Post, SEO, Broken Link, Webmaster Tools
5
  Requires at least: 4.0
6
  Tested up to: 4.7.4
7
- Stable tag: 0.3.2
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -14,7 +14,7 @@ Automatically Redirect any 404 to a Similar Post based on the Title, Post Type,
14
 
15
  Welcome to WP 404 Auto Redirect to Similar Post!
16
 
17
- This plugin will automatically redirect all your 404 to a similar post, based on the Title, Post Type, Category & Taxonomy. If nothing similar is found, the plugin will redirect the page to your homepage. All redirects are done via 301 HTTP Status Code.
18
 
19
  = Features: =
20
 
@@ -41,12 +41,16 @@ This plugin will automatically redirect all your 404 to a similar post, based on
41
  = What are the redirect priorities? =
42
 
43
  1. Post Title + Post Type
44
- 2. Category
45
- 3. Taxonomy
46
  4. Homepage
47
 
48
  == Changelog ==
49
 
 
 
 
 
 
50
  = 0.3.2 =
51
  * Added Debug monitoring
52
  * Better management of paged requests
4
  Tags: 404, Redirect, 301, Similar Post, SEO, Broken Link, Webmaster Tools
5
  Requires at least: 4.0
6
  Tested up to: 4.7.4
7
+ Stable tag: 0.4
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
14
 
15
  Welcome to WP 404 Auto Redirect to Similar Post!
16
 
17
+ This plugin will automatically redirect all your 404 to a similar post, based on the Title, Post Type, Category & Taxonomy. If nothing similar is found, the plugin will redirect to your homepage. All redirects are done via 301 HTTP Status Code.
18
 
19
  = Features: =
20
 
41
  = What are the redirect priorities? =
42
 
43
  1. Post Title + Post Type
44
+ 2. Category / Taxonomy
 
45
  4. Homepage
46
 
47
  == Changelog ==
48
 
49
+ = 0.4 =
50
+ * Revamped Code
51
+ * Improved Speed
52
+ * Better Post Type / Category / Taxonomy matching
53
+
54
  = 0.3.2 =
55
  * Added Debug monitoring
56
  * Better management of paged requests
wp-404-auto-redirect-similar-post.php CHANGED
@@ -3,7 +3,7 @@
3
  * Plugin Name: WP 404 Auto Redirect to Similar Post
4
  * Description: Automatically Redirect any 404 to a Similar Post based on the Title, Post Type, Category & Taxonomy using 301 Redirects!
5
  * Author: hwk-fr
6
- * Version: 0.3.2
7
  * Author URI: http://hwk.fr
8
  */
9
 
@@ -17,419 +17,296 @@ function wp404arsp_settings(){
17
 
18
  add_action('template_redirect', 'wp404arsp');
19
  function wp404arsp(){
20
- global $wpdb, $wp_query;
21
 
22
  if(!is_404() || (defined('DOING_AJAX') && DOING_AJAX))
23
  return;
24
 
25
- $wp404arsp_query = wp404arsp_query_setup($_SERVER['REQUEST_URI'], $wp_query);
26
-
27
- if(!empty($wp404arsp_query['wp_query']['post_type'])){
28
- $args = array(
29
- array(
30
- 'method' => 'sql',
31
- 'search' => $wp404arsp_query['post_name']['exploded'],
32
- 'post_type' => $wp404arsp_query['wp_query']['post_type'],
33
- 'query' => $wp404arsp_query
34
- ),
35
- array(
36
- 'method' => 'metaphone',
37
- 'search' => $wp404arsp_query['post_name']['exploded'],
38
- 'post_type' => $wp404arsp_query['wp_query']['post_type'],
39
- 'query' => $wp404arsp_query
40
- ),
41
- array(
42
- 'method' => 'like',
43
- 'search' => $wp404arsp_query['post_name']['exploded'],
44
- 'post_type' => $wp404arsp_query['wp_query']['post_type'],
45
- 'query' => $wp404arsp_query
46
- ),
47
- );
48
-
49
- foreach($args as $arg){
50
- wp404arsp_search($arg);
51
- }
52
-
53
- }
54
-
55
- if(!empty($wp404arsp_query['wp_query']['category']) && !empty($wp404arsp_query['wp_query']['paged'])){
56
-
57
- $args = array(
58
- 'method' => 'category',
59
- 'search' => $wp404arsp_query['post_name']['imploded'],
60
- 'category' => $wp404arsp_query['wp_query']['category'],
61
- 'name' => (!empty($wp404arsp_query['wp_query']['name']) ? $wp404arsp_query['wp_query']['name'] : ''),
62
- 'paged' => $wp404arsp_query['wp_query']['paged'],
63
- 'query' => $wp404arsp_query
64
- );
65
-
66
- wp404arsp_search($args);
67
-
68
- }
69
-
70
- if(!empty($wp404arsp_query['wp_query']['taxonomy']) && !empty($wp404arsp_query['wp_query']['term']) && !empty($wp404arsp_query['wp_query']['paged'])){
71
-
72
- $args = array(
73
- 'method' => 'taxonomy',
74
- 'search' => $wp404arsp_query['post_name']['imploded'],
75
- 'taxonomy' => $wp404arsp_query['wp_query']['taxonomy'],
76
- 'term' => (!empty($wp404arsp_query['wp_query']['term']) ? $wp404arsp_query['wp_query']['term'] : ''),
77
- 'paged' => $wp404arsp_query['wp_query']['paged'],
78
- 'query' => $wp404arsp_query
79
- );
80
-
81
- wp404arsp_search($args);
82
-
83
- }
84
-
85
- $args = array(
86
- array(
87
- 'method' => 'post_types',
88
- 'search' => $wp404arsp_query['post_name']['imploded'],
89
- 'query' => $wp404arsp_query
90
- ),
91
- array(
92
- 'method' => 'post_types',
93
- 'search' => $wp404arsp_query['path']['imploded'],
94
- 'query' => $wp404arsp_query
95
- ),
96
- array(
97
- 'method' => 'sql',
98
- 'search' => $wp404arsp_query['post_name']['exploded'],
99
- 'query' => $wp404arsp_query
100
- ),
101
- array(
102
- 'method' => 'metaphone',
103
- 'search' => $wp404arsp_query['post_name']['exploded'],
104
- 'query' => $wp404arsp_query
105
- ),
106
- array(
107
- 'method' => 'like',
108
- 'search' => $wp404arsp_query['post_name']['exploded'],
109
- 'query' => $wp404arsp_query
110
- ),
111
- );
112
-
113
- foreach($args as $arg){
114
- wp404arsp_search($arg);
115
- }
116
-
117
- wp404arsp_redirect('/', 'home', 'Go home, you\'re drunk.');
118
-
119
 
120
  }
121
 
122
- function wp404arsp_query_setup($query, $wp_query){
123
 
124
- $path = pathinfo( urldecode( htmlspecialchars( strtok($query, '?') ) ) );
125
- $path['dirname'] = ltrim($path['dirname'], '/');
126
- $path['directories'] = explode('/', $path['dirname']);
127
- foreach($path['directories'] as $k => $v){
128
- $path['directories'][$k] = sanitize_title($v);
129
- }
130
-
131
- $return = array();
132
- $return['post_name']['imploded'] = sanitize_title( urldecode( htmlspecialchars( pathinfo( basename( strtok($query, '?') ), PATHINFO_FILENAME) ) ) );
133
- $return['post_name']['exploded'] = explode('-', $return['post_name']['imploded']);
134
-
135
- $return['path']['exploded'] = $path['directories'];
136
- $return['path']['exploded'][] = $return['post_name']['imploded'];
137
- $return['path']['imploded'] = implode('/', $return['path']['exploded']);
138
-
139
- $wp404arsp_wp_query['query'] = $wp_query->query;
140
- $wp404arsp_wp_query['query_vars'] = $wp_query->query_vars;
141
 
142
- if(!empty($wp404arsp_wp_query['query']['post_type']))
143
- $return['wp_query']['post_type'] = $wp404arsp_wp_query['query']['post_type'];
144
-
145
- if(!empty($wp404arsp_wp_query['query']['category_name'])){
146
- $return['wp_query']['category'] = $wp404arsp_wp_query['query']['category_name'];
147
 
148
- if(strpos($return['wp_query']['category'], '/') !== false){
149
- $return['wp_query']['category'] = reset( array_slice( explode( '/', $return['wp_query']['category'] ), -1, 1, true ) );
 
 
 
 
 
150
  }
151
 
152
- }
153
-
154
- if(!empty($wp404arsp_wp_query['query']['paged']))
155
- $return['wp_query']['paged'] = $wp404arsp_wp_query['query']['paged'];
156
-
157
- if(!empty($wp404arsp_wp_query['query']['name']))
158
- $return['wp_query']['name'] = $wp404arsp_wp_query['query']['name'];
159
-
160
- if(!empty($wp404arsp_wp_query['query_vars']['taxonomy']))
161
- $return['wp_query']['taxonomy'] = $wp404arsp_wp_query['query_vars']['taxonomy'];
162
-
163
- if(!empty($wp404arsp_wp_query['query_vars']['taxonomy']) && !empty($wp404arsp_wp_query['query_vars'][$wp404arsp_wp_query['query_vars']['taxonomy']]))
164
- $return['wp_query']['term'] = $wp404arsp_wp_query['query_vars'][$wp404arsp_wp_query['query_vars']['taxonomy']];
165
-
166
- if(empty($return['wp_query']['post_type']) && !empty($return['wp_query']['category'])){
167
- $get_post_type = get_post_type_object($return['wp_query']['category']);
168
- if(!empty($get_post_type)){
169
- $return['wp_query']['post_type'] = $return['wp_query']['category'];
170
- }
171
  }
172
 
173
- // Clean permalink end /page/10/
174
- if(is_numeric($return['post_name']['imploded']) && count($return['path']['exploded']) > 1 && !empty($return['wp_query']['paged'])){
175
- if(($key = array_search($return['post_name']['imploded'], $return['path']['exploded'])) !== false) {
176
- unset($return['path']['exploded'][$key]);
177
- }
178
- $return['path']['exploded'] = array_values($return['path']['exploded']);
179
- $last = key(array_slice($return['path']['exploded'], -1, 1, true));
180
- $return['post_name']['imploded'] = $return['path']['exploded'][$last];
181
- $return['post_name']['exploded'] = explode('-', $return['path']['exploded'][$last]);
182
- $return['path']['imploded'] = implode('/', $return['path']['exploded']);
183
- }
184
 
185
- // Clean permalink end /page/
186
- if($return['post_name']['imploded'] == 'page' && count($return['path']['exploded']) > 1 && !empty($return['wp_query']['paged'])){
187
- if(($key = array_search($return['post_name']['imploded'], $return['path']['exploded'])) !== false) {
188
- unset($return['path']['exploded'][$key]);
189
- }
190
- $return['path']['exploded'] = array_values($return['path']['exploded']);
191
- $last = key(array_slice($return['path']['exploded'], -1, 1, true));
192
- $return['post_name']['imploded'] = $return['path']['exploded'][$last];
193
- $return['post_name']['exploded'] = explode('-', $return['path']['exploded'][$last]);
194
- $return['path']['imploded'] = implode('/', $return['path']['exploded']);
195
- }
196
 
197
- return $return;
198
 
199
- }
200
-
201
- function wp404arsp_redirect($post = '', $method = '', $debug = ''){
202
-
203
- $wp404arsp_settings = wp404arsp_settings();
204
- $wp404arsp_permalink = wp404arsp_get_permalink($post, $method);
205
 
206
- if($wp404arsp_settings['debug']){
207
- if(!empty($post)){
208
- echo "<pre>"; echo 'Redirect to: ' . $wp404arsp_permalink . ' ('.$post.' - '.$method.')' . ' - ' . $debug['search']['method'] . "</pre>";
209
 
210
- }else{
211
- echo "<pre>"; echo $debug['search']['method'] . ' <span style="color:#bb0000;">Failed!</span> ' . "</pre>";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
212
  }
213
 
214
- echo "<pre>"; print_r($debug); echo "</pre>";
215
- echo "<hr />";
216
- return;
217
-
218
  }
219
 
220
- if(!empty($post)){
221
- wp_redirect($wp404arsp_permalink, 301);
222
- exit;
223
- }
224
 
225
- return false;
226
  }
227
 
228
- function wp404arsp_search($args){
 
229
  global $wpdb;
230
 
231
- if(empty($args) || !is_array($args) || empty($args['method']) || empty($args['search']))
232
  return false;
233
 
234
- $post = false;
235
- $debug = array();
236
 
237
- // All post_types
238
- if($args['method'] == 'post_types'){
239
-
240
- $debug['search'] = $args;
241
-
242
- $get_posts_types = get_post_types(array('public' => true), 'names');
243
- foreach($get_posts_types as $post_type){
244
- $post = get_page_by_path($args['search'], 'object', $post_type);
245
- if(!empty($post)){
246
- $post = $post->ID;
247
- break;
248
- }
249
- }
250
-
251
- wp404arsp_redirect($post, 'post', $debug);
252
-
253
-
254
- // SQL
255
- }elseif($args['method'] == 'sql'){
256
-
257
- $debug['search'] = $args;
258
-
259
- $search = wp404arsp_remove_stop_words($args['search']);
260
 
261
- $sql = "SELECT ID, ";
262
- foreach($search as $search_word){
263
- $sql .= " if(INSTR(LCASE(post_name),'" . $search_word . "'), 1, 0) + ";
264
  }
265
- $sql .= "0 as score FROM " . $wpdb->posts . " WHERE post_status = 'publish'
266
- AND post_type <> 'attachment' AND post_type <> 'nav_menu_item' ";
 
 
 
 
 
267
 
268
- if(!empty($args['post_type'])){
269
- $sql .= "AND post_type = '" . $args['post_type'] . "'";
270
-
 
 
271
  }
 
 
 
272
 
273
- $sql .= "ORDER BY score DESC, post_modified DESC LIMIT 1";
274
  $debug['sql'] = $sql;
275
 
276
- $row = $wpdb->get_row($sql);
277
-
278
  $get_post = $wpdb->get_row($sql);
279
  if(!empty($get_post)){
280
- $debug['results'][] = array('id' => (int)$get_post->ID, 'url' => get_permalink($get_post->ID), 'score' => $get_post->score);
281
- $post = (int)$get_post->ID;
 
 
282
  }
283
 
284
- wp404arsp_redirect($post, 'post', $debug);
285
 
286
-
287
- // Metaphone
288
- }elseif($args['method'] == 'metaphone'){
289
 
290
- $debug['search'] = $args;
291
- $search_words = wp404arsp_remove_stop_words($args['search']);
292
-
293
- $search = array();
294
- foreach($search_words as $word){
295
- $search[] = metaphone($word);
296
  }
297
 
298
- $sql = "SELECT ID as sql_id, post_name as sql_name
299
- FROM $wpdb->posts
300
- WHERE post_status = 'publish'
301
- AND post_type <> 'attachment' AND post_type <> 'nav_menu_item' ";
 
 
302
 
303
- if(!empty($args['post_type'])){
304
- $sql .= "AND post_type = '" . $args['post_type'] . "'";
 
 
305
  }
306
-
307
- $sql .= "ORDER BY post_modified DESC";
308
 
309
- $get_posts = $wpdb->get_results($sql);
 
310
 
311
- $result = array();
312
- if(!empty($get_posts)){
313
- foreach($get_posts as $row){
314
- $row->sql_name = wp404arsp_remove_stop_words(explode('-' , sanitize_title($row->sql_name)));
315
-
316
- $score = 0;
317
- $raw_name = array();
318
- if(!empty($row->sql_name) && count($row->sql_name) >= 2){
319
- foreach($row->sql_name as $name){
320
- $meta = metaphone($name);
321
- if(strlen($meta) > 1 && in_array($meta, $search)){
322
- $score++;
323
- $raw_name[] = array('word' => $name, 'metaphone' => $meta);
324
- }
325
-
326
- }
327
- if($score >= 2){
328
- $result[$row->sql_id] = array('rows' => $raw_name, 'score' => $score);
329
- }
330
- }
331
-
332
- }
333
-
334
-
335
 
336
- if(!empty($result)){
337
- arsort($result);
338
- reset($result);
339
- $post = key($result);
340
  }
341
-
342
 
343
- $debug['sql'] = $sql;
344
- $debug['results'] = array_slice($result, 0, 1, true);
345
 
346
- wp404arsp_redirect($post, 'post', $debug);
347
- }
348
-
349
-
350
- // Like
351
- }elseif($args['method'] == 'like'){
352
-
353
- $debug['search'] = $args;
354
- $search = wp404arsp_remove_stop_words($args['search']);
355
-
356
- foreach($search as $word){
357
- $words[] = "post_name LIKE '%" . $word . "%'";
358
- }
359
- $words = implode(' AND ', $words);
360
-
361
- $sql = "SELECT ID
362
- FROM $wpdb->posts
363
- WHERE ";
 
 
 
 
 
364
 
365
- $sql .= $words;
366
-
367
- $sql .= "
368
- AND post_status = 'publish'
369
- AND post_type <> 'attachment' AND post_type <> 'nav_menu_item' " . (!empty($args['post_type']) ? "AND post_type = '" . $args['post_type'] . "'" : "") . "
370
- LIMIT 1";
371
- $debug['sql'] = $sql;
372
- $post = $wpdb->get_var($sql);
373
- $post = (int)$post;
374
- wp404arsp_redirect($post, 'post', $debug);
375
-
376
-
377
- // Category
378
- }elseif($args['method'] == 'category'){
379
-
380
- $debug['search'] = $args;
381
-
382
- if(!empty($args['paged'])){
383
- $category = (!empty($args['name'])) ? $args['name'] : $args['category'];
384
-
385
- $get_category = get_category_by_slug($category);
386
- if(!empty($get_category)){
387
- $post = (int)$get_category->term_id;
388
- wp404arsp_redirect($post, 'category', $debug);
389
  }
390
- }
391
-
392
-
393
- // Taxonomy
394
- }elseif($args['method'] == 'taxonomy'){
395
 
396
- $debug['search'] = $args;
397
-
398
- if(!empty($args['taxonomy']) && !empty($args['term']) && !empty($args['paged']) ){
399
- $get_term = get_term_by( 'slug', $args['term'], $args['taxonomy']);
400
- if(!empty($get_term)){
401
- $post = (int)$get_term->term_id;
402
- wp404arsp_redirect($post, 'taxonomy', $debug);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
403
  }
 
404
  }
 
 
 
405
 
406
  }
407
 
 
 
408
  }
409
 
410
- function wp404arsp_get_permalink($post, $method){
411
-
412
- $get_permalink = home_url();
413
-
414
- if($method == 'post')
415
- $get_permalink = get_permalink($post);
416
 
417
- if($method == 'category')
418
- $get_permalink = get_category_link($post);
419
 
420
- if($method == 'taxonomy')
421
- $get_permalink = get_term_link($post);
 
 
 
 
 
 
 
422
 
423
- return $get_permalink;
 
 
 
424
 
425
  }
426
 
427
  function wp404arsp_remove_stop_words($input){
428
 
429
- $words = array('a', 'about', 'above', 'after', 'again', 'against', 'all', 'am', 'an', 'and', 'any', 'are', 'arent', 'as', 'at', 'be', 'because', 'been', 'before', 'being', 'below', 'between', 'both', 'but', 'by', 'cant', 'cannot', 'could', 'couldnt', 'did', 'didnt', 'do', 'does', 'doesnt', 'doing', 'dont', 'down', 'during', 'each', 'few', 'for', 'from', 'further', 'had', 'hadnt', 'has', 'hasnt', 'have', 'havent', 'having', 'he', 'hed', 'hell', 'hes', 'her', 'here', 'heres', 'hers', 'herself', 'him', 'himself', 'his', 'how', 'hows', 'i', 'id', 'ill', 'im', 'ive', 'if', 'in', 'into', 'is', 'isnt', 'it', 'its', 'itself', 'lets', 'me', 'more', 'most', 'mustnt', 'my', 'myself', 'no', 'nor', 'not', 'of', 'off', 'on', 'once', 'only', 'or', 'other', 'ought', 'our', 'ours', 'ourselves', 'out', 'over', 'own', 'same', 'shant', 'she', 'shed', 'shell', 'shes', 'should', 'shouldnt', 'so', 'some', 'such', 'than', 'that', 'thats', 'the', 'their', 'theirs', 'them', 'themselves', 'then', 'there', 'theres', 'these', 'they', 'theyd', 'theyll', 'theyre', 'theyve', 'this', 'those', 'to', 'too', 'under', 'until', 'up', 'very', 'was', 'wasnt', 'we', 'wed', 'well', 'were', 'weve', 'werent', 'what', 'whats', 'when', 'whens', 'where', 'wheres', 'which', 'while', 'who', 'whos', 'whom', 'why', 'whys', 'with', 'wont', 'would', 'wouldnt', 'you', 'youd', 'youll', 'youre', 'youve', 'your', 'yours');
430
 
431
  if(is_array($input)){
432
  $return = array_diff($input, $words);
 
 
 
 
 
 
433
  return array_values($return);
434
 
435
  }else{
3
  * Plugin Name: WP 404 Auto Redirect to Similar Post
4
  * Description: Automatically Redirect any 404 to a Similar Post based on the Title, Post Type, Category & Taxonomy using 301 Redirects!
5
  * Author: hwk-fr
6
+ * Version: 0.4
7
  * Author URI: http://hwk.fr
8
  */
9
 
17
 
18
  add_action('template_redirect', 'wp404arsp');
19
  function wp404arsp(){
 
20
 
21
  if(!is_404() || (defined('DOING_AJAX') && DOING_AJAX))
22
  return;
23
 
24
+ $query = wp404arsp_query_setup($_SERVER['REQUEST_URI']);
25
+ wp404arsp_search($query);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
  }
28
 
29
+ function wp404arsp_query_setup($request){
30
 
31
+ $path = pathinfo(urldecode(htmlspecialchars(strtok($request, '?'))));
32
+ $path['filename'] = sanitize_title(pathinfo(basename($request), PATHINFO_FILENAME));
33
+ $path['dirname'] = ltrim($path['dirname'], '/');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
+ if($path['dirname'] != '\\'){
 
 
 
 
36
 
37
+ if(strpos($path['dirname'], '/') !== false){
38
+ $path['directories'] = explode('/', $path['dirname']);
39
+ foreach($path['directories'] as $k => $v){
40
+ $path['directories'][$k] = sanitize_title($v);
41
+ }
42
+ }else{
43
+ $path['directories'][] = sanitize_title($path['dirname']);
44
  }
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  }
47
 
48
+ $path['directories'][] = $path['filename'];
 
 
 
 
 
 
 
 
 
 
49
 
50
+ $keywords['split'] = $path['directories'];
51
+ $keywords['split'] = wp404arsp_remove_stop_words($keywords['split']);
 
 
 
 
 
 
 
 
 
52
 
53
+ $keywords['full'] = implode('/', $keywords['split']);
54
 
55
+ $return = array();
56
+ $return['keywords']['split'] = $keywords['split'];
57
+ $return['keywords']['full'] = $keywords['full'];
 
 
 
58
 
59
+ if(!empty($return['keywords']['split'])){
60
+ $i=0; foreach($return['keywords']['split'] as $keyword){
 
61
 
62
+ // Test: Post
63
+ $get_posts_types = get_post_types(array('public' => true), 'names');
64
+ foreach($get_posts_types as $post_type){
65
+ if($post = get_page_by_path($keyword, 'object', $post_type)){
66
+ $return['known'][$i]['slug'] = $keyword;
67
+ $return['known'][$i]['type'] = 'post';
68
+ $return['known'][$i]['data']['post_type'] = $post->post_type;
69
+ $return['known'][$i]['data']['ID'] = $post->ID;
70
+ break;
71
+ }
72
+ }
73
+
74
+ if(empty($return['known'][$i]['type'])){
75
+
76
+ // Test: Post Type
77
+ $get_posts_types = get_post_types(array('public' => true), 'names');
78
+ foreach($get_posts_types as $post_type){
79
+ $post_type = get_post_type_object($post_type);
80
+ if($post_type->rewrite['slug'] == $keyword || $post_type->has_archive == $keyword){
81
+ $return['known'][$i]['slug'] = $keyword;
82
+ $return['known'][$i]['type'] = 'post_type';
83
+ $return['known'][$i]['data']['post_type'] = $post_type->name;
84
+ break;
85
+ }
86
+ }
87
+
88
+ }
89
+
90
+ if(empty($return['known'][$i]['type'])){
91
+
92
+ // Test: Taxonomy
93
+ $get_taxonomies = get_taxonomies(array('public' => true), 'names');
94
+ foreach($get_taxonomies as $taxonomy){
95
+ if($tax = get_term_by('slug', $keyword, $taxonomy)){
96
+ $return['known'][$i]['slug'] = $keyword;
97
+ $return['known'][$i]['type'] = 'term';
98
+ $return['known'][$i]['data']['taxonomy'] = $taxonomy;
99
+ break;
100
+ }
101
+ }
102
+
103
+ }
104
+
105
+ if(empty($return['known'][$i]['slug']))
106
+ $return['unknown'][]['slug'] = $keyword;
107
+
108
+ $i++;
109
  }
110
 
 
 
 
 
111
  }
112
 
113
+ return $return;
 
 
 
114
 
 
115
  }
116
 
117
+ function wp404arsp_search($query){
118
+
119
  global $wpdb;
120
 
121
+ if(empty($query))
122
  return false;
123
 
124
+ $known = (!empty($query['known'])) ? array_reverse($query['known']) : '';
125
+ $unknown = (!empty($query['unknown'])) ? array_reverse($query['unknown']) : '';
126
 
127
+ $debug['query'] = $query;
128
+
129
+ if(empty($unknown) && !empty($known)){
130
+ foreach($known as $slug){
131
+
132
+ $get_permalink = false;
133
+
134
+ if($slug['type'] == 'post')
135
+ $get_permalink = get_permalink($slug['data']['ID']);
136
+
137
+ if($slug['type'] == 'post_type')
138
+ $get_permalink = get_post_type_archive_link($slug['data']['post_type']);
139
+
140
+ if($slug['type'] == 'term')
141
+ $get_permalink = get_term_link($slug['slug'], $slug['data']['taxonomy']);
 
 
 
 
 
 
 
 
142
 
143
+ wp404arsp_redirect($get_permalink, $debug);
 
 
144
  }
145
+ }
146
+
147
+ if(empty($known) && !empty($unknown)){
148
+ foreach($unknown as $word){
149
+ $collapse[] = $word['slug'];
150
+ }
151
+ $explode = explode('-', implode('-', $collapse));
152
 
153
+ $get_permalink = false;
154
+
155
+ $sql = "SELECT p.ID, ";
156
+ foreach($explode as $word){
157
+ $sql .= " if(INSTR(LCASE(p.post_name),'" . $word . "'), 1, 0) + ";
158
  }
159
+ $sql .= "0 as score FROM " . $wpdb->posts . " AS p
160
+ WHERE p.post_status = 'publish' AND p.post_type <> 'attachment' AND p.post_type <> 'nav_menu_item'
161
+ ORDER BY score DESC, post_modified DESC LIMIT 1";
162
 
 
163
  $debug['sql'] = $sql;
164
 
 
 
165
  $get_post = $wpdb->get_row($sql);
166
  if(!empty($get_post)){
167
+ $debug['result'] = array('score' => $get_post->score, 'id' => (int)$get_post->ID, 'url' => get_permalink($get_post->ID));
168
+ if($get_post->score > 0){
169
+ $get_permalink = get_permalink((int)$get_post->ID);
170
+ }
171
  }
172
 
173
+ wp404arsp_redirect($get_permalink, $debug);
174
 
175
+ /*
176
+ $sql = "SELECT t.term_id, ";
 
177
 
178
+ foreach(wp404arsp_remove_stop_words($search) as $word){
179
+ $sql .= " if(INSTR(LCASE(t.slug),'" . $word . "'), 1, 0) + ";
 
 
 
 
180
  }
181
 
182
+ $sql .= "0 as score FROM " . $wpdb->terms . " AS t
183
+ INNER JOIN $wpdb->term_taxonomy AS tt ON (t.term_id = tt.term_id)
184
+ WHERE tt.taxonomy = '".$query['wp_query']['taxonomy']."'
185
+ ORDER BY score DESC LIMIT 1";
186
+
187
+ $debug['sql'] = $sql;
188
 
189
+ $get_term = $wpdb->get_row($sql);
190
+ if(!empty($get_term)){
191
+ $debug['results'][] = array('id' => (int)$get_term->term_id, 'url' => get_term_link((int)$get_term->term_id, $query['wp_query']['taxonomy']), 'score' => $get_term->score);
192
+ $post = (int)$get_term->term_id;
193
  }
194
+ */
 
195
 
196
+ // TODO
197
+ // Search Term and compare with Post search
198
 
199
+ }
200
+
201
+ if(!empty($known) && !empty($unknown)){
202
+ foreach($known as $slug){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
 
204
+ foreach($unknown as $word){
205
+ $collapse[] = $word['slug'];
 
 
206
  }
207
+ $explode = explode('-', implode('-', $collapse));
208
 
209
+ $get_permalink = false;
 
210
 
211
+ if($slug['type'] == 'post_type'){
212
+
213
+ $sql = "SELECT p.ID, ";
214
+ foreach($explode as $word){
215
+ $sql .= " if(INSTR(LCASE(p.post_name),'" . $word . "'), 1, 0) + ";
216
+ }
217
+ $sql .= "0 as score FROM " . $wpdb->posts . " AS p
218
+ WHERE
219
+ p.post_type = '".$slug['data']['post_type']."'
220
+ AND p.post_status = 'publish' AND p.post_type <> 'attachment' AND p.post_type <> 'nav_menu_item'
221
+ ORDER BY score DESC, post_modified DESC LIMIT 1";
222
+
223
+ $debug['sql'] = $sql;
224
+
225
+ $get_post = $wpdb->get_row($sql);
226
+ if(!empty($get_post)){
227
+ $debug['result'] = array('score' => $get_post->score, 'id' => (int)$get_post->ID, 'url' => get_permalink($get_post->ID));
228
+ if($get_post->score > 0){
229
+ $get_permalink = get_permalink((int)$get_post->ID);
230
+ }
231
+ }
232
+
233
+ wp404arsp_redirect($get_permalink, $debug);
234
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
235
  }
 
 
 
 
 
236
 
237
+ if($slug['type'] == 'term'){
238
+
239
+ $sql = "SELECT p.ID, ";
240
+ foreach($explode as $word){
241
+ $sql .= " if(INSTR(LCASE(p.post_name),'" . $word . "'), 1, 0) + ";
242
+ }
243
+ $sql .= "0 as score FROM " . $wpdb->posts . " AS p
244
+ INNER JOIN $wpdb->term_relationships AS tr ON (p.ID = tr.object_id)
245
+ INNER JOIN $wpdb->term_taxonomy AS tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
246
+ INNER JOIN $wpdb->terms AS t ON (t.term_id = tt.term_id)
247
+ WHERE
248
+ tt.taxonomy = '".$slug['data']['taxonomy']."' AND t.slug = '".$slug['slug']."'
249
+ AND p.post_status = 'publish' AND p.post_type <> 'attachment' AND p.post_type <> 'nav_menu_item'
250
+ ORDER BY score DESC, post_modified DESC LIMIT 1";
251
+
252
+ $debug['sql'] = $sql;
253
+
254
+ $get_post = $wpdb->get_row($sql);
255
+ if(!empty($get_post)){
256
+ $debug['result'] = array('score' => $get_post->score, 'id' => (int)$get_post->ID, 'url' => get_permalink($get_post->ID));
257
+ if($get_post->score > 0){
258
+ $get_permalink = get_permalink((int)$get_post->ID);
259
+ }
260
+ }
261
+
262
+ wp404arsp_redirect($get_permalink, $debug);
263
+
264
  }
265
+
266
  }
267
+
268
+ unset($query['unknown']);
269
+ wp404arsp_search($query, $debug);
270
 
271
  }
272
 
273
+ wp404arsp_redirect(home_url(), 'Go home, you\'re drunk...');
274
+
275
  }
276
 
277
+ function wp404arsp_redirect($url = '', $debug = ''){
 
 
 
 
 
278
 
279
+ $wp404arsp_settings = wp404arsp_settings();
 
280
 
281
+ if(is_super_admin() && $wp404arsp_settings['debug']){
282
+ echo "<pre>"; echo 'Redirect to: ' . $url . "</pre>";
283
+
284
+ if(!empty($debug))
285
+ echo "<pre>"; print_r($debug); echo "</pre>";
286
+
287
+ echo "<hr />";
288
+ return;
289
+ }
290
 
291
+ if(!empty($url)){
292
+ wp_redirect($url, 301);
293
+ exit;
294
+ }
295
 
296
  }
297
 
298
  function wp404arsp_remove_stop_words($input){
299
 
300
+ $words = array('a', 'about', 'above', 'after', 'again', 'against', 'all', 'am', 'an', 'and', 'any', 'are', 'arent', 'as', 'at', 'be', 'because', 'been', 'before', 'being', 'below', 'between', 'both', 'but', 'by', 'cant', 'cannot', 'could', 'couldnt', 'did', 'didnt', 'do', 'does', 'doesnt', 'doing', 'dont', 'down', 'during', 'each', 'few', 'for', 'from', 'further', 'had', 'hadnt', 'has', 'hasnt', 'have', 'havent', 'having', 'he', 'hed', 'hell', 'hes', 'her', 'here', 'heres', 'hers', 'herself', 'him', 'himself', 'his', 'how', 'hows', 'i', 'id', 'ill', 'im', 'ive', 'if', 'in', 'into', 'is', 'isnt', 'it', 'its', 'itself', 'lets', 'me', 'more', 'most', 'mustnt', 'my', 'myself', 'no', 'nor', 'not', 'of', 'off', 'on', 'once', 'only', 'or', 'other', 'ought', 'our', 'ours', 'ourselves', 'out', 'over', 'own', 'same', 'shant', 'she', 'shed', 'shell', 'shes', 'should', 'shouldnt', 'so', 'some', 'such', 'than', 'that', 'thats', 'the', 'their', 'theirs', 'them', 'themselves', 'then', 'there', 'theres', 'these', 'they', 'theyd', 'theyll', 'theyre', 'theyve', 'this', 'those', 'to', 'too', 'under', 'until', 'up', 'very', 'was', 'wasnt', 'we', 'wed', 'well', 'were', 'weve', 'werent', 'what', 'whats', 'when', 'whens', 'where', 'wheres', 'which', 'while', 'who', 'whos', 'whom', 'why', 'whys', 'with', 'wont', 'would', 'wouldnt', 'you', 'youd', 'youll', 'youre', 'youve', 'your', 'yours', 'category', 'page', 'paged');
301
 
302
  if(is_array($input)){
303
  $return = array_diff($input, $words);
304
+
305
+ foreach($return as $key => $val){
306
+ if(is_numeric($val))
307
+ unset($return[$key]);
308
+ }
309
+
310
  return array_values($return);
311
 
312
  }else{