| 1 |
<?php |
| 2 |
|
| 3 |
include dirname( __FILE__ ).'/models/group.php'; |
| 4 |
include dirname( __FILE__ ).'/models/monitor.php'; |
| 5 |
include dirname( __FILE__ ).'/models/file-io.php'; |
| 6 |
include dirname( __FILE__ ).'/redirection-api.php'; |
| 7 |
|
| 8 |
define( 'RED_DEFAULT_PER_PAGE', 25 ); |
| 9 |
define( 'RED_MAX_PER_PAGE', 200 ); |
| 10 |
|
| 11 |
class Redirection_Admin { |
| 12 |
private static $instance = null; |
| 13 |
private $monitor; |
| 14 |
|
| 15 |
static function init() { |
| 16 |
if ( is_null( self::$instance ) ) { |
| 17 |
self::$instance = new Redirection_Admin(); |
| 18 |
|
| 19 |
load_plugin_textdomain( 'redirection', false, dirname( plugin_basename( REDIRECTION_FILE ) ).'/locale/' ); |
| 20 |
} |
| 21 |
|
| 22 |
return self::$instance; |
| 23 |
} |
| 24 |
|
| 25 |
function __construct() { |
| 26 |
add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
| 27 |
add_action( 'load-tools_page_redirection', array( $this, 'redirection_head' ) ); |
| 28 |
add_action( 'plugin_action_links_'.basename( dirname( REDIRECTION_FILE ) ).'/'.basename( REDIRECTION_FILE ), array( $this, 'plugin_settings' ), 10, 4 ); |
| 29 |
add_action( 'redirection_save_options', array( $this, 'flush_schedule' ) ); |
| 30 |
add_filter( 'set-screen-option', array( $this, 'set_per_page' ), 10, 3 ); |
| 31 |
|
| 32 |
register_deactivation_hook( REDIRECTION_FILE, array( 'Redirection_Admin', 'plugin_deactivated' ) ); |
| 33 |
register_uninstall_hook( REDIRECTION_FILE, array( 'Redirection_Admin', 'plugin_uninstall' ) ); |
| 34 |
|
| 35 |
$this->monitor = new Red_Monitor( red_get_options() ); |
| 36 |
$this->api = new Redirection_Api(); |
| 37 |
} |
| 38 |
|
| 39 |
public static function plugin_activated() { |
| 40 |
Redirection_Admin::update(); |
| 41 |
Red_Flusher::schedule(); |
| 42 |
|
| 43 |
update_option( 'redirection_options', red_get_options() ); |
| 44 |
} |
| 45 |
|
| 46 |
public static function plugin_deactivated() { |
| 47 |
Red_Flusher::clear(); |
| 48 |
} |
| 49 |
|
| 50 |
public static function plugin_uninstall() { |
| 51 |
include_once dirname( REDIRECTION_FILE ).'/models/database.php'; |
| 52 |
|
| 53 |
$db = new RE_Database(); |
| 54 |
$db->remove( REDIRECTION_FILE ); |
| 55 |
|
| 56 |
delete_option( 'redirection_options' ); |
| 57 |
} |
| 58 |
|
| 59 |
public function flush_schedule() { |
| 60 |
Red_Flusher::schedule(); |
| 61 |
} |
| 62 |
|
| 63 |
private static function update() { |
| 64 |
$version = get_option( 'redirection_version' ); |
| 65 |
|
| 66 |
Red_Flusher::schedule(); |
| 67 |
|
| 68 |
if ( $version !== REDIRECTION_VERSION ) { |
| 69 |
include_once dirname( REDIRECTION_FILE ).'/models/database.php'; |
| 70 |
|
| 71 |
$database = new RE_Database(); |
| 72 |
|
| 73 |
if ( $version === false ) { |
| 74 |
$database->install(); |
| 75 |
} |
| 76 |
|
| 77 |
return $database->upgrade( $version, REDIRECTION_VERSION ); |
| 78 |
} |
| 79 |
|
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
function set_per_page( $status, $option, $value ) { |
| 84 |
if ( $option === 'redirection_log_per_page' ) { |
| 85 |
return max( 1, min( intval( $value, 10 ), RED_MAX_PER_PAGE ) ); |
| 86 |
} |
| 87 |
|
| 88 |
return $status; |
| 89 |
} |
| 90 |
|
| 91 |
function plugin_settings( $links ) { |
| 92 |
$settings_link = '<a href="tools.php?page='.basename( REDIRECTION_FILE ).'&sub=options">'.__( 'Settings', 'redirection' ).'</a>'; |
| 93 |
array_unshift( $links, $settings_link ); |
| 94 |
return $links; |
| 95 |
} |
| 96 |
|
| 97 |
function redirection_head() { |
| 98 |
global $wp_version; |
| 99 |
|
| 100 |
$options = red_get_options(); |
| 101 |
$version = get_plugin_data( REDIRECTION_FILE ); |
| 102 |
$version = $version['Version']; |
| 103 |
|
| 104 |
$this->inject(); |
| 105 |
|
| 106 |
if ( ! isset( $_GET['sub'] ) || ( isset( $_GET['sub'] ) && ( in_array( $_GET['sub'], array( 'log', '404s', 'groups' ) ) ) ) ) { |
| 107 |
add_screen_option( 'per_page', array( 'label' => sprintf( __( 'Log entries (%d max)', 'redirection' ), RED_MAX_PER_PAGE ), 'default' => RED_DEFAULT_PER_PAGE, 'option' => 'redirection_log_per_page' ) ); |
| 108 |
} |
| 109 |
|
| 110 |
if ( defined( 'REDIRECTION_DEV_MODE' ) && REDIRECTION_DEV_MODE ) { |
| 111 |
wp_enqueue_script( 'redirection', 'http://localhost:3312/redirection.js', array(), $version ); |
| 112 |
} else { |
| 113 |
wp_enqueue_script( 'redirection', plugin_dir_url( REDIRECTION_FILE ).'redirection.js', array(), $version ); |
| 114 |
} |
| 115 |
|
| 116 |
wp_enqueue_style( 'redirection', plugin_dir_url( REDIRECTION_FILE ).'admin.css', $version ); |
| 117 |
|
| 118 |
wp_localize_script( 'redirection', 'Redirectioni10n', array( |
| 119 |
'WP_API_root' => admin_url( 'admin-ajax.php' ), |
| 120 |
'WP_API_nonce' => wp_create_nonce( 'wp_rest' ), |
| 121 |
'pluginBaseUrl' => plugins_url( '', REDIRECTION_FILE ), |
| 122 |
'pluginRoot' => admin_url( 'tools.php?page=redirection.php' ), |
| 123 |
'per_page' => $this->get_per_page(), |
| 124 |
'locale' => $this->get_i18n_data(), |
| 125 |
'localeSlug' => get_locale(), |
| 126 |
'token' => $options['token'], |
| 127 |
'autoGenerate' => $options['auto_target'], |
| 128 |
'versions' => implode( ', ', array( 'Plugin '.$version, 'WordPress '.$wp_version, 'PHP '.phpversion() ) ), |
| 129 |
) ); |
| 130 |
} |
| 131 |
|
| 132 |
private function get_per_page() { |
| 133 |
$per_page = intval( get_user_meta( get_current_user_id(), 'redirection_log_per_page', true ), 10 ); |
| 134 |
|
| 135 |
return $per_page > 0 ? $per_page : RED_DEFAULT_PER_PAGE; |
| 136 |
} |
| 137 |
|
| 138 |
private function get_i18n_data() { |
| 139 |
$i18n_json = REDIRECTION_FILE . 'locale/json/redirection-' . get_locale() . '.json'; |
| 140 |
|
| 141 |
if ( is_file( $i18n_json ) && is_readable( $i18n_json ) ) { |
| 142 |
$locale_data = @file_get_contents( $i18n_json ); |
| 143 |
|
| 144 |
if ( $locale_data ) { |
| 145 |
return $locale_data; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
// Return empty if we have nothing to return so it doesn't fail when parsed in JS |
| 150 |
return '{}'; |
| 151 |
} |
| 152 |
|
| 153 |
function admin_menu() { |
| 154 |
add_management_page( 'Redirection', 'Redirection', apply_filters( 'redirection_role', 'administrator' ), basename( REDIRECTION_FILE ), array( &$this, 'admin_screen' ) ); |
| 155 |
} |
| 156 |
|
| 157 |
function admin_screen() { |
| 158 |
Redirection_Admin::update(); |
| 159 |
|
| 160 |
?> |
| 161 |
<div id="react-ui"> |
| 162 |
<h1><?php _e( 'Loading the bits, please wait...', 'redirection' ); ?></h1> |
| 163 |
<div class="react-loading"> |
| 164 |
<span class="react-loading-spinner" /> |
| 165 |
</div> |
| 166 |
<noscript>Please enable JavaScript</noscript> |
| 167 |
</div> |
| 168 |
|
| 169 |
<script> |
| 170 |
addLoadEvent( function() { |
| 171 |
redirection.show( 'react-ui' ); |
| 172 |
} ); |
| 173 |
</script> |
| 174 |
<?php |
| 175 |
} |
| 176 |
|
| 177 |
private function user_has_access() { |
| 178 |
return current_user_can( apply_filters( 'redirection_role', 'administrator' ) ); |
| 179 |
} |
| 180 |
|
| 181 |
function inject() { |
| 182 |
if ( isset( $_GET['page'] ) && isset( $_GET['sub'] ) && $_GET['page'] === 'redirection.php' ) { |
| 183 |
$this->tryExportLogs(); |
| 184 |
$this->tryExportRedirects(); |
| 185 |
$this->tryExportRSS(); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
function tryExportRSS() { |
| 190 |
if ( isset( $_GET['token'] ) && $_GET['sub'] === 'rss' ) { |
| 191 |
$options = red_get_options(); |
| 192 |
|
| 193 |
if ( $_GET['token'] === $options['token'] && !empty( $options['token'] ) ) { |
| 194 |
$items = Red_Item::get_all_for_module( intval( $_GET['module'] ) ); |
| 195 |
|
| 196 |
$exporter = Red_FileIO::create( 'rss' ); |
| 197 |
$exporter->force_download(); |
| 198 |
echo $exporter->get_data( $items, array() ); |
| 199 |
die(); |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
private function tryExportLogs() { |
| 205 |
if ( $this->user_has_access() && isset( $_POST['export-csv'] ) && check_admin_referer( 'wp_rest' ) ) { |
| 206 |
if ( isset( $_GET['sub'] ) && $_GET['sub'] === 'log' ) { |
| 207 |
RE_Log::export_to_csv(); |
| 208 |
} else { |
| 209 |
RE_404::export_to_csv(); |
| 210 |
} |
| 211 |
|
| 212 |
die(); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
private function tryExportRedirects() { |
| 217 |
if ( $this->user_has_access() && $_GET['sub'] === 'modules' && isset( $_GET['exporter'] ) && isset( $_GET['export'] ) ) { |
| 218 |
$export = Red_FileIO::export( $_GET['export'], $_GET['exporter'] ); |
| 219 |
|
| 220 |
if ( $export !== false ) { |
| 221 |
$export['exporter']->force_download(); |
| 222 |
echo $export['data']; |
| 223 |
die(); |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
register_activation_hook( REDIRECTION_FILE, array( 'Redirection_Admin', 'plugin_activated' ) ); |
| 230 |
|
| 231 |
add_action( 'init', array( 'Redirection_Admin', 'init' ) ); |
| 232 |
|