| 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 |
if ( defined( 'REDIRECTION_FLYING_SOLO' ) && REDIRECTION_FLYING_SOLO ) { |
| 33 |
add_filter( 'script_loader_src', array( $this, 'flying_solo' ), 10, 2 ); |
| 34 |
} |
| 35 |
|
| 36 |
register_deactivation_hook( REDIRECTION_FILE, array( 'Redirection_Admin', 'plugin_deactivated' ) ); |
| 37 |
register_uninstall_hook( REDIRECTION_FILE, array( 'Redirection_Admin', 'plugin_uninstall' ) ); |
| 38 |
|
| 39 |
$this->monitor = new Red_Monitor( red_get_options() ); |
| 40 |
$this->api = new Redirection_Api(); |
| 41 |
} |
| 42 |
|
| 43 |
public static function plugin_activated() { |
| 44 |
Redirection_Admin::update(); |
| 45 |
Red_Flusher::schedule(); |
| 46 |
|
| 47 |
update_option( 'redirection_options', red_get_options() ); |
| 48 |
} |
| 49 |
|
| 50 |
public static function plugin_deactivated() { |
| 51 |
Red_Flusher::clear(); |
| 52 |
} |
| 53 |
|
| 54 |
public static function plugin_uninstall() { |
| 55 |
include_once dirname( REDIRECTION_FILE ).'/models/database.php'; |
| 56 |
|
| 57 |
$db = new RE_Database(); |
| 58 |
$db->remove( REDIRECTION_FILE ); |
| 59 |
|
| 60 |
delete_option( 'redirection_options' ); |
| 61 |
} |
| 62 |
|
| 63 |
// So it finally came to this... some plugins include their JS in all pages, whether they are needed or not. If there is an error |
| 64 |
// then this can prevent Redirection running, it's a little sensitive about that. We use the nuclear option here to disable |
| 65 |
// all other JS while viewing Redirection |
| 66 |
public function flying_solo( $src, $handle ) { |
| 67 |
if ( isset( $_SERVER['REQUEST_URI'] ) && strpos( $_SERVER['REQUEST_URI'], 'page=redirection.php' ) !== false ) { |
| 68 |
if ( substr( $src, 0, 4 ) === 'http' && $handle !== 'redirection' && strpos( $src, 'plugins' ) !== false ) { |
| 69 |
if ( $this->ignore_this_plugin( $src ) ) { |
| 70 |
return false; |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
return $src; |
| 76 |
} |
| 77 |
|
| 78 |
private function ignore_this_plugin( $src ) { |
| 79 |
if ( strpos( $src, 'mootools' ) !== false ) { |
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
if ( strpos( $src, 'wp-seo-' ) !== false ) { |
| 84 |
return true; |
| 85 |
} |
| 86 |
|
| 87 |
return false; |
| 88 |
} |
| 89 |
|
| 90 |
public function flush_schedule() { |
| 91 |
Red_Flusher::schedule(); |
| 92 |
} |
| 93 |
|
| 94 |
private static function update() { |
| 95 |
$version = get_option( 'redirection_version' ); |
| 96 |
|
| 97 |
Red_Flusher::schedule(); |
| 98 |
|
| 99 |
if ( $version !== REDIRECTION_DB_VERSION ) { |
| 100 |
include_once dirname( REDIRECTION_FILE ).'/models/database.php'; |
| 101 |
|
| 102 |
$database = new RE_Database(); |
| 103 |
|
| 104 |
if ( $version === false ) { |
| 105 |
$database->install(); |
| 106 |
} |
| 107 |
|
| 108 |
return $database->upgrade( $version, REDIRECTION_DB_VERSION ); |
| 109 |
} |
| 110 |
|
| 111 |
return true; |
| 112 |
} |
| 113 |
|
| 114 |
function set_per_page( $status, $option, $value ) { |
| 115 |
if ( $option === 'redirection_log_per_page' ) { |
| 116 |
return max( 1, min( intval( $value, 10 ), RED_MAX_PER_PAGE ) ); |
| 117 |
} |
| 118 |
|
| 119 |
return $status; |
| 120 |
} |
| 121 |
|
| 122 |
function plugin_settings( $links ) { |
| 123 |
$settings_link = '<a href="tools.php?page='.basename( REDIRECTION_FILE ).'&sub=options">'.__( 'Settings', 'redirection' ).'</a>'; |
| 124 |
array_unshift( $links, $settings_link ); |
| 125 |
return $links; |
| 126 |
} |
| 127 |
|
| 128 |
function redirection_head() { |
| 129 |
global $wp_version; |
| 130 |
|
| 131 |
$build = REDIRECTION_VERSION.'-'.REDIRECTION_BUILD; |
| 132 |
$options = red_get_options(); |
| 133 |
$versions = array( |
| 134 |
'Plugin: '.REDIRECTION_VERSION, |
| 135 |
'WordPress: '.$wp_version, |
| 136 |
'PHP: '.phpversion(), |
| 137 |
'Browser: '.Redirection_Request::get_user_agent(), |
| 138 |
); |
| 139 |
|
| 140 |
$this->inject(); |
| 141 |
|
| 142 |
if ( ! isset( $_GET['sub'] ) || ( isset( $_GET['sub'] ) && ( in_array( $_GET['sub'], array( 'log', '404s', 'groups' ) ) ) ) ) { |
| 143 |
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' ) ); |
| 144 |
} |
| 145 |
|
| 146 |
if ( defined( 'REDIRECTION_DEV_MODE' ) && REDIRECTION_DEV_MODE ) { |
| 147 |
wp_enqueue_script( 'redirection', 'http://localhost:3312/redirection.js', array(), $build, true ); |
| 148 |
} else { |
| 149 |
wp_enqueue_script( 'redirection', plugin_dir_url( REDIRECTION_FILE ).'redirection.js', array(), $build, true ); |
| 150 |
} |
| 151 |
|
| 152 |
wp_enqueue_style( 'redirection', plugin_dir_url( REDIRECTION_FILE ).'redirection.css', array(), $build ); |
| 153 |
|
| 154 |
wp_localize_script( 'redirection', 'Redirectioni10n', array( |
| 155 |
'WP_API_root' => admin_url( 'admin-ajax.php' ), |
| 156 |
'WP_API_nonce' => wp_create_nonce( 'wp_rest' ), |
| 157 |
'pluginBaseUrl' => plugins_url( '', REDIRECTION_FILE ), |
| 158 |
'pluginRoot' => admin_url( 'tools.php?page=redirection.php' ), |
| 159 |
'per_page' => $this->get_per_page(), |
| 160 |
'locale' => $this->get_i18n_data(), |
| 161 |
'localeSlug' => get_locale(), |
| 162 |
'token' => $options['token'], |
| 163 |
'autoGenerate' => $options['auto_target'], |
| 164 |
'versions' => implode( "\n", $versions ), |
| 165 |
'version' => REDIRECTION_VERSION, |
| 166 |
) ); |
| 167 |
} |
| 168 |
|
| 169 |
private function get_per_page() { |
| 170 |
$per_page = intval( get_user_meta( get_current_user_id(), 'redirection_log_per_page', true ), 10 ); |
| 171 |
|
| 172 |
return $per_page > 0 ? $per_page : RED_DEFAULT_PER_PAGE; |
| 173 |
} |
| 174 |
|
| 175 |
private function get_i18n_data() { |
| 176 |
$i18n_json = REDIRECTION_FILE . 'locale/json/redirection-' . get_locale() . '.json'; |
| 177 |
|
| 178 |
if ( is_file( $i18n_json ) && is_readable( $i18n_json ) ) { |
| 179 |
$locale_data = @file_get_contents( $i18n_json ); |
| 180 |
|
| 181 |
if ( $locale_data ) { |
| 182 |
return $locale_data; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
// Return empty if we have nothing to return so it doesn't fail when parsed in JS |
| 187 |
return '{}'; |
| 188 |
} |
| 189 |
|
| 190 |
function admin_menu() { |
| 191 |
add_management_page( 'Redirection', 'Redirection', apply_filters( 'redirection_role', 'administrator' ), basename( REDIRECTION_FILE ), array( &$this, 'admin_screen' ) ); |
| 192 |
} |
| 193 |
|
| 194 |
function admin_screen() { |
| 195 |
Redirection_Admin::update(); |
| 196 |
|
| 197 |
$version = get_plugin_data( REDIRECTION_FILE ); |
| 198 |
$version = $version['Version']; |
| 199 |
?> |
| 200 |
<div id="react-ui"> |
| 201 |
<div class="react-loading"> |
| 202 |
<h1><?php _e( 'Loading, please wait...', 'redirection' ); ?></h1> |
| 203 |
|
| 204 |
<span class="react-loading-spinner"></span> |
| 205 |
</div> |
| 206 |
<noscript>Please enable JavaScript</noscript> |
| 207 |
|
| 208 |
<div class="react-error" style="display: none"> |
| 209 |
<h1><?php _e( 'An error occurred loading Redirection', 'redirection' ); ?></h1> |
| 210 |
<p><?php _e( "This may be caused by another plugin - look at your browser's error console for more details.", 'redirection' ); ?></p> |
| 211 |
<p><?php _e( "If you think Redirection is at fault then create an issue.", 'redirection' ); ?></p> |
| 212 |
<p class="versions"></p> |
| 213 |
<p> |
| 214 |
<a class="button-primary" target="_blank" href="https://github.com/johngodley/redirection/issues/new?title=Problem%20starting%20Redirection%20<?php echo esc_attr( $version ) ?>"> |
| 215 |
<?php _e( 'Create Issue', 'redirection' ); ?> |
| 216 |
</a> |
| 217 |
</p> |
| 218 |
</div> |
| 219 |
</div> |
| 220 |
|
| 221 |
<script> |
| 222 |
var prevError = window.onerror; |
| 223 |
var errors = []; |
| 224 |
var timeout = 0; |
| 225 |
var timer = setInterval( function() { |
| 226 |
if ( isRedirectionLoaded() ) { |
| 227 |
resetAll(); |
| 228 |
} else if ( errors.length > 0 || timeout++ === 5 ) { |
| 229 |
showError(); |
| 230 |
resetAll(); |
| 231 |
} |
| 232 |
}, 5000 ); |
| 233 |
|
| 234 |
function isRedirectionLoaded() { |
| 235 |
return typeof redirection !== 'undefined'; |
| 236 |
} |
| 237 |
|
| 238 |
function showError() { |
| 239 |
document.querySelector( '.react-loading' ).style.display = 'none'; |
| 240 |
document.querySelector( '.react-error' ).style.display = 'block'; |
| 241 |
document.querySelector( '.versions' ).innerHTML = Redirectioni10n.versions.replace( /\n/g, '<br />' ); |
| 242 |
document.querySelector( '.react-error .button-primary' ).href += '&body=' + encodeURIComponent( "```\n" + errors.join( ',' ) + "\n```\n\n" ) + encodeURIComponent( Redirectioni10n.versions ); |
| 243 |
} |
| 244 |
|
| 245 |
function resetAll() { |
| 246 |
clearInterval( timer ); |
| 247 |
window.onerror = prevError; |
| 248 |
} |
| 249 |
|
| 250 |
window.onerror = function( error, url, line ) { |
| 251 |
console.error( error ); |
| 252 |
errors.push( error + ' ' + url + ' ' + line ); |
| 253 |
}; |
| 254 |
</script> |
| 255 |
<?php |
| 256 |
} |
| 257 |
|
| 258 |
private function user_has_access() { |
| 259 |
return current_user_can( apply_filters( 'redirection_role', 'administrator' ) ); |
| 260 |
} |
| 261 |
|
| 262 |
function inject() { |
| 263 |
if ( isset( $_GET['page'] ) && isset( $_GET['sub'] ) && $_GET['page'] === 'redirection.php' ) { |
| 264 |
$this->tryExportLogs(); |
| 265 |
$this->tryExportRedirects(); |
| 266 |
$this->tryExportRSS(); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
function tryExportRSS() { |
| 271 |
if ( isset( $_GET['token'] ) && $_GET['sub'] === 'rss' ) { |
| 272 |
$options = red_get_options(); |
| 273 |
|
| 274 |
if ( $_GET['token'] === $options['token'] && !empty( $options['token'] ) ) { |
| 275 |
$items = Red_Item::get_all_for_module( intval( $_GET['module'] ) ); |
| 276 |
|
| 277 |
$exporter = Red_FileIO::create( 'rss' ); |
| 278 |
$exporter->force_download(); |
| 279 |
echo $exporter->get_data( $items, array() ); |
| 280 |
die(); |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
private function tryExportLogs() { |
| 286 |
if ( $this->user_has_access() && isset( $_POST['export-csv'] ) && check_admin_referer( 'wp_rest' ) ) { |
| 287 |
if ( isset( $_GET['sub'] ) && $_GET['sub'] === 'log' ) { |
| 288 |
RE_Log::export_to_csv(); |
| 289 |
} else { |
| 290 |
RE_404::export_to_csv(); |
| 291 |
} |
| 292 |
|
| 293 |
die(); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
private function tryExportRedirects() { |
| 298 |
if ( $this->user_has_access() && $_GET['sub'] === 'modules' && isset( $_GET['exporter'] ) && isset( $_GET['export'] ) ) { |
| 299 |
$export = Red_FileIO::export( $_GET['export'], $_GET['exporter'] ); |
| 300 |
|
| 301 |
if ( $export !== false ) { |
| 302 |
$export['exporter']->force_download(); |
| 303 |
echo $export['data']; |
| 304 |
die(); |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
register_activation_hook( REDIRECTION_FILE, array( 'Redirection_Admin', 'plugin_activated' ) ); |
| 311 |
|
| 312 |
add_action( 'init', array( 'Redirection_Admin', 'init' ) ); |
| 313 |
|