PluginProbe
Redirection / 2.6.5
Redirection v2.6.5
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / redirection-admin.php

redirection-admin.php in Redirection 2.6.5, at redirection-admin.php

804 lines 22.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 include dirname( __FILE__ ).'/models/group.php';
4 include dirname( __FILE__ ).'/models/monitor.php';
5 include dirname( __FILE__ ).'/models/pager.php';
6 include dirname( __FILE__ ).'/models/file-io.php';
7
8 class Redirection_Admin {
9 private static $instance = null;
10 private $monitor;
11
12 static function init() {
13 if ( is_null( self::$instance ) ) {
14 self::$instance = new Redirection_Admin();
15
16 load_plugin_textdomain( 'redirection', false, dirname( plugin_basename( REDIRECTION_FILE ) ).'/locale/' );
17 }
18
19 return self::$instance;
20 }
21
22 function __construct() {
23 add_action( 'admin_menu', array( $this, 'admin_menu' ) );
24 add_action( 'load-tools_page_redirection', array( $this, 'redirection_head' ) );
25 add_action( 'plugin_action_links_'.basename( dirname( REDIRECTION_FILE ) ).'/'.basename( REDIRECTION_FILE ), array( $this, 'plugin_settings' ), 10, 4 );
26
27 add_filter( 'set-screen-option', array( $this, 'set_per_page' ), 10, 3 );
28
29 register_deactivation_hook( REDIRECTION_FILE, array( 'Redirection_Admin', 'plugin_deactivated' ) );
30 register_uninstall_hook( REDIRECTION_FILE, array( 'Redirection_Admin', 'plugin_uninstall' ) );
31
32 add_action( 'wp_ajax_red_redirect_add', array( $this, 'ajax_redirect_add' ) );
33 add_action( 'wp_ajax_red_redirect_edit', array( $this, 'ajax_redirect_edit' ) );
34 add_action( 'wp_ajax_red_redirect_save', array( $this, 'ajax_redirect_save' ) );
35
36 add_action( 'wp_ajax_red_load_settings', array( $this, 'ajax_load_settings' ) );
37 add_action( 'wp_ajax_red_save_settings', array( $this, 'ajax_save_settings' ) );
38
39 add_action( 'wp_ajax_red_get_logs', array( $this, 'ajax_get_logs' ) );
40 add_action( 'wp_ajax_red_log_action', array( $this, 'ajax_log_action' ) );
41
42 add_action( 'wp_ajax_red_delete_plugin', array( $this, 'ajax_delete_plugin' ) );
43 add_action( 'wp_ajax_red_delete_all', array( $this, 'ajax_delete_all' ) );
44
45 add_action( 'wp_ajax_red_get_module', array( $this, 'ajax_get_module' ) );
46 add_action( 'wp_ajax_red_set_module', array( $this, 'ajax_set_module' ) );
47
48 add_action( 'wp_ajax_red_get_group', array( $this, 'ajax_get_group' ) );
49 add_action( 'wp_ajax_red_set_group', array( $this, 'ajax_set_group' ) );
50 add_action( 'wp_ajax_red_group_action', array( $this, 'ajax_group_action' ) );
51 add_action( 'wp_ajax_red_create_group', array( $this, 'ajax_create_group' ) );
52
53 add_action( 'redirection_save_options', array( $this, 'flush_schedule' ) );
54
55 $this->monitor = new Red_Monitor( red_get_options() );
56
57 $this->export_rss();
58 }
59
60 public static function plugin_activated() {
61 Redirection_Admin::update();
62 Red_Flusher::schedule();
63
64 update_option( 'redirection_options', red_get_options() );
65 }
66
67 public static function plugin_deactivated() {
68 Red_Flusher::clear();
69 }
70
71 public static function plugin_uninstall() {
72 include_once dirname( REDIRECTION_FILE ).'/models/database.php';
73
74 $db = new RE_Database();
75 $db->remove( REDIRECTION_FILE );
76
77 delete_option( 'redirection_options' );
78 }
79
80 public function flush_schedule() {
81 Red_Flusher::schedule();
82 }
83
84 private function render( $template, $template_vars = array() ) {
85 foreach ( $template_vars as $key => $val ) {
86 $$key = $val;
87 }
88
89 if ( file_exists( dirname( REDIRECTION_FILE )."/view/$template.php" ) ) {
90 include dirname( REDIRECTION_FILE )."/view/$template.php";
91 }
92 }
93
94 private function capture( $ug_name, $ug_vars = array() ) {
95 ob_start();
96
97 $this->render( $ug_name, $ug_vars );
98 $output = ob_get_contents();
99
100 ob_end_clean();
101 return $output;
102 }
103
104 private function render_error( $message ) {
105 ?>
106 <div class="fade error" id="message">
107 <p><?php echo $message ?></p>
108 </div>
109 <?php
110 }
111
112 private function render_message( $message, $timeout = 0 ) {
113 ?>
114 <div class="updated" id="message" onclick="this.parentNode.removeChild(this)">
115 <p><?php echo $message ?></p>
116 </div>
117 <?php
118 }
119
120 private static function update() {
121 $version = get_option( 'redirection_version' );
122
123 Red_Flusher::schedule();
124
125 if ( $version !== REDIRECTION_VERSION ) {
126 include_once dirname( REDIRECTION_FILE ).'/models/database.php';
127
128 $database = new RE_Database();
129
130 if ( $version === false ) {
131 $database->install();
132 }
133
134 return $database->upgrade( $version, REDIRECTION_VERSION );
135 }
136
137 return true;
138 }
139
140 private function select( $items, $default = '' ) {
141 foreach ( $items as $key => $value ) {
142 if ( is_array( $value ) ) {
143 echo '<optgroup label="'.esc_attr( $key ).'">';
144
145 foreach ( $value as $sub => $subvalue ) {
146 echo '<option value="'.esc_attr( $sub ).'"'.( $sub === $default ? ' selected="selected"' : '' ).'>'.esc_html( $subvalue ).'</option>';
147 }
148
149 echo '</optgroup>';
150 }
151 else
152 echo '<option value="'.esc_attr( $key ).'"'.( $key === $default ? ' selected="selected"' : '' ).'>'.esc_html( $value ).'</option>';
153 }
154 }
155
156 function set_per_page( $status, $option, $value ) {
157 if ( $option === 'redirection_log_per_page' ) {
158 return max( 1, min( intval( $value, 10 ), 100 ) );
159 }
160
161 return $status;
162 }
163
164 function plugin_settings( $links ) {
165 $settings_link = '<a href="tools.php?page='.basename( REDIRECTION_FILE ).'&amp;sub=options">'.__( 'Settings', 'redirection' ).'</a>';
166 array_unshift( $links, $settings_link );
167 return $links;
168 }
169
170 function redirection_head() {
171 global $wp_version;
172
173 $options = red_get_options();
174 $version = get_plugin_data( REDIRECTION_FILE );
175 $version = $version['Version'];
176
177 $this->inject();
178
179 if ( ! isset( $_GET['sub'] ) || ( isset( $_GET['sub'] ) && ( in_array( $_GET['sub'], array( 'log', '404s', 'groups' ) ) ) ) ) {
180 add_screen_option( 'per_page', array( 'label' => __( 'Log entries (100 max)', 'redirection' ), 'default' => 25, 'option' => 'redirection_log_per_page' ) );
181 }
182
183 wp_enqueue_script( 'redirection', plugin_dir_url( REDIRECTION_FILE ).'redirection.js', array( 'jquery-form', 'jquery-ui-sortable' ), $version );
184
185 if ( defined( 'REDIRECTION_DEV_MODE' ) && REDIRECTION_DEV_MODE ) {
186 wp_enqueue_script( 'redirection-ui', 'http://localhost:3312/redirection-ui.js', array( 'redirection' ), $version );
187 } else {
188 wp_enqueue_script( 'redirection-ui', plugin_dir_url( REDIRECTION_FILE ).'redirection-ui.js', array( 'redirection' ), $version );
189 }
190
191 wp_enqueue_style( 'redirection', plugin_dir_url( REDIRECTION_FILE ).'admin.css', $version );
192
193 wp_localize_script( 'redirection', 'Redirectioni10n', array(
194 'WP_API_root' => admin_url( 'admin-ajax.php' ),
195 'WP_API_nonce' => wp_create_nonce( 'wp_rest' ),
196 'pluginBaseUrl' => plugins_url( '', REDIRECTION_FILE ),
197 'pluginRoot' => admin_url( 'tools.php?page=redirection.php' ),
198 'per_page' => $this->get_per_page(),
199 'locale' => $this->get_i18n_data(),
200 'localeSlug' => get_locale(),
201 'token' => $options['token'],
202 'versions' => implode( ', ', array( 'Plugin '.$version, 'WordPress '.$wp_version, 'PHP '.phpversion() ) ),
203 ) );
204 }
205
206 private function get_per_page() {
207 $per_page = intval( get_user_meta( get_current_user_id(), 'redirection_log_per_page', true ), 10 );
208
209 return $per_page > 0 ? $per_page : 25;
210 }
211
212 private function get_i18n_data() {
213 $i18n_json = REDIRECTION_FILE . 'locale/json/redirection-' . get_locale() . '.json';
214
215 if ( is_file( $i18n_json ) && is_readable( $i18n_json ) ) {
216 $locale_data = @file_get_contents( $i18n_json );
217
218 if ( $locale_data ) {
219 return $locale_data;
220 }
221 }
222
223 // Return empty if we have nothing to return so it doesn't fail when parsed in JS
224 return '{}';
225 }
226
227 function admin_menu() {
228 add_management_page( 'Redirection', 'Redirection', apply_filters( 'redirection_role', 'administrator' ), basename( REDIRECTION_FILE ), array( &$this, 'admin_screen' ) );
229 }
230
231 function export_rss() {
232 if ( isset( $_GET['token'] ) && isset( $_GET['page'] ) && isset( $_GET['sub'] ) && $_GET['page'] === 'redirection.php' && $_GET['sub'] === 'rss' ) {
233 $options = red_get_options();
234
235 if ( $_GET['token'] === $options['token'] && !empty( $options['token'] ) ) {
236 $items = Red_Item::get_all_for_module( intval( $_GET['module'] ) );
237
238 $exporter = Red_FileIO::create( 'rss' );
239 $exporter->export( $items );
240 die();
241 }
242 }
243 }
244
245 function admin_screen() {
246 Redirection_Admin::update();
247
248 if ( isset( $_GET['sub'] ) ) {
249 if ( $_GET['sub'] === 'log' )
250 return $this->admin_screen_log();
251 elseif ( $_GET['sub'] === '404s' )
252 return $this->admin_screen_404();
253 elseif ( $_GET['sub'] === 'options' )
254 return $this->admin_screen_options();
255 elseif ( $_GET['sub'] === 'process' )
256 return $this->admin_screen_process();
257 elseif ( $_GET['sub'] === 'groups' )
258 return $this->admin_groups( isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0 );
259 elseif ( $_GET['sub'] === 'modules' )
260 return $this->admin_screen_modules();
261 elseif ( $_GET['sub'] === 'support' )
262 return $this->render( 'support', array( 'options' => red_get_options() ) );
263 }
264
265 return $this->admin_redirects( isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0 );
266 }
267
268 function admin_screen_modules() {
269 $options = red_get_options();
270
271 $this->render( 'module-list', array( 'options' => $options ) );
272 }
273
274 function inject() {
275 if ( isset( $_POST['id'] ) && ! isset( $_POST['action'] ) ) {
276 wp_safe_redirect( add_query_arg( 'id', intval( $_POST['id'] ), $_SERVER['REQUEST_URI'] ) );
277 die();
278 }
279
280 if ( isset( $_GET['page'] ) && isset( $_GET['sub'] ) && $_GET['page'] === 'redirection.php' ) {
281 if ( isset( $_POST['export-csv'] ) && check_admin_referer( 'wp_rest' ) ) {
282 if ( isset( $_GET['sub'] ) && $_GET['sub'] === 'log' ) {
283 RE_Log::export_to_csv();
284 } else {
285 RE_404::export_to_csv();
286 }
287
288 die();
289 }
290
291 if ( $this->user_has_access() && $_GET['sub'] === 'modules' && isset( $_GET['exporter'] ) && isset( $_GET['export'] ) ) {
292 $exporter = Red_FileIO::create( $_GET['exporter'] );
293
294 if ( $exporter ) {
295 $items = Red_Item::get_all_for_module( intval( $_GET['export'] ) );
296
297 $exporter->export( $items );
298 die();
299 }
300 }
301 }
302 }
303
304 function admin_screen_options() {
305 if ( isset( $_POST['import'] ) && check_admin_referer( 'wp_rest' ) ) {
306 $count = Red_FileIO::import( $_POST['group'], $_FILES['upload'] );
307
308 if ( $count > 0 ) {
309 $this->render_message( sprintf( _n( '%d redirection was successfully imported','%d redirections were successfully imported', $count, 'redirection' ), $count ) );
310 } else {
311 $this->render_message( __( 'No items were imported', 'redirection' ) );
312 }
313 }
314
315 $groups = Red_Group::get_for_select();
316 $this->render( 'options', array( 'options' => red_get_options(), 'groups' => $groups ) );
317 }
318
319 function admin_screen_log() {
320 $options = red_get_options();
321
322 $this->render( 'log', array( 'options' => $options, 'title' => __( 'Redirection Log', 'redirection' ) ) );
323 }
324
325 function admin_screen_404() {
326 $options = red_get_options();
327
328 $this->render( 'log', array( 'options' => $options, 'title' => __( 'Redirection 404', 'redirection' ) ) );
329 }
330
331 function admin_groups( $module ) {
332 $this->render( 'group-list', array( 'options' => red_get_options() ) );
333 }
334
335 function admin_redirects( $group_id ) {
336 $table = new Redirection_Table( Red_Group::get_for_select(), $group_id );
337 $table->prepare_items();
338
339 $this->render( 'item-list', array( 'options' => red_get_options(), 'group' => $group_id, 'table' => $table, 'date_format' => get_option( 'date_format' ) ) );
340 }
341
342 private function check_ajax_referer( $nonce ) {
343 if ( check_ajax_referer( $nonce, false, false ) === false ) {
344 return $this->output_ajax_response( array( 'error' => __( 'Unable to perform action' ).' - bad nonce "'.$nonce.'" ('.__LINE__.')' ) );
345 }
346
347 if ( $this->user_has_access() === false ) {
348 return $this->output_ajax_response( array( 'error' => __( 'No permissions to perform action ('.__LINE__.')' ) ) );
349 }
350
351 return true;
352 }
353
354 private function user_has_access() {
355 return current_user_can( apply_filters( 'redirection_role', 'administrator' ) );
356 }
357
358 public function ajax_redirect_edit() {
359 $this->check_ajax_referer( 'red-edit_'.intval( $_POST['id'] ) );
360 $redirect = Red_Item::get_by_id( intval( $_POST['id'] ) );
361
362 if ( $redirect )
363 $json['html'] = $this->capture( 'item-edit', array( 'redirect' => $redirect, 'groups' => Red_Group::get_for_select() ) );
364 else
365 $json['error'] = __( 'Unable to perform action' ).' - could not find redirect';
366
367 return $this->output_ajax_response( $json );
368 }
369
370 public function ajax_redirect_save() {
371 global $hook_suffix;
372
373 $hook_suffix = '';
374
375 $red_id = intval( $_POST['id'] );
376
377 $this->check_ajax_referer( 'redirection-redirect_save_'.$red_id );
378
379 $redirect = Red_Item::get_by_id( $red_id );
380 if ( $redirect ) {
381 $redirect->update( $_POST );
382
383 $pager = new Redirection_Table( array(), 0 );
384 $json = array( 'html' => $pager->column_url( $redirect ), 'code' => $redirect->get_action_code() );
385 }
386 else
387 $json['error'] = __( 'Unable to perform action' ).' - could not find redirect';
388
389 return $this->output_ajax_response( $json );
390 }
391
392 public function ajax_redirect_add() {
393 global $hook_suffix;
394
395 $hook_suffix = '';
396
397 $this->check_ajax_referer( 'redirection-redirect_add' );
398
399 $item = Red_Item::create( $_POST );
400 if ( is_wp_error( $item ) )
401 $json['error'] = $item->get_error_message();
402 elseif ( $item !== false ) {
403 $pager = new Redirection_Table( array(), 0 );
404 $json = array( 'html' => $pager->get_row( $item ) );
405 }
406 else
407 $json['error'] = __( 'Sorry, but your redirection was not created', 'redirection' );
408
409 return $this->output_ajax_response( $json );
410 }
411
412 public function ajax_group_action( $params ) {
413 $ajax = $this->check_ajax_referer( 'wp_rest' );
414 if ( $ajax !== true ) {
415 return $ajax;
416 }
417
418 if ( empty( $params ) ) {
419 $params = $_POST;
420 }
421
422 $action = false;
423 $items = array();
424 if ( isset( $params['items'] ) ) {
425 $items = array_map( 'intval', explode( ',', $params['items'] ) );
426 }
427
428 if ( isset( $params['bulk'] ) && in_array( $params['bulk'], array( 'delete', 'enable', 'disable' ) ) ) {
429 $action = $params['bulk'];
430
431 foreach ( $items as $item ) {
432 $group = Red_Group::get( intval( $item, 10 ) );
433
434 if ( $group ) {
435 if ( $action === 'delete' ) {
436 $group->delete();
437 } else if ( $action === 'disable' ) {
438 $group->disable();
439 } else if ( $action === 'enable' ) {
440 $group->enable();
441 }
442 }
443 }
444 }
445
446 return $this->output_ajax_response( Red_Group::get_filtered( $params ) );
447 }
448
449 public function ajax_create_group( $params ) {
450 $ajax = $this->check_ajax_referer( 'wp_rest' );
451 if ( $ajax !== true ) {
452 return $ajax;
453 }
454
455 if ( empty( $params ) ) {
456 $params = $_POST;
457 }
458
459 if ( ! Red_Group::create( isset( $params['name'] ) ? $params['name'] : '', isset( $params['moduleId'] ) ? $params['moduleId'] : 0 ) ) {
460 global $wpdb;
461
462 $cause = 'unknown';
463 if ( !isset( $params['name'] ) || !isset( $params['module_id'] ) ) {
464 $cause = 'invalid parameters';
465 } else if ( $wpdb->last_error ) {
466 $cause = $wpdb->last_error;
467 }
468
469 return $this->output_ajax_response( array( 'error' => 'Failed creating group - '.$cause.' ('.__LINE__.')' ) );
470 }
471
472 return $this->output_ajax_response( Red_Group::get_filtered( $params ) );
473 }
474
475 public function ajax_get_group( $params ) {
476 $ajax = $this->check_ajax_referer( 'wp_rest' );
477 if ( $ajax !== true ) {
478 return $ajax;
479 }
480
481 if ( empty( $params ) ) {
482 $params = $_POST;
483 }
484
485 return $this->output_ajax_response( Red_Group::get_filtered( $params ) );
486 }
487
488 public function ajax_set_group( $params ) {
489 $ajax = $this->check_ajax_referer( 'wp_rest' );
490 if ( $ajax !== true ) {
491 return $ajax;
492 }
493
494 if ( empty( $params ) ) {
495 $params = $_POST;
496 }
497
498 $groupId = 0;
499 $name = '';
500 $moduleId = 0;
501 if ( isset( $params['groupId'] ) ) {
502 $groupId = intval( $params['groupId'], 10 );
503 }
504
505 $result = array( 'error' => 'Invalid group or parameters ('.__LINE__.')' );
506 if ( $groupId > 0 ) {
507 $group = Red_Group::get( $params['groupId'] );
508
509 if ( $group && isset( $params['name'] ) && isset( $params['moduleId'] ) ) {
510 $group->update( $params );
511 $result = $group->to_json();
512 }
513 }
514
515 return $this->output_ajax_response( $result );
516 }
517
518 public function ajax_get_module( $params ) {
519 $ajax = $this->check_ajax_referer( 'wp_rest' );
520 if ( $ajax !== true ) {
521 return $ajax;
522 }
523
524 if ( empty( $params ) ) {
525 $params = $_POST;
526 }
527
528 $moduleType = false;
529
530 $modules = array( 1, 2, 3 );
531 if ( isset( $params['moduleId'] ) ) {
532 $moduleId = intval( $params['moduleId'], 10 );
533
534 if ( $moduleId > 0 && $moduleId <= 3 ) {
535 $modules = array( $moduleId );
536 }
537 }
538
539 if ( isset( $params['moduleType'] ) && in_array( $params['moduleType'], array( 'csv', 'apache', 'nginx' ) ) ) {
540 $moduleType = $params['moduleType'];
541 }
542
543 foreach ( $modules as $moduleId ) {
544 $module = Red_Module::get( $moduleId );
545 $moduleData = $this->get_module_data( $module, $moduleType );
546
547 if ( $module->get_id() === Apache_Module::MODULE_ID ) {
548 $moduleData['data'] = array(
549 'installed' => ABSPATH,
550 'location' => $module->get_location(),
551 'canonical' => $module->get_canonical(),
552 );
553 }
554
555 $result[] = $moduleData;
556 }
557
558 return $this->output_ajax_response( $result );
559 }
560
561 public function ajax_set_module( $params ) {
562 $ajax = $this->check_ajax_referer( 'wp_rest' );
563 if ( $ajax !== true ) {
564 return $ajax;
565 }
566
567 if ( empty( $params ) ) {
568 $params = $_POST;
569 }
570
571 if ( isset( $params['module'] ) ) {
572 $module = $this->get_module( $params['module'] );
573
574 if ( $module ) {
575 $module->update( $params );
576 }
577 }
578
579 return $this->ajax_get_module( $params );
580 }
581
582 private function get_module( $module_name ) {
583 $module_id_name = array(
584 'apache' => Apache_Module::MODULE_ID,
585 'wordpress' => WordPress_Module::MODULE_ID,
586 'nginx' => Nginx_Module::MODULE_ID,
587 );
588
589 if ( isset( $module_id_name[ $module_name ] ) ) {
590 return Red_Module::get( $module_id_name[ $module_name ] );
591 }
592
593 return false;
594 }
595
596 private function get_module_data( $module, $type ) {
597 $data = array(
598 'redirects' => $module->get_total_redirects(),
599 'module_id' => $module->get_id(),
600 'displayName' => $module->get_name(),
601 );
602
603 if ( $type ) {
604 $exporter = Red_FileIO::create( $type );
605 $data['data'] = $exporter->get( Red_Item::get_all_for_module( $module->get_id() ) );
606 }
607
608 return $data;
609 }
610
611 public function ajax_delete_plugin() {
612 $ajax = $this->check_ajax_referer( 'wp_rest' );
613 if ( $ajax !== true ) {
614 return $ajax;
615 }
616
617 $this->plugin_uninstall();
618
619 $current = get_option( 'active_plugins' );
620 array_splice( $current, array_search( basename( dirname( REDIRECTION_FILE ) ).'/'.basename( REDIRECTION_FILE ), $current ), 1 );
621 update_option( 'active_plugins', $current );
622
623 return $this->output_ajax_response( array( 'location' => admin_url().'plugins.php' ) );
624 }
625
626 public function ajax_load_settings() {
627 $ajax = $this->check_ajax_referer( 'wp_rest' );
628 if ( $ajax !== true ) {
629 return $ajax;
630 }
631 return $this->output_ajax_response( array( 'settings' => red_get_options(), 'groups' => $this->groups_to_json( Red_Group::get_for_select() ) ) );
632 }
633
634 public function ajax_save_settings( $settings = array() ) {
635 $ajax = $this->check_ajax_referer( 'wp_rest' );
636 if ( $ajax !== true ) {
637 return $ajax;
638 }
639
640 if ( empty( $settings ) ) {
641 $settings = $_POST;
642 }
643
644 $options = red_get_options();
645
646 if ( isset( $settings['monitor_post'] ) ) {
647 $options['monitor_post'] = max( 0, intval( $settings['monitor_post'], 10 ) );
648 }
649
650 if ( isset( $settings['auto_target'] ) ) {
651 $options['auto_target'] = stripslashes( $settings['auto_target'] );
652 }
653
654 if ( isset( $settings['support'] ) ) {
655 $options['support'] = $settings['support'] === 'true' ? true : false;
656 }
657
658 if ( isset( $settings['token'] ) ) {
659 $options['token'] = stripslashes( $settings['token'] );
660 }
661
662 if ( !isset( $settings['token'] ) || trim( $options['token'] ) === '' ) {
663 $options['token'] = md5( uniqid() );
664 }
665
666 if ( isset( $settings['newsletter'] ) ) {
667 $options['newsletter'] = $settings['newsletter'] === 'true' ? true : false;
668 }
669
670 if ( isset( $settings['expire_redirect'] ) ) {
671 $options['expire_redirect'] = max( -1, min( intval( $settings['expire_redirect'], 10 ), 60 ) );
672 }
673
674 if ( isset( $settings['expire_404'] ) ) {
675 $options['expire_404'] = max( -1, min( intval( $settings['expire_404'], 10 ), 60 ) );
676 }
677
678 update_option( 'redirection_options', $options );
679 do_action( 'redirection_save_options', $options );
680
681 return $this->output_ajax_response( array( 'settings' => $options, 'groups' => $this->groups_to_json( Red_Group::get_for_select() ) ) );
682 }
683
684 public function ajax_get_logs( $params ) {
685 $ajax = $this->check_ajax_referer( 'wp_rest' );
686 if ( $ajax !== true ) {
687 return $ajax;
688 }
689
690 if ( empty( $params ) ) {
691 $params = $_POST;
692 }
693
694 $result = $this->get_logs( $params );
695
696 return $this->output_ajax_response( $result );
697 }
698
699 public function ajax_log_action( $params ) {
700 $ajax = $this->check_ajax_referer( 'wp_rest' );
701 if ( $ajax !== true ) {
702 return $ajax;
703 }
704
705 if ( empty( $params ) ) {
706 $params = $_POST;
707 }
708
709 // Do the action
710 if ( isset( $params['bulk'] ) && isset( $params['items'] ) && $params['bulk'] === 'delete' ) {
711 $items = explode( ',', $params['items'] );
712
713 if ( $this->get_log_type( $params ) === 'log' ) {
714 array_map( array( 'RE_Log', 'delete' ), $items );
715 } else {
716 array_map( array( 'RE_404', 'delete' ), $items );
717 }
718 }
719
720 $result = $this->get_logs( $params );
721
722 return $this->output_ajax_response( $result );
723 }
724
725 public function ajax_delete_all( $params ) {
726 $ajax = $this->check_ajax_referer( 'wp_rest' );
727 if ( $ajax !== true ) {
728 return $ajax;
729 }
730
731 if ( empty( $params ) ) {
732 $params = $_POST;
733 }
734
735 if ( isset( $params['logType'] ) ) {
736 if ( $params['logType'] === 'log' ) {
737 RE_Log::delete_all();
738 } else {
739 RE_404::delete_all();
740 }
741 }
742
743 $result = $this->get_logs( $params );
744
745 return $this->output_ajax_response( $result );
746 }
747
748 private function get_log_type( $params ) {
749 $type = 'log';
750
751 if ( isset( $params['logType'] ) && in_array( $params['logType'], array( 'log', '404' ), true ) ) {
752 $type = $params['logType'];
753 }
754
755 return $type;
756 }
757
758 private function get_logs( array $params ) {
759 $type = $this->get_log_type( $params );
760
761 if ( $type === 'log' ) {
762 return RE_Filter_Log::get( 'redirection_logs', 'RE_Log', $params );
763 } else if ( $type === '404' ) {
764 if ( isset( $params['filterBy'] ) && isset( $params['filter'] ) && $params['filterBy'] === 'ip' ) {
765 $params['filter'] = ip2long( $params['filter'] );
766 }
767
768 return RE_Filter_Log::get( 'redirection_404', 'RE_404', $params );
769 }
770
771 return array( 'items' => array(), 'total' => 0 );
772 }
773
774 private function groups_to_json( $groups, $depth = 0 ) {
775 $items = array();
776
777 foreach ( $groups as $text => $value ) {
778 if ( is_array( $value ) && $depth === 0 ) {
779 $items[] = (object)array( 'text' => $text, 'value' => $this->groups_to_json( $value, 1 ) );
780 } else {
781 $items[] = (object)array( 'text' => $value, 'value' => $text );
782 }
783 }
784
785 return $items;
786 }
787
788 private function output_ajax_response( $data ) {
789 $result = wp_json_encode( $data );
790
791 if ( defined( 'DOING_AJAX' ) ) {
792 header( 'Content-Type: application/json' );
793 echo $result;
794 wp_die();
795 }
796
797 return $result;
798 }
799 }
800
801 register_activation_hook( REDIRECTION_FILE, array( 'Redirection_Admin', 'plugin_activated' ) );
802
803 add_action( 'init', array( 'Redirection_Admin', 'init' ) );
804