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

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