PluginProbe
Redirection / 2.6.3
Redirection v2.6.3
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.3, at redirection-admin.php

710 lines 21.8 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_module_edit', array( $this, 'ajax_module_edit' ) );
33 add_action( 'wp_ajax_red_module_save', array( $this, 'ajax_module_save' ) );
34 add_action( 'wp_ajax_red_group_edit', array( $this, 'ajax_group_edit' ) );
35 add_action( 'wp_ajax_red_group_save', array( $this, 'ajax_group_save' ) );
36 add_action( 'wp_ajax_red_redirect_add', array( $this, 'ajax_redirect_add' ) );
37 add_action( 'wp_ajax_red_redirect_edit', array( $this, 'ajax_redirect_edit' ) );
38 add_action( 'wp_ajax_red_redirect_save', array( $this, 'ajax_redirect_save' ) );
39 add_action( 'wp_ajax_red_get_htaccess', array( $this, 'ajax_get_htaccess' ) );
40 add_action( 'wp_ajax_red_get_nginx', array( $this, 'ajax_get_nginx' ) );
41
42 add_action( 'wp_ajax_red_load_settings', array( $this, 'ajax_load_settings' ) );
43 add_action( 'wp_ajax_red_save_settings', array( $this, 'ajax_save_settings' ) );
44 add_action( 'wp_ajax_red_get_logs', array( $this, 'ajax_get_logs' ) );
45 add_action( 'wp_ajax_red_log_action', array( $this, 'ajax_log_action' ) );
46 add_action( 'wp_ajax_red_delete_plugin', array( $this, 'ajax_delete_plugin' ) );
47 add_action( 'wp_ajax_red_delete_all', array( $this, 'ajax_delete_all' ) );
48
49 add_action( 'redirection_save_options', array( $this, 'flush_schedule' ) );
50
51 $this->monitor = new Red_Monitor( red_get_options() );
52
53 $this->export_rss();
54 }
55
56 public static function plugin_activated() {
57 Redirection_Admin::update();
58 Red_Flusher::schedule();
59
60 update_option( 'redirection_options', red_get_options() );
61 }
62
63 public static function plugin_deactivated() {
64 Red_Flusher::clear();
65 }
66
67 public static function plugin_uninstall() {
68 include_once dirname( REDIRECTION_FILE ).'/models/database.php';
69
70 $db = new RE_Database();
71 $db->remove( REDIRECTION_FILE );
72
73 delete_option( 'redirection_options' );
74 }
75
76 public function flush_schedule() {
77 Red_Flusher::schedule();
78 }
79
80 private function render( $template, $template_vars = array() ) {
81 foreach ( $template_vars as $key => $val ) {
82 $$key = $val;
83 }
84
85 if ( file_exists( dirname( REDIRECTION_FILE )."/view/$template.php" ) ) {
86 include dirname( REDIRECTION_FILE )."/view/$template.php";
87 }
88 }
89
90 private function capture( $ug_name, $ug_vars = array() ) {
91 ob_start();
92
93 $this->render( $ug_name, $ug_vars );
94 $output = ob_get_contents();
95
96 ob_end_clean();
97 return $output;
98 }
99
100 private function render_error( $message ) {
101 ?>
102 <div class="fade error" id="message">
103 <p><?php echo $message ?></p>
104 </div>
105 <?php
106 }
107
108 private function render_message( $message, $timeout = 0 ) {
109 ?>
110 <div class="updated" id="message" onclick="this.parentNode.removeChild(this)">
111 <p><?php echo $message ?></p>
112 </div>
113 <?php
114 }
115
116 private static function update() {
117 $version = get_option( 'redirection_version' );
118
119 Red_Flusher::schedule();
120
121 if ( $version !== REDIRECTION_VERSION ) {
122 include_once dirname( REDIRECTION_FILE ).'/models/database.php';
123
124 $database = new RE_Database();
125
126 if ( $version === false ) {
127 $database->install();
128 }
129
130 return $database->upgrade( $version, REDIRECTION_VERSION );
131 }
132
133 return true;
134 }
135
136 private function select( $items, $default = '' ) {
137 foreach ( $items as $key => $value ) {
138 if ( is_array( $value ) ) {
139 echo '<optgroup label="'.esc_attr( $key ).'">';
140
141 foreach ( $value as $sub => $subvalue ) {
142 echo '<option value="'.esc_attr( $sub ).'"'.( $sub === $default ? ' selected="selected"' : '' ).'>'.esc_html( $subvalue ).'</option>';
143 }
144
145 echo '</optgroup>';
146 }
147 else
148 echo '<option value="'.esc_attr( $key ).'"'.( $key === $default ? ' selected="selected"' : '' ).'>'.esc_html( $value ).'</option>';
149 }
150 }
151
152 function set_per_page( $status, $option, $value ) {
153 if ( $option === 'redirection_log_per_page' )
154 return $value;
155 return $status;
156 }
157
158 function plugin_settings( $links ) {
159 $settings_link = '<a href="tools.php?page='.basename( REDIRECTION_FILE ).'&amp;sub=options">'.__( 'Settings', 'redirection' ).'</a>';
160 array_unshift( $links, $settings_link );
161 return $links;
162 }
163
164 function redirection_head() {
165 $version = get_plugin_data( REDIRECTION_FILE );
166 $version = $version['Version'];
167
168 $this->inject();
169
170 if ( ! isset( $_GET['sub'] ) || ( isset( $_GET['sub'] ) && ( in_array( $_GET['sub'], array( 'log', '404s', 'groups' ) ) ) ) ) {
171 add_screen_option( 'per_page', array( 'label' => __( 'Log entries', 'redirection' ), 'default' => 25, 'option' => 'redirection_log_per_page' ) );
172 }
173
174 wp_enqueue_script( 'redirection', plugin_dir_url( REDIRECTION_FILE ).'redirection.js', array( 'jquery-form', 'jquery-ui-sortable' ), $version );
175
176 if ( defined( 'REDIRECTION_DEV_MODE' ) && REDIRECTION_DEV_MODE ) {
177 wp_enqueue_script( 'redirection-ui', 'http://localhost:3312/redirection-ui.js', array( 'redirection' ), $version );
178 } else {
179 wp_enqueue_script( 'redirection-ui', plugin_dir_url( REDIRECTION_FILE ).'redirection-ui.js', array( 'redirection' ), $version );
180 }
181
182 wp_enqueue_style( 'redirection', plugin_dir_url( REDIRECTION_FILE ).'admin.css', $version );
183
184 wp_localize_script( 'redirection', 'Redirectioni10n', array(
185 'WP_API_root' => admin_url( 'admin-ajax.php' ),
186 'WP_API_nonce' => wp_create_nonce( 'wp_rest' ),
187 'pluginBaseUrl' => plugins_url( '', REDIRECTION_FILE ),
188 'pluginRoot' => admin_url( 'tools.php?page=redirection.php' ),
189 'per_page' => $this->get_per_page(),
190 'locale' => $this->get_i18n_data(),
191 'localeSlug' => get_locale(),
192 ) );
193 }
194
195 private function get_per_page() {
196 $per_page = intval( get_user_meta( get_current_user_id(), 'redirection_log_per_page', true ), 10 );
197
198 return $per_page > 0 ? $per_page : 25;
199 }
200
201 private function get_i18n_data() {
202 $i18n_json = REDIRECTION_FILE . 'locale/json/redirection-' . get_locale() . '.json';
203
204 if ( is_file( $i18n_json ) && is_readable( $i18n_json ) ) {
205 $locale_data = @file_get_contents( $i18n_json );
206
207 if ( $locale_data ) {
208 return $locale_data;
209 }
210 }
211
212 // Return empty if we have nothing to return so it doesn't fail when parsed in JS
213 return '{}';
214 }
215
216 function admin_menu() {
217 add_management_page( 'Redirection', 'Redirection', apply_filters( 'redirection_role', 'administrator' ), basename( REDIRECTION_FILE ), array( &$this, 'admin_screen' ) );
218 }
219
220 function export_rss() {
221 if ( isset( $_GET['token'] ) && isset( $_GET['page'] ) && isset( $_GET['sub'] ) && $_GET['page'] === 'redirection.php' && $_GET['sub'] === 'rss' ) {
222 $options = red_get_options();
223
224 if ( $_GET['token'] === $options['token'] && !empty( $options['token'] ) ) {
225 $items = Red_Item::get_all_for_module( intval( $_GET['module'] ) );
226
227 $exporter = Red_FileIO::create( 'rss' );
228 $exporter->export( $items );
229 die();
230 }
231 }
232 }
233
234 function admin_screen() {
235 Redirection_Admin::update();
236
237 if ( isset( $_GET['sub'] ) ) {
238 if ( $_GET['sub'] === 'log' )
239 return $this->admin_screen_log();
240 elseif ( $_GET['sub'] === '404s' )
241 return $this->admin_screen_404();
242 elseif ( $_GET['sub'] === 'options' )
243 return $this->admin_screen_options();
244 elseif ( $_GET['sub'] === 'process' )
245 return $this->admin_screen_process();
246 elseif ( $_GET['sub'] === 'groups' )
247 return $this->admin_groups( isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0 );
248 elseif ( $_GET['sub'] === 'modules' )
249 return $this->admin_screen_modules();
250 elseif ( $_GET['sub'] === 'support' )
251 return $this->render( 'support', array( 'options' => red_get_options() ) );
252 }
253
254 return $this->admin_redirects( isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0 );
255 }
256
257 function admin_screen_modules() {
258 $options = red_get_options();
259 $pager = new Redirection_Module_Table( $options['token'] );
260 $pager->prepare_items();
261
262 $this->render( 'module-list', array( 'options' => $options, 'table' => $pager ) );
263 }
264
265 function inject() {
266 if ( isset( $_POST['id'] ) && ! isset( $_POST['action'] ) ) {
267 wp_safe_redirect( add_query_arg( 'id', intval( $_POST['id'] ), $_SERVER['REQUEST_URI'] ) );
268 die();
269 }
270
271 if ( isset( $_GET['page'] ) && isset( $_GET['sub'] ) && $_GET['page'] === 'redirection.php' ) {
272 if ( isset( $_POST['export-csv'] ) && check_admin_referer( 'wp_rest' ) ) {
273 if ( isset( $_GET['sub'] ) && $_GET['sub'] === 'log' ) {
274 RE_Log::export_to_csv();
275 } else {
276 RE_404::export_to_csv();
277 }
278
279 die();
280 } else {
281 $exporter = Red_FileIO::create( $_GET['sub'] );
282
283 if ( $exporter ) {
284 $items = Red_Item::get_all_for_module( intval( $_GET['module'] ) );
285
286 $exporter->export( $items );
287 die();
288 }
289 }
290 }
291 }
292
293 function admin_screen_options() {
294 if ( isset( $_POST['import'] ) && check_admin_referer( 'wp_rest' ) ) {
295 $count = Red_FileIO::import( $_POST['group'], $_FILES['upload'] );
296
297 if ( $count > 0 ) {
298 $this->render_message( sprintf( _n( '%d redirection was successfully imported','%d redirections were successfully imported', $count, 'redirection' ), $count ) );
299 } else {
300 $this->render_message( __( 'No items were imported', 'redirection' ) );
301 }
302 }
303
304 $groups = Red_Group::get_for_select();
305 $this->render( 'options', array( 'options' => red_get_options(), 'groups' => $groups ) );
306 }
307
308 function admin_screen_log() {
309 $options = red_get_options();
310
311 $this->render( 'log', array( 'options' => $options, 'title' => __( 'Redirection Log', 'redirection' ) ) );
312 }
313
314 function admin_screen_404() {
315 $options = red_get_options();
316
317 $this->render( 'log', array( 'options' => $options, 'title' => __( 'Redirection 404', 'redirection' ) ) );
318 }
319
320 function admin_groups( $module ) {
321 if ( isset( $_POST['add'] ) && check_admin_referer( 'redirection-add_group' ) ) {
322 if ( Red_Group::create( stripslashes( $_POST['name'] ), intval( $_POST['module_id'] ) ) ) {
323 $this->render_message( __( 'Your group was added successfully', 'redirection' ) );
324 }
325 else
326 $this->render_error( __( 'Please specify a group name', 'redirection' ) );
327 }
328
329 $table = new Redirection_Group_Table( Red_Module::get_for_select() );
330 $table->prepare_items();
331
332 $this->render( 'group-list', array( 'options' => red_get_options(), 'table' => $table, 'modules' => Red_Module::get_for_select(), 'module' => $module ) );
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.'")' ) );
345 }
346
347 if ( current_user_can( apply_filters( 'redirection_role', 'administrator' ) ) === false ) {
348 return $this->output_ajax_response( array( 'error' => __( 'No permissions to perform action' ) ) );
349 }
350
351 return true;
352 }
353
354 public function ajax_module_edit() {
355 $module_id = intval( $_POST['id'] );
356
357 $this->check_ajax_referer( 'red_edit-'.$module_id );
358
359 $module = Red_Module::get( $module_id );
360 if ( $module )
361 $json['html'] = $this->capture( 'module-edit', array( 'module' => $module ) );
362 else
363 $json['error'] = __( 'Unable to perform action' ).' - could not find module';
364
365 return $this->output_ajax_response( $json );
366 }
367
368 public function ajax_module_save() {
369 $module_id = intval( $_POST['id'] );
370
371 $this->check_ajax_referer( 'red_module_save_'.$module_id );
372
373 $module = Red_Module::get( $module_id );
374 if ( $module ) {
375 $result = $module->update( $_POST );
376
377 if ( $result === true ) {
378 global $hook_suffix;
379
380 $hook_suffix = '';
381 $options = red_get_options();
382 $pager = new Redirection_Module_Table( $options['token'] );
383
384 $json = array( 'html' => $pager->column_name( $module ) );
385 }
386 else
387 $json['error'] = $result;
388 }
389 else
390 $json['error'] = __( 'Unable to perform action' ).' - could not find module';
391
392 return $this->output_ajax_response( $json );
393 }
394
395 public function ajax_group_edit() {
396 $group_id = intval( $_POST['id'] );
397
398 $this->check_ajax_referer( 'red-edit_'.$group_id );
399
400 $group = Red_Group::get( $group_id );
401 if ( $group )
402 $json['html'] = $this->capture( 'group-edit', array( 'group' => $group, 'modules' => Red_Module::get_for_select() ) );
403 else
404 $json['error'] = __( 'Unable to perform action' ).' - could not find group';
405
406 return $this->output_ajax_response( $json );
407 }
408
409 public function ajax_group_save() {
410 global $hook_suffix;
411
412 $hook_suffix = '';
413 $group_id = intval( $_POST['id'] );
414
415 $this->check_ajax_referer( 'redirection-group_save_'.$group_id );
416
417 $group = Red_Group::get( $group_id );
418 if ( $group ) {
419 $group->update( $_POST );
420 $module = Red_Module::get( $group->get_module_id() );
421
422 $pager = new Redirection_Group_Table( array(), false );
423 $json = array( 'html' => $pager->column_name( $group ), 'module' => $module->get_name() );
424 }
425 else
426 $json['error'] = __( 'Unable to perform action' ).' - could not find redirect';
427
428 return $this->output_ajax_response( $json );
429 }
430
431 public function ajax_redirect_edit() {
432 $this->check_ajax_referer( 'red-edit_'.intval( $_POST['id'] ) );
433 $redirect = Red_Item::get_by_id( intval( $_POST['id'] ) );
434
435 if ( $redirect )
436 $json['html'] = $this->capture( 'item-edit', array( 'redirect' => $redirect, 'groups' => Red_Group::get_for_select() ) );
437 else
438 $json['error'] = __( 'Unable to perform action' ).' - could not find redirect';
439
440 return $this->output_ajax_response( $json );
441 }
442
443 public function ajax_redirect_save() {
444 global $hook_suffix;
445
446 $hook_suffix = '';
447
448 $red_id = intval( $_POST['id'] );
449
450 $this->check_ajax_referer( 'redirection-redirect_save_'.$red_id );
451
452 $redirect = Red_Item::get_by_id( $red_id );
453 if ( $redirect ) {
454 $redirect->update( $_POST );
455
456 $pager = new Redirection_Table( array(), 0 );
457 $json = array( 'html' => $pager->column_url( $redirect ), 'code' => $redirect->get_action_code() );
458 }
459 else
460 $json['error'] = __( 'Unable to perform action' ).' - could not find redirect';
461
462 return $this->output_ajax_response( $json );
463 }
464
465 public function ajax_redirect_add() {
466 global $hook_suffix;
467
468 $hook_suffix = '';
469
470 $this->check_ajax_referer( 'redirection-redirect_add' );
471
472 $item = Red_Item::create( $_POST );
473 if ( is_wp_error( $item ) )
474 $json['error'] = $item->get_error_message();
475 elseif ( $item !== false ) {
476 $pager = new Redirection_Table( array(), 0 );
477 $json = array( 'html' => $pager->get_row( $item ) );
478 }
479 else
480 $json['error'] = __( 'Sorry, but your redirection was not created', 'redirection' );
481
482 return $this->output_ajax_response( $json );
483 }
484
485 private function get_module_column( $module_id, $export_type ) {
486 $json['error'] = __( 'Invalid module', 'redirection' );
487
488 $module = Red_Module::get( $module_id );
489 $exporter = Red_FileIO::create( $export_type );
490
491 if ( $module && $exporter ) {
492 global $hook_suffix;
493
494 $hook_suffix = '';
495 $options = red_get_options();
496 $pager = new Redirection_Module_Table( $options['token'] );
497 $items = Red_Item::get_all_for_module( $module_id );
498
499 $json = array( 'html' => $pager->column_name( $module ) );
500
501 $json['html'] .= '<textarea readonly="readonly" class="module-export" rows="10">'.esc_textarea( $exporter->get( $items ) ).'</textarea>';
502 $json['html'] .= '<div class="table-actions"><a href="?page=redirection.php&amp;token='.$options['token'].'&amp;sub='.$export_type.'&amp;module='.$module_id.'"><input class="button-primary" type="button" value="'.__( 'Download', 'redirection' ).'"/></a> ';
503 $json['html'] .= '<input class="button-secondary" type="submit" name="cancel" value="'.__( 'Cancel', 'redirection' ).'"/>';
504 }
505
506 return $this->output_ajax_response( $json );
507 }
508
509 public function ajax_get_nginx() {
510 $this->get_module_column( intval( $_POST['id'] ), 'nginx' );
511 }
512
513 public function ajax_get_htaccess() {
514 $this->get_module_column( intval( $_POST['id'] ), 'apache' );
515 }
516
517 public function ajax_delete_plugin() {
518 $ajax = $this->check_ajax_referer( 'wp_rest' );
519 if ( $ajax !== true ) {
520 return $ajax;
521 }
522
523 $this->plugin_uninstall();
524
525 $current = get_option( 'active_plugins' );
526 array_splice( $current, array_search( basename( dirname( REDIRECTION_FILE ) ).'/'.basename( REDIRECTION_FILE ), $current ), 1 );
527 update_option( 'active_plugins', $current );
528
529 return $this->output_ajax_response( array( 'location' => admin_url().'plugins.php' ) );
530 }
531
532 public function ajax_load_settings() {
533 $ajax = $this->check_ajax_referer( 'wp_rest' );
534 if ( $ajax !== true ) {
535 return $ajax;
536 }
537 return $this->output_ajax_response( array( 'settings' => red_get_options(), 'groups' => $this->groups_to_json( Red_Group::get_for_select() ) ) );
538 }
539
540 public function ajax_save_settings( $settings = array() ) {
541 $ajax = $this->check_ajax_referer( 'wp_rest' );
542 if ( $ajax !== true ) {
543 return $ajax;
544 }
545
546 if ( empty( $settings ) ) {
547 $settings = $_POST;
548 }
549
550 $options = red_get_options();
551
552 if ( isset( $settings['monitor_post'] ) ) {
553 $options['monitor_post'] = max( 0, intval( $settings['monitor_post'], 10 ) );
554 }
555
556 if ( isset( $settings['auto_target'] ) ) {
557 $options['auto_target'] = stripslashes( $settings['auto_target'] );
558 }
559
560 if ( isset( $settings['support'] ) ) {
561 $options['support'] = $settings['support'] === 'true' ? true : false;
562 }
563
564 if ( isset( $settings['token'] ) ) {
565 $options['token'] = stripslashes( $settings['token'] );
566
567 if ( trim( $options['token'] ) === '' ) {
568 $options['token'] = md5( uniqid() );
569 }
570 }
571
572 if ( isset( $settings['newsletter'] ) ) {
573 $options['newsletter'] = $settings['newsletter'] === 'true' ? true : false;
574 }
575
576 if ( isset( $settings['expire_redirect'] ) ) {
577 $options['expire_redirect'] = max( -1, min( intval( $settings['expire_redirect'], 10 ), 60 ) );
578 }
579
580 if ( isset( $settings['expire_404'] ) ) {
581 $options['expire_404'] = max( -1, min( intval( $settings['expire_404'], 10 ), 60 ) );
582 }
583
584 update_option( 'redirection_options', $options );
585 do_action( 'redirection_save_options', $options );
586
587 return $this->output_ajax_response( array( 'settings' => $options, 'groups' => $this->groups_to_json( Red_Group::get_for_select() ) ) );
588 }
589
590 public function ajax_get_logs( $params ) {
591 $ajax = $this->check_ajax_referer( 'wp_rest' );
592 if ( $ajax !== true ) {
593 return $ajax;
594 }
595
596 if ( empty( $params ) ) {
597 $params = $_POST;
598 }
599
600 $result = $this->get_logs( $params );
601
602 return $this->output_ajax_response( $result );
603 }
604
605 public function ajax_log_action( $params ) {
606 $ajax = $this->check_ajax_referer( 'wp_rest' );
607 if ( $ajax !== true ) {
608 return $ajax;
609 }
610
611 if ( empty( $params ) ) {
612 $params = $_POST;
613 }
614
615 // Do the action
616 if ( isset( $params['bulk'] ) && isset( $params['items'] ) && $params['bulk'] === 'delete' ) {
617 $items = explode( ',', $params['items'] );
618
619 if ( $this->get_log_type( $params ) === 'log' ) {
620 array_map( array( 'RE_Log', 'delete' ), $items );
621 } else {
622 array_map( array( 'RE_404', 'delete' ), $items );
623 }
624 }
625
626 $result = $this->get_logs( $params );
627
628 return $this->output_ajax_response( $result );
629 }
630
631 public function ajax_delete_all( $params ) {
632 $ajax = $this->check_ajax_referer( 'wp_rest' );
633 if ( $ajax !== true ) {
634 return $ajax;
635 }
636
637 if ( empty( $params ) ) {
638 $params = $_POST;
639 }
640
641 if ( isset( $params['logType'] ) ) {
642 if ( $params['logType'] === 'log' ) {
643 RE_Log::delete_all();
644 } else {
645 RE_404::delete_all();
646 }
647 }
648
649 $result = $this->get_logs( $params );
650
651 return $this->output_ajax_response( $result );
652 }
653
654 private function get_log_type( $params ) {
655 $type = 'log';
656
657 if ( isset( $params['logType'] ) && in_array( $params['logType'], array( 'log', '404' ), true ) ) {
658 $type = $params['logType'];
659 }
660
661 return $type;
662 }
663
664 private function get_logs( array $params ) {
665 $type = $this->get_log_type( $params );
666
667 if ( $type === 'log' ) {
668 return RE_Filter_Log::get( 'redirection_logs', 'RE_Log', $params );
669 } else if ( $type === '404' ) {
670 if ( isset( $params['filterBy'] ) && isset( $params['filter'] ) && $params['filterBy'] === 'ip' ) {
671 $params['filter'] = ip2long( $params['filter'] );
672 }
673
674 return RE_Filter_Log::get( 'redirection_404', 'RE_404', $params );
675 }
676
677 return array( 'items' => array(), 'total' => 0 );
678 }
679
680 private function groups_to_json( $groups, $depth = 0 ) {
681 $items = array();
682
683 foreach ( $groups as $text => $value ) {
684 if ( is_array( $value ) && $depth === 0 ) {
685 $items[] = (object)array( 'text' => $text, 'value' => $this->groups_to_json( $value, 1 ) );
686 } else {
687 $items[] = (object)array( 'text' => $value, 'value' => $text );
688 }
689 }
690
691 return $items;
692 }
693
694 private function output_ajax_response( array $data ) {
695 $result = wp_json_encode( $data );
696
697 if ( defined( 'DOING_AJAX' ) ) {
698 header( 'Content-Type: application/json' );
699 echo $result;
700 wp_die();
701 }
702
703 return $result;
704 }
705 }
706
707 register_activation_hook( REDIRECTION_FILE, array( 'Redirection_Admin', 'plugin_activated' ) );
708
709 add_action( 'init', array( 'Redirection_Admin', 'init' ) );
710