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

1,569 lines 65.4 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: 9.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', 9.4);
27 }
28
29 $util = new clogica_util_1();
30 $util->init(WP_SEO_REDIRECTION_OPTIONS, __FILE__);
31
32
33 add_action('admin_enqueue_scripts', 'WPSR_header_code');
34 add_action('admin_menu', 'WPSR_admin_menu');
35 add_action('wp', 'WPSR_redirect', 1);
36 add_action('save_post', 'WPSR_get_post_redirection');
37 add_action('add_meta_boxes', 'WPSR_adding_custom_meta_boxes', 10, 3);
38 add_action('admin_head', 'WPSR_check_default_permalink');
39 add_action('plugins_loaded', 'WPSR_upgrade');
40
41 register_activation_hook(__FILE__, 'WPSR_upgrade');
42 register_uninstall_hook(__FILE__, 'WPSR_uninstall');
43
44 /////////////////////////////////////////////////////////////////////////
45
46 if(!function_exists("WPSR__filter_action_links")){
47 function WPSR__filter_action_links( $links ) {
48 $links['settings'] = sprintf('<a href="%s">Settings</a>', admin_url( 'options-general.php?page=seo-redirection.php' ));
49 $network_dir_append = "";
50 If (is_multisite()) $network_dir_append = "network/";
51 $links['MorePlugins'] = sprintf('<a href="%s"><b style="color:#f18500">More Plugins</b></a>', admin_url( $network_dir_append . 'plugin-install.php?s=wp-buy&tab=search&type=author' ));
52 return $links;
53 }
54 add_filter( 'plugin_action_links_'.plugin_basename(__FILE__), 'WPSR__filter_action_links', 10, 1 );
55 }
56
57 /////////////////////////////////////////////////////////////////////////
58 if (!function_exists("WPSR_add_link_to_admin_bar")) {
59 function WPSR_add_link_to_admin_bar($wp_admin_bar) {
60 global $wpdb;
61
62 if (!is_admin_bar_showing()) {
63 return;
64 }
65
66 $table_name = $wpdb->prefix . 'WP_SEO_Redirection';
67
68 $current_url = (is_ssl() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
69 $relative_url = str_replace(home_url(), '', $current_url);
70
71 $wp_admin_bar->add_node(array(
72 'id' => 'seo_redirection',
73 'title' => '<img src="' . plugins_url( 'icon.png', __FILE__ ) . '" class="seo-redirection-icon" alt="Icon" /> SEO Redirection',
74 'href' => '#',
75 'meta' => array(
76 'class' => 'seo-redirection-admin-bar', // Add a custom CSS class
77 ),
78 ));
79
80 // Check if we are on an admin page or not
81 if (is_admin()) {
82 // Fetch the total number of 404 errors
83 $total_404_errors = WPSR_Get_total_404();
84 // Format the number with commas for readability
85 $formatted_404_errors = number_format($total_404_errors);
86
87 // Admin pages sub-nodes
88 $wp_admin_bar->add_node(array(
89 'id' => 'manage_redirects',
90 'title' => 'Manage Redirects',
91 'href' => admin_url('options-general.php?page=seo-redirection.php&tab=cutom'),
92 'parent' => 'seo_redirection',
93 ));
94
95 $wp_admin_bar->add_node(array(
96 'id' => 'post_redirects',
97 'title' => 'Post Redirects',
98 'href' => admin_url('options-general.php?page=seo-redirection.php&tab=posts'),
99 'parent' => 'seo_redirection',
100 ));
101
102 $wp_admin_bar->add_node(array(
103 'id' => 'history_redirects',
104 'title' => 'History',
105 'href' => admin_url('options-general.php?page=seo-redirection.php&tab=history'),
106 'parent' => 'seo_redirection',
107 ));
108
109 // Add the 404 errors node with a dynamic total and red background
110 $wp_admin_bar->add_node(array(
111 'id' => 'manage_404_errors',
112 'title' => '404 Errors (' . $formatted_404_errors . ')', // Display number of 404 errors with formatting
113 'href' => admin_url('options-general.php?page=seo-redirection.php&tab=404'),
114 'parent' => 'seo_redirection',
115 'meta' => array(
116 'class' => 'wpsr-404-errors-admin-bar', // Custom class for styling
117 ),
118 ));
119
120 } else {
121 // Front-end sub-nodes (same as your original function)
122 $result = $wpdb->get_results($wpdb->prepare("SELECT * FROM $table_name WHERE redirect_from = %s", $relative_url));
123
124 if (!empty($result)) {
125 $wp_admin_bar->add_node(array(
126 'id' => 'edit_redirection',
127 'title' => 'Edit Redirection',
128 'href' => admin_url('options-general.php?page=seo-redirection.php&tab=cutom&redirect_from=' . $relative_url ),
129 'parent' => 'seo_redirection',
130 ));
131
132 $wp_admin_bar->add_node(array(
133 'id' => 'view_history',
134 'title' => 'View History',
135 'href' => admin_url('options-general.php?page=seo-redirection.php&tab=history'),
136 'parent' => 'seo_redirection',
137 ));
138 } else {
139 $wp_admin_bar->add_node(array(
140 'id' => 'add_redirection',
141 'title' => 'Redirect this page',
142 'href' => admin_url('options-general.php?page=seo-redirection.php&tab=cutom&redirect_from=' . $relative_url),
143 'parent' => 'seo_redirection',
144 ));
145 }
146 }
147 }
148
149 add_action('admin_bar_menu', 'WPSR_add_link_to_admin_bar', 999);
150 }
151 /////////////////////////////////////////////////////////////////////////
152
153 function custom_admin_bar_styles() {
154 if (is_admin_bar_showing()) {
155 echo '<style>
156 #wp-admin-bar-seo_redirection > .ab-item {
157 background-color: #114e0e; /* Light green background color */
158 color: white;
159 display: flex;
160 align-items: center;
161 padding: 0 10px; /* Add padding for better alignment */
162 }
163
164 #wp-admin-bar-seo_redirection > .ab-item:hover {
165 background-color: #7ccd7c; /* Darker green on hover */
166 }
167
168 .wpsr-404-notice {
169 border-left: 4px solid red;
170 padding: 15px;
171 background-color: #ffebe6; /* Light red background */
172 }
173
174 .wpsr-404-notice .button-primary {
175 background-color: orange;
176 border-color: orange;
177 }
178
179 #wp-admin-bar-seo_redirection .seo-redirection-icon {
180 width: 20px !important; /* Resize the image to a smaller size */
181 height: 20px !important; /* Keep it proportional */
182 vertical-align: middle; /* Align icon vertically */
183 }
184
185 #wp-admin-bar-seo_redirection .ab-item .seo-redirection-icon {
186 max-width: 100%;
187 max-height: 100%;
188 }
189
190 /* Custom style for the 404 node with red background */
191 #wp-admin-bar-manage_404_errors > .ab-item {
192 background-color: red; /* Red background for the 404 node */
193 color: white; /* White text color */
194 font-weight: bold; /* Bold text to highlight the 404 count */
195 }
196
197 #wp-admin-bar-manage_404_errors > .ab-item:hover {
198 background-color: darkred; /* Darker red on hover */
199 }
200 </style>';
201 }
202 }
203
204 // Apply styles to the front-end
205 add_action('wp_head', 'custom_admin_bar_styles');
206
207 // Apply styles to the admin dashboard
208 add_action('admin_head', 'custom_admin_bar_styles');
209
210
211
212 /////////////////////////////////////////////////////////////////////////
213 if(!function_exists('wpsr_dashboard_notice')) {
214
215 function wpsr_dashboard_notice() {
216 // Get the total number of 404 errors
217 $total_404_errors = WPSR_Get_total_404();
218
219 // Only show the notice if there are more than 100 broken links and if the user has not dismissed it
220 if ($total_404_errors > 100 && !get_user_meta(get_current_user_id(), 'wpsr_404_notice_dismissed')) {
221 // Image URL for the icon (Replace with your own uploaded image or the default WordPress icon)
222 $icon_url =plugins_url( 'icon.png', __FILE__ );
223
224 // Message content with buttons
225 $message = __('<strong>SEO Redirection</strong>: You have', 'seo-redirection') . ' <b style="color:red; padding:3px;">' . intval($total_404_errors) . '</b>' . __(' broken links (404). Manage them now to fix the issue and improve your site\'s SEO performance.', 'seo-redirection');
226
227 // Display the message with inline styles
228 echo '
229 <div class="notice notice-error is-dismissible wpsr-404-notice" style="border-left: 4px solid red; display: flex; align-items: center;">
230 <div style="display: flex; align-items: center;">
231 <img src="' . esc_url($icon_url) . '" style="margin-right: 15px; width: 40px; height: 40px;" alt="Error icon" />
232 <div>
233 <p>' . $message . '</p>
234 <p>
235 <a href="' . admin_url('options-general.php?page=seo-redirection.php&tab=404') . '" class="button button-primary" style="background-color: green; border-color: green; margin-right: 10px;">' . __('Fix Now', 'seo-redirection') . '</a>
236 <button type="button" class="button button-secondary wpsr-dismiss-notice" style="border-color: #0073aa; color: #0073aa;">' . __('Dismiss', 'seo-redirection') . '</button>
237 </p>
238 </div>
239 </div>
240 </div>
241 ';
242
243 // Add the script for handling the dismiss action
244 echo '
245 <script type="text/javascript">
246 jQuery(document).ready(function($) {
247 $(\'.wpsr-dismiss-notice\').on(\'click\', function() {
248 var ajaxurl = "' . admin_url('admin-ajax.php') . '";
249 $.post(ajaxurl, {
250 action: "wpsr_dismiss_404_notice",
251 user_id: ' . get_current_user_id() . '
252 });
253 $(this).closest(\'.wpsr-404-notice\').remove();
254 });
255 });
256 </script>
257 ';
258 }
259 }
260 add_action('admin_notices', 'wpsr_dashboard_notice');
261
262 }
263
264
265 if(!function_exists('wpsr_dismiss_404_notice')) {
266
267 function wpsr_dismiss_404_notice() {
268 // Update the user meta to mark the notice as dismissed
269 update_user_meta(get_current_user_id(), 'wpsr_404_notice_dismissed', true);
270 wp_die(); // This is required to terminate immediately and return a proper response
271 }
272 add_action('wp_ajax_wpsr_dismiss_404_notice', 'wpsr_dismiss_404_notice');
273 }
274 //////////////////////////////////////////////////////////////////////////////////////////////////
275 if(!function_exists("WPSR_multiple_plugin_activate_trial")){
276 function WPSR_multiple_plugin_activate_trial()
277 {
278 global $wpdb;
279 if (is_multisite()) {
280 if (is_plugin_active_for_network(__FILE__)) {
281 $blogids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs");
282 foreach ($blogids as $blog_id) {
283 switch_to_blog($blog_id);
284 }
285 }
286 }
287 }
288
289 register_activation_hook(__FILE__, 'WPSR_multiple_plugin_activate_trial');
290
291 }
292
293 if(!function_exists("WPSR_adding_custom_meta_boxes")) {
294
295 function WPSR_adding_custom_meta_boxes()
296 {
297 global $util;
298 if ($util->get_option_value('show_redirect_box') == '1') {
299
300 $screens = array('post', 'page');
301
302 foreach ($screens as $screen) {
303
304 add_meta_box(
305 'WPSR_meta_box',
306 __('SEO Redirection'),
307 'WPSR_render_meta_box',
308 $screen
309 );
310 }
311
312 }
313 }
314 }
315 if(!function_exists("WPSR_render_meta_box")) {
316
317 function WPSR_render_meta_box($post)
318 {
319 global $wpdb, $table_prefix, $util;
320 $table_name = $table_prefix . 'WP_SEO_Redirection';
321
322 if (get_post_status() != 'auto-draft') {
323 $permalink = "";
324 if (in_array($post->post_status, array('draft', 'pending'))) {
325 list($permalink, $postname) = get_sample_permalink($post->ID);
326 $permalink = str_replace('%postname%', $postname, $permalink);
327
328 } else {
329
330 $permalink = get_permalink($post->ID);
331 }
332
333 $permalink = $util->make_relative_url(urldecode($permalink));
334
335 $postID = $post->ID;
336
337
338 $theurl = $wpdb->get_row($wpdb->prepare(" select redirect_to,redirect_from from $table_name where postID=%d ", $postID));
339
340 $urlredirect_to = '';
341 if ($wpdb->num_rows > 0)
342 $urlredirect_to = $theurl->redirect_to;
343
344 if ($urlredirect_to != '' && $theurl->redirect_from != $permalink) {
345 // the post_name field changed!
346 $wpdb->query($wpdb->prepare(" update $table_name set redirect_from=%s where postID=%d ", $permalink, $postID));
347 if ($util->get_option_value('reflect_modifications') == '1') {
348 $wpdb->query($wpdb->prepare(" update $table_name set redirect_to=%s where redirect_to=%s ", $permalink, $theurl->redirect_from));
349 $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'));
350 }
351 //-------------------------------------------
352 }
353
354 echo '
355 <table border="0" width="100%" cellpadding="2">
356 <tr>
357 <td width="99%"><input onchange="redirect_check_click()" type="checkbox" name="redirect_check" id="redirect_check" value="ON">
358 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>
359 </tr>
360 </table>
361 <div id="redirect_frame">
362 <table border="0" width="100%" cellpadding="2">
363 <tr>
364 <td>
365
366 <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>
367 </tr>
368 <tr>
369 <td>
370 <ul>
371 <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>
372 <li>' . __("If you have a caching plugin installed, clear cache to reflect the
373 changes immediately.", 'seo-redirection') . '</li>
374
375 <li>' . __("To remove the redirection, just uncheck the check box above and then click the button", 'seo-redirection') . ' <b>' . __("Update", 'seo-redirection') . '</b>.</li>
376 </ul>
377 </td>
378 </tr>
379 </table>
380 </div>';
381
382 echo "
383
384 <script type='text/javascript'>
385 function WSR_check_status(x)
386 {
387
388 if(x==0)
389 {
390 document.getElementById('redirect_check').checked=false;
391 document.getElementById('redirect_frame').style.display = 'none';
392 document.getElementById('wp_seo_redirection_url').value='';
393 }else
394 {
395 document.getElementById('redirect_check').checked=true;
396 document.getElementById('redirect_frame').style.display= 'block';
397 }
398
399 }
400
401 function redirect_check_click()
402 {
403 if(document.getElementById('redirect_check').checked)
404 WSR_check_status(1);
405 else
406 WSR_check_status(0);
407 }
408 </script>
409 ";
410
411 if ($urlredirect_to == '')
412 echo "<script type='text/javascript'>WSR_check_status(0);</script>";
413 else
414 echo "<script type='text/javascript'>WSR_check_status(1);</script>";
415
416
417 } else {
418 echo __('You can not make a redirection for the new posts before saving them.', 'seo-redirection');
419 }
420 }
421 }
422
423 //--------------------------------------------------------------------------------------------
424
425 //---------------------------------------------------------------
426 // added 2/2/2020
427 if(!function_exists("WPSR_get_site_404_page_path")) {
428
429 function WPSR_get_site_404_page_path()
430 {
431 $url = str_ireplace("://", "", site_url());
432 $site_404_page = substr($url, stripos($url, "/"));
433
434 if (stripos($url, "/") === FALSE || $site_404_page == "/")
435 $site_404_page = "/index.php?error=404";
436 else
437 $site_404_page = $site_404_page . "/index.php?error=404";
438
439 return $site_404_page;
440 }
441
442 }
443 //---------------------------------------------------------------
444 // updated 2/2/2020
445
446 function WPSR_check_default_permalink()
447 {
448 $file= get_home_path() . "/.htaccess";
449 $content="ErrorDocument 404 " . WPSR_get_site_404_page_path();
450
451 $marker_name="FRedirect_ErrorDocument";
452 $filestr ="";
453
454
455 if(is_readable($file))
456 {
457 $f = @fopen( $file, 'r+' );
458 if ($f !== false) {
459 $filestr = @fread($f , filesize($file));
460 if (strpos($filestr , $marker_name) === false)
461 {
462 insert_with_markers( $file, $marker_name, $content );
463 }
464 }
465 }else{
466 echo $file.' is not readable!';
467 }
468
469 }
470
471 //------------------------------------------------------------------------
472
473
474 /**
475 * Recursive sanitation for text or array
476 *
477 * @param $array_or_string (array|string)
478 * @since 0.1
479 * @return mixed
480 */
481 if(!function_exists("WPSR_sanitize_text_or_array_field")) {
482
483 function WPSR_sanitize_text_or_array_field($array_or_string)
484 {
485 if (is_string($array_or_string)) {
486 $array_or_string = sanitize_text_field($array_or_string);
487 } elseif (is_array($array_or_string)) {
488 foreach ($array_or_string as $key => &$value) {
489 if (is_array($value)) {
490 $value = WPSR_sanitize_text_or_array_field($value);
491 } else {
492 $value = sanitize_text_field($value);
493 }
494 }
495 }
496
497 return $array_or_string;
498 }
499 }
500 if(!function_exists("WPSR_get_post_redirection")) {
501
502 function WPSR_get_post_redirection($post_id)
503 {
504
505 global $wpdb, $util, $table_prefix;
506 $table_name = $table_prefix . 'WP_SEO_Redirection';
507
508 // Autosave
509 if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
510 return;
511 // AJAX
512 if (defined('DOING_AJAX') && DOING_AJAX)
513 return;
514 // Post revision
515 if (false !== wp_is_post_revision($post_id))
516 return;
517
518 $redirect_from = isset($_POST['wp_seo_redirection_url_from']) ? WPSR_sanitize_text_or_array_field($_POST['wp_seo_redirection_url_from']) : '';
519 $redirect_to = isset($_POST['wp_seo_redirection_url']) ? WPSR_sanitize_text_or_array_field($_POST['wp_seo_redirection_url']) : '';
520
521 if ($redirect_to != '') {
522
523
524 $wpdb->get_results($wpdb->prepare("select ID from $table_name where postID=%d ", $post_id));
525
526 if ($wpdb->num_rows > 0) {
527
528 $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);
529 $wpdb->query($sql);
530
531 } else {
532 $wpdb->query($wpdb->prepare("delete from $table_name where redirect_from=%s", $redirect_from));
533 $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);
534 $wpdb->query($sql);
535 }
536
537
538 } else {
539 $wpdb->query($wpdb->prepare("delete from $table_name where postID=%d", $post_id));
540 }
541
542 $SR_redirect_cache = new free_SR_redirect_cache();
543 $SR_redirect_cache->free_cache();
544 }
545
546 }
547 //-------------------------------------------------------------
548 if(!function_exists("WPSR_log_404_redirection")) {
549
550 function WPSR_log_404_redirection($link)
551 {
552 global $wpdb, $table_prefix, $util;
553 $table_name = $table_prefix . 'WP_SEO_404_links';
554
555 $referrer = $util->get_ref();
556 $ip = $util->get_visitor_IP();
557 $country = "";//$util->get_visitor_country();
558 $os = $util->get_visitor_OS();
559 $browser = $util->get_visitor_Browser();
560
561 if ($os != 'Unknown' || $browser != 'Unknown') {
562 $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));
563 }
564 }
565
566 }
567 //-------------------------------------------------------------
568 if(!function_exists("WPSR_log_redirection_history")) {
569
570 function WPSR_log_redirection_history($rID, $postID, $rfrom, $rto, $rtype, $rsrc)
571 {
572 global $wpdb, $table_prefix, $util;
573 $SR_redirect_cache = new free_SR_redirect_cache();
574 $SR_redirect_cache->free_cache();
575 $table_name = $table_prefix . 'WP_SEO_Redirection_LOG';
576 $rfrom = esc_url($rfrom);
577 $referrer = $util->get_ref();
578 $ip = $util->get_visitor_IP();
579 $country = "";//$util->get_visitor_country();
580 $os = $util->get_visitor_OS();
581 $browser = $util->get_visitor_Browser();
582
583 $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));
584
585 $limit = $util->get_option_value('history_limit');
586
587 $expdate = date('Y-n-j', time() - (intval($limit) * 24 * 60 * 60));
588 $wpdb->query("delete FROM $table_name WHERE date_format(date(ctime),'%Y-%m-%d') < date_format(date('$expdate'),'%Y-%m-%d')");
589
590
591 }
592 }
593 //-------------------------------------------------------------
594 if(!function_exists("WPSR_make_redirect")) {
595
596 function WPSR_make_redirect($redirect_to, $redirect_type, $redirect_from, $obj = '')
597 {
598
599 global $wpdb, $util, $table_prefix, $post;
600
601 if (is_admin()) {
602 return 0;
603 }
604 $SR_redirect_cache = new free_SR_redirect_cache();
605 if ($redirect_to == $redirect_from || !$util->is_valid_url($redirect_to))
606 return 0;
607
608 if ($util->make_relative_url($redirect_from) == $util->make_relative_url($redirect_to))
609 return 0;
610
611 if (substr($redirect_from, -1) == "/" || substr($redirect_to, -1) == "/") {
612 if (substr($redirect_from, -1) != "/") {
613 if (($redirect_from . "/") == $redirect_to)
614 return 0;
615 } else {
616 if (($redirect_to . "/") == $redirect_from)
617 return 0;
618 }
619 }
620
621
622 if ($util->make_relative_url($redirect_from) == $util->make_relative_url($redirect_to))
623 return 0;
624
625 if (is_object($obj)) {
626 if ($obj->ID > 0) {
627 $table_name = $table_prefix . 'WP_SEO_Redirection';
628 $sql = "update " . $table_name . " set hits=hits+1, access_date= NOW() where ID='" . $obj->ID . "'";
629 $wpdb->query($sql);
630 }
631 }
632
633 if (is_object($obj) && $obj->redirect_to_type == 'Folder' && $obj->redirect_to_folder_settings == '2') {
634
635 if ($obj->redirect_from_type == 'Folder') {
636
637 if ($obj->redirect_from_folder_settings == '2' || $obj->redirect_from_folder_settings == '3') {
638 if (strlen($redirect_from) > strlen($obj->redirect_from)) {
639 $difference = substr($redirect_from, intval(strlen($obj->redirect_from) - strlen($redirect_from)));
640 $redirect_to = $redirect_to . $difference;
641 }
642 }
643
644 } else if ($obj->redirect_from_type == 'Regex') {
645 $page = substr(strrchr($redirect_from, "/"), 1);
646 $redirect_to = $redirect_to . '/' . $page;
647 }
648
649 }
650
651 $rID = 0;
652 $rsrc = '404';
653 $postID = 0;
654
655 if (is_object($obj)) {
656 $rID = $obj->ID;
657 $postID = $obj->postID;
658 if ($obj->url_type == 1)
659 $rsrc = 'Custom';
660 else if ($obj->url_type == 2)
661 $rsrc = 'Post';
662
663 }
664
665 if ($util->get_option_value('history_status') == '1') {
666
667 WPSR_log_redirection_history($rID, $postID, $redirect_from, $redirect_to, $redirect_type, $rsrc);
668 }
669
670 $redirect_to = $util->make_absolute_url($redirect_to);
671
672
673 if (is_singular()) {
674 //$SR_redirect_cache = new free_SR_redirect_cache();
675
676 $SR_redirect_cache->add_redirect($post->ID, 1, $redirect_from, $redirect_to, $redirect_type);
677 $SR_redirect_cache->free_cache();
678 }
679
680 if ($redirect_type == '301') {
681 header('HTTP/1.1 301 Moved Permanently');
682 header("Location: " . $redirect_to);
683 exit();
684 } else if ($redirect_type == '307') {
685 header('HTTP/1.0 307 Temporary Redirect');
686 header("Location: " . $redirect_to);
687 exit();
688 } else if ($redirect_type == '302') {
689 header("Location: " . $redirect_to);
690 exit();
691 }
692
693 }
694 }
695 add_filter('pre_get_table_charset', function($charset, $table) {
696 global $table_prefix;
697 $table_name = $table_prefix . 'WP_SEO_Redirection';
698 if($table == $table_name){
699 return 'utf8mb4';
700 }
701
702 }, 10, 2);
703 //-------------------------------------------------------------
704 if(!function_exists("WPSR_redirect")) {
705
706 function WPSR_redirect()
707 {
708 global $wpdb, $post, $table_prefix, $util;
709
710
711 if ($util->get_option_value('plugin_status') != '0') { // if not disabled
712
713 // if disable for admin and the user is admin
714 if (current_user_can('manage_options') == 1 && $util->get_option_value('plugin_status') == 2) {
715 // nothing
716
717 } else {
718
719 $table_name = $table_prefix . 'WP_SEO_Redirection';
720 $permalink = urldecode($util->get_current_relative_url());
721
722 if (substr($permalink, 0, 1) == ":") {
723 $first_slash = stripos($permalink, "/");
724 $permalink = substr($permalink, $first_slash, strlen($permalink) - $first_slash);
725 }
726 $permalink_alternative = "";
727 if (substr($permalink, -1) == '/') {
728 $permalink_alternative = substr($permalink, 0, intval(strlen($permalink) - 1));
729 } else {
730 $permalink_alternative = $permalink . '/';
731 }
732
733 $post_cache_result = "";
734 $SR_redirect_cache = new free_SR_redirect_cache();
735 if (is_singular()) {
736 $post_cache_result = $SR_redirect_cache->redirect_cached($post->ID);
737 }
738
739 if ($post_cache_result == 'not_redirected') {
740
741 return 0;
742 }
743
744 $permalink_options = $wpdb->prepare("( redirect_from = %s OR redirect_from = %s)", $permalink, $permalink_alternative);
745
746 $permalink_regex_options = $wpdb->prepare("(%s regexp regex or %s regexp regex )", $permalink, $permalink_alternative);
747
748
749 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))) {
750
751 $theurl = $wpdb->get_row(" select * from $table_name where enabled=1 and regex='' and $permalink_options ");
752 if ($wpdb->num_rows > 0 && $theurl->redirect_to != '') {
753 WPSR_make_redirect($theurl->redirect_to, $theurl->redirect_type, $permalink, $theurl);
754 }
755
756 $theurl = $wpdb->get_row(" select * from $table_name where enabled=1 and regex<>'' and $permalink_regex_options order by LENGTH(regex) desc ");
757
758 if ($wpdb->num_rows > 0 && $theurl->redirect_to != '') {
759 WPSR_make_redirect($theurl->redirect_to, $theurl->redirect_type, $permalink, $theurl);
760 }
761
762
763 if (is_404()) {
764
765 if ($util->get_option_value('p404_discovery_status') == '1') {
766 WPSR_log_404_redirection($permalink);
767 }
768
769 $options = $util->get_my_options();
770 if ($options['p404_status'] == '1') {
771
772 WPSR_make_redirect($options['p404_redirect_to'], '301', $permalink);
773
774 }
775 }
776 }
777
778 if (is_singular() && $post_cache_result == 'not_found') {
779 $SR_redirect_cache->add_redirect($post->ID, 0, '', '', 0);
780 }
781
782 }
783 }
784 }
785 }
786 //---------------------------------------------------------------
787 if(!function_exists("WPSR_header_code")) {
788
789 function WPSR_header_code()
790 {
791
792 $my_page = isset($_REQUEST['page']) ? $_REQUEST['page'] : '';
793 if ($my_page == 'seo-redirection.php') {
794
795 wp_register_style('c_admin_css_common', plugins_url() . '/' . basename(dirname(__FILE__)) . '/common/' . "style.css");
796 wp_enqueue_style('sweetalert', plugins_url() . '/' . basename(dirname(__FILE__)) . '/common/' . "sweetalert.css");
797 wp_register_style('c_admin_css_custom', plugins_url() . '/' . basename(dirname(__FILE__)) . '/custom/' . "style.css");
798 wp_enqueue_script('jquery');
799 wp_localize_script('jquery', 'seoredirection', array('ajax_url' => admin_url('admin-ajax.php'), 'msg' => ""));
800 wp_enqueue_style('c_admin_css_common'); $rand = rand( 1, 99999999999 ); wp_register_style('c_css_common', plugins_url() . '/' . basename(dirname(__FILE__)) . '/common/' . "custom.css", '', $rand); wp_enqueue_style('c_css_common');
801 wp_enqueue_style('c_admin_css_custom');
802
803 wp_enqueue_script('custom', plugins_url() . '/' . basename(dirname(__FILE__)) . '/common/js/' . 'bootstrap.min.js', array('jquery'), false, true);
804 wp_enqueue_script('customJS', plugins_url() . '/' . basename(dirname(__FILE__)) . '/common/' . "customJs.js", array('jquery'), 1.2, true);
805 wp_enqueue_script('sweetalert', plugins_url() . '/' . basename(dirname(__FILE__)) . '/common/js/' . "sweetalert.min.js", array('jquery'), false, true);
806
807 wp_enqueue_style('bootstrap', plugins_url() . '/' . basename(dirname(__FILE__)) . '/common/' . "bootstrap.css");
808
809
810 }
811 }
812 }
813 //---------------------------------------------------------------
814 if(!function_exists("WPSR_customAddUpdate_callback")) {
815
816 add_action("wp_ajax_customAddUpdate", "WPSR_customAddUpdate_callback");
817
818 function WPSR_customAddUpdate_callback()
819 {
820 global $wpdb, $table_prefix, $util;
821 $table_name = $table_prefix . 'WP_SEO_Redirection';
822 $table_name_404 = $table_prefix . 'WP_SEO_404_links';
823 parse_str($_POST['formData'], $_POST);
824 $nonce = "";
825 if (isset($_POST['_wpnonce']))
826 $nonce = WPSR_sanitize_text_or_array_field($_POST['_wpnonce']);
827 $data = array();
828 $data['error_string'] = array();
829 $data['inputerror'] = array();
830 $data['bool'] = TRUE;
831 if (trim($_POST['redirect_from']) == '') {
832 $data['inputerror'][] = 'redirect_from';
833 $data['error_string'][] = __("You must input the 'Redirect From' URL", "seo-redirection");
834 $data['bool'] = FALSE;
835 }
836
837 $redirect_from = isset($_POST['redirect_from']) ? WPSR_sanitize_text_or_array_field($_POST['redirect_from']) : '';
838
839 /* $wpdb->get_results(" select ID from $table_name where redirect_from='" . trim($redirect_from) . "' ");
840 if ($wpdb->num_rows > 0) {
841 $data['inputerror'][] = 'redirect_from';
842 $data['error_string'][] = __("This 'Redirect From' value already exists in database!", "seo-redirection");
843 $data['bool'] = FALSE;
844 }
845
846 */
847 // elseif (!preg_match( '/((http|https)\:\/\/)?[a-zA-Z0-9\.\/\?\:@\-_=#]+\.([a-zA-Z0-9\&\.\/\?\:@\-_=#])*/', $_POST['redirect_from'])) {
848 // $data['inputerror'][] = 'redirect_from';
849 // $data['error_string'][] = __("Invalid redirect from target URL!",'seo-redirection');
850 // $data['bool'] = FALSE;
851 // }
852 if (trim($_POST['redirect_to']) == '') {
853 $data['inputerror'][] = 'redirect_to';
854 $data['error_string'][] = __("You must input the 'Redirect To' URL", "seo-redirection");
855 $data['bool'] = FALSE;
856 } elseif ($_POST['edit_exist'] == '' && substr(strtolower($_POST['redirect_to']), 0, 4) != "http" && substr(strtolower($_POST['redirect_to']), 0, 1) != "/") {
857 $data['inputerror'][] = 'redirect_to';
858 $data['error_string'][] = __("Invalid redirect target URL!", 'seo-redirection');
859 $data['bool'] = FALSE;
860 }
861 if ($data['bool'] === FALSE) {
862 echo json_encode($data);
863 exit();
864 } else {
865 if ($_POST['redirect_from'] != '' && wp_verify_nonce($nonce, 'seoredirection')) {
866
867
868 $redirect_from = isset($_POST['redirect_from']) ? WPSR_sanitize_text_or_array_field($_POST['redirect_from']) : '';
869 $redirect_to = isset($_POST['redirect_to']) ? WPSR_sanitize_text_or_array_field($_POST['redirect_to']) : '';
870
871
872 $redirect_from = urldecode($util->make_relative_url($redirect_from));
873
874 $redirect_to = $util->make_relative_url($redirect_to);
875 $redirect_type = WPSR_sanitize_text_or_array_field($_POST['redirect_type']);
876
877 $redirect_from_type = WPSR_sanitize_text_or_array_field($_POST['redirect_from_type']);
878 $redirect_from_folder_settings = WPSR_sanitize_text_or_array_field($_POST['redirect_from_folder_settings']);
879 $redirect_from_subfolders = WPSR_sanitize_text_or_array_field($_POST['redirect_from_subfolders']);
880
881 $redirect_to_type = WPSR_sanitize_text_or_array_field($_POST['redirect_to_type']);
882 $redirect_to_folder_settings = WPSR_sanitize_text_or_array_field($_POST['redirect_to_folder_settings']);
883
884 $enabled = WPSR_sanitize_text_or_array_field($_POST['enabled']);
885
886 $regex = "";
887
888 if ($redirect_from_type == 'Folder') {
889
890 if (substr($redirect_from, -1) != '/')
891 $redirect_from = $redirect_from . '/';
892
893 if ($redirect_from_folder_settings == 2) {
894 if ($redirect_from_subfolders == 0) {
895 $regex = '^' . $util->regex_prepare($redirect_from) . '.*';;
896 } else {
897 $regex = '^' . $util->regex_prepare($redirect_from) . '[^/]*$';
898 }
899 } else if ($redirect_from_folder_settings == 3) {
900 if ($redirect_from_subfolders == 0) {
901 $regex = '^' . $util->regex_prepare($redirect_from) . '.+';
902 } else {
903 $regex = '^' . $util->regex_prepare($redirect_from) . '[^/]+$';
904 }
905 }
906
907 } else if ($redirect_from_type == 'Regex') {
908 $regex = $redirect_from;
909 }
910
911 if ($redirect_from_type == 'Page' || $redirect_from_type == 'Regex') {
912 $redirect_from_folder_settings = "";
913 $redirect_from_subfolders = "";
914 }
915
916 if ($redirect_to_type == 'Page') {
917 $redirect_to_folder_settings = "";
918 }
919
920 if ($redirect_to_type == 'Folder') {
921 if (substr($redirect_to, -1) != '/')
922 $redirect_to = $redirect_to . '/';
923 }
924
925
926 if ($_POST['add_new'] != '') {
927
928 $theurl = $wpdb->get_row($wpdb->prepare(" select count(ID) as cnt from $table_name where redirect_from=%s ", $redirect_from));
929
930 if ($theurl->cnt > 0) {
931 $msg = __("This URL", 'seo-redirection') . " <b>".esc_html($redirect_from)."</b>" . __("is added previously!", 'seo-redirection');
932 echo json_encode(array('status' => 'error', 'msg' => $msg));
933 // $util->failure_option_msg(__("This URL",'seo-redirection')." <b>'$redirect_from'</b>". __("is added previously!",'seo-redirection'));
934 } else {
935
936
937 if ($redirect_from == '' || $redirect_to == '' || $redirect_type == '') {
938 $util->failure_option_msg(__('Please input all required fields!', 'seo-redirection'));
939 } else {
940
941 $wpdb->insert($table_name, array(
942 'redirect_from' => $redirect_from,
943 'redirect_to' => $redirect_to,
944 'redirect_type' => $redirect_type,
945 'url_type' => 1,
946 'redirect_from_type' => $redirect_from_type,
947 'redirect_from_folder_settings' => $redirect_from_folder_settings,
948 'redirect_from_subfolders' => $redirect_from_subfolders,
949 'redirect_to_type' => $redirect_to_type,
950 'redirect_to_folder_settings' => $redirect_to_folder_settings,
951 'regex' => $regex,
952 'enabled' => $enabled
953
954 ));
955
956 $wpdb->query($wpdb->prepare(" delete from $table_name_404 where link=%s ", $redirect_from));
957 $SR_redirect_cache = new free_SR_redirect_cache();
958 $SR_redirect_cache->free_cache();
959 $msg = "Redirection Added Successfully";
960 echo json_encode(array('status' => 'success', 'msg' => $msg, 'url' => admin_url('options-general.php?page=seo-redirection.php')));
961 die;
962 }
963
964 }
965 } else if ($_POST['edit_exist'] != '') {
966
967 $edit = WPSR_sanitize_text_or_array_field($_POST['edit']);
968
969 if ($redirect_from == '' || $redirect_to == '' || $redirect_type == '') {
970 $util->failure_option_msg('Please input all required fields!');
971 } else {
972
973 $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));
974
975 $SR_redirect_cache = new free_SR_redirect_cache();
976 $SR_redirect_cache->free_cache();
977 }
978 $msg = "Redirection Update Successfully";
979 echo json_encode(array('status' => 'success', 'msg' => $msg, 'url' => admin_url('options-general.php?page=seo-redirection.php')));
980 die;
981
982 }
983
984 if ($util->there_is_cache() != '')
985 $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'));
986
987 }
988
989 }
990
991
992 die;
993 }
994 }
995 if(!function_exists("WPSR_customUpdateRec_callback")) {
996
997 add_action("wp_ajax_customUpdateRec", "WPSR_customUpdateRec_callback");
998 function WPSR_customUpdateRec_callback()
999 {
1000
1001
1002 global $wpdb, $table_prefix, $util;
1003
1004 $table_name = $table_prefix . 'WP_SEO_Redirection';
1005 $table_name_404 = $table_prefix . 'WP_SEO_404_links';
1006
1007 $myid = (int)$_POST['ID'];
1008 $item = $wpdb->get_row($wpdb->prepare(" select * from $table_name where ID=%d ", $myid));
1009
1010 if ($wpdb->num_rows == 0) {
1011 echo json_encode(array('status' => 'error', 'msg' => __("Sorry, this redirect rule is not found, it may deleted by the user!", 'seo-redirection')));
1012 die;
1013 }
1014
1015
1016 $data = array(
1017 "redirect_from" => $item->redirect_from,
1018 "redirect_to" => $item->redirect_to,
1019 "redirect_type" => $item->redirect_type,
1020
1021 "redirect_from_type" => $item->redirect_from_type,
1022 "redirect_from_folder_settings" => $item->redirect_from_folder_settings,
1023 "redirect_from_subfolders" => $item->redirect_from_subfolders,
1024
1025 "redirect_to_type" => $item->redirect_to_type,
1026 "redirect_to_folder_settings" => $item->redirect_to_folder_settings,
1027
1028 "enabled" => $item->enabled
1029 );
1030 echo json_encode(array('status' => 'suucess', 'rec' => $data));
1031 die;
1032 }
1033 }
1034
1035 if(!function_exists("WPSR_admin_menu")) {
1036
1037 function WPSR_admin_menu()
1038 {
1039 add_options_page('SEO Redirection', 'SEO Redirection', 'manage_options', basename(__FILE__), 'WPSR_options_menu');
1040 }
1041 }
1042 //---------------------------------------------------------------
1043 if(!function_exists("WPSR_options_menu")) {
1044
1045 function WPSR_options_menu(){
1046 global $util;
1047
1048 if (!current_user_can('manage_options')) {
1049 wp_die(__('You do not have sufficient permissions to access this page.', 'seo-redirection'));
1050 }
1051
1052
1053 if ($util->get_option_value('plugin_status') == '0') {
1054 $util->info_option_msg(__('SEO Redirection is disabled now, you can go to option tab and enable it!', 'seo-redirection'));
1055 } else if ($util->get_option_value('plugin_status') == '2') {
1056 $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'));
1057 }
1058 $total_404_errors = (WPSR_Get_total_404() > -1) ? __('You have', 'seo-redirection') . ' <b style="color:red; background-color:yellow; padding:3px;">' . intval(WPSR_Get_total_404()) .'</b>' . __(' broken link (404 links)', 'seo-redirection') . ', <br>' : '';
1059
1060
1061 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/>';
1062
1063 if ($total_404_errors != '') {
1064 ?>
1065 <script type="text/javascript">
1066
1067 seoredirection.msg = '<?php echo wp_kses_post($total_404_errors); ?>';
1068
1069 </script>
1070
1071 <?php
1072 }
1073
1074 if (is_multisite()) {
1075
1076 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/">
1077 https://www.wp-buy.com/product/seo-redirection-premium-wordpress-plugin/</a>) <p></p></div>';
1078
1079 }
1080
1081 $mytabs = new phptab();
1082
1083 $mytabs->set_ignore_parameter(array( 'search', 'page_num', 'add', 'edit', 'page404', 'do_404_del'));
1084 $mytabs->add_file_tab('cutom', __('Custom Redirects', 'seo-redirection'), 'option_page_custome_redirection.php', 'file');
1085 $mytabs->add_file_tab('posts', __('Post Redirects', 'seo-redirection'), 'option_page_post_redirection_list.php', 'file');
1086 $mytabs->add_file_tab('history', __('History', 'seo-redirection'), 'option_page_history.php', 'file');
1087 $mytabs->add_file_tab('404', '<span style="color:red;"><b>404 Errors</b></span>', 'option_page_404.php', 'file');
1088 $mytabs->add_file_tab('goptions', __('Options', 'seo-redirection'), 'option_page_goptions.php', 'file');
1089 $mytabs->add_file_tab('help', '<span style="color:green;"><b>' . __('Help', 'seo-redirection') . '</b></span>', 'help.php', 'file');
1090 $mytabs->add_file_tab('premium', '<span style="color:brown;"><b>&#9658; ' . __('Premium Features', 'seo-redirection') . '</b></span>', 'premium.php', 'file');
1091 $mytabs->run();
1092
1093 $imgpath = $util->get_plugin_url() . 'custom/images/';
1094
1095 echo '<p>&nbsp;</p><p style="color:green"><a target="_blank" href="http://www.wp-buy.com/product/seo-redirection-premium-wordpress-plugin"><b>' . __("Upgrade to premium version now", "wsr") . '</b></a>' . __(" to get more features", "wsr") . ' , <small>' . __("The premium version of SEO redirection is completely different from the free version as there are a lot more features included.", "wsr") . '</small></p>';
1096 echo __('<p><a href="https://www.wp-buy.com/product/seo-redirection-premium-wordpress-plugin" target="_blank"><img src="' . $imgpath . 'seopro.png" /></a></p>');
1097 }
1098 }
1099 if(!function_exists("WPSR_upgrade")) {
1100
1101 function WPSR_upgrade()
1102 {
1103
1104 $util = new clogica_util_1();
1105 $util->init(WP_SEO_REDIRECTION_OPTIONS, __FILE__);
1106
1107 if ($util->get_option_value('plugin_version') != WP_SEO_REDIRECTION_VERSION) {
1108 WPSR_install();
1109 $util->update_option('plugin_version', WP_SEO_REDIRECTION_VERSION);
1110 }
1111 }
1112 }
1113
1114 //-----------------------------------------------------
1115
1116 if(!function_exists("WPSR_install")) {
1117
1118 function WPSR_install()
1119 {
1120 global $wpdb, $table_prefix;
1121
1122 $util = new clogica_util_1();
1123 $util->init(WP_SEO_REDIRECTION_OPTIONS, __FILE__);
1124
1125 $options = get_option(WP_SEO_REDIRECTION_OPTIONS);
1126 if (!is_array($options)) {
1127 add_option(WP_SEO_REDIRECTION_OPTIONS);
1128 $options = array();
1129 }
1130
1131
1132 if (!array_key_exists('plugin_status', $options))
1133 $options['plugin_status'] = '1';
1134
1135 if (!array_key_exists('ip_logging_status', $options))
1136 $options['ip_logging_status'] = '1';
1137
1138 if (!array_key_exists('redirection_base', $options))
1139 $options['redirection_base'] = site_url();
1140
1141 if (!array_key_exists('redirect_control_panel', $options))
1142 $options['redirect_control_panel'] = '1';
1143
1144 if (!array_key_exists('show_redirect_box', $options))
1145 $options['show_redirect_box'] = '1';
1146
1147 if (!array_key_exists('reflect_modifications', $options))
1148 $options['reflect_modifications'] = '1';
1149
1150 if (!array_key_exists('history_status', $options))
1151 $options['history_status'] = '1';
1152
1153 if (!array_key_exists('history_limit', $options))
1154 $options['history_limit'] = '30';
1155
1156 if (!array_key_exists('p404_discovery_status', $options))
1157 $options['p404_discovery_status'] = '1';
1158
1159 if (!array_key_exists('p404_redirect_to', $options))
1160 $options['p404_redirect_to'] = site_url();
1161
1162 if (!array_key_exists('p404_status', $options))
1163 $options['p404_status'] = '2';
1164
1165 if (!array_key_exists('keep_data', $options))
1166 $options['keep_data'] = '1';
1167
1168 update_option(WP_SEO_REDIRECTION_OPTIONS, $options);
1169
1170
1171 $table_name = $table_prefix . 'WP_SEO_Redirection';
1172 if (strtolower($wpdb->get_var("show tables like '$table_name'")) != strtolower($table_name)) {
1173 $sql = "CREATE TABLE IF NOT EXISTS `$table_name` (
1174 `ID` int(11) unsigned NOT NULL AUTO_INCREMENT,
1175 `enabled` int(1) NOT NULL DEFAULT '1',
1176 `redirect_from` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
1177 `redirect_from_type` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1178 `redirect_from_folder_settings` int(1) NOT NULL,
1179 `redirect_from_subfolders` int(1) NOT NULL DEFAULT '1',
1180 `redirect_to` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1181 `redirect_to_type` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1182 `redirect_to_folder_settings` int(1) NOT NULL DEFAULT '1',
1183 `regex` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1184 `redirect_type` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1185 `url_type` int(2) NOT NULL DEFAULT 1,
1186 `postID` int(11) unsigned DEFAULT NULL,
1187 `import_flag` tinyint(1) NOT NULL DEFAULT 0,
1188 `hits` int(11) unsigned NOT NULL DEFAULT 0,
1189 `access_date` datetime DEFAULT NULL,
1190 PRIMARY KEY (`ID`),
1191 UNIQUE KEY `redirect_from` (`redirect_from`)
1192 )ENGINE = MyISAM ;";
1193 $wpdb->query($sql);
1194
1195
1196 } else {
1197 //check if Innodb convert it to myisam.
1198 $status = $wpdb->get_row("SHOW TABLE STATUS WHERE Name = '$table_name'");
1199 if ($status->Engine == 'InnoDB') {
1200 $wpdb->query("alter table $table_name engine = MyISAM;");
1201 }
1202
1203 /* add column for import flag */
1204 $column_data = $wpdb->get_row("SHOW COLUMNS FROM $table_name LIKE 'import_flag'");
1205
1206 if (!$column_data) {
1207 $wpdb->query("ALTER TABLE $table_name ADD COLUMN import_flag tinyint(1) DEFAULT 0");
1208 }
1209
1210 $wpdb->query("ALTER TABLE $table_name CHANGE `redirect_from` `redirect_from` VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL;");
1211
1212 // if the table exists
1213 $redirects = $wpdb->get_results(" select redirect_from,redirect_to,ID from $table_name; ");
1214 foreach ($redirects as $redirect) {
1215 $redirect_from = $util->make_relative_url($redirect->redirect_from);
1216 $redirect_to = $util->make_relative_url($redirect->redirect_to);
1217 $ID = $redirect->ID;
1218 $wpdb->query($wpdb->prepare(" update $table_name set redirect_from=%s,redirect_to=%s where ID=%d", $redirect_from, $redirect_to, $ID));
1219 }
1220
1221 // Fix add blog field if not exist.
1222 if ($wpdb->get_var(" SELECT count(*) as cnt FROM INFORMATION_SCHEMA.COLUMNS
1223 WHERE TABLE_NAME = '$table_name'
1224 AND table_schema = DATABASE()
1225 AND COLUMN_NAME = 'hits' ") == '0') {
1226
1227 $sql = "
1228 ALTER TABLE $table_name
1229 ADD COLUMN `hits` int(11) unsigned NOT NULL DEFAULT 0,
1230 ADD COLUMN `access_date` datetime DEFAULT NULL;
1231 ";
1232
1233 $wpdb->query($sql);
1234 }
1235
1236
1237 }
1238
1239 $table_name = $table_prefix . 'WP_SEO_Cache';
1240 if (strtolower($wpdb->get_var("show tables like '$table_name'")) != strtolower($table_name)) {
1241 $sql = "CREATE TABLE IF NOT EXISTS `$table_name` (
1242 `ID` int(11) unsigned NOT NULL,
1243 `is_redirected` int(1) unsigned NOT NULL,
1244 `redirect_from` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1245 `redirect_to` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1246 `redirect_type` int(3) unsigned NOT NULL DEFAULT 301,
1247 PRIMARY KEY (`ID`)
1248 ) ENGINE = MyISAM ;
1249 ";
1250 $wpdb->query($sql);
1251 } else {
1252 //check if Innodb convert it to myisam.
1253 $status = $wpdb->get_row("SHOW TABLE STATUS WHERE Name = '$table_name'");
1254 if ($status->Engine == 'InnoDB') {
1255 $wpdb->query("alter table $table_name engine = MyISAM;");
1256 }
1257 }
1258
1259
1260 $res = $wpdb->get_var(" SELECT count(*) as cnt FROM INFORMATION_SCHEMA.COLUMNS
1261 WHERE TABLE_NAME = '$table_name'
1262 AND table_schema = DATABASE()
1263 AND COLUMN_NAME = 'redirect_from' ");
1264
1265 if ($res == '0') {
1266
1267 $sql = "
1268 ALTER TABLE $table_name
1269 ADD COLUMN `redirect_from` varchar(255) COLLATE utf8_unicode_ci NOT NULL;
1270 ";
1271 $wpdb->query($sql);
1272 }
1273
1274
1275 $table_name = $table_prefix . 'WP_SEO_404_links';
1276 if (strtolower($wpdb->get_var("show tables like '$table_name'")) != strtolower($table_name)) {
1277 $sql = "CREATE TABLE IF NOT EXISTS `$table_name` (
1278 `ID` int(11) unsigned NOT NULL AUTO_INCREMENT,
1279 `ctime` datetime NOT NULL,
1280 `link` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1281 `referrer` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1282 `ip` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
1283 `country` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
1284 `os` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
1285 `browser` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
1286 PRIMARY KEY (`ID`),
1287 UNIQUE KEY `link` (`link`)
1288 ) ENGINE = MyISAM ;
1289 ";
1290 $wpdb->query($sql);
1291 } else {
1292 //check if Innodb convert it to myisam.
1293 $status = $wpdb->get_row("SHOW TABLE STATUS WHERE Name = '$table_name'");
1294 if ($status->Engine == 'InnoDB') {
1295 $wpdb->query("alter table $table_name engine = MyISAM;");
1296 }
1297 }
1298
1299
1300 $table_name = $table_prefix . 'WP_SEO_Redirection_LOG';
1301 if (strtolower($wpdb->get_var("show tables like '$table_name'")) != strtolower($table_name)) {
1302 $sql = "CREATE TABLE IF NOT EXISTS `$table_name` (
1303 `ID` int(11) unsigned NOT NULL AUTO_INCREMENT,
1304 `rID` int(11) unsigned DEFAULT NULL,
1305 `postID` int(11) unsigned DEFAULT NULL,
1306 `ctime` datetime NOT NULL,
1307 `rfrom` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1308 `rto` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1309 `rtype` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1310 `rsrc` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
1311 `referrer` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
1312 `ip` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
1313 `country` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
1314 `os` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
1315 `browser` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
1316 PRIMARY KEY (`ID`)
1317 ) ENGINE = MyISAM ;
1318 ";
1319
1320 $wpdb->query($sql);
1321 } else {
1322 //check if Innodb convert it to myisam.
1323 $status = $wpdb->get_row("SHOW TABLE STATUS WHERE Name = '$table_name'");
1324 if ($status->Engine == 'InnoDB') {
1325 $wpdb->query("alter table $table_name engine = MyISAM;");
1326 }
1327
1328 $res = $wpdb->get_var(" SELECT count(*) as cnt FROM INFORMATION_SCHEMA.COLUMNS
1329 WHERE TABLE_NAME = '$table_name'
1330 AND table_schema = DATABASE()
1331 AND COLUMN_NAME = 'postID' ");
1332
1333 if ($res == '0') {
1334
1335 $sql = "
1336 ALTER TABLE $table_name
1337 ADD COLUMN `postID` int(11) unsigned DEFAULT NULL;
1338 ";
1339 $wpdb->query($sql);
1340 }
1341
1342
1343 }
1344
1345 }
1346
1347 }
1348 //---------------------------------------------------------------
1349
1350 if(!function_exists("WPSR_uninstall")) {
1351
1352 function WPSR_uninstall()
1353 {
1354 global $wpdb, $table_prefix;
1355
1356 $util = new clogica_util_1();
1357 $util->init(WP_SEO_REDIRECTION_OPTIONS, __FILE__);
1358
1359
1360 if ($util->get_option_value('keep_data') != '1') {
1361
1362 $table_name = $table_prefix . 'WP_SEO_Redirection';
1363 $wpdb->query($wpdb->prepare(" DROP TABLE %s ", $table_name));
1364
1365 $table_name = $table_prefix . 'WP_SEO_Cache';
1366 $wpdb->query($wpdb->prepare(" DROP TABLE %s ", $table_name));
1367
1368 $table_name = $table_prefix . 'WP_SEO_404_links';
1369 $wpdb->query($wpdb->prepare(" DROP TABLE %s ", $table_name));
1370
1371 $table_name = $table_prefix . 'WP_SEO_Redirection_LOG';
1372 $wpdb->query($wpdb->prepare(" DROP TABLE %s ", $table_name));
1373
1374
1375 $util->delete_my_options();
1376 }
1377
1378
1379 }
1380
1381 }
1382 //---------------------------------------------------------------
1383 if(!function_exists("WPSR_HideMessageAjaxFunction")) {
1384
1385 function WPSR_HideMessageAjaxFunction()
1386 {
1387 add_option('nsr_upgrade_message', 'yes');
1388 }
1389 }
1390 //---------------------------------------------------------------
1391 if(!function_exists("WPSR_admin_notice_callback")) {
1392
1393 /* display import from redirection plugin in admin notice */
1394 function WPSR_admin_notice_callback() {
1395 global $wpdb;
1396 global $current_user;
1397 $plugins = get_option( 'active_plugins', array() );
1398 $found = false;
1399 $user_id = $current_user->ID;
1400 $val = get_user_meta($user_id, 'sr_notice_dismissed',true);
1401
1402 foreach ( $plugins as $plugin )
1403 {
1404 if ( strpos( strval($plugin), 'redirection.php' ) == true && $val != 1 && strpos( strval($plugin), 'seo-redirection.php' ) == FALSE )
1405 {
1406 $found = true;
1407 //$total = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_items");
1408 $total = WPSR_getRedirectCount();
1409 if($total > 0){
1410 ?>
1411 <div class="notice-success notice is-dismissible sr_notice">
1412 <p>
1413 <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'); ?>
1414 <?php
1415 $SR_import = isset($_GET['SR_import']) ? sanitize_text_field($_GET['SR_import']) : '';
1416 if($SR_import == 'yes'){?>
1417 <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>
1418 <?php }else{ ?>
1419 <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>
1420 <?php }?>
1421 </p>
1422 </div>
1423 <?php
1424 }
1425 break;
1426 }
1427 }
1428 }
1429 add_action( 'admin_notices', 'WPSR_admin_notice_callback' );
1430 }
1431
1432 if(!function_exists("WPSR_dismiss_notice_callback")) {
1433
1434 add_action('wp_ajax_sr_dismiss_notice', 'WPSR_dismiss_notice_callback');
1435 function WPSR_dismiss_notice_callback()
1436 {
1437 global $current_user;
1438 $user_id = $current_user->ID;
1439 update_user_meta($user_id, 'sr_notice_dismissed', '1');
1440 echo "1";
1441 exit;
1442 }
1443 }
1444 if(!function_exists("WPSR_getRedirectCount")) {
1445
1446 function WPSR_getRedirectCount()
1447 {
1448 global $wpdb;
1449
1450 $table_name_ = $wpdb->prefix . 'redirection_items';
1451 if (strtolower($wpdb->get_var("show tables like '$table_name_'")) == strtolower($table_name_)) {
1452 $result = $wpdb->get_results("SELECT url FROM {$wpdb->prefix}redirection_items");
1453 $cnt = 0;
1454 $table_name = $wpdb->prefix . 'WP_SEO_Redirection';
1455 if ($result) {
1456 foreach ($result as $redirect) {
1457 $redirect_from = stripslashes($redirect->url);
1458 $redirect_from_slash = ltrim(stripslashes($redirect->url), '/');
1459 $redirectID = $wpdb->get_var($wpdb->prepare("select ID from $table_name where redirect_from=%s or redirect_from=%s", $redirect_from, $redirect_from_slash));
1460 if ($redirectID > 0) {
1461
1462 } else {
1463 $cnt++;
1464 }
1465 }
1466
1467 }
1468 return $cnt;
1469 } else {
1470 return 0;
1471 }
1472 }
1473 }
1474 if(!function_exists("SR_init_delete_callback")) {
1475
1476 add_action('admin_init', 'SR_init_delete_callback');
1477 function SR_init_delete_callback()
1478 {
1479 if (isset($_POST['redirect_id']) && count($_POST['redirect_id']) > 0) {
1480
1481 $nonce = '';
1482 if(isset($_REQUEST['_wpnonce']))
1483 $nonce = WPSR_sanitize_text_or_array_field($_REQUEST['_wpnonce']);
1484
1485 if(wp_verify_nonce( $nonce, 'seoredirection' )){
1486
1487 global $wpdb, $table_prefix, $util;
1488 $table_name = $wpdb->prefix . 'WP_SEO_Redirection';
1489 foreach ($_POST['redirect_id'] as $post_id) {
1490 $post_id = (int)$post_id;
1491 $wpdb->query($wpdb->prepare(" delete from $table_name where ID=%s ", $post_id));
1492 $SR_redirect_cache = new free_SR_redirect_cache();
1493 $SR_redirect_cache->free_cache();
1494 }
1495 }
1496 }
1497 }
1498 }
1499
1500
1501
1502
1503 //---------------------------------------------------------------
1504
1505 function WPSR_HideMessageAjaxFunction()
1506 {
1507 add_option('nsr_upgrade_message', 'yes');
1508 }
1509
1510
1511 function WPSR_after_plugin_row($plugin_file, $plugin_data, $status)
1512 {
1513
1514 if (get_option('nsr_upgrade_message') != 'yes') {
1515 $class_name = $plugin_data['slug'];
1516 $class_name = isset($plugin_data['slug']) ? $plugin_data['slug'] : 'seo-redirection';
1517
1518 echo '<tr id="' . $class_name . '-plugin-update-tr" class="plugin-update-tr active">';
1519 echo '<td colspan="6" class="plugin-update">';
1520 echo '<div id="' . $class_name . '-upgradeMsg" class="update-message notice inline notice-warning notice-alt" >';
1521
1522 echo 'You are running SEO redirection free. To get more features, you can <a href="http://www.wp-buy.com/product/seo-redirection-premium-wordpress-plugin" target="_blank"><strong>upgrade now</strong></a> or ';
1523
1524 echo '<span id="HideMe" style="cursor:pointer" ><a href="javascript:void(0)"><strong>dismiss</strong></a> this message</span>';
1525 echo '</div>';
1526 echo '</td>';
1527 echo '</tr>';
1528
1529 ?>
1530
1531 <script type="text/javascript">
1532 jQuery(document).ready(function () {
1533 var row = jQuery('#<?php echo $class_name;?>-plugin-update-tr').closest('tr').prev();
1534 jQuery(row).addClass('update');
1535
1536 jQuery("#HideMe").click(function () {
1537 jQuery.ajax({
1538 type: 'POST',
1539 url: '<?php echo admin_url();?>/admin-ajax.php',
1540 data: {
1541 action: 'WPSR_HideMessageAjaxFunction'
1542 },
1543 success: function (data, textStatus, XMLHttpRequest) {
1544
1545 jQuery("#<?php echo $class_name;?>-upgradeMsg").hide();
1546
1547 },
1548 error: function (MLHttpRequest, textStatus, errorThrown) {
1549 alert(errorThrown);
1550 }
1551 });
1552 });
1553
1554 });
1555 </script>
1556
1557 <?php
1558 }
1559 }
1560
1561 $path = plugin_basename(__FILE__);
1562 add_action("after_plugin_row_{$path}", 'WPSR_after_plugin_row', 10, 3);
1563 // creating Ajax call for WordPress
1564 add_action('wp_ajax_nopriv_WPSR_HideMessageAjaxFunction', 'WPSR_HideMessageAjaxFunction');
1565 add_action('wp_ajax_WPSR_HideMessageAjaxFunction', 'WPSR_HideMessageAjaxFunction');
1566 //---------------------------------------------------------------
1567
1568 ?>
1569