PluginProbe
Quick 301 Redirects / 1.1
Quick 301 Redirects v1.1
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 1.1, at quick-301-redirects.php

473 lines 16.0 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
9 License: GPL2
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 wp_redirect( admin_url( 'options-general.php?page=quick-301-redirects' ) ) ;
25 exit;
26 }
27 }
28 add_action( 'activated_plugin', 'quick_301_redirects_activation_redirect' );
29
30 /**
31 * Plug-in activation - settings link.
32 * Settings link in plugin page.
33 * @param array $links
34 *
35 * @since 1.0.0
36 */
37 function quick_301_redirects_action_links( $links ){
38
39 $plugin_links = array( '<a href="' . admin_url( 'options-general.php?page=quick-301-redirects' ) . '">' . __( 'Settings', 'quick-301-redirects' ) . '</a>' );
40
41 return array_merge( $plugin_links, $links );
42 }
43 add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'quick_301_redirects_action_links' );
44
45 /**
46 * Enqueue admin scripts and styles.
47 *
48 * @Since 1.0
49 */
50 function quick_301_redirects_admin_scripts(){
51 wp_enqueue_style( 'wp-jquery-ui-dialog' );
52 wp_enqueue_script( 'jquery-ui-dialog' );
53
54 wp_enqueue_script( 'jquery-session', plugins_url('/', __FILE__). 'jquery.session.js', array('jquery') );
55 }
56 add_action( 'admin_enqueue_scripts', 'quick_301_redirects_admin_scripts' );
57 /**
58 * quick_301_redirects_menu function
59 * generate the link to the options page under settings
60 *
61 * @Since 1.0
62 */
63 function quick_301_redirects_menu(){
64 add_options_page( __( 'Quick 301 Redirects', 'quick-301-redirects' ), __( 'Quick 301 Redirects', 'quick-301-redirects' ), 'manage_options', 'quick-301-redirects', 'quick_301_redirects_options_page' );
65 }
66 add_action('admin_menu', 'quick_301_redirects_menu');
67
68 /**
69 * quick_301_redirects_options_page function
70 * generate the options page in the wordpress admin inside settings menu
71 *
72 * @Since 1.0
73 */
74 function quick_301_redirects_options_page(){
75
76 if (isset($_POST['quick_301_redirects_']) && !empty($_POST['quick_301_redirects_'])) {
77 quick_301_redirects_save_data();
78 ?>
79 <div id="message" class="updated"><p><?php _e( 'Settings saved', 'quick-301-redirects' );?></p></div>
80 <?php
81 }
82 ?>
83 <div class="wrap">
84 <h2><?php _e( 'Quick 301 Redirects', 'quick-301-redirects' );?></h2>
85 <?php
86 if ( isset($_GET['bulk_upload_successfull']) && $_GET['bulk_upload_successfull'] == true) {
87
88 echo "<div class='updated inline'><p>";
89 _e( 'Upload Bluk csv file successfully.', 'woocommerce' ) ;
90 echo "</p></div>";
91 }
92 ?>
93 <div class="stuffbox" style="padding: 20px 20px 0; margin-bottom:50px">
94 <form method="post" id="quick_301_redirects_form" action="options-general.php?page=quick-301-redirects">
95
96 <table id="quick-301-redirects-tbl" class="widefat quick-301-redirects">
97 <thead>
98 <tr>
99 <th colspan="2"><strong><?php _e( 'Request', 'quick-301-redirects' );?></strong></th>
100 <th colspan="2"><strong><?php _e( 'Destination', 'quick-301-redirects' );?></strong></th>
101 </tr>
102 </thead>
103 <tbody>
104 <tr>
105 <td colspan="2"><small><?php _e( 'Example: /page.html', 'quick-301-redirects' );?></small></td>
106 <td colspan="2"><small><?php printf(__( 'Example: %s/page/', 'quick-301-redirects'), get_option('home') );?></small></td>
107 </tr>
108 <?php quick_301_redirects_data_lists(); ?>
109 <tr id="quick-301-redirects-main">
110 <td style="width:45%;">
111 <input type="text" name="quick_301_redirects_[request][]" value="" class="regular-text" style="width:100%;" />
112 </td>
113 <td style="width:1%;">&rarr;</td>
114 <td style="width:45%;">
115 <input type="text" name="quick_301_redirects_[destination][]" value="" class="regular-text" style="width:100%;" />
116 </td>
117 <td>
118 <span class="quick-301-redirects-add"><a href="#" id="quick-301-redirects-add" class="dashicons dashicons-plus"><?php __( 'Add', 'quick-301-redirects' );?></a></span>
119 </td>
120 </tr>
121 </tbody>
122 </table>
123 <div class="submit quick-301-redirects-submit">
124 <input type="submit" name="submit_quick_301_redirects_" class="button-primary" value="<?php _e('Save Changes', 'quick-301-redirects' ) ?>" />
125 </div>
126
127 <?php wp_nonce_field( 'save_quick_301_redirects_', '_quick_301_redirects_nonce' ); ?>
128 </form>
129 </div>
130 <div id="quick-301-redirects-delete-confirm" style="display:none;" title="<?php esc_attr_e( 'Delete Redirect option', 'the-ansh' ); ?>">
131 <p><?php _e('Delete This Redirect 301 link?', 'quick-301-redirects' ); ?></p>
132 <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>
133 </div>
134 <script>
135 ( function( $ ) {
136 "use strict";
137
138 $(document).ready(function(){
139 /* Checkbox checked delete confirm box when checked save on session */
140 if ( $.session.get("quick_301_redirects_again") === 'true' ) {
141 $('#quick_301_redirects_again').prop('checked', true);
142 $('#quick_301_redirects_again').is(':checked');
143 }
144
145 $('a#quick-301-redirects-add').on( 'click', function(event) {
146 event.preventDefault();
147 $('#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>');
148 });
149
150 $('a.quick-301-redirects-delete').live( 'click', function(event){
151 event.preventDefault();
152 var $delete_tr = $(this);
153
154 if ( $(this).hasClass('delete-empty-field')) {
155 $(this).parent().parent().parent().remove();
156 return;
157 }
158
159 if ( $('#quick_301_redirects_again').is(':checked') !== true ) {
160 jQuery( "#quick-301-redirects-delete-confirm" ).dialog({
161 resizable: false,
162 modal: true,
163 draggable: false,
164 height: 'auto',
165 width: 400,
166 buttons: {
167 "OK": function () {
168 $delete_tr.parent().parent().parent().remove();
169
170 if ( $('#quick_301_redirects_again').is(':checked') === true ) {
171 $.session.set("quick_301_redirects_again", true);
172 }
173 if ( !$delete_tr.hasClass('delete-empty-field')) {
174 jQuery('form#quick_301_redirects_form').submit();
175 }
176 $(this).dialog('close');
177 },
178 "Cancel": function () {
179 $(this).dialog('close');
180 }
181 }
182 });
183
184 } else {
185 $(this).parent().parent().parent().remove();
186 if ( !$delete_tr.hasClass('delete-empty-field')) {
187 jQuery('form#quick_301_redirects_form').submit();
188 }
189 }
190
191 /*var delete_confirm = confirm('<?php ?>');
192 if ( delete_confirm ) {
193 $(this).parent().parent().parent().remove();
194
195 if ( !$(this).hasClass('delete-empty-field')) {
196 jQuery('form#quick_301_redirects_form').submit();
197 }
198 }*/
199
200 });
201 });
202 })( jQuery );
203 </script>
204 <style>
205 #quick-301-redirects-tbl tr td{
206 vertical-align: middle;
207 }
208 #quick-301-redirects-tbl tr td input[type="text"]{
209 border-radius: 12px;
210 }
211 .quick-301-redirects-submit .button-primary{
212 background: #F51366;
213 color: #fff;
214 border: solid 1px #F51366;
215 box-shadow: 0 3px 5px -3px #333333;
216 text-shadow: none;
217 }
218 .quick-301-redirects-submit .button-primary:hover, .quick-301-redirects-submit .button-primary:focus {
219 background: #bc0f50;
220 color: #ffffff;
221 border: solid 1px #bc0f50;
222 }
223 #quick-301-redirects-bulk-upload .button.button-primary{
224 background: #06CB53;
225 color: #fff;
226 border: solid 1px #06CB53;
227 box-shadow: 0 3px 5px -3px #333333;
228 text-shadow: none;
229 }
230
231 #quick-301-redirects-bulk-upload .button.button-primary:hover{
232 background: #039b4c;
233 color: #ffffff;
234 border: solid 1px #039b4c;
235 }
236 </style>
237 <?php quick_301_redirects_bulk_upload();?>
238 </div>
239 <?php
240 }
241
242 /**
243 * quick_301_redirects_data_lists function
244 * Display the current list of redirects as form fields
245 * @return string <html>
246 *
247 * @Since 1.0
248 */
249 function quick_301_redirects_data_lists(){
250 $quick_301_redirects_ = get_option('quick_301_redirects_');
251 /* Upload CSV file in View Process */
252 if ( isset($_POST['quick_301_redirects_bulk_upload']) && !empty($_FILES['import_bulk_301_csv']) ) {
253
254 $quick_301_redirects_ = get_option('quick_301_redirects_');
255 $filename = $_FILES["import_bulk_301_csv"]["tmp_name"];
256 if ( $_FILES["import_bulk_301_csv"]["size"] > 0 ) {
257 $file = fopen($filename, "r");
258 $i=0;
259 while (($getData = fgetcsv($file, 10000, ",")) !== FALSE) {
260 if ( $i != 0 ) {
261 $quick_301_redirects_[$getData[0]] = $getData[1];
262 }
263 $i++;
264 }
265 fclose($file);
266 }
267
268 }
269
270 if (!empty($quick_301_redirects_)) {
271 foreach ($quick_301_redirects_ as $request => $destination) { ?>
272 <tr>
273 <td>
274 <input type="text" name="quick_301_redirects_[request][]" value="<?php echo $request;?>" class="regular-text" style="width:100%;" />
275 </td>
276 <td>&rarr;</td>
277 <td>
278 <input type="text" name="quick_301_redirects_[destination][]" value="<?php echo $destination;?>" class="regular-text" style="width:100%;" />
279 </td>
280 <td>
281 <span class="quick-301-redirects-delete"><a href="#" class="quick-301-redirects-delete dashicons dashicons-trash"><?php __( 'Delete', 'quick-301-redirects' );?></a></span>
282 </td>
283 </tr>
284 <?php
285
286 }
287 } // end if
288
289 }
290
291 /**
292 * quick_301_redirects_save_data function
293 * save the redirects datas into options table
294 * @return array
295 *
296 * @Since 1.0
297 */
298 function quick_301_redirects_save_data(){
299
300 if ( !current_user_can('manage_options') ) {
301 wp_die( __( 'You do not have sufficient permissions to access this page.', 'quick-301-redirects' ) );
302 }
303
304 check_admin_referer( 'save_quick_301_redirects_', '_quick_301_redirects_nonce' );
305
306 $quick_301_redirects_ = array();
307
308
309 for($i = 0; $i < sizeof($_POST['quick_301_redirects_']['request']); ++$i) {
310
311 $request = sanitize_text_field( trim($_POST['quick_301_redirects_']['request'][$i]) );
312 $destination = sanitize_text_field( trim($_POST['quick_301_redirects_']['destination'][$i] ) );
313
314 if ($request != '' && $destination != '') {
315 $quick_301_redirects_[$request] = $destination;
316 }
317 }
318
319 update_option('quick_301_redirects_', $quick_301_redirects_);
320
321 }
322
323 /**
324 * quick_301_redirects_bulk_upload function
325 * Upload Bulk CSV file upload section
326 * @return string <html>
327 *
328 * @Since 1.0
329 */
330 function quick_301_redirects_bulk_upload() {
331
332 if( isset($_POST['quick_301_redirects_bulk_upload']) ) {
333 $is_import=true;
334 $filetype = wp_check_filetype( wp_unslash( $_FILES['import_bulk_301_csv']['name'] ) , array(
335 'csv' => 'text/csv',
336 ) );
337 if ( ! in_array( $filetype['type'], array('csv' => 'text/csv'), true ) ) {
338 echo "<div class='error inline'><p>";
339 _e( 'Invalid file type. The importer supports CSV file formats.', 'woocommerce' ) ;
340 echo "</p></div>";
341 $is_import=false;
342 }
343
344 if ($is_import == true) {
345 //quick_301_redirects_bulk_import_csv_file();
346 }
347 }
348
349 $bytes = apply_filters( 'import_upload_size_limit', wp_max_upload_size() );
350 $size = size_format( $bytes );
351 ?>
352 <div id="quick-301-redirects-bulk-upload" class="stuffbox quick-301-redirects-bulk-upload" style="padding:0 20px 20px 20px;">
353 <div class="wrap">
354 <form enctype="multipart/form-data" method="post">
355 <h2><?php _e( 'Import 301 Redirects from a CSV file', 'quick-301-redirects' );?></h2>
356 <table class="form-table">
357 <tbody>
358 <tr>
359 <th scope="row">
360 <label for="upload"><?php _e( 'Choose a CSV file from your computer:', 'quick-301-redirects' ); ?></label>
361 </th>
362 <td>
363 <input type="file" id="upload" name="import_bulk_301_csv" size="25">
364 <input type="hidden" name="action" value="save">
365 <input type="hidden" name="max_file_size" value="<?php echo esc_attr( $bytes ); ?>">
366 <br>
367 <small>
368 <?php
369 /* translators: %s: maximum upload size */
370 printf( __( 'Maximum size: %s', 'quick-301-redirects' ), esc_html( $size ));
371 ?>
372 </small>
373 </td>
374 </tr>
375 </tbody>
376 </table>
377 <div class="bulk-actions">
378 <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>
379 </div>
380 </form>
381 </div>
382 </div>
383 <?php
384 }
385
386 /**
387 * quick_301_redirects_bulk_import_csv_file function
388 * Save Upload Bulk CSV file data
389 * @return array
390 *
391 * @Since 1.0
392 */
393 function quick_301_redirects_bulk_import_csv_file() {
394
395 if ( isset($_POST['quick_301_redirects_bulk_upload']) && !empty($_FILES['import_bulk_301_csv']) ) {
396
397 $quick_301_redirects_ = get_option('quick_301_redirects_');
398 $filename = $_FILES["import_bulk_301_csv"]["tmp_name"];
399
400 if ( $_FILES["import_bulk_301_csv"]["size"] > 0 ) {
401 $file = fopen($filename, "r");
402 $i=0;
403 while (($getData = fgetcsv($file, 10000, ",")) !== FALSE) {
404 if ( $i != 0 ) {
405 $quick_301_redirects_[$getData[0]] = $getData[1];
406 }
407 $i++;
408 }
409 fclose($file);
410 update_option( 'quick_301_redirects_', $quick_301_redirects_ );
411 }
412 ?>
413 <script>
414 window.location.href = "<?php echo admin_url('options-general.php?page=quick-301-redirects&bulk_upload_successfull=true');?>";
415 </script>
416 <?php
417 }
418 }
419
420
421 /**
422 * quick_301_redirects_template_redirect function
423 * Redirect 301 to Destination URL if found.
424 * @return URL
425 *
426 * @Since 1.0
427 */
428 function quick_301_redirects_template_redirect(){
429
430 $protocol = ( is_ssl()) ? 'https:' : 'http';
431 $request_url = $protocol . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
432 $user_request = quick_301_redirects_replace( get_option( 'home' ), '', $request_url );
433 $quick_301_redirects_ = get_option('quick_301_redirects_');
434
435 if (!empty($quick_301_redirects_)) {
436 $redirect = '';
437 /* compare user request to each 301 stored in the table */
438 foreach ($quick_301_redirects_ as $request => $destination) {
439
440 if( strpos( rtrim($request_url,'/'), rtrim($request,'/') ) != '' || urldecode($user_request) == rtrim($request,'/') || rtrim($request_url,'/') == rtrim($request,'/') ) {
441 /* comparison redirect */
442 $redirect = $destination;
443 }
444
445 if ($redirect !== '' && trim($redirect,'/') !== trim($user_request,'/')) {
446 /* check if destination needs the domain prepended */
447 if ( strpos($redirect,'/') === 0 ){
448 $redirect = home_url() . $redirect;
449 }
450 header ('HTTP/1.1 301 Moved Permanently');
451 wp_redirect ( $redirect );
452 exit();
453 }
454 }
455 }
456 }
457 add_action( 'template_redirect', 'quick_301_redirects_template_redirect', 0 );
458
459 /**
460 * quick_301_redirects_replace function
461 * replace Request URL to subject slug.
462 * @return URL
463 *
464 * @Since 1.0
465 */
466 function quick_301_redirects_replace( $url_search, $url_replace, $request_url ) {
467
468 $token = chr(1);
469 if ( ( $pos = strpos( strtolower($request_url), strtolower($url_search) ) ) !== FALSE ){
470 $request_url = substr_replace( $request_url, $token, $pos, strlen($url_search) );
471 }
472 return rtrim( str_replace( $token, $url_replace, $request_url ), '/' );
473 }