monitor = new Red_Monitor( red_get_options() );
$this->export_rss();
}
public static function plugin_activated() {
Redirection_Admin::update();
Red_Flusher::schedule();
update_option( 'redirection_options', red_get_options() );
}
public static function plugin_deactivated() {
Red_Flusher::clear();
}
public static function plugin_uninstall() {
include_once dirname( REDIRECTION_FILE ).'/models/database.php';
$db = new RE_Database();
$db->remove( REDIRECTION_FILE );
delete_option( 'redirection_options' );
}
public function flush_schedule() {
Red_Flusher::schedule();
}
private function render( $template, $template_vars = array() ) {
foreach ( $template_vars as $key => $val ) {
$$key = $val;
}
if ( file_exists( dirname( REDIRECTION_FILE )."/view/$template.php" ) ) {
include dirname( REDIRECTION_FILE )."/view/$template.php";
}
}
private function capture( $ug_name, $ug_vars = array() ) {
ob_start();
$this->render( $ug_name, $ug_vars );
$output = ob_get_contents();
ob_end_clean();
return $output;
}
private function render_error( $message ) {
?>
';
$json['html'] .= '
';
}
return $this->output_ajax_response( $json );
}
public function ajax_get_nginx() {
$this->get_module_column( intval( $_POST['id'] ), 'nginx' );
}
public function ajax_get_htaccess() {
$this->get_module_column( intval( $_POST['id'] ), 'apache' );
}
public function ajax_delete_plugin() {
$ajax = $this->check_ajax_referer( 'wp_rest' );
if ( $ajax !== true ) {
return $ajax;
}
$this->plugin_uninstall();
$current = get_option( 'active_plugins' );
array_splice( $current, array_search( basename( dirname( REDIRECTION_FILE ) ).'/'.basename( REDIRECTION_FILE ), $current ), 1 );
update_option( 'active_plugins', $current );
return $this->output_ajax_response( array( 'location' => admin_url().'plugins.php' ) );
}
public function ajax_load_settings() {
$ajax = $this->check_ajax_referer( 'wp_rest' );
if ( $ajax !== true ) {
return $ajax;
}
return $this->output_ajax_response( array( 'settings' => red_get_options(), 'groups' => $this->groups_to_json( Red_Group::get_for_select() ) ) );
}
public function ajax_save_settings( $settings = array() ) {
$ajax = $this->check_ajax_referer( 'wp_rest' );
if ( $ajax !== true ) {
return $ajax;
}
if ( empty( $settings ) ) {
$settings = $_POST;
}
$options = red_get_options();
if ( isset( $settings['monitor_post'] ) ) {
$options['monitor_post'] = max( 0, intval( $settings['monitor_post'], 10 ) );
}
if ( isset( $settings['auto_target'] ) ) {
$options['auto_target'] = stripslashes( $settings['auto_target'] );
}
if ( isset( $settings['support'] ) ) {
$options['support'] = $settings['support'] === 'true' ? true : false;
}
if ( isset( $settings['token'] ) ) {
$options['token'] = stripslashes( $settings['token'] );
if ( trim( $options['token'] ) === '' ) {
$options['token'] = md5( uniqid() );
}
}
if ( isset( $settings['newsletter'] ) ) {
$options['newsletter'] = $settings['newsletter'] === 'true' ? true : false;
}
if ( isset( $settings['expire_redirect'] ) ) {
$options['expire_redirect'] = max( -1, min( intval( $settings['expire_redirect'], 10 ), 60 ) );
}
if ( isset( $settings['expire_404'] ) ) {
$options['expire_404'] = max( -1, min( intval( $settings['expire_404'], 10 ), 60 ) );
}
update_option( 'redirection_options', $options );
do_action( 'redirection_save_options', $options );
return $this->output_ajax_response( array( 'settings' => $options, 'groups' => $this->groups_to_json( Red_Group::get_for_select() ) ) );
}
public function ajax_get_logs( $params ) {
$ajax = $this->check_ajax_referer( 'wp_rest' );
if ( $ajax !== true ) {
return $ajax;
}
if ( empty( $params ) ) {
$params = $_POST;
}
$result = $this->get_logs( $params );
return $this->output_ajax_response( $result );
}
public function ajax_log_action( $params ) {
$ajax = $this->check_ajax_referer( 'wp_rest' );
if ( $ajax !== true ) {
return $ajax;
}
if ( empty( $params ) ) {
$params = $_POST;
}
// Do the action
if ( isset( $params['bulk'] ) && isset( $params['items'] ) && $params['bulk'] === 'delete' ) {
$items = explode( ',', $params['items'] );
if ( $this->get_log_type( $params ) === 'log' ) {
array_map( array( 'RE_Log', 'delete' ), $items );
} else {
array_map( array( 'RE_404', 'delete' ), $items );
}
}
$result = $this->get_logs( $params );
return $this->output_ajax_response( $result );
}
public function ajax_delete_all( $params ) {
$ajax = $this->check_ajax_referer( 'wp_rest' );
if ( $ajax !== true ) {
return $ajax;
}
if ( empty( $params ) ) {
$params = $_POST;
}
if ( isset( $params['logType'] ) ) {
if ( $params['logType'] === 'log' ) {
RE_Log::delete_all();
} else {
RE_404::delete_all();
}
}
$result = $this->get_logs( $params );
return $this->output_ajax_response( $result );
}
private function get_log_type( $params ) {
$type = 'log';
if ( isset( $params['logType'] ) && in_array( $params['logType'], array( 'log', '404' ), true ) ) {
$type = $params['logType'];
}
return $type;
}
private function get_logs( array $params ) {
$type = $this->get_log_type( $params );
if ( $type === 'log' ) {
return RE_Filter_Log::get( 'redirection_logs', 'RE_Log', $params );
} else if ( $type === '404' ) {
if ( isset( $params['filterBy'] ) && isset( $params['filter'] ) && $params['filterBy'] === 'ip' ) {
$params['filter'] = ip2long( $params['filter'] );
}
return RE_Filter_Log::get( 'redirection_404', 'RE_404', $params );
}
return array( 'items' => array(), 'total' => 0 );
}
private function groups_to_json( $groups, $depth = 0 ) {
$items = array();
foreach ( $groups as $text => $value ) {
if ( is_array( $value ) && $depth === 0 ) {
$items[] = (object)array( 'text' => $text, 'value' => $this->groups_to_json( $value, 1 ) );
} else {
$items[] = (object)array( 'text' => $value, 'value' => $text );
}
}
return $items;
}
private function output_ajax_response( array $data ) {
$result = wp_json_encode( $data );
if ( defined( 'DOING_AJAX' ) ) {
header( 'Content-Type: application/json' );
echo $result;
wp_die();
}
return $result;
}
}
register_activation_hook( REDIRECTION_FILE, array( 'Redirection_Admin', 'plugin_activated' ) );
add_action( 'init', array( 'Redirection_Admin', 'init' ) );