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

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