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

1,435 lines 55.3 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.3
8 Author URI: http://www.clogica.com
9 Text Domain: wsr
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", 'wsr') . '</b>' . __('has detected a change in Permalink, this will be reflected to the redirection records!', 'wsr'));
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">' . $permalink . '</font><input type="hidden" ID="wp_seo_redirection_url_from" name="wp_seo_redirection_url_from" value="' . $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", 'wsr') . '</b><input type="text" name="wp_seo_redirection_url" id="wp_seo_redirection_url" value="' . $urlredirect_to . '" size="62"></td>
133 </tr>
134 <tr>
135 <td>
136 <ul>
137 <li>' . __(" To make a redirection, put the", 'wsr') . ' <b>' . __("URL", 'wsr') . '</b> ' . __("in the text field above and then click the button ", 'wsr') . '<b>' . __("Update", 'wsr') . '</b>.</li>
138 <li>' . __("If you have a caching plugin installed, clear cache to reflect the
139 changes immediately.", 'wsr') . '</li>
140
141 <li>' . __("To remove the redirection, just uncheck the check box above and then click the button", 'wsr') . ' <b>' . __("Update", 'wsr') . '</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.', 'wsr');
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", "wsr");
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!", "wsr");
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!",'wsr');
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", "wsr");
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!", 'wsr');
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", 'wsr') . " <b>'$redirect_from'</b>" . __("is added previously!", 'wsr');
674 echo json_encode(array('status' => 'error', 'msg' => $msg));
675 // $util->failure_option_msg(__("This URL",'wsr')." <b>'$redirect_from'</b>". __("is added previously!",'wsr'));
676 } else {
677
678
679 if ($redirect_from == '' || $redirect_to == '' || $redirect_type == '') {
680 $util->failure_option_msg(__('Please input all required fields!', 'wsr'));
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", 'wsr') . " <b>'" . $util->there_is_cache() . "'</b>, " . __("you have to clear cache after any changes to get the changes reflected immediately! ", 'wsr'));
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!", 'wsr')));
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 ','wsr').'<span class="insert_cnt">'.$inscnt.'</span>'.__(' record(s) inserted and ','wsr').'<span class="update_cnt">'.$udtcnt.'</span>'.__(' record(s) updated.','wsr');
862 echo json_encode(array('status' => 'success','updateCnt'=>$udtcnt,'insertCnt'=>$inscnt,'msg'=>$msg));
863 }
864 else
865 {
866 $msg = __('No redirects found to import.','wsr');
867 echo json_encode(array('status' => 'error','msg'=>$msg));
868 }
869 }
870 }
871 else
872 {
873 $msg = __('Something went wrong plase try again.','wsr');
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.', 'wsr'));
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!', 'wsr'));
900 } else if ($util->get_option_value('plugin_status') == '2') {
901 $util->info_option_msg(__('SEO Redirection is', 'wsr') . ' <b>' . __('disabled for admin', 'wsr') . '</b>' . __(' only, you can go to option tab and enable it!', 'wsr'));
902 }
903 $total_404_errors = (WPSR_Get_total_404() > 10) ? __('You have', 'wsr') . ' <b style="color:red; background-color:yellow; padding:3px;"> too many </b>' . __(' broken link (404 links)', 'wsr') . ', <br>' : '';
904
905
906 echo '<div class="wrap"><h2>' . __("SEO Redirection Free", 'wsr') . '</h2><b>' . __('Upgrade to', 'wsr') . ' <a target="_blank" onclick="swal.clickConfirm();" href="http://www.clogica.com/product/seo-redirection-premium-wordpress-plugin">' . __("Pro Version", "wsr") . '</a>' . __(" to manage 404 errors and empower your site SEO", "wsr") . '&nbsp;&nbsp;&nbsp;<strong style="color:yellow; background-color:red; padding:3px;"> ' . __("NOW 50% OFF ", 'wsr') . '</strong></b><br/><br/>';
907
908 if ($total_404_errors != '') {
909 ?>
910 <script type="text/javascript">
911
912 seoredirection.msg = '<?php echo $total_404_errors . '<div class="wrap" style="font-weight:normal; line-height:30px">' . __('Upgrade to', 'wsr') . ' <a target="_blank" href="http://www.clogica.com/product/seo-redirection-premium-wordpress-plugin">' . __("Pro Version", "wsr") . '</a>' . __(" to manage 404 errors and empower your site SEO", "wsr") ?></div>';
913
914 </script>
915
916 <?php
917 }
918
919 if (is_multisite()) {
920
921 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', 'wsr') . '(<a target="_blank" href="http://www.clogica.com/product/seo-redirection-premium-wordpress-plugin">
922 http://www.clogica.com/product/seo-redirection-premium-wordpress-plugin</a>) <p></p></div>';
923
924 }
925
926 $mytabs = new phptab();
927
928 $mytabs->set_ignore_parameter(array('del', 'search', 'page_num', 'add', 'edit', 'page404', 'do_404_del'));
929 $mytabs->add_file_tab('cutom', __('Custom Redirects', 'wsr'), 'option_page_custome_redirection.php', 'file');
930 $mytabs->add_file_tab('posts', __('Post Redirects', 'wsr'), 'option_page_post_redirection_list.php', 'file');
931 $mytabs->add_file_tab('history', __('History', 'wsr'), 'option_page_history.php', 'file');
932 $mytabs->add_file_tab('export_import', __('Export/Import', 'wsr'), 'option_export_import.php', 'file');
933 $mytabs->add_file_tab('404','<span style="color:red;"><b>404 Errors</b></span>','option_page_404.php','file');
934 $mytabs->add_file_tab('goptions', __('Options', 'wsr'), 'option_page_goptions.php', 'file');
935 $mytabs->add_file_tab('help', '<span style="color:green;"><b>' . __('Help', 'wsr') . '</b></span>', 'help.php', 'file');
936 $mytabs->add_file_tab('premium', '<span style="color:brown;"><b>&#9658; ' . __('Premium Features', 'wsr') . '</b></span>', 'premium.php', 'file');
937 $mytabs->run();
938
939 }
940
941
942 function WPSR_upgrade()
943 {
944
945 $util = new clogica_util_1();
946 $util->init(WP_SEO_REDIRECTION_OPTIONS, __FILE__);
947
948 if ($util->get_option_value('plugin_version') != WP_SEO_REDIRECTION_VERSION) {
949 WPSR_install();
950 $util->update_option('plugin_version', WP_SEO_REDIRECTION_VERSION);
951 }
952 }
953
954 //-----------------------------------------------------
955 function WPSR_install()
956 {
957 global $wpdb, $table_prefix;
958
959 $util = new clogica_util_1();
960 $util->init(WP_SEO_REDIRECTION_OPTIONS, __FILE__);
961
962 $options = get_option(WP_SEO_REDIRECTION_OPTIONS);
963 if (!is_array($options)) {
964 add_option(WP_SEO_REDIRECTION_OPTIONS);
965 $options = array();
966 }
967
968
969 if (!array_key_exists('plugin_status', $options))
970 $options['plugin_status'] = '1';
971
972 if (!array_key_exists('ip_logging_status', $options))
973 $options['ip_logging_status'] = '1';
974
975 if (!array_key_exists('redirection_base', $options))
976 $options['redirection_base'] = site_url();
977
978 if (!array_key_exists('redirect_control_panel', $options))
979 $options['redirect_control_panel'] = '1';
980
981 if (!array_key_exists('show_redirect_box', $options))
982 $options['show_redirect_box'] = '1';
983
984 if (!array_key_exists('reflect_modifications', $options))
985 $options['reflect_modifications'] = '1';
986
987 if (!array_key_exists('history_status', $options))
988 $options['history_status'] = '1';
989
990 if (!array_key_exists('history_limit', $options))
991 $options['history_limit'] = '30';
992
993 if (!array_key_exists('p404_discovery_status', $options))
994 $options['p404_discovery_status'] = '1';
995
996 if (!array_key_exists('p404_redirect_to', $options))
997 $options['p404_redirect_to'] = site_url();
998
999 if (!array_key_exists('p404_status', $options))
1000 $options['p404_status'] = '2';
1001
1002 if (!array_key_exists('keep_data', $options))
1003 $options['keep_data'] = '1';
1004
1005 update_option(WP_SEO_REDIRECTION_OPTIONS, $options);
1006
1007
1008 $table_name = $table_prefix . 'WP_SEO_Redirection';
1009 if (strtolower($wpdb->get_var("show tables like '$table_name'")) != strtolower($table_name)) {
1010 $sql = "CREATE TABLE IF NOT EXISTS `$table_name` (
1011 `ID` int(11) unsigned NOT NULL AUTO_INCREMENT,
1012 `enabled` int(1) NOT NULL DEFAULT '1',
1013 `redirect_from` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1014 `redirect_from_type` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1015 `redirect_from_folder_settings` int(1) NOT NULL,
1016 `redirect_from_subfolders` int(1) NOT NULL DEFAULT '1',
1017 `redirect_to` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1018 `redirect_to_type` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1019 `redirect_to_folder_settings` int(1) NOT NULL DEFAULT '1',
1020 `regex` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1021 `redirect_type` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1022 `url_type` int(2) NOT NULL DEFAULT 1,
1023 `postID` int(11) unsigned DEFAULT NULL,
1024 `import_flag` tinyint(1) NOT NULL DEFAULT 0,
1025 `hits` int(11) unsigned NOT NULL DEFAULT 0,
1026 `access_date` datetime DEFAULT NULL,
1027 PRIMARY KEY (`ID`),
1028 UNIQUE KEY `redirect_from` (`redirect_from`)
1029 )ENGINE = MyISAM ;";
1030 $wpdb->query($sql);
1031
1032
1033 } else {
1034 //check if Innodb convert it to myisam.
1035 $status = $wpdb->get_row("SHOW TABLE STATUS WHERE Name = '$table_name'");
1036 if ($status->Engine == 'InnoDB') {
1037 $wpdb->query("alter table $table_name engine = MyISAM;");
1038 }
1039
1040 /* add column for import flag */
1041 $column_data = $wpdb->get_row("SHOW COLUMNS FROM $table_name LIKE 'import_flag'");
1042
1043 if (!$column_data)
1044 {
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
1187 function WPSR_uninstall()
1188 {
1189 global $wpdb, $table_prefix;
1190
1191 $util = new clogica_util_1();
1192 $util->init(WP_SEO_REDIRECTION_OPTIONS, __FILE__);
1193
1194
1195 if ($util->get_option_value('keep_data') != '1') {
1196
1197 $table_name = $table_prefix . 'WP_SEO_Redirection';
1198 $wpdb->query($wpdb->prepare(" DROP TABLE %s ", $table_name));
1199
1200 $table_name = $table_prefix . 'WP_SEO_Cache';
1201 $wpdb->query($wpdb->prepare(" DROP TABLE %s ", $table_name));
1202
1203 $table_name = $table_prefix . 'WP_SEO_404_links';
1204 $wpdb->query($wpdb->prepare(" DROP TABLE %s ", $table_name));
1205
1206 $table_name = $table_prefix . 'WP_SEO_Redirection_LOG';
1207 $wpdb->query($wpdb->prepare(" DROP TABLE %s ", $table_name));
1208
1209
1210 $util->delete_my_options();
1211 }
1212
1213
1214 }
1215
1216
1217 //---------------------------------------------------------------
1218
1219 function WPSR_HideMessageAjaxFunction()
1220 {
1221 add_option('nsr_upgrade_message', 'yes');
1222 }
1223
1224 /*
1225 function SR_after_plugin_row($plugin_file, $plugin_data, $status)
1226 {
1227
1228 if (get_option('nsr_upgrade_message') != 'yes') {
1229 $class_name = $plugin_data['slug'];
1230
1231 echo '<tr id="' . $class_name . '-plugin-update-tr" class="plugin-update-tr active">';
1232 echo '<td colspan="3" class="plugin-update">';
1233 echo '<div id="' . $class_name . '-upgradeMsg" class="update-message notice inline notice-warning notice-alt" >';
1234
1235 echo 'You are running SEO redirection free. To get more features, you can <a href="http://www.clogica.com/product/seo-redirection-premium-wordpress-plugin" target="_blank"><strong>upgrade now</strong></a> or ';
1236
1237 echo '<span id="HideMe" style="cursor:pointer" ><a href="javascript:void(0)"><strong>dismiss</strong></a> this message</span>';
1238 echo '</div>';
1239 echo '</td>';
1240 echo '</tr>';
1241
1242 ?>
1243
1244 <script type="text/javascript">
1245 jQuery(document).ready(function () {
1246 var row = jQuery('#<?php echo $class_name;?>-plugin-update-tr').closest('tr').prev();
1247 jQuery(row).addClass('update');
1248
1249 jQuery("#HideMe").click(function () {
1250 jQuery.ajax({
1251 type: 'POST',
1252 url: '<?php echo admin_url();?>/admin-ajax.php',
1253 data: {
1254 action: 'WPSR_HideMessageAjaxFunction'
1255 },
1256 success: function (data, textStatus, XMLHttpRequest) {
1257
1258 jQuery("#<?php echo $class_name;?>-upgradeMsg").hide();
1259
1260 },
1261 error: function (MLHttpRequest, textStatus, errorThrown) {
1262 alert(errorThrown);
1263 }
1264 });
1265 });
1266
1267 });
1268 </script>
1269
1270 <?php
1271 }
1272 }
1273
1274 $path = plugin_basename(__FILE__);
1275 //add_action("after_plugin_row_{$path}", 'SR_after_plugin_row', 10, 3);
1276 // creating Ajax call for WordPress
1277 //add_action('wp_ajax_nopriv_WPSR_HideMessageAjaxFunction', 'WPSR_HideMessageAjaxFunction');
1278 //add_action('wp_ajax_WPSR_HideMessageAjaxFunction', 'WPSR_HideMessageAjaxFunction');
1279
1280 */
1281 //---------------------------------------------------------------
1282
1283 /* display import from redirection plugin in admin notice */
1284 function WPSR_admin_notice_callback() {
1285 global $wpdb;
1286 global $current_user;
1287 $plugins = get_option( 'active_plugins', array() );
1288 $found = false;
1289 $user_id = $current_user->ID;
1290 $val = get_user_meta($user_id, 'sr_notice_dismissed',true);
1291
1292 foreach ( $plugins as $plugin )
1293 {
1294 if ( strpos( strval($plugin), 'redirection.php' ) == true && $val != 1 && strpos( strval($plugin), 'seo-redirection.php' ) == FALSE )
1295 {
1296 $found = true;
1297 //$total = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_items");
1298 $total = WPSR_getRedirectCount();
1299 if($total > 0){
1300 ?>
1301 <div class="notice-success notice is-dismissible sr_notice">
1302 <p>
1303 <strong><?php _e('SEO Redirection : ', 'wsr'); ?></strong><?php echo __( 'The plugin detected ','wsr').$total.__(' redirects in the Redirection plugin, Import it now. ','wsr'); ?>
1304 <?php
1305 $SR_import = isset($_GET['SR_import']) ? sanitize_text_field($_GET['SR_import']) : '';
1306 if($SR_import == 'yes'){?>
1307 <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','wsr'); ?></button>
1308 <?php }else{ ?>
1309 <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','wsr'); ?></a>
1310 <?php }?>
1311 </p>
1312 </div>
1313 <?php
1314 }
1315 break;
1316 }
1317 }
1318 }
1319 add_action( 'admin_notices', 'WPSR_admin_notice_callback' );
1320
1321 add_action('admin_footer','WPSR_import_popup_in_footer');
1322 function WPSR_import_popup_in_footer()
1323 {
1324
1325 global $wpdb;
1326 global $current_user;
1327 $user_id = $current_user->ID;
1328 $val = get_user_meta($user_id, 'sr_notice_dismissed',true);
1329
1330 $plugins = get_option( 'active_plugins', array() );
1331 $found = false;
1332 foreach ( $plugins as $plugin )
1333 {
1334 if ( strpos( strval($plugin), 'redirection.php' ) == true && strpos( strval($plugin), 'seo-redirection.php' ) == FALSE )
1335 {
1336
1337 $table_name = $wpdb->prefix . 'redirection_items';
1338 if (strtolower($wpdb->get_var("show tables like '$table_name'")) == strtolower($table_name))
1339 {
1340
1341 $found = true;
1342 $total_org = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_items");
1343 $total = WPSR_getRedirectCount();
1344 $SR_import = isset($_GET['SR_import']) ? sanitize_text_field($_GET['SR_import']) : '';
1345
1346
1347 if($total > 0 && $SR_import == 'yes'){ ?>
1348 <div class="modal fade" id="import_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" <?php echo $val; ?>>
1349 <div class="modal-dialog" role="document">
1350 <div class="modal-content">
1351 <div class="modal-header">
1352 <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
1353 <h4 class="modal-title" id="myModalLabel">Import Redirects</h4>
1354 </div>
1355 <div class="modal-body">
1356 <div class="import_msg"></div>
1357 <div class="progress_wrap"></div>
1358 <h3 class="text-center"><?php echo __('Total ','wsr').$total.__(' redirects from redirection plugin','wsr'); ?></h3>
1359 </div>
1360 <div class="modal-footer">
1361 <input class="button-primary" id="btnImport" type="button" value="Import It" onclick="return import_function(<?php echo $total_org; ?>,0)">
1362 <input type="hidden" id="_wpnonce" name="_wpnonce" value="<?php echo $nonce = wp_create_nonce('seoredirection_import'); ?>" />
1363 <input data-dismiss="modal" aria-label="Close" class="button-primary " type="button" value="Cancel" name="cancel">
1364 </div>
1365 </div>
1366 </div>
1367 </div>
1368 <?php }
1369 break;
1370 }
1371 }
1372 }
1373 }
1374
1375 add_action('wp_ajax_sr_dismiss_notice','WPSR_dismiss_notice_callback');
1376 function WPSR_dismiss_notice_callback()
1377 {
1378 global $current_user;
1379 $user_id = $current_user->ID;
1380 update_user_meta( $user_id, 'sr_notice_dismissed', '1');
1381 echo "1";
1382 exit;
1383 }
1384 function WPSR_getRedirectCount()
1385 {
1386 global $wpdb;
1387
1388 $table_name_ = $wpdb->prefix . 'redirection_items';
1389 if (strtolower($wpdb->get_var("show tables like '$table_name_'")) == strtolower($table_name_))
1390 {
1391 $result = $wpdb->get_results("SELECT url FROM {$wpdb->prefix}redirection_items");
1392 $cnt=0;
1393 $table_name = $wpdb->prefix . 'WP_SEO_Redirection';
1394 if($result)
1395 {
1396 foreach($result as $redirect)
1397 {
1398 $redirect_from = stripslashes($redirect->url);
1399 $redirect_from_slash = ltrim(stripslashes($redirect->url),'/');
1400 $redirectID = $wpdb->get_var($wpdb->prepare("select ID from $table_name where redirect_from=%s or redirect_from=%s", $redirect_from,$redirect_from_slash));
1401 if($redirectID >0)
1402 {
1403
1404 }
1405 else
1406 {
1407 $cnt++;
1408 }
1409 }
1410
1411 }
1412 return $cnt;
1413 }else{
1414 return 0;
1415 }
1416 }
1417 add_action('admin_init','SR_init_delete_callback');
1418 function SR_init_delete_callback()
1419 {
1420 if(isset($_POST['redirect_id']) && count($_POST['redirect_id']) > 0)
1421 {
1422
1423 global $wpdb,$table_prefix,$util;
1424 $table_name = $wpdb->prefix. 'WP_SEO_Redirection';
1425 foreach($_POST['redirect_id'] as $post_id)
1426 {
1427 $post_id = (int) $post_id;
1428 $wpdb->query($wpdb->prepare(" delete from $table_name where ID=%s ",$post_id));
1429 $SR_redirect_cache = new free_SR_redirect_cache();
1430 $SR_redirect_cache->free_cache();
1431 }
1432
1433 }
1434 }
1435