PluginProbe
Redirection / 2.6.4
Redirection v2.6.4
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.4, at redirection-admin.php

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