PluginProbe
SEO Redirection Plugin – 301 Redirect Manager / 7.5
SEO Redirection Plugin – 301 Redirect Manager v7.5
9.19 trunk 4.17 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 9.1 9.10 9.11 9.12 All 39 releases
seo-redirection / seo-redirection.php

seo-redirection.php in SEO Redirection Plugin – 301 Redirect Manager 7.5, at seo-redirection.php

1,379 lines 53.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: SEO Redirection
4 Plugin URI: http://www.clogica.com/product/seo-redirection-premium-wordpress-plugin
5 Description: By this plugin you can manage all your website redirection types easily.
6 Author: Fakhri Alsadi
7 Version: 7.5
8 Author URI: http://www.clogica.com
9 Text Domain: seo-redirection
10 */
11
12 require_once('common/controls.php');
13 require_once('custom/controls.php');
14 require_once('custom/controls/cf.SR_redirect_cache.class.php');
15
16 define('WPSR_PATH', plugin_dir_path(__FILE__));
17
18 if (!defined('WPSR_URL')) define('WPSR_URL', plugin_dir_url(__FILE__));
19
20
21 if (!defined('WP_SEO_REDIRECTION_OPTIONS')) {
22 define('WP_SEO_REDIRECTION_OPTIONS', 'wp-seo-redirection-group');
23 }
24
25 if (!defined('WP_SEO_REDIRECTION_VERSION')) {
26 define('WP_SEO_REDIRECTION_VERSION', 6.4);
27 }
28
29 $util = new clogica_util_1();
30 $util->init(WP_SEO_REDIRECTION_OPTIONS, __FILE__);
31
32 add_action('admin_enqueue_scripts', 'WPSR_header_code');
33 add_action('admin_menu', 'WPSR_admin_menu');
34 add_action('wp', 'WPSR_redirect', 1);
35 add_action('save_post', 'WPSR_get_post_redirection');
36 add_action('add_meta_boxes', 'WPSR_adding_custom_meta_boxes', 10, 3);
37 add_action('admin_head', 'WPSR_check_default_permalink');
38 add_action('plugins_loaded', 'WPSR_upgrade');
39
40 register_activation_hook(__FILE__, 'WPSR_upgrade');
41 register_uninstall_hook(__FILE__, 'WPSR_uninstall');
42
43 /////////////////////////////////////////////////////////////////////////
44
45
46 function WPSR_multiple_plugin_activate_trial()
47 {
48 global $wpdb;
49 if (is_multisite()) {
50 if (is_plugin_active_for_network(__FILE__)) {
51 $blogids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs");
52 foreach ($blogids as $blog_id) {
53 switch_to_blog($blog_id);
54 }
55 }
56 }
57 }
58
59 register_activation_hook(__FILE__, 'WPSR_multiple_plugin_activate_trial');
60
61
62 function WPSR_adding_custom_meta_boxes()
63 {
64 global $util;
65 if ($util->get_option_value('show_redirect_box') == '1') {
66
67 $screens = array('post', 'page');
68
69 foreach ($screens as $screen) {
70
71 add_meta_box(
72 'WPSR_meta_box',
73 __('SEO Redirection'),
74 'WPSR_render_meta_box',
75 $screen
76 );
77 }
78
79 }
80 }
81
82
83 function WPSR_render_meta_box($post)
84 {
85 global $wpdb, $table_prefix, $util;
86 $table_name = $table_prefix . 'WP_SEO_Redirection';
87
88 if (get_post_status() != 'auto-draft') {
89 $permalink = "";
90 if (in_array($post->post_status, array('draft', 'pending'))) {
91 list($permalink, $postname) = get_sample_permalink($post->ID);
92 $permalink = str_replace('%postname%', $postname, $permalink);
93
94 } else {
95
96 $permalink = get_permalink($post->ID);
97 }
98
99 $permalink = $util->make_relative_url(urldecode($permalink));
100
101 $postID = $post->ID;
102
103
104 $theurl = $wpdb->get_row($wpdb->prepare(" select redirect_to,redirect_from from $table_name where postID=%d ", $postID));
105
106 $urlredirect_to = '';
107 if ($wpdb->num_rows > 0)
108 $urlredirect_to = $theurl->redirect_to;
109
110 if ($urlredirect_to != '' && $theurl->redirect_from != $permalink) {
111 // the post_name field changed!
112 $wpdb->query($wpdb->prepare(" update $table_name set redirect_from=%s where postID=%d ", $permalink, $postID));
113 if ($util->get_option_value('reflect_modifications') == '1') {
114 $wpdb->query($wpdb->prepare(" update $table_name set redirect_to=%s where redirect_to=%s ", $permalink, $theurl->redirect_from));
115 $util->info_option_msg('<b>' . __("SEO Redirection", 'seo-redirection') . '</b>' . __('has detected a change in Permalink, this will be reflected to the redirection records!', 'seo-redirection'));
116 }
117 //-------------------------------------------
118 }
119
120 echo '
121 <table border="0" width="100%" cellpadding="2">
122 <tr>
123 <td width="99%"><input onchange="redirect_check_click()" type="checkbox" name="redirect_check" id="redirect_check" value="ON">
124 Redirect&nbsp;<font id="wp_seo_redirection_url_from_label" color="#008000">' . esc_html($permalink) . '</font><input type="hidden" ID="wp_seo_redirection_url_from" name="wp_seo_redirection_url_from" value="' . esc_attr($permalink) . '"></td>
125 </tr>
126 </table>
127 <div id="redirect_frame">
128 <table border="0" width="100%" cellpadding="2">
129 <tr>
130 <td>
131
132 <b>' . __(" Redirect to", 'seo-redirection') . '</b><input type="text" name="wp_seo_redirection_url" id="wp_seo_redirection_url" value="' . esc_attr($urlredirect_to) . '" size="62"></td>
133 </tr>
134 <tr>
135 <td>
136 <ul>
137 <li>' . __(" To make a redirection, put the", 'seo-redirection') . ' <b>' . __("URL", 'seo-redirection') . '</b> ' . __("in the text field above and then click the button ", 'seo-redirection') . '<b>' . __("Update", 'seo-redirection') . '</b>.</li>
138 <li>' . __("If you have a caching plugin installed, clear cache to reflect the
139 changes immediately.", 'seo-redirection') . '</li>
140
141 <li>' . __("To remove the redirection, just uncheck the check box above and then click the button", 'seo-redirection') . ' <b>' . __("Update", 'seo-redirection') . '</b>.</li>
142 </ul>
143 </td>
144 </tr>
145 </table>
146 </div>';
147
148 echo "
149
150 <script type='text/javascript'>
151 function WSR_check_status(x)
152 {
153
154 if(x==0)
155 {
156 document.getElementById('redirect_check').checked=false;
157 document.getElementById('redirect_frame').style.display = 'none';
158 document.getElementById('wp_seo_redirection_url').value='';
159 }else
160 {
161 document.getElementById('redirect_check').checked=true;
162 document.getElementById('redirect_frame').style.display= 'block';
163 }
164
165 }
166
167 function redirect_check_click()
168 {
169 if(document.getElementById('redirect_check').checked)
170 WSR_check_status(1);
171 else
172 WSR_check_status(0);
173 }
174 </script>
175 ";
176
177 if ($urlredirect_to == '')
178 echo "<script type='text/javascript'>WSR_check_status(0);</script>";
179 else
180 echo "<script type='text/javascript'>WSR_check_status(1);</script>";
181
182
183 } else {
184 echo __('You can not make a redirection for the new posts before saving them.', 'seo-redirection');
185 }
186 }
187
188
189 //--------------------------------------------------------------------------------------------
190
191 //---------------------------------------------------------------
192 // added 2/2/2020
193 function WPSR_get_site_404_page_path()
194 {
195 $url= str_ireplace("://", "", site_url());
196 $site_404_page = substr($url, stripos($url, "/"));
197
198 if (stripos($url, "/")=== FALSE || $site_404_page == "/")
199 $site_404_page = "/index.php?error=404";
200 else
201 $site_404_page = $site_404_page . "/index.php?error=404";
202
203 return $site_404_page;
204 }
205 //---------------------------------------------------------------
206 // updated 2/2/2020
207 function WPSR_check_default_permalink()
208 {
209 $file= get_home_path() . "/.htaccess";
210 $content="ErrorDocument 404 " . WPSR_get_site_404_page_path();
211
212 $marker_name="FRedirect_ErrorDocument";
213 $filestr ="";
214
215 if(file_exists($file)){
216 $f = @fopen( $file, 'r+' );
217 $filestr = @fread($f , filesize($file));
218 if (strpos($filestr , $marker_name) === false)
219 {
220 insert_with_markers( $file, $marker_name, $content );
221 }
222 }else
223 {
224 insert_with_markers( $file, $marker_name, $content );
225 }
226
227 }
228
229 //------------------------------------------------------------------------
230
231
232 /**
233 * Recursive sanitation for text or array
234 *
235 * @param $array_or_string (array|string)
236 * @since 0.1
237 * @return mixed
238 */
239 function WPSR_sanitize_text_or_array_field($array_or_string) {
240 if( is_string($array_or_string) ){
241 $array_or_string = sanitize_text_field($array_or_string);
242 }elseif( is_array($array_or_string) ){
243 foreach ( $array_or_string as $key => &$value ) {
244 if ( is_array( $value ) ) {
245 $value = WPSR_sanitize_text_or_array_field($value);
246 }
247 else {
248 $value = sanitize_text_field( $value );
249 }
250 }
251 }
252
253 return $array_or_string;
254 }
255
256
257 function WPSR_get_post_redirection($post_id)
258 {
259
260 global $wpdb, $util, $table_prefix;
261 $table_name = $table_prefix . 'WP_SEO_Redirection';
262
263 // Autosave
264 if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
265 return;
266 // AJAX
267 if (defined('DOING_AJAX') && DOING_AJAX)
268 return;
269 // Post revision
270 if (false !== wp_is_post_revision($post_id))
271 return;
272
273 $redirect_from = isset($_POST['wp_seo_redirection_url_from']) ? WPSR_sanitize_text_or_array_field($_POST['wp_seo_redirection_url_from']) : '';
274 $redirect_to = isset($_POST['wp_seo_redirection_url']) ? WPSR_sanitize_text_or_array_field($_POST['wp_seo_redirection_url']) : '';
275
276 if ($redirect_to != '') {
277
278
279 $wpdb->get_results($wpdb->prepare("select ID from $table_name where postID=%d ", $post_id));
280
281 if ($wpdb->num_rows > 0) {
282
283 $sql = $wpdb->prepare("update $table_name set redirect_to=%s,redirect_from=%s,redirect_type='301',url_type=2 where postID=%d", $redirect_to, $redirect_from, $post_id);
284 $wpdb->query($sql);
285
286 } else {
287 $wpdb->query($wpdb->prepare("delete from $table_name where redirect_from=%s", $redirect_from));
288 $sql = $wpdb->prepare("insert into $table_name(redirect_from,redirect_to,redirect_type,url_type,postID) values (%s,%s,'301',2,%d) ", $redirect_from, $redirect_to, $post_id);
289 $wpdb->query($sql);
290 }
291
292
293 } else {
294 $wpdb->query($wpdb->prepare("delete from $table_name where postID=%d", $post_id));
295 }
296
297 $SR_redirect_cache = new free_SR_redirect_cache();
298 $SR_redirect_cache->free_cache();
299 }
300
301
302 //-------------------------------------------------------------
303
304 function WPSR_log_404_redirection($link)
305 {
306 global $wpdb, $table_prefix, $util;
307 $table_name = $table_prefix . 'WP_SEO_404_links';
308
309 $referrer = $util->get_ref();
310 $ip = $util->get_visitor_IP();
311 $country = "";//$util->get_visitor_country();
312 $os = $util->get_visitor_OS();
313 $browser = $util->get_visitor_Browser();
314
315 if ($os != 'Unknown' || $browser != 'Unknown') {
316 $wpdb->query($wpdb->prepare(" insert IGNORE into $table_name(ctime,link,referrer,ip,country,os,browser) values(NOW(),%s,%s,%s,%s,%s,%s) ", $link, $referrer, $ip, $country, $os, $browser));
317 }
318 }
319
320
321 //-------------------------------------------------------------
322
323 function WPSR_log_redirection_history($rID, $postID, $rfrom, $rto, $rtype, $rsrc)
324 {
325 global $wpdb, $table_prefix, $util;
326 $SR_redirect_cache = new free_SR_redirect_cache();
327 $SR_redirect_cache->free_cache();
328 $table_name = $table_prefix . 'WP_SEO_Redirection_LOG';
329 $rfrom = esc_url($rfrom);
330 $referrer = $util->get_ref();
331 $ip = $util->get_visitor_IP();
332 $country = "";//$util->get_visitor_country();
333 $os = $util->get_visitor_OS();
334 $browser = $util->get_visitor_Browser();
335
336 $wpdb->query($wpdb->prepare(" insert into $table_name(rID,postID,rfrom,rto,rtype,rsrc,ctime,referrer,ip,country,os,browser) values(%d ,%d,%s,%s,%s,%s,NOW(),%s,%s,%s,%s,%s) ", $rID, $postID, $rfrom, $rto, $rtype, $rsrc, $referrer, $ip, $country, $os, $browser));
337
338 $limit = $util->get_option_value('history_limit');
339
340 $expdate = date('Y-n-j', time() - (intval($limit) * 24 * 60 * 60));
341 $wpdb->query("delete FROM $table_name WHERE date_format(date(ctime),'%Y-%m-%d') < date_format(date('$expdate'),'%Y-%m-%d')");
342
343
344 }
345
346 //-------------------------------------------------------------
347
348 function WPSR_make_redirect($redirect_to, $redirect_type, $redirect_from, $obj = '')
349 {
350 global $wpdb, $util,$table_prefix, $post;
351 if (is_admin()) {
352 return 0;
353 }
354 $SR_redirect_cache = new free_SR_redirect_cache();
355 if ($redirect_to == $redirect_from || !$util->is_valid_url($redirect_to))
356 return 0;
357
358 if($util->make_relative_url($redirect_from) == $util->make_relative_url($redirect_to))
359 return 0;
360
361 if(substr($redirect_from, -1) == "/" || substr($redirect_to, -1) == "/")
362 {
363 if(substr($redirect_from, -1) != "/")
364 {
365 if(($redirect_from . "/") == $redirect_to )
366 return 0;
367 }else
368 {
369 if(( $redirect_to . "/") == $redirect_from )
370 return 0;
371 }
372 }
373
374
375 if($util->make_relative_url($redirect_from) == $util->make_relative_url($redirect_to))
376 return 0;
377
378 if(is_object($obj))
379 {
380 if($obj->ID >0){
381 $table_name = $table_prefix . 'WP_SEO_Redirection';
382 $sql = "update " . $table_name . " set hits=hits+1, access_date= NOW() where ID='" . $obj->ID . "'";
383 $wpdb->query($sql);
384 }
385 }
386
387 if (is_object($obj) && $obj->redirect_to_type == 'Folder' && $obj->redirect_to_folder_settings == '2') {
388
389 if ($obj->redirect_from_type == 'Folder') {
390
391 if ($obj->redirect_from_folder_settings == '2' || $obj->redirect_from_folder_settings == '3') {
392 if (strlen($redirect_from) > strlen($obj->redirect_from)) {
393 $difference = substr($redirect_from, intval(strlen($obj->redirect_from) - strlen($redirect_from)));
394 $redirect_to = $redirect_to . $difference;
395 }
396 }
397
398 } else if ($obj->redirect_from_type == 'Regex') {
399 $page = substr(strrchr($redirect_from, "/"), 1);
400 $redirect_to = $redirect_to . '/' . $page;
401 }
402
403 }
404
405 $rID = 0;
406 $rsrc = '404';
407 $postID = 0;
408
409 if (is_object($obj)) {
410 $rID = $obj->ID;
411 $postID = $obj->postID;
412 if ($obj->url_type == 1)
413 $rsrc = 'Custom';
414 else if ($obj->url_type == 2)
415 $rsrc = 'Post';
416
417 }
418
419 if ($util->get_option_value('history_status') == '1') {
420
421 WPSR_log_redirection_history($rID, $postID, $redirect_from, $redirect_to, $redirect_type, $rsrc);
422 }
423
424 $redirect_to = $util->make_absolute_url($redirect_to);
425
426
427 if (is_singular()) {
428 //$SR_redirect_cache = new free_SR_redirect_cache();
429
430 $SR_redirect_cache->add_redirect($post->ID, 1, $redirect_from, $redirect_to, $redirect_type);
431 $SR_redirect_cache->free_cache();
432 }
433
434 if ($redirect_type == '301') {
435 header('HTTP/1.1 301 Moved Permanently');
436 header("Location: " . $redirect_to);
437 exit();
438 } else if ($redirect_type == '307') {
439 header('HTTP/1.0 307 Temporary Redirect');
440 header("Location: " . $redirect_to);
441 exit();
442 } else if ($redirect_type == '302') {
443 header("Location: " . $redirect_to);
444 exit();
445 }
446
447 }
448
449
450 //-------------------------------------------------------------
451
452 function WPSR_redirect()
453 {
454 global $wpdb, $post, $table_prefix, $util;
455
456
457 if ($util->get_option_value('plugin_status') != '0') { // if not disabled
458
459 // if disable for admin and the user is admin
460 if (current_user_can('manage_options') == 1 && $util->get_option_value('plugin_status') == 2) {
461 // nothing
462
463 } else {
464
465 $table_name = $table_prefix . 'WP_SEO_Redirection';
466 $permalink = urldecode($util->get_current_relative_url());
467 if (substr($permalink, 0, 1) == ":") {
468 $first_slash = stripos($permalink, "/");
469 $permalink = substr($permalink, $first_slash, strlen($permalink) - $first_slash);
470 }
471 $permalink_alternative = "";
472 if (substr($permalink, -1) == '/') {
473 $permalink_alternative = substr($permalink, 0, intval(strlen($permalink) - 1));
474 } else {
475 $permalink_alternative = $permalink . '/';
476 }
477
478 $post_cache_result = "";
479 $SR_redirect_cache = new free_SR_redirect_cache();
480 if (is_singular()) {
481 $post_cache_result = $SR_redirect_cache->redirect_cached($post->ID);
482 }
483 if ($post_cache_result == 'not_redirected') {
484 return 0;
485 }
486
487 $permalink_options = $wpdb->prepare("( redirect_from = %s OR redirect_from = %s)", $permalink, $permalink_alternative);
488
489 $permalink_regex_options = $wpdb->prepare("(%s regexp regex or %s regexp regex )", $permalink, $permalink_alternative);
490
491 if (($util->get_option_value('redirect_control_panel') != '1') || ($util->get_option_value('redirect_control_panel') == '1' && !preg_match('/^' . str_replace('/', '\/', get_admin_url()) . '/i', $permalink) && !preg_match('/^' . str_replace('/', '\/', site_url()) . '\/wp-login.php/i', $permalink))) {
492
493
494 $theurl = $wpdb->get_row(" select * from $table_name where enabled=1 and regex='' and $permalink_options ");
495 if ($wpdb->num_rows > 0 && $theurl->redirect_to != '') {
496 WPSR_make_redirect($theurl->redirect_to, $theurl->redirect_type, $permalink, $theurl);
497 }
498
499 $theurl = $wpdb->get_row(" select * from $table_name where enabled=1 and regex<>'' and $permalink_regex_options order by LENGTH(regex) desc ");
500 if ($wpdb->num_rows > 0 && $theurl->redirect_to != '') {
501 WPSR_make_redirect($theurl->redirect_to, $theurl->redirect_type, $permalink, $theurl);
502 }
503
504
505 if (is_404()) {
506
507 if ($util->get_option_value('p404_discovery_status') == '1') {
508 WPSR_log_404_redirection($permalink);
509 }
510
511 $options = $util->get_my_options();
512 if ($options['p404_status'] == '1') {
513
514 WPSR_make_redirect($options['p404_redirect_to'], '301', $permalink);
515
516 }
517 }
518 }
519
520 if (is_singular() && $post_cache_result == 'not_found') {
521 $SR_redirect_cache->add_redirect($post->ID, 0, '', '', 0);
522 }
523
524 }
525 }
526 }
527
528 //---------------------------------------------------------------
529
530 function WPSR_header_code()
531 {
532 //wp_enqueue_script('ajaxcustomvar');
533 /* condition removed to display import popup on Import button(admin notioce message) click on any admin page*/
534 if (is_admin() && array_key_exists('page', $_GET) && $_GET['page'] == 'seo-redirection.php')
535
536 {
537
538 wp_register_style('c_admin_css_common', plugins_url() . '/' . basename(dirname(__FILE__)) . '/common/' . "style.css");
539 wp_enqueue_style('sweetalert', plugins_url() . '/' . basename(dirname(__FILE__)) . '/common/' . "sweetalert.css");
540 wp_register_style('c_admin_css_custom', plugins_url() . '/' . basename(dirname(__FILE__)) . '/custom/' . "style.css");
541 wp_enqueue_script('jquery');
542 wp_localize_script('jquery', 'seoredirection', array('ajax_url' => admin_url('admin-ajax.php'), 'msg' => ""));
543 wp_enqueue_style('c_admin_css_common');
544 wp_enqueue_style('c_admin_css_custom');
545
546 wp_enqueue_script('custom', plugins_url() . '/' . basename(dirname(__FILE__)) . '/common/js/' . 'bootstrap.min.js', array('jquery'), false, true);
547 wp_enqueue_script('customJS', plugins_url() . '/' . basename(dirname(__FILE__)) . '/common/' . "customJs.js", array('jquery'), false, true);
548 wp_enqueue_script('sweetalert', plugins_url() . '/' . basename(dirname(__FILE__)) . '/common/js/' . "sweetalert.min.js", array('jquery'), false, true);
549
550 wp_enqueue_style('bootstrap', plugins_url() . '/' . basename(dirname(__FILE__)) . '/common/' . "bootstrap.css");
551
552
553 }
554 }
555
556 //---------------------------------------------------------------
557
558 add_action("wp_ajax_customAddUpdate", "WPSR_customAddUpdate_callback");
559
560 function WPSR_customAddUpdate_callback()
561 {
562 global $wpdb, $table_prefix, $util;
563 $table_name = $table_prefix . 'WP_SEO_Redirection';
564 $table_name_404 = $table_prefix . 'WP_SEO_404_links';
565 parse_str($_POST['formData'], $_POST);
566 $nonce = "";
567 if (isset($_POST['_wpnonce']))
568 $nonce = WPSR_sanitize_text_or_array_field($_POST['_wpnonce']);
569 $data = array();
570 $data['error_string'] = array();
571 $data['inputerror'] = array();
572 $data['bool'] = TRUE;
573 if (trim($_POST['redirect_from']) == '') {
574 $data['inputerror'][] = 'redirect_from';
575 $data['error_string'][] = __("You must input the 'Redirect From' URL", "seo-redirection");
576 $data['bool'] = FALSE;
577 }
578
579 $redirect_from = isset($_POST['redirect_from']) ? WPSR_sanitize_text_or_array_field($_POST['redirect_from']) : '';
580
581 $wpdb->get_results(" select ID from $table_name where redirect_from='" . trim($redirect_from) . "' ");
582 if($wpdb->num_rows > 0){
583 $data['inputerror'][] = 'redirect_from';
584 $data['error_string'][] = __("This 'Redirect From' value already exists in database!", "seo-redirection");
585 $data['bool'] = FALSE;
586 }
587
588
589 // elseif (!preg_match( '/((http|https)\:\/\/)?[a-zA-Z0-9\.\/\?\:@\-_=#]+\.([a-zA-Z0-9\&\.\/\?\:@\-_=#])*/', $_POST['redirect_from'])) {
590 // $data['inputerror'][] = 'redirect_from';
591 // $data['error_string'][] = __("Invalid redirect from target URL!",'seo-redirection');
592 // $data['bool'] = FALSE;
593 // }
594 if (trim($_POST['redirect_to']) == '') {
595 $data['inputerror'][] = 'redirect_to';
596 $data['error_string'][] = __("You must input the 'Redirect To' URL", "seo-redirection");
597 $data['bool'] = FALSE;
598 } elseif ($_POST['edit_exist'] == '' && substr (strtolower ($_POST['redirect_to']), 0, 4)!="http" && substr (strtolower ($_POST['redirect_to']), 0, 1)!="/" ) {
599 $data['inputerror'][] = 'redirect_to';
600 $data['error_string'][] = __("Invalid redirect target URL!", 'seo-redirection');
601 $data['bool'] = FALSE;
602 }
603 if ($data['bool'] === FALSE) {
604 echo json_encode($data);
605 exit();
606 } else {
607 if ($_POST['redirect_from'] != '' && wp_verify_nonce($nonce, 'seoredirection')) {
608
609
610 $redirect_from = isset($_POST['redirect_from']) ? WPSR_sanitize_text_or_array_field($_POST['redirect_from']) : '';
611 $redirect_to = isset($_POST['redirect_to']) ? WPSR_sanitize_text_or_array_field($_POST['redirect_to']) : '';
612
613
614 $redirect_from = urldecode($util->make_relative_url($redirect_from));
615
616 $redirect_to = $util->make_relative_url($redirect_to);
617 $redirect_type = WPSR_sanitize_text_or_array_field($_POST['redirect_type']);
618
619 $redirect_from_type = WPSR_sanitize_text_or_array_field($_POST['redirect_from_type']);
620 $redirect_from_folder_settings = WPSR_sanitize_text_or_array_field($_POST['redirect_from_folder_settings']);
621 $redirect_from_subfolders = WPSR_sanitize_text_or_array_field($_POST['redirect_from_subfolders']);
622
623 $redirect_to_type = WPSR_sanitize_text_or_array_field($_POST['redirect_to_type']);
624 $redirect_to_folder_settings = WPSR_sanitize_text_or_array_field($_POST['redirect_to_folder_settings']);
625
626 $enabled = WPSR_sanitize_text_or_array_field($_POST['enabled']);
627
628 $regex = "";
629
630 if ($redirect_from_type == 'Folder') {
631
632 if (substr($redirect_from, -1) != '/')
633 $redirect_from = $redirect_from . '/';
634
635 if ($redirect_from_folder_settings == 2) {
636 if ($redirect_from_subfolders == 0) {
637 $regex = '^' . $util->regex_prepare($redirect_from) . '.*';;
638 } else {
639 $regex = '^' . $util->regex_prepare($redirect_from) . '[^/]*$';
640 }
641 } else if ($redirect_from_folder_settings == 3) {
642 if ($redirect_from_subfolders == 0) {
643 $regex = '^' . $util->regex_prepare($redirect_from) . '.+';
644 } else {
645 $regex = '^' . $util->regex_prepare($redirect_from) . '[^/]+$';
646 }
647 }
648
649 } else if ($redirect_from_type == 'Regex') {
650 $regex = $redirect_from;
651 }
652
653 if ($redirect_from_type == 'Page' || $redirect_from_type == 'Regex') {
654 $redirect_from_folder_settings = "";
655 $redirect_from_subfolders = "";
656 }
657
658 if ($redirect_to_type == 'Page') {
659 $redirect_to_folder_settings = "";
660 }
661
662 if ($redirect_to_type == 'Folder') {
663 if (substr($redirect_to, -1) != '/')
664 $redirect_to = $redirect_to . '/';
665 }
666
667
668 if ($_POST['add_new'] != '') {
669
670 $theurl = $wpdb->get_row($wpdb->prepare(" select count(ID) as cnt from $table_name where redirect_from=%s ", $redirect_from));
671
672 if ($theurl->cnt > 0) {
673 $msg = __("This URL", 'seo-redirection') . " <b>'$redirect_from'</b>" . __("is added previously!", 'seo-redirection');
674 echo json_encode(array('status' => 'error', 'msg' => $msg));
675 // $util->failure_option_msg(__("This URL",'seo-redirection')." <b>'$redirect_from'</b>". __("is added previously!",'seo-redirection'));
676 } else {
677
678
679 if ($redirect_from == '' || $redirect_to == '' || $redirect_type == '') {
680 $util->failure_option_msg(__('Please input all required fields!', 'seo-redirection'));
681 } else {
682
683 $wpdb->insert($table_name, array(
684 'redirect_from' => $redirect_from,
685 'redirect_to' => $redirect_to,
686 'redirect_type' => $redirect_type,
687 'url_type' => 1,
688 'redirect_from_type' => $redirect_from_type,
689 'redirect_from_folder_settings' => $redirect_from_folder_settings,
690 'redirect_from_subfolders' => $redirect_from_subfolders,
691 'redirect_to_type' => $redirect_to_type,
692 'redirect_to_folder_settings' => $redirect_to_folder_settings,
693 'regex' => $regex,
694 'enabled' => $enabled
695
696 ));
697
698 $wpdb->query($wpdb->prepare(" delete from $table_name_404 where link=%s ", $redirect_from));
699 $SR_redirect_cache = new free_SR_redirect_cache();
700 $SR_redirect_cache->free_cache();
701 $msg = "Redirection Added Successfully";
702 echo json_encode(array('status' => 'success', 'msg' => $msg, 'url' => admin_url('options-general.php?page=seo-redirection.php')));
703 die;
704 }
705
706 }
707 } else if ($_POST['edit_exist'] != '') {
708
709 $edit = WPSR_sanitize_text_or_array_field($_POST['edit']);
710
711 if ($redirect_from == '' || $redirect_to == '' || $redirect_type == '') {
712 $util->failure_option_msg('Please input all required fields!');
713 } else {
714
715 $wpdb->query($wpdb->prepare("update $table_name set redirect_from=%s,redirect_to=%s,redirect_type=%s,redirect_from_type=%s ,redirect_from_folder_settings=%d,redirect_from_subfolders=%d ,redirect_to_type=%s ,redirect_to_folder_settings=%d ,regex=%s,enabled=%s where ID=%d ", $redirect_from, $redirect_to, $redirect_type, $redirect_from_type, $redirect_from_folder_settings, $redirect_from_subfolders, $redirect_to_type, $redirect_to_folder_settings, $regex, $enabled, $edit));
716
717 $SR_redirect_cache = new free_SR_redirect_cache();
718 $SR_redirect_cache->free_cache();
719 }
720 $msg = "Redirection Update Successfully";
721 echo json_encode(array('status' => 'success', 'msg' => $msg, 'url' => admin_url('options-general.php?page=seo-redirection.php')));
722 die;
723
724 }
725
726 if ($util->there_is_cache() != '')
727 $util->info_option_msg(__("You have a cache plugin installed", 'seo-redirection') . " <b>'" . $util->there_is_cache() . "'</b>, " . __("you have to clear cache after any changes to get the changes reflected immediately! ", 'seo-redirection'));
728
729 }
730
731 }
732
733
734 die;
735 }
736
737 add_action("wp_ajax_customUpdateRec", "WPSR_customUpdateRec_callback");
738 function WPSR_customUpdateRec_callback()
739 {
740
741
742 global $wpdb, $table_prefix, $util;
743
744 $table_name = $table_prefix . 'WP_SEO_Redirection';
745 $table_name_404 = $table_prefix . 'WP_SEO_404_links';
746
747 $myid = (int) $_POST['ID'];
748 $item = $wpdb->get_row($wpdb->prepare(" select * from $table_name where ID=%d ", $myid));
749
750 if ($wpdb->num_rows == 0) {
751 echo json_encode(array('status' => 'error', 'msg' => __("Sorry, this redirect rule is not found, it may deleted by the user!", 'seo-redirection')));
752 die;
753 }
754
755
756 $data = array(
757 "redirect_from" => $item->redirect_from,
758 "redirect_to" => $item->redirect_to,
759 "redirect_type" => $item->redirect_type,
760
761 "redirect_from_type" => $item->redirect_from_type,
762 "redirect_from_folder_settings" => $item->redirect_from_folder_settings,
763 "redirect_from_subfolders" => $item->redirect_from_subfolders,
764
765 "redirect_to_type" => $item->redirect_to_type,
766 "redirect_to_folder_settings" => $item->redirect_to_folder_settings,
767
768 "enabled" => $item->enabled
769 );
770 echo json_encode(array('status' => 'suucess', 'rec' => $data));
771 die;
772 }
773
774 add_action("wp_ajax_importFromRedirection","WPSR_importFromRedirection_callback");
775 function WPSR_importFromRedirection_callback()
776 {
777 global $wpdb;
778
779 $plugins = get_option( 'active_plugins', array() );
780 $found = false;
781 foreach ( $plugins as $plugin )
782 {
783 if ( strpos( strval($plugin), 'redirection.php' ) == true && strpos( strval($plugin), 'seo-redirection.php' ) == FALSE )
784 {
785 if(isset($_REQUEST['offset']))
786 {
787 $offset = WPSR_sanitize_text_or_array_field($_REQUEST['offset']);
788 $count = 0;
789 $table_name = $wpdb->prefix . 'WP_SEO_Redirection';
790 $table_name_404 = $wpdb->prefix . 'WP_SEO_404_links';
791
792
793 $table_name_ = $wpdb->prefix . 'redirection_items';
794 if (strtolower($wpdb->get_var("show tables like '$table_name_'")) == strtolower($table_name_))
795 {
796
797
798 $redirects = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}redirection_items order by id limit ".$offset.",1" );
799 if($redirects)
800 {
801 //print_r($redirects);
802 $udtcnt = 0;
803 $inscnt=0;
804 foreach ( $redirects as $redirect )
805 {
806 $redirect_from = stripslashes($redirect->url);
807 $redirect_from_slash = ltrim(stripslashes($redirect->url),'/');
808 if($redirect->status=="enabled")
809 $enabled =1;
810 else
811 $enabled =0;
812
813 $group_id = 1;
814 if($redirect->group_id == "1")
815 {
816 $group_id = 2;
817 }
818 else if($redirect->group_id == "2")
819 {
820 $group_id = 3;
821 }
822
823 $redirect_from_type = $redirect->regex ? 'Regex' : 'Page';
824 $redirect_to_type = $redirect->regex ? 'Regex' : 'Page';
825 $regexp = $redirect->regex ? $redirect->url : '';
826 $redirectID = $wpdb->get_var($wpdb->prepare("select ID from $table_name where redirect_from=%s or redirect_from=%s", $redirect_from,$redirect_from_slash));
827
828 if ($redirectID > 0)
829 {
830
831 $update = $wpdb->query($wpdb->prepare("update $table_name set redirect_from=%s,redirect_to=%s,redirect_type=%d, redirect_from_type=%s, redirect_to_type=%s,regex=%s,enabled=%d, grpID=%d,import_flag=%d, redirect_from_folder_settings=%d,redirect_from_subfolders=%d,redirect_to_folder_settings=%d where ID=%d", $redirect_from, stripslashes($redirect->action_data),intval( $redirect->action_code, 10),$redirect_from_type,$redirect_to_type,$regexp,$enabled,$group_id,1,0,0,0, $redirectID));
832 if($update)
833 {
834 $udtcnt++;
835 }
836 }
837 else
838 {
839 $wpdb->insert($table_name, array(
840 'redirect_from' => stripslashes($redirect->url),
841 'redirect_to' => stripslashes($redirect->action_data),
842 'redirect_type' =>intval( $redirect->action_code, 10),
843 'url_type' => 1,
844 'redirect_from_type' => $redirect->regex ? 'Regex' : 'Page',
845 'redirect_from_folder_settings' => 0,
846 'redirect_from_subfolders' => 0,
847 'redirect_to_type' => $redirect->regex ? 'Regex' : 'Page',
848 'redirect_to_folder_settings' => 0,
849 'regex' => $redirect->regex ? $redirect->url : '',
850 'enabled' => $enabled,
851 'grpID' => $group_id,
852 'import_flag' =>1,
853 ));
854
855 $inscnt++;
856 $wpdb->query($wpdb->prepare(" delete from $table_name_404 where link=%s ", $redirect_from));
857 $SR_redirect_cache = new free_SR_redirect_cache();
858 $SR_redirect_cache->free_cache();
859 }
860 }
861 $msg = __('Total ','seo-redirection').'<span class="insert_cnt">'.$inscnt.'</span>'.__(' record(s) inserted and ','seo-redirection').'<span class="update_cnt">'.$udtcnt.'</span>'.__(' record(s) updated.','seo-redirection');
862 echo json_encode(array('status' => 'success','updateCnt'=>$udtcnt,'insertCnt'=>$inscnt,'msg'=>$msg));
863 }
864 else
865 {
866 $msg = __('No redirects found to import.','seo-redirection');
867 echo json_encode(array('status' => 'error','msg'=>$msg));
868 }
869 }
870 }
871 else
872 {
873 $msg = __('Something went wrong plase try again.','seo-redirection');
874 echo json_encode(array('status' => 'error','msg'=>$msg));
875 }
876 break;
877 }
878
879 }
880 exit;
881 }
882
883 function WPSR_admin_menu()
884 {
885 add_options_page('SEO Redirection', 'SEO Redirection', 'manage_options', basename(__FILE__), 'WPSR_options_menu');
886 }
887
888 //---------------------------------------------------------------
889 function WPSR_options_menu()
890 {
891 global $util;
892
893 if (!current_user_can('manage_options')) {
894 wp_die(__('You do not have sufficient permissions to access this page.', 'seo-redirection'));
895 }
896
897
898 if ($util->get_option_value('plugin_status') == '0') {
899 $util->info_option_msg(__('SEO Redirection is disabled now, you can go to option tab and enable it!', 'seo-redirection'));
900 } else if ($util->get_option_value('plugin_status') == '2') {
901 $util->info_option_msg(__('SEO Redirection is', 'seo-redirection') . ' <b>' . __('disabled for admin', 'seo-redirection') . '</b>' . __(' only, you can go to option tab and enable it!', 'seo-redirection'));
902 }
903 $total_404_errors = (WPSR_Get_total_404() > 10) ? __('You have', 'seo-redirection') . ' <b style="color:red; background-color:yellow; padding:3px;"> too many </b>' . __(' broken link (404 links)', 'seo-redirection') . ', <br>' : '';
904
905
906 echo '<div class="wrap"><h2>' . __("SEO Redirection Free", 'seo-redirection') . '</h2><b>' . __('Upgrade to', 'seo-redirection') . ' <a target="_blank" onclick="swal.clickConfirm();" href="http://www.clogica.com/product/seo-redirection-premium-wordpress-plugin">' . __("Pro Version", "seo-redirection") . '</a>' . __(" to manage 404 errors and empower your site SEO", "seo-redirection") . '&nbsp;&nbsp;&nbsp;<strong style="color:yellow; background-color:red; padding:3px;"> ' . __("NOW 50% OFF ", 'seo-redirection') . '</strong></b><br/><br/>';
907
908 /*if ($total_404_errors != '') {
909
910 ?>
911 <script type="text/javascript">
912
913 seoredirection.msg = '<?php echo $total_404_errors . '<div class="wrap" style="font-weight:normal; line-height:30px">' . __('Upgrade to', 'seo-redirection') . ' <a target="_blank" href="http://www.clogica.com/product/seo-redirection-premium-wordpress-plugin">' . __("Pro Version", "seo-redirection") . '</a>' . __(" to manage 404 errors and empower your site SEO", "seo-redirection") ?></div>';
914
915 </script>
916
917 <?php
918 }*/
919
920 if (is_multisite()) {
921
922 echo '<div class="error" id="message"><p></p><div class="warning_icon"></div>' . __('This version does not support Multisite WordPress installation, you may face troubles like losing redirects when adding new sites to your network, the premium version supports multisite well', 'seo-redirection') . '(<a target="_blank" href="http://www.clogica.com/product/seo-redirection-premium-wordpress-plugin">
923 http://www.clogica.com/product/seo-redirection-premium-wordpress-plugin</a>) <p></p></div>';
924
925 }
926
927 $mytabs = new phptab();
928
929 $mytabs->set_ignore_parameter(array('del', 'search', 'page_num', 'add', 'edit', 'page404', 'do_404_del'));
930 $mytabs->add_file_tab('cutom', __('Custom Redirects', 'seo-redirection'), 'option_page_custome_redirection.php', 'file');
931 $mytabs->add_file_tab('posts', __('Post Redirects', 'seo-redirection'), 'option_page_post_redirection_list.php', 'file');
932 $mytabs->add_file_tab('history', __('History', 'seo-redirection'), 'option_page_history.php', 'file');
933 $mytabs->add_file_tab('export_import', __('Export/Import', 'seo-redirection'), 'option_export_import.php', 'file');
934 $mytabs->add_file_tab('404','<span style="color:red;"><b>404 Errors</b></span>','option_page_404.php','file');
935 $mytabs->add_file_tab('goptions', __('Options', 'seo-redirection'), 'option_page_goptions.php', 'file');
936 $mytabs->add_file_tab('help', '<span style="color:green;"><b>' . __('Help', 'seo-redirection') . '</b></span>', 'help.php', 'file');
937 $mytabs->add_file_tab('premium', '<span style="color:brown;"><b>&#9658; ' . __('Premium Features', 'seo-redirection') . '</b></span>', 'premium.php', 'file');
938 $mytabs->run();
939
940 }
941
942
943 function WPSR_upgrade()
944 {
945
946 $util = new clogica_util_1();
947 $util->init(WP_SEO_REDIRECTION_OPTIONS, __FILE__);
948
949 if ($util->get_option_value('plugin_version') != WP_SEO_REDIRECTION_VERSION) {
950 WPSR_install();
951 $util->update_option('plugin_version', WP_SEO_REDIRECTION_VERSION);
952 }
953 }
954
955 //-----------------------------------------------------
956 function WPSR_install()
957 {
958 global $wpdb, $table_prefix;
959
960 $util = new clogica_util_1();
961 $util->init(WP_SEO_REDIRECTION_OPTIONS, __FILE__);
962
963 $options = get_option(WP_SEO_REDIRECTION_OPTIONS);
964 if (!is_array($options)) {
965 add_option(WP_SEO_REDIRECTION_OPTIONS);
966 $options = array();
967 }
968
969
970 if (!array_key_exists('plugin_status', $options))
971 $options['plugin_status'] = '1';
972
973 if (!array_key_exists('ip_logging_status', $options))
974 $options['ip_logging_status'] = '1';
975
976 if (!array_key_exists('redirection_base', $options))
977 $options['redirection_base'] = site_url();
978
979 if (!array_key_exists('redirect_control_panel', $options))
980 $options['redirect_control_panel'] = '1';
981
982 if (!array_key_exists('show_redirect_box', $options))
983 $options['show_redirect_box'] = '1';
984
985 if (!array_key_exists('reflect_modifications', $options))
986 $options['reflect_modifications'] = '1';
987
988 if (!array_key_exists('history_status', $options))
989 $options['history_status'] = '1';
990
991 if (!array_key_exists('history_limit', $options))
992 $options['history_limit'] = '30';
993
994 if (!array_key_exists('p404_discovery_status', $options))
995 $options['p404_discovery_status'] = '1';
996
997 if (!array_key_exists('p404_redirect_to', $options))
998 $options['p404_redirect_to'] = site_url();
999
1000 if (!array_key_exists('p404_status', $options))
1001 $options['p404_status'] = '2';
1002
1003 if (!array_key_exists('keep_data', $options))
1004 $options['keep_data'] = '1';
1005
1006 update_option(WP_SEO_REDIRECTION_OPTIONS, $options);
1007
1008
1009 $table_name = $table_prefix . 'WP_SEO_Redirection';
1010 if (strtolower($wpdb->get_var("show tables like '$table_name'")) != strtolower($table_name)) {
1011 $sql = "CREATE TABLE IF NOT EXISTS `$table_name` (
1012 `ID` int(11) unsigned NOT NULL AUTO_INCREMENT,
1013 `enabled` int(1) NOT NULL DEFAULT '1',
1014 `redirect_from` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1015 `redirect_from_type` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1016 `redirect_from_folder_settings` int(1) NOT NULL,
1017 `redirect_from_subfolders` int(1) NOT NULL DEFAULT '1',
1018 `redirect_to` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1019 `redirect_to_type` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1020 `redirect_to_folder_settings` int(1) NOT NULL DEFAULT '1',
1021 `regex` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1022 `redirect_type` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1023 `url_type` int(2) NOT NULL DEFAULT 1,
1024 `postID` int(11) unsigned DEFAULT NULL,
1025 `import_flag` tinyint(1) NOT NULL DEFAULT 0,
1026 `hits` int(11) unsigned NOT NULL DEFAULT 0,
1027 `access_date` datetime DEFAULT NULL,
1028 PRIMARY KEY (`ID`),
1029 UNIQUE KEY `redirect_from` (`redirect_from`)
1030 )ENGINE = MyISAM ;";
1031 $wpdb->query($sql);
1032
1033
1034 } else {
1035 //check if Innodb convert it to myisam.
1036 $status = $wpdb->get_row("SHOW TABLE STATUS WHERE Name = '$table_name'");
1037 if ($status->Engine == 'InnoDB') {
1038 $wpdb->query("alter table $table_name engine = MyISAM;");
1039 }
1040
1041 /* add column for import flag */
1042 $column_data = $wpdb->get_row("SHOW COLUMNS FROM $table_name LIKE 'import_flag'");
1043
1044 if (!$column_data)
1045 {
1046 $wpdb->query("ALTER TABLE $table_name ADD COLUMN import_flag tinyint(1) DEFAULT 0");
1047 }
1048
1049 // if the table exists
1050 $redirects = $wpdb->get_results(" select redirect_from,redirect_to,ID from $table_name; ");
1051 foreach ($redirects as $redirect) {
1052 $redirect_from = $util->make_relative_url($redirect->redirect_from);
1053 $redirect_to = $util->make_relative_url($redirect->redirect_to);
1054 $ID = $redirect->ID;
1055 $wpdb->query($wpdb->prepare(" update $table_name set redirect_from=%s,redirect_to=%s where ID=%d", $redirect_from, $redirect_to, $ID));
1056 }
1057
1058 // Fix add blog field if not exist.
1059 if($wpdb->get_var(" SELECT count(*) as cnt FROM INFORMATION_SCHEMA.COLUMNS
1060 WHERE TABLE_NAME = '$table_name'
1061 AND table_schema = DATABASE()
1062 AND COLUMN_NAME = 'hits' ") == '0') {
1063
1064 $sql="
1065 ALTER TABLE $table_name
1066 ADD COLUMN `hits` int(11) unsigned NOT NULL DEFAULT 0,
1067 ADD COLUMN `access_date` datetime DEFAULT NULL;
1068 ";
1069
1070 $wpdb->query($sql);
1071 }
1072
1073
1074 }
1075
1076 $table_name = $table_prefix . 'WP_SEO_Cache';
1077 if (strtolower($wpdb->get_var("show tables like '$table_name'")) != strtolower($table_name)) {
1078 $sql = "CREATE TABLE IF NOT EXISTS `$table_name` (
1079 `ID` int(11) unsigned NOT NULL,
1080 `is_redirected` int(1) unsigned NOT NULL,
1081 `redirect_from` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1082 `redirect_to` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1083 `redirect_type` int(3) unsigned NOT NULL DEFAULT 301,
1084 PRIMARY KEY (`ID`)
1085 ) ENGINE = MyISAM ;
1086 ";
1087 $wpdb->query($sql);
1088 } else {
1089 //check if Innodb convert it to myisam.
1090 $status = $wpdb->get_row("SHOW TABLE STATUS WHERE Name = '$table_name'");
1091 if ($status->Engine == 'InnoDB') {
1092 $wpdb->query("alter table $table_name engine = MyISAM;");
1093 }
1094 }
1095
1096
1097 $res = $wpdb->get_var(" SELECT count(*) as cnt FROM INFORMATION_SCHEMA.COLUMNS
1098 WHERE TABLE_NAME = '$table_name'
1099 AND table_schema = DATABASE()
1100 AND COLUMN_NAME = 'redirect_from' ");
1101
1102 if ($res == '0') {
1103
1104 $sql = "
1105 ALTER TABLE $table_name
1106 ADD COLUMN `redirect_from` varchar(255) COLLATE utf8_unicode_ci NOT NULL;
1107 ";
1108 $wpdb->query($sql);
1109 }
1110
1111
1112 $table_name = $table_prefix . 'WP_SEO_404_links';
1113 if (strtolower($wpdb->get_var("show tables like '$table_name'")) != strtolower($table_name)) {
1114 $sql = "CREATE TABLE IF NOT EXISTS `$table_name` (
1115 `ID` int(11) unsigned NOT NULL AUTO_INCREMENT,
1116 `ctime` datetime NOT NULL,
1117 `link` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1118 `referrer` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1119 `ip` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
1120 `country` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
1121 `os` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
1122 `browser` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
1123 PRIMARY KEY (`ID`),
1124 UNIQUE KEY `link` (`link`)
1125 ) ENGINE = MyISAM ;
1126 ";
1127 $wpdb->query($sql);
1128 } else {
1129 //check if Innodb convert it to myisam.
1130 $status = $wpdb->get_row("SHOW TABLE STATUS WHERE Name = '$table_name'");
1131 if ($status->Engine == 'InnoDB') {
1132 $wpdb->query("alter table $table_name engine = MyISAM;");
1133 }
1134 }
1135
1136
1137 $table_name = $table_prefix . 'WP_SEO_Redirection_LOG';
1138 if (strtolower($wpdb->get_var("show tables like '$table_name'")) != strtolower($table_name)) {
1139 $sql = "CREATE TABLE IF NOT EXISTS `$table_name` (
1140 `ID` int(11) unsigned NOT NULL AUTO_INCREMENT,
1141 `rID` int(11) unsigned DEFAULT NULL,
1142 `postID` int(11) unsigned DEFAULT NULL,
1143 `ctime` datetime NOT NULL,
1144 `rfrom` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1145 `rto` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1146 `rtype` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1147 `rsrc` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
1148 `referrer` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1149 `ip` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
1150 `country` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
1151 `os` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
1152 `browser` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
1153 PRIMARY KEY (`ID`)
1154 ) ENGINE = MyISAM ;
1155 ";
1156
1157 $wpdb->query($sql);
1158 } else {
1159 //check if Innodb convert it to myisam.
1160 $status = $wpdb->get_row("SHOW TABLE STATUS WHERE Name = '$table_name'");
1161 if ($status->Engine == 'InnoDB') {
1162 $wpdb->query("alter table $table_name engine = MyISAM;");
1163 }
1164
1165 $res = $wpdb->get_var(" SELECT count(*) as cnt FROM INFORMATION_SCHEMA.COLUMNS
1166 WHERE TABLE_NAME = '$table_name'
1167 AND table_schema = DATABASE()
1168 AND COLUMN_NAME = 'postID' ");
1169
1170 if ($res == '0') {
1171
1172 $sql = "
1173 ALTER TABLE $table_name
1174 ADD COLUMN `postID` int(11) unsigned DEFAULT NULL;
1175 ";
1176 $wpdb->query($sql);
1177 }
1178
1179
1180 }
1181
1182 }
1183
1184
1185 //---------------------------------------------------------------
1186
1187
1188 function WPSR_uninstall()
1189 {
1190 global $wpdb, $table_prefix;
1191
1192 $util = new clogica_util_1();
1193 $util->init(WP_SEO_REDIRECTION_OPTIONS, __FILE__);
1194
1195
1196 if ($util->get_option_value('keep_data') != '1') {
1197
1198 $table_name = $table_prefix . 'WP_SEO_Redirection';
1199 $wpdb->query($wpdb->prepare(" DROP TABLE %s ", $table_name));
1200
1201 $table_name = $table_prefix . 'WP_SEO_Cache';
1202 $wpdb->query($wpdb->prepare(" DROP TABLE %s ", $table_name));
1203
1204 $table_name = $table_prefix . 'WP_SEO_404_links';
1205 $wpdb->query($wpdb->prepare(" DROP TABLE %s ", $table_name));
1206
1207 $table_name = $table_prefix . 'WP_SEO_Redirection_LOG';
1208 $wpdb->query($wpdb->prepare(" DROP TABLE %s ", $table_name));
1209
1210
1211 $util->delete_my_options();
1212 }
1213
1214
1215 }
1216
1217
1218 //---------------------------------------------------------------
1219
1220 function WPSR_HideMessageAjaxFunction()
1221 {
1222 add_option('nsr_upgrade_message', 'yes');
1223 }
1224
1225 //---------------------------------------------------------------
1226
1227 /* display import from redirection plugin in admin notice */
1228 function WPSR_admin_notice_callback() {
1229 global $wpdb;
1230 global $current_user;
1231 $plugins = get_option( 'active_plugins', array() );
1232 $found = false;
1233 $user_id = $current_user->ID;
1234 $val = get_user_meta($user_id, 'sr_notice_dismissed',true);
1235
1236 foreach ( $plugins as $plugin )
1237 {
1238 if ( strpos( strval($plugin), 'redirection.php' ) == true && $val != 1 && strpos( strval($plugin), 'seo-redirection.php' ) == FALSE )
1239 {
1240 $found = true;
1241 //$total = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_items");
1242 $total = WPSR_getRedirectCount();
1243 if($total > 0){
1244 ?>
1245 <div class="notice-success notice is-dismissible sr_notice">
1246 <p>
1247 <strong><?php _e('SEO Redirection : ', 'seo-redirection'); ?></strong><?php echo __( 'The plugin detected ','seo-redirection').intval($total).__(' redirects in the Redirection plugin, Import it now. ','seo-redirection'); ?>
1248 <?php
1249 $SR_import = isset($_GET['SR_import']) ? sanitize_text_field($_GET['SR_import']) : '';
1250 if($SR_import == 'yes'){?>
1251 <button type='button' data-toggle="modal" class="button" href="#" data-target="#import_modal" value="btn_import"><span style="margin-top: 3px;" class="dashicons dashicons-migrate"></span>&nbsp; <?php _e('Import Now','seo-redirection'); ?></button>
1252 <?php }else{ ?>
1253 <a href="options-general.php?page=seo-redirection.php&tab=export_import&SR_import=yes" data-target="#import_modal" value="btn_import"><?php _e('Import','seo-redirection'); ?></a>
1254 <?php }?>
1255 </p>
1256 </div>
1257 <?php
1258 }
1259 break;
1260 }
1261 }
1262 }
1263 add_action( 'admin_notices', 'WPSR_admin_notice_callback' );
1264
1265 add_action('admin_footer','WPSR_import_popup_in_footer');
1266 function WPSR_import_popup_in_footer()
1267 {
1268
1269 global $wpdb;
1270 global $current_user;
1271 $user_id = $current_user->ID;
1272 $val = get_user_meta($user_id, 'sr_notice_dismissed',true);
1273
1274 $plugins = get_option( 'active_plugins', array() );
1275 $found = false;
1276 foreach ( $plugins as $plugin )
1277 {
1278 if ( strpos( strval($plugin), 'redirection.php' ) == true && strpos( strval($plugin), 'seo-redirection.php' ) == FALSE )
1279 {
1280
1281 $table_name = $wpdb->prefix . 'redirection_items';
1282 if (strtolower($wpdb->get_var("show tables like '$table_name'")) == strtolower($table_name))
1283 {
1284
1285 $found = true;
1286 $total_org = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_items");
1287 $total = WPSR_getRedirectCount();
1288 $SR_import = isset($_GET['SR_import']) ? sanitize_text_field($_GET['SR_import']) : '';
1289
1290
1291 if($total > 0 && $SR_import == 'yes'){ ?>
1292 <div class="modal fade" id="import_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" <?php echo $val; ?>>
1293 <div class="modal-dialog" role="document">
1294 <div class="modal-content">
1295 <div class="modal-header">
1296 <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
1297 <h4 class="modal-title" id="myModalLabel">Import Redirects</h4>
1298 </div>
1299 <div class="modal-body">
1300 <div class="import_msg"></div>
1301 <div class="progress_wrap"></div>
1302 <h3 class="text-center"><?php echo __('Total ','seo-redirection').intval($total).__(' redirects from redirection plugin','seo-redirection'); ?></h3>
1303 </div>
1304 <div class="modal-footer">
1305 <input class="button-primary" id="btnImport" type="button" value="Import It" onclick="return import_function(<?php echo esc_js($total_org); ?>,0)">
1306 <input type="hidden" id="_wpnonce" name="_wpnonce" value="<?php echo $nonce = wp_create_nonce('seoredirection_import'); ?>" />
1307 <input data-dismiss="modal" aria-label="Close" class="button-primary " type="button" value="Cancel" name="cancel">
1308 </div>
1309 </div>
1310 </div>
1311 </div>
1312 <?php }
1313 break;
1314 }
1315 }
1316 }
1317 }
1318
1319 add_action('wp_ajax_sr_dismiss_notice','WPSR_dismiss_notice_callback');
1320 function WPSR_dismiss_notice_callback()
1321 {
1322 global $current_user;
1323 $user_id = $current_user->ID;
1324 update_user_meta( $user_id, 'sr_notice_dismissed', '1');
1325 echo "1";
1326 exit;
1327 }
1328 function WPSR_getRedirectCount()
1329 {
1330 global $wpdb;
1331
1332 $table_name_ = $wpdb->prefix . 'redirection_items';
1333 if (strtolower($wpdb->get_var("show tables like '$table_name_'")) == strtolower($table_name_))
1334 {
1335 $result = $wpdb->get_results("SELECT url FROM {$wpdb->prefix}redirection_items");
1336 $cnt=0;
1337 $table_name = $wpdb->prefix . 'WP_SEO_Redirection';
1338 if($result)
1339 {
1340 foreach($result as $redirect)
1341 {
1342 $redirect_from = stripslashes($redirect->url);
1343 $redirect_from_slash = ltrim(stripslashes($redirect->url),'/');
1344 $redirectID = $wpdb->get_var($wpdb->prepare("select ID from $table_name where redirect_from=%s or redirect_from=%s", $redirect_from,$redirect_from_slash));
1345 if($redirectID >0)
1346 {
1347
1348 }
1349 else
1350 {
1351 $cnt++;
1352 }
1353 }
1354
1355 }
1356 return $cnt;
1357 }else{
1358 return 0;
1359 }
1360 }
1361 add_action('admin_init','SR_init_delete_callback');
1362 function SR_init_delete_callback()
1363 {
1364 if(isset($_POST['redirect_id']) && count($_POST['redirect_id']) > 0)
1365 {
1366
1367 global $wpdb,$table_prefix,$util;
1368 $table_name = $wpdb->prefix. 'WP_SEO_Redirection';
1369 foreach($_POST['redirect_id'] as $post_id)
1370 {
1371 $post_id = (int) $post_id;
1372 $wpdb->query($wpdb->prepare(" delete from $table_name where ID=%s ",$post_id));
1373 $SR_redirect_cache = new free_SR_redirect_cache();
1374 $SR_redirect_cache->free_cache();
1375 }
1376
1377 }
1378 }
1379