PluginProbe
Quick 301 Redirects / trunk
Quick 301 Redirects vtrunk
trunk 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8
quick-301-redirects / quick-301-redirects.php

quick-301-redirects.php in Quick 301 Redirects trunk, at quick-301-redirects.php

566 lines 19.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Quick 301 Redirects
4 Contributors: galdub, tomeraharon, premio
5 Description:The fastest and easiest way to do 301 redirects.
6 Author: Premio
7 Author URI: https://premio.io/
8 Version: 1.1.8
9 License: GPLv3
10 */
11 function quick_301_redirects_load_plugin_textdomain() {
12 load_plugin_textdomain( 'quick-301-redirects', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
13 }
14 add_action( 'plugins_loaded', 'quick_301_redirects_load_plugin_textdomain' );
15
16
17 /**
18 * Redirect to plugin setting page after activate the plugin.
19 *
20 * @since 1.0.0
21 */
22 function quick_301_redirects_activation_redirect( $plugin ) {
23 if( $plugin == plugin_basename( __FILE__ ) ) {
24 if(!defined( 'DOING_AJAX' )) {
25 add_option("redirection_301", 1);
26 }
27 }
28 }
29 add_action( 'activated_plugin', 'quick_301_redirects_activation_redirect' );
30
31 /**
32 * check_for_prm_redirection function
33 * Checks if a redirection is needed based on the status option.
34 * If a redirection is needed, it deletes the option, redirects to the quick 301 redirects admin page and exits the script.
35 *
36 * @return void
37 */
38 if(!function_exists("check_for_prm_redirection")) {
39 function check_for_prm_redirection()
40 {
41 if(!defined( 'DOING_AJAX' )) {
42 $status = get_option("redirection_301");
43 if($status) {
44 delete_option("redirection_301");
45 wp_redirect(admin_url('admin.php?page=quick-301-redirects'));
46 exit;
47 }
48 }
49
50 if(isset($_GET['quick_301_redirects_plugins_page'])) {
51 $nonce = isset($_GET['qtr_nonce'])?esc_attr($_GET['qtr_nonce']):'';
52 if($nonce && wp_verify_nonce($nonce, 'quick_301_redirects_plugins_page')) {
53 add_option('quick_301_redirects_plugins_page', 1);
54 wp_redirect(admin_url("admin.php?page=quick-301-redirects"));
55 exit;
56 }
57 }
58 }
59
60 add_action("admin_init", "check_for_prm_redirection");
61 }
62
63 /**
64 * Plug-in activation - settings link.
65 * Settings link in plugin page.
66 * @param array $links
67 *
68 * @since 1.0.0
69 */
70 function quick_301_redirects_action_links( $links ){
71
72 $plugin_links = array( '<a href="' . admin_url( 'admin.php?page=quick-301-redirects' ) . '">' . __( 'Settings', 'quick-301-redirects' ) . '</a>' );
73
74 return array_merge( $plugin_links, $links );
75 }
76 add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'quick_301_redirects_action_links' );
77
78 /**
79 * Enqueue admin scripts and styles.
80 *
81 * @Since 1.0
82 */
83 function quick_301_redirects_admin_scripts(){
84 wp_enqueue_style( 'wp-jquery-ui-dialog' );
85 wp_enqueue_script( 'jquery-ui-dialog' );
86
87 wp_enqueue_script( 'jquery-session', plugins_url('/', __FILE__). 'js/jquery.session.js', array('jquery') );
88 }
89 add_action( 'admin_enqueue_scripts', 'quick_301_redirects_admin_scripts' );
90 /**
91 * quick_301_redirects_menu function
92 * generate the link to the options page under settings
93 *
94 * @Since 1.0
95 */
96 function quick_301_redirects_menu(){
97 add_menu_page(
98 esc_attr__('Quick 301 Redirects', 'quick-301-redirects'),
99 esc_attr__('Quick 301 Redirects', 'quick-301-redirects'),
100 'manage_options',
101 'quick-301-redirects',
102 'quick_301_redirects_options_page',
103 'dashicons-admin-links'
104 );
105 add_submenu_page(
106 'quick-301-redirects',
107 esc_attr__('Settings', 'quick-301-redirects'),
108 esc_attr__('Settings', 'quick-301-redirects'),
109 'manage_options',
110 'quick-301-redirects',
111 'quick_301_redirects_options_page'
112 );
113 $hasPluginPage = get_option("quick_301_redirects_plugins_page");
114 if($hasPluginPage != 1) {
115 add_submenu_page(
116 'quick-301-redirects',
117 esc_attr__('Recommended Plugins', 'quick-301-redirects'),
118 esc_attr__('Recommended Plugins', 'quick-301-redirects'),
119 'manage_options',
120 'quick-301-redirects-plugins',
121 'quick_301_redirects_recommended_plugins'
122 );
123 }
124 }
125 add_action('admin_menu', 'quick_301_redirects_menu');
126
127
128 function quick_301_redirects_recommended_plugins()
129 {
130 include plugin_dir_path( __FILE__ ) . 'recommended-plugins.php';
131 }
132
133 /**
134 * quick_301_redirects_options_page function
135 * generate the options page in the wordpress admin inside settings menu
136 *
137 * @Since 1.0
138 */
139 function quick_301_redirects_options_page(){
140
141 if (isset($_POST['quick_301_redirects_']) && !empty($_POST['quick_301_redirects_'])) {
142 quick_301_redirects_save_data();
143 ?>
144 <div id="message" class="updated"><p><?php _e( 'Settings saved', 'quick-301-redirects' );?></p></div>
145 <?php
146 }
147 ?>
148 <div class="wrap quick-301-redirects-wrap" dir="ltr">
149 <h1 class="wp-heading-inline"><?php _e( 'Quick 301 Redirects', 'quick-301-redirects' );?></h1>
150 <?php
151 if ( isset($_GET['bulk_upload_successfull']) && $_GET['bulk_upload_successfull'] == true) {
152
153 echo "<div class='updated inline'><p>";
154 _e( 'Upload Bluk csv file successfully.', 'woocommerce' ) ;
155 echo "</p></div>";
156 }
157 ?>
158 <div class="stuffbox" style="padding: 20px 20px 0; margin-bottom:50px; margin-top:20px">
159 <form method="post" id="quick_301_redirects_form" action="admin.php?page=quick-301-redirects">
160
161 <table id="quick-301-redirects-tbl" class="widefat quick-301-redirects">
162 <thead>
163 <tr>
164 <th colspan="2" style="text-align:left;"><strong><?php _e( 'Request', 'quick-301-redirects' );?></strong></th>
165 <th colspan="2" style="text-align:left;"><strong><?php _e( 'Destination', 'quick-301-redirects' );?></strong></th>
166 </tr>
167 </thead>
168 <tbody>
169 <tr>
170 <td colspan="2"><small><?php _e( 'Example: /page.html', 'quick-301-redirects' );?></small></td>
171 <td colspan="2"><small><?php printf(__( 'Example: %s/page/', 'quick-301-redirects'), get_option('home') );?></small></td>
172 </tr>
173 <?php quick_301_redirects_data_lists(); ?>
174 <tr id="quick-301-redirects-main">
175 <td style="width:45%;">
176 <input type="text" name="quick_301_redirects_[request][]" value="" class="regular-text" style="width:95%;" />
177 <p class="description"><?php esc_html_e( 'if / isn\'t added in the request part, add it yourself in the beginning (otherwise it won\'t work)', 'quick-301-redirects');?></p>
178 </td>
179 <td style="width:1%;">&rarr;</td>
180 <td style="width:45%;">
181 <input type="text" name="quick_301_redirects_[destination][]" value="" class="regular-text" style="width:95%;" />
182 </td>
183 <td>
184 <span class="quick-301-redirects-add"><a href="#" id="quick-301-redirects-add" class="dashicons dashicons-plus"><?php __( 'Add', 'quick-301-redirects' );?></a></span>
185 </td>
186 </tr>
187 </tbody>
188 </table>
189 <div class="submit quick-301-redirects-submit">
190 <input type="submit" name="submit_quick_301_redirects_" class="button-primary" value="<?php _e('Save Changes', 'quick-301-redirects' ) ?>" />
191 </div>
192
193 <?php wp_nonce_field( 'save_quick_301_redirects_', '_quick_301_redirects_nonce' ); ?>
194 </form>
195 </div>
196 <div id="quick-301-redirects-delete-confirm" style="display:none;" title="<?php esc_attr_e( 'Delete Redirect option', 'the-ansh' ); ?>">
197 <p><?php _e('Delete This Redirect 301 link?', 'quick-301-redirects' ); ?></p>
198 <label><input type="checkbox" id="quick_301_redirects_again" name="quick_301_redirects_again" value="1" /> &nbsp;<?php _e( "don't ask me again", "quick-301-redirects" );?></label>
199 </div>
200 <script>
201 ( function( $ ) {
202 "use strict";
203
204 $(document).ready(function(){
205 /* Checkbox checked delete confirm box when checked save on session */
206 if ( $.session.get("quick_301_redirects_again") === 'true' ) {
207 $('#quick_301_redirects_again').prop('checked', true);
208 $('#quick_301_redirects_again').is(':checked');
209 }
210
211 $('a#quick-301-redirects-add').on( 'click', function(event) {
212 event.preventDefault();
213 $('#quick-301-redirects-tbl tbody tr#quick-301-redirects-main').before('<tr><td style="width:45%;"><input type="text" name="quick_301_redirects_[request][]" value="" class="regular-text" style="width:100%;" /></td><td style="width:2%;">&rarr;</td><td style="width:45%;"><input type="text" name="quick_301_redirects_[destination][]" value="" class="regular-text" style="width:100%;" /></td><td><span class="quick-301-redirects-delete"><a href="#" class="quick-301-redirects-delete delete-empty-field dashicons dashicons-trash"><?php __('Delete', 'quick-301-redirects' );?></a></span></td></tr>');
214 });
215
216 $(document).on( "click", 'a.quick-301-redirects-delete' , function(e){
217 event.preventDefault();
218 var $delete_tr = $(this);
219
220 if ( $(this).hasClass('delete-empty-field')) {
221 $(this).parent().parent().parent().remove();
222 return;
223 }
224
225 if ( $('#quick_301_redirects_again').is(':checked') !== true ) {
226 jQuery( "#quick-301-redirects-delete-confirm" ).dialog({
227 resizable: false,
228 modal: true,
229 draggable: false,
230 height: 'auto',
231 width: 400,
232 buttons: {
233 "OK": function () {
234 $delete_tr.parent().parent().parent().remove();
235
236 if ( $('#quick_301_redirects_again').is(':checked') === true ) {
237 $.session.set("quick_301_redirects_again", true);
238 }
239 if ( !$delete_tr.hasClass('delete-empty-field')) {
240 jQuery('form#quick_301_redirects_form').submit();
241 }
242 $(this).dialog('close');
243 },
244 "Cancel": function () {
245 $(this).dialog('close');
246 }
247 }
248 });
249
250 } else {
251 $(this).parent().parent().parent().remove();
252 if ( !$delete_tr.hasClass('delete-empty-field')) {
253 jQuery('form#quick_301_redirects_form').submit();
254 }
255 }
256
257 /*var delete_confirm = confirm('<?php ?>');
258 if ( delete_confirm ) {
259 $(this).parent().parent().parent().remove();
260
261 if ( !$(this).hasClass('delete-empty-field')) {
262 jQuery('form#quick_301_redirects_form').submit();
263 }
264 }*/
265
266 });
267 });
268 })( jQuery );
269 </script>
270 <style>
271 #quick-301-redirects-tbl tr td{
272 vertical-align: top;
273 }
274 #quick-301-redirects-tbl tr td input[type="text"]{
275 border-radius: 12px;
276 }
277 .quick-301-redirects-submit .button-primary{
278 background: #F51366;
279 color: #fff;
280 border: solid 1px #F51366;
281 box-shadow: 0 3px 5px -3px #333333;
282 text-shadow: none;
283 }
284 .quick-301-redirects-submit .button-primary:hover, .quick-301-redirects-submit .button-primary:focus {
285 background: #bc0f50;
286 color: #ffffff;
287 border: solid 1px #bc0f50;
288 }
289 .quick-301-redirects-wrap .button.button-primary,
290 #quick-301-redirects-bulk-upload .button.button-primary{
291 background: #06CB53;
292 color: #fff;
293 border: solid 1px #06CB53;
294 box-shadow: 0 3px 5px -3px #333333;
295 text-shadow: none;
296 vertical-align: unset;
297 }
298 .quick-301-redirects-wrap .button.button-primary:hover,
299 #quick-301-redirects-bulk-upload .button.button-primary:hover{
300 background: #039b4c;
301 color: #ffffff;
302 border: solid 1px #039b4c;
303 }
304 </style>
305 <?php quick_301_redirects_bulk_upload();?>
306 </div>
307 <?php
308 }
309
310 /**
311 * quick_301_redirects_data_lists function
312 * Display the current list of redirects as form fields
313 * @return string <html>
314 *
315 * @Since 1.0
316 */
317 function quick_301_redirects_data_lists(){
318 $quick_301_redirects_ = get_option('quick_301_redirects_');
319 /* Upload CSV file in View Process */
320 if ( isset($_POST['quick_301_redirects_bulk_upload']) && !empty($_FILES['import_bulk_301_csv']) ) {
321
322 $quick_301_redirects_ = get_option('quick_301_redirects_');
323 $filename = $_FILES["import_bulk_301_csv"]["tmp_name"];
324 if ( $_FILES["import_bulk_301_csv"]["size"] > 0 ) {
325 $file = fopen($filename, "r");
326 $i=0;
327 while (($getData = fgetcsv($file, 10000, ",")) !== FALSE) {
328 if ( $i != 0 ) {
329 $quick_301_redirects_[$getData[0]] = $getData[1];
330 }
331 $i++;
332 }
333 fclose($file);
334 }
335
336 }
337
338 if (!empty($quick_301_redirects_)) {
339 foreach ($quick_301_redirects_ as $request => $destination) {
340 $len = strlen($request);
341 $request_link = ( substr($request, 0, $len) === '/') ? site_url() . $request : $request;
342 $url = parse_url($request);
343 if( isset($url['scheme']) && ( $url['scheme'] == 'https' || $url['scheme'] == 'http' ) ){
344 $request_link = $request;
345 } else {
346 $request_link = ( substr($request, 0, 1) == '/' ) ? site_url() . $request : site_url() . '/' . $request;
347 }
348
349 ?>
350 <tr>
351 <td>
352 <input type="text" name="quick_301_redirects_[request][]" value="<?php echo $request;?>" class="regular-text" style="width:95%;" />
353 <a href="<?php echo $request_link;?>" target="_balnk" class="" style="vertical-align:middle;"><img src="<?php echo plugins_url('/', __FILE__) . '/images/external-link-16.png';?>" /></a>
354 </td>
355 <td>&rarr;</td>
356 <td>
357 <input type="text" name="quick_301_redirects_[destination][]" value="<?php echo $destination;?>" class="regular-text" style="width:95%;" />
358 </td>
359 <td>
360 <span class="quick-301-redirects-delete"><a href="#" class="quick-301-redirects-delete dashicons dashicons-trash"><?php __( 'Delete', 'quick-301-redirects' );?></a></span>
361 </td>
362 </tr>
363 <?php
364
365 }
366 } // end if
367
368 }
369
370 /**
371 * quick_301_redirects_save_data function
372 * save the redirects datas into options table
373 * @return array
374 *
375 * @Since 1.0
376 */
377 function quick_301_redirects_save_data(){
378
379 if ( !current_user_can('manage_options') ) {
380 wp_die( __( 'You do not have sufficient permissions to access this page.', 'quick-301-redirects' ) );
381 }
382
383 check_admin_referer( 'save_quick_301_redirects_', '_quick_301_redirects_nonce' );
384
385 $quick_301_redirects_ = array();
386
387
388 for($i = 0; $i < sizeof($_POST['quick_301_redirects_']['request']); ++$i) {
389
390 $request = sanitize_text_field( trim($_POST['quick_301_redirects_']['request'][$i]) );
391 $destination = sanitize_text_field( trim($_POST['quick_301_redirects_']['destination'][$i] ) );
392
393 if ($request != '' && $destination != '') {
394 $url = parse_url($request);
395 if( isset($url['scheme']) && ( $url['scheme'] == 'https' || $url['scheme'] == 'http' ) ){
396 $request = $request;
397 } else {
398 $request = ( substr($request, 0, 1) == '/' ) ? $request : '/' . $request;
399 }
400 $quick_301_redirects_[$request] = $destination;
401 }
402 }
403
404 update_option('quick_301_redirects_', $quick_301_redirects_);
405
406 }
407
408 /**
409 * quick_301_redirects_bulk_upload function
410 * Upload Bulk CSV file upload section
411 * @return string <html>
412 *
413 * @Since 1.0
414 */
415 function quick_301_redirects_bulk_upload() {
416
417 if( isset($_POST['quick_301_redirects_bulk_upload']) ) {
418 $is_import=true;
419 $filetype = wp_check_filetype( wp_unslash( $_FILES['import_bulk_301_csv']['name'] ) , array(
420 'csv' => 'text/csv',
421 ) );
422 if ( ! in_array( $filetype['type'], array('csv' => 'text/csv'), true ) ) {
423 echo "<div class='error inline'><p>";
424 _e( 'Invalid file type. The importer supports CSV file formats.', 'woocommerce' ) ;
425 echo "</p></div>";
426 $is_import=false;
427 }
428
429 if ($is_import == true) {
430 //quick_301_redirects_bulk_import_csv_file();
431 }
432 }
433
434 $bytes = apply_filters( 'import_upload_size_limit', wp_max_upload_size() );
435 $size = size_format( $bytes );
436 ?>
437 <div id="quick-301-redirects-bulk-upload" class="stuffbox quick-301-redirects-bulk-upload" style="padding:0 20px 20px 20px;">
438 <div class="wrap" dir="ltr">
439 <form enctype="multipart/form-data" method="post">
440 <h2><?php _e( 'Import 301 Redirects from a CSV file', 'quick-301-redirects' );?></h2>
441 <table class="form-table">
442 <tbody>
443 <tr>
444 <th scope="row" style="text-align:left;">
445 <label for="upload"><?php _e( 'Choose a CSV file from your computer:', 'quick-301-redirects' ); ?></label>
446 </th>
447 <td>
448 <input type="file" id="upload" name="import_bulk_301_csv" size="25">
449 <input type="hidden" name="action" value="save">
450 <input type="hidden" name="max_file_size" value="<?php echo esc_attr( $bytes ); ?>">
451 <br>
452 <small>
453 <?php
454 /* translators: %s: maximum upload size */
455 printf( __( 'Maximum size: %s', 'quick-301-redirects' ), esc_html( $size ));
456 ?>
457 </small>
458 </td>
459 </tr>
460 </tbody>
461 </table>
462 <div class="bulk-actions">
463 <button type="submit" class="button button-primary button-next" value="<?php esc_attr_e( 'Import CSV File', 'quick-301-redirects' ); ?>" name="quick_301_redirects_bulk_upload"><?php _e( 'Import CSV File', 'quick-301-redirects' ); ?></button>
464 </div>
465 </form>
466 </div>
467 </div>
468
469 <div class="stuffbox quick-301-redirects-export" style="padding:0 20px 20px 20px;">
470 <div class="wrap" dir="ltr">
471 <h3><?php esc_html_e('After saving your changes, you can export all your redirects to a CSV file', 'quick-301-redirects');?></h3>
472 <a href="<?php echo plugins_url('export-quick-301-redirects.php?download_file=quick-301-redirects.csv',__FILE__); ?>" class="page-title-action button button-primary button-next"><?php esc_html_e('Export CSV', 'quick-301-redirects');?></a>
473 </div>
474 </div>
475 <?php
476 }
477
478 /**
479 * quick_301_redirects_bulk_import_csv_file function
480 * Save Upload Bulk CSV file data
481 * @return array
482 *
483 * @Since 1.0
484 */
485 function quick_301_redirects_bulk_import_csv_file() {
486
487 if ( isset($_POST['quick_301_redirects_bulk_upload']) && !empty($_FILES['import_bulk_301_csv']) ) {
488
489 $quick_301_redirects_ = get_option('quick_301_redirects_');
490 $filename = $_FILES["import_bulk_301_csv"]["tmp_name"];
491
492 if ( $_FILES["import_bulk_301_csv"]["size"] > 0 ) {
493 $file = fopen($filename, "r");
494 $i=0;
495 while (($getData = fgetcsv($file, 10000, ",")) !== FALSE) {
496 if ( $i != 0 ) {
497 $quick_301_redirects_[$getData[0]] = $getData[1];
498 }
499 $i++;
500 }
501 fclose($file);
502 update_option( 'quick_301_redirects_', $quick_301_redirects_ );
503 }
504 ?>
505 <script>
506 window.location.href = "<?php echo admin_url('admin.php?page=quick-301-redirects&bulk_upload_successfull=true');?>";
507 </script>
508 <?php
509 }
510 }
511
512
513 /**
514 * quick_301_redirects_template_redirect function
515 * Redirect 301 to Destination URL if found.
516 * @return URL
517 *
518 * @Since 1.0
519 */
520 function quick_301_redirects_template_redirect(){
521
522 $protocol = ( is_ssl()) ? 'https' : 'http';
523 $request_url = $protocol . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
524 $user_request = quick_301_redirects_replace( get_option( 'home' ), '', $request_url );
525 $quick_301_redirects_ = get_option('quick_301_redirects_');
526
527 if (!empty($quick_301_redirects_)) {
528 $redirect = '';
529 /* compare user request to each 301 stored in the table */
530 foreach ($quick_301_redirects_ as $request => $destination) {
531
532 if( urldecode($user_request) == rtrim($request,'/') || rtrim($request_url,'/') == rtrim($request,'/') ) {
533 /* comparison redirect */
534 $redirect = $destination;
535 }
536
537 if ($redirect !== '' && trim($redirect,'/') !== trim($user_request,'/')) {
538 /* check if destination needs the domain prepended */
539 if ( strpos($redirect,'/') === 0 ){
540 $redirect = home_url() . $redirect;
541 }
542 header ('HTTP/1.1 301 Moved Permanently');
543 wp_redirect ( $redirect, 301 );
544 exit();
545 }
546 }
547 }
548 }
549 add_action( 'template_redirect', 'quick_301_redirects_template_redirect', 0 );
550
551 /**
552 * quick_301_redirects_replace function
553 * replace Request URL to subject slug.
554 * @return URL
555 *
556 * @Since 1.0
557 */
558 function quick_301_redirects_replace( $url_search, $url_replace, $request_url ) {
559
560 $token = chr(1);
561 if ( ( $pos = strpos( strtolower($request_url), strtolower($url_search) ) ) !== FALSE ){
562 $request_url = substr_replace( $request_url, $token, $pos, strlen($url_search) );
563 }
564 return rtrim( str_replace( $token, $url_replace, $request_url ), '/' );
565 }
566