PluginProbe
SEO Redirection Plugin – 301 Redirect Manager / 7.8
SEO Redirection Plugin – 301 Redirect Manager v7.8
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.8, at seo-redirection.php

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