| @@ -1,10 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -include_once dirname( __FILE__ ) . '/models/group.php'; | |
| 4 | -include_once dirname( __FILE__ ) . '/models/monitor.php'; | |
| 5 | -include_once dirname( __FILE__ ) . '/models/file-io.php'; | |
| 6 | -include_once dirname( __FILE__ ) . '/database/database.php'; | |
| 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 | 7 | |
| 8 | 8 | define( 'RED_DEFAULT_PER_PAGE', 25 ); |
| 9 | 9 | define( 'RED_MAX_PER_PAGE', 200 ); |
| 10 | 10 | |
| @@ -10,13 +10,14 @@ | ||
| 10 | 10 | |
| 11 | 11 | class Redirection_Admin { |
| 12 | 12 | private static $instance = null; |
| 13 | 13 | private $monitor; |
| 14 | - private $fixit_failed = false; | |
| 15 | 14 | |
| 16 | 15 | static function init() { |
| 17 | 16 | if ( is_null( self::$instance ) ) { |
| 18 | 17 | self::$instance = new Redirection_Admin(); |
| 18 | + | |
| 19 | + load_plugin_textdomain( 'redirection', false, dirname( plugin_basename( REDIRECTION_FILE ) ).'/locale/' ); | |
| 19 | 20 | } |
| 20 | 21 | |
| 21 | 22 | return self::$instance; |
| 22 | 23 | } |
| @@ -21,114 +22,65 @@ | ||
| 21 | 22 | return self::$instance; |
| 22 | 23 | } |
| 23 | 24 | |
| 24 | 25 | function __construct() { |
| 25 | - add_action( 'admin_menu', [ $this, 'admin_menu' ] ); | |
| 26 | - add_action( 'admin_notices', [ $this, 'update_nag' ] ); | |
| 27 | - // add_action( 'network_admin_notices', [ $this, 'update_nag' ] ); | |
| 28 | - add_action( 'plugin_action_links_' . basename( dirname( REDIRECTION_FILE ) ) . '/' . basename( REDIRECTION_FILE ), [ $this, 'plugin_settings' ], 10, 4 ); | |
| 29 | - add_filter( 'plugin_row_meta', [ $this, 'plugin_row_meta' ], 10, 4 ); | |
| 30 | - add_filter( 'redirection_save_options', [ $this, 'flush_schedule' ] ); | |
| 31 | - add_filter( 'set-screen-option', [ $this, 'set_per_page' ], 10, 3 ); | |
| 32 | - add_action( 'redirection_redirect_updated', [ $this, 'set_default_group' ], 10, 2 ); | |
| 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 ); | |
| 33 | 31 | |
| 34 | - if ( defined( 'REDIRECTION_FLYING_SOLO' ) && REDIRECTION_FLYING_SOLO ) { | |
| 35 | - add_filter( 'script_loader_src', [ $this, 'flying_solo' ], 10, 2 ); | |
| 36 | - } | |
| 32 | + register_deactivation_hook( REDIRECTION_FILE, array( 'Redirection_Admin', 'plugin_deactivated' ) ); | |
| 33 | + register_uninstall_hook( REDIRECTION_FILE, array( 'Redirection_Admin', 'plugin_uninstall' ) ); | |
| 37 | 34 | |
| 38 | - register_deactivation_hook( REDIRECTION_FILE, [ 'Redirection_Admin', 'plugin_deactivated' ] ); | |
| 39 | - register_uninstall_hook( REDIRECTION_FILE, [ 'Redirection_Admin', 'plugin_uninstall' ] ); | |
| 40 | - | |
| 41 | 35 | $this->monitor = new Red_Monitor( red_get_options() ); |
| 42 | - $this->run_hacks(); | |
| 36 | + $this->api = new Redirection_Api(); | |
| 43 | 37 | } |
| 44 | 38 | |
| 45 | - // These are only called on the single standard site, or in the network admin of the multisite - they run across all available sites | |
| 46 | 39 | public static function plugin_activated() { |
| 47 | - Red_Database::apply_to_sites( function() { | |
| 48 | - Red_Flusher::clear(); | |
| 49 | - red_set_options(); | |
| 50 | - } ); | |
| 40 | + Redirection_Admin::update(); | |
| 41 | + Red_Flusher::schedule(); | |
| 42 | + | |
| 43 | + update_option( 'redirection_options', red_get_options() ); | |
| 51 | 44 | } |
| 52 | 45 | |
| 53 | - // These are only called on the single standard site, or in the network admin of the multisite - they run across all available sites | |
| 54 | 46 | public static function plugin_deactivated() { |
| 55 | - Red_Database::apply_to_sites( function() { | |
| 56 | - Red_Flusher::clear(); | |
| 57 | - } ); | |
| 47 | + Red_Flusher::clear(); | |
| 58 | 48 | } |
| 59 | 49 | |
| 60 | - // These are only called on the single standard site, or in the network admin of the multisite - they run across all available sites | |
| 61 | 50 | public static function plugin_uninstall() { |
| 62 | - $database = Red_Database::get_latest_database(); | |
| 51 | + include_once dirname( REDIRECTION_FILE ).'/models/database.php'; | |
| 63 | 52 | |
| 64 | - Red_Database::apply_to_sites( function() use ( $database ) { | |
| 65 | - $database->remove(); | |
| 66 | - } ); | |
| 53 | + $db = new RE_Database(); | |
| 54 | + $db->remove( REDIRECTION_FILE ); | |
| 55 | + | |
| 56 | + delete_option( 'redirection_options' ); | |
| 67 | 57 | } |
| 68 | 58 | |
| 69 | - public function update_nag() { | |
| 70 | - if ( ! $this->user_has_access() ) { | |
| 71 | - return; | |
| 72 | - } | |
| 59 | + public function flush_schedule() { | |
| 60 | + Red_Flusher::schedule(); | |
| 61 | + } | |
| 73 | 62 | |
| 74 | - $status = new Red_Database_Status(); | |
| 63 | + private static function update() { | |
| 64 | + $version = get_option( 'redirection_version' ); | |
| 75 | 65 | |
| 76 | - $message = false; | |
| 77 | - if ( $status->needs_installing() ) { | |
| 78 | - /* translators: 1: URL to plugin page */ | |
| 79 | - $message = sprintf( __( 'Please complete your <a href="%s">Redirection setup</a> to activate the plugin.' ), 'tools.php?page=' . basename( REDIRECTION_FILE ) ); | |
| 80 | - } elseif ( $status->needs_updating() ) { | |
| 81 | - /* translators: 1: URL to plugin page, 2: current version, 3: target version */ | |
| 82 | - $message = sprintf( __( 'Redirection\'s database needs to be updated - <a href="%1$1s">click to update</a>.' ), 'tools.php?page=' . basename( REDIRECTION_FILE ) ); | |
| 83 | - } | |
| 66 | + Red_Flusher::schedule(); | |
| 84 | 67 | |
| 85 | - if ( ! $message || strpos( Redirection_Request::get_request_url(), 'page=redirection.php' ) !== false ) { | |
| 86 | - return; | |
| 87 | - } | |
| 68 | + if ( $version !== REDIRECTION_VERSION ) { | |
| 69 | + include_once dirname( REDIRECTION_FILE ).'/models/database.php'; | |
| 88 | 70 | |
| 89 | - // Contains HTML | |
| 90 | - echo '<div class="update-nag">' . $message . '</div>'; | |
| 91 | - } | |
| 71 | + $database = new RE_Database(); | |
| 92 | 72 | |
| 93 | - // 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 | |
| 94 | - // then this can prevent Redirection running and it's a little sensitive about that. We use the nuclear option here to disable | |
| 95 | - // all other JS while viewing Redirection | |
| 96 | - public function flying_solo( $src, $handle ) { | |
| 97 | - if ( isset( $_SERVER['REQUEST_URI'] ) && strpos( $_SERVER['REQUEST_URI'], 'page=redirection.php' ) !== false ) { | |
| 98 | - if ( substr( $src, 0, 4 ) === 'http' && $handle !== 'redirection' && strpos( $src, 'plugins' ) !== false ) { | |
| 99 | - if ( $this->ignore_this_plugin( $src ) ) { | |
| 100 | - return false; | |
| 101 | - } | |
| 73 | + if ( $version === false ) { | |
| 74 | + $database->install(); | |
| 102 | 75 | } |
| 103 | - } | |
| 104 | 76 | |
| 105 | - return $src; | |
| 106 | - } | |
| 107 | - | |
| 108 | - private function ignore_this_plugin( $src ) { | |
| 109 | - $ignore = array( | |
| 110 | - 'mootools', | |
| 111 | - 'wp-seo-', | |
| 112 | - 'authenticate', | |
| 113 | - 'wordpress-seo', | |
| 114 | - 'yikes', | |
| 115 | - ); | |
| 116 | - | |
| 117 | - foreach ( $ignore as $text ) { | |
| 118 | - if ( strpos( $src, $text ) !== false ) { | |
| 119 | - return true; | |
| 120 | - } | |
| 77 | + return $database->upgrade( $version, REDIRECTION_VERSION ); | |
| 121 | 78 | } |
| 122 | 79 | |
| 123 | - return false; | |
| 80 | + return true; | |
| 124 | 81 | } |
| 125 | 82 | |
| 126 | - public function flush_schedule( $options ) { | |
| 127 | - Red_Flusher::schedule(); | |
| 128 | - return $options; | |
| 129 | - } | |
| 130 | - | |
| 131 | 83 | function set_per_page( $status, $option, $value ) { |
| 132 | 84 | if ( $option === 'redirection_log_per_page' ) { |
| 133 | 85 | return max( 1, min( intval( $value, 10 ), RED_MAX_PER_PAGE ) ); |
| 134 | 86 | } |
| @@ -136,367 +88,110 @@ | ||
| 136 | 88 | return $status; |
| 137 | 89 | } |
| 138 | 90 | |
| 139 | 91 | function plugin_settings( $links ) { |
| 140 | - $status = new Red_Database_Status(); | |
| 141 | - if ( $status->needs_updating() ) { | |
| 142 | - array_unshift( $links, '<a style="color: red" href="tools.php?page=' . basename( REDIRECTION_FILE ) . '&sub=support">' . __( 'Upgrade Database', 'redirection' ) . '</a>' ); | |
| 143 | - } | |
| 144 | - | |
| 145 | - array_unshift( $links, '<a href="tools.php?page=' . basename( REDIRECTION_FILE ) . '&sub=options">' . __( 'Settings', 'redirection' ) . '</a>' ); | |
| 92 | + $settings_link = '<a href="tools.php?page='.basename( REDIRECTION_FILE ).'&sub=options">'.__( 'Settings', 'redirection' ).'</a>'; | |
| 93 | + array_unshift( $links, $settings_link ); | |
| 146 | 94 | return $links; |
| 147 | 95 | } |
| 148 | 96 | |
| 149 | - function plugin_row_meta( $plugin_meta, $plugin_file, $plugin_data, $status ) { | |
| 150 | - if ( $plugin_file === basename( dirname( REDIRECTION_FILE ) ) . '/' . basename( REDIRECTION_FILE ) ) { | |
| 151 | - $plugin_data['Description'] .= '<p>' . __( 'Please upgrade your database', 'redirection' ) . '</p>'; | |
| 152 | - } | |
| 153 | - | |
| 154 | - return $plugin_meta; | |
| 155 | - } | |
| 156 | - | |
| 157 | 97 | function redirection_head() { |
| 158 | 98 | global $wp_version; |
| 159 | 99 | |
| 160 | - if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'wp_rest' ) ) { | |
| 161 | - if ( $_REQUEST['action'] === 'fixit' ) { | |
| 162 | - $this->run_fixit(); | |
| 163 | - } elseif ( $_REQUEST['action'] === 'rest_api' ) { | |
| 164 | - $this->set_rest_api( intval( $_REQUEST['rest_api'], 10 ) ); | |
| 165 | - } | |
| 166 | - } | |
| 167 | - | |
| 168 | - $build = REDIRECTION_VERSION . '-' . REDIRECTION_BUILD; | |
| 169 | - $preload = $this->get_preload_data(); | |
| 170 | 100 | $options = red_get_options(); |
| 171 | - $versions = array( | |
| 172 | - 'Plugin: ' . REDIRECTION_VERSION, | |
| 173 | - 'WordPress: ' . $wp_version . ' (' . ( is_multisite() ? 'multi' : 'single' ) . ')', | |
| 174 | - 'PHP: ' . phpversion(), | |
| 175 | - 'Browser: ' . Redirection_Request::get_user_agent(), | |
| 176 | - 'JavaScript: ' . plugin_dir_url( REDIRECTION_FILE ) . 'redirection.js', | |
| 177 | - 'REST API: ' . red_get_rest_api(), | |
| 178 | - ); | |
| 101 | + $version = get_plugin_data( REDIRECTION_FILE ); | |
| 102 | + $version = $version['Version']; | |
| 179 | 103 | |
| 180 | 104 | $this->inject(); |
| 181 | 105 | |
| 182 | - if ( in_array( $this->get_menu_page(), array( 'redirects', 'log', '404s', 'groups' ) ) ) { | |
| 183 | - add_screen_option( 'per_page', array( | |
| 184 | - /* translators: maximum number of log entries */ | |
| 185 | - 'label' => sprintf( __( 'Log entries (%d max)', 'redirection' ), RED_MAX_PER_PAGE ), | |
| 186 | - 'default' => RED_DEFAULT_PER_PAGE, | |
| 187 | - 'option' => 'redirection_log_per_page', | |
| 188 | - ) ); | |
| 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' ) ); | |
| 189 | 108 | } |
| 190 | 109 | |
| 191 | 110 | if ( defined( 'REDIRECTION_DEV_MODE' ) && REDIRECTION_DEV_MODE ) { |
| 192 | - wp_enqueue_script( 'redirection', 'http://localhost:3312/redirection.js', array(), $build, true ); | |
| 111 | + wp_enqueue_script( 'redirection', 'http://localhost:3312/redirection.js', array(), $version ); | |
| 193 | 112 | } else { |
| 194 | - wp_enqueue_script( 'redirection', plugin_dir_url( REDIRECTION_FILE ) . 'redirection.js', array(), $build, true ); | |
| 113 | + wp_enqueue_script( 'redirection', plugin_dir_url( REDIRECTION_FILE ).'redirection.js', array(), $version ); | |
| 195 | 114 | } |
| 196 | 115 | |
| 197 | - wp_enqueue_style( 'redirection', plugin_dir_url( REDIRECTION_FILE ) . 'redirection.css', array(), $build ); | |
| 116 | + wp_enqueue_style( 'redirection', plugin_dir_url( REDIRECTION_FILE ).'admin.css', $version ); | |
| 198 | 117 | |
| 199 | - $status = new Red_Database_Status(); | |
| 200 | - $status->check_tables_exist(); | |
| 201 | - | |
| 202 | 118 | wp_localize_script( 'redirection', 'Redirectioni10n', array( |
| 203 | - 'api' => [ | |
| 204 | - 'WP_API_root' => esc_url_raw( red_get_rest_api() ), | |
| 205 | - 'WP_API_nonce' => wp_create_nonce( 'wp_rest' ), | |
| 206 | - 'current' => $options['rest_api'], | |
| 207 | - 'routes' => [ | |
| 208 | - REDIRECTION_API_JSON => red_get_rest_api( REDIRECTION_API_JSON ), | |
| 209 | - REDIRECTION_API_JSON_INDEX => red_get_rest_api( REDIRECTION_API_JSON_INDEX ), | |
| 210 | - REDIRECTION_API_JSON_RELATIVE => red_get_rest_api( REDIRECTION_API_JSON_RELATIVE ), | |
| 211 | - ], | |
| 212 | - ], | |
| 119 | + 'WP_API_root' => admin_url( 'admin-ajax.php' ), | |
| 120 | + 'WP_API_nonce' => wp_create_nonce( 'wp_rest' ), | |
| 213 | 121 | 'pluginBaseUrl' => plugins_url( '', REDIRECTION_FILE ), |
| 214 | 122 | 'pluginRoot' => admin_url( 'tools.php?page=redirection.php' ), |
| 215 | 123 | 'per_page' => $this->get_per_page(), |
| 216 | 124 | 'locale' => $this->get_i18n_data(), |
| 217 | 125 | 'localeSlug' => get_locale(), |
| 218 | - 'settings' => $options, | |
| 219 | - 'preload' => $preload, | |
| 220 | - 'versions' => implode( "\n", $versions ), | |
| 221 | - 'version' => REDIRECTION_VERSION, | |
| 222 | - 'database' => $status->get_json(), | |
| 126 | + 'token' => $options['token'], | |
| 127 | + 'autoGenerate' => $options['auto_target'], | |
| 128 | + 'versions' => implode( ', ', array( 'Plugin '.$version, 'WordPress '.$wp_version, 'PHP '.phpversion() ) ), | |
| 223 | 129 | ) ); |
| 224 | - | |
| 225 | - $this->add_help_tab(); | |
| 226 | 130 | } |
| 227 | 131 | |
| 228 | - // Some plugins misbehave, so this attempts to 'fix' them so Redirection can get on with it's work | |
| 229 | - private function run_hacks() { | |
| 230 | - add_filter( 'ip-geo-block-admin', array( $this, 'ip_geo_block' ) ); | |
| 231 | - } | |
| 232 | - | |
| 233 | - /** | |
| 234 | - * This works around the IP Geo Block plugin being very aggressive and breaking Redirection | |
| 235 | - */ | |
| 236 | - public function ip_geo_block( $validate ) { | |
| 237 | - $url = Redirection_Request::get_request_url(); | |
| 238 | - $override = array( | |
| 239 | - 'tools.php?page=redirection.php', | |
| 240 | - 'action=red_proxy&rest_path=redirection', | |
| 241 | - ); | |
| 242 | - | |
| 243 | - foreach ( $override as $path ) { | |
| 244 | - if ( strpos( $url, $path ) !== false ) { | |
| 245 | - return array( | |
| 246 | - 'result' => 'passed', | |
| 247 | - 'auth' => false, | |
| 248 | - 'asn' => false, | |
| 249 | - 'code' => false, | |
| 250 | - 'ip' => false, | |
| 251 | - ); | |
| 252 | - } | |
| 253 | - } | |
| 254 | - | |
| 255 | - return $validate; | |
| 256 | - } | |
| 257 | - | |
| 258 | - private function run_fixit() { | |
| 259 | - if ( $this->user_has_access() ) { | |
| 260 | - include_once dirname( REDIRECTION_FILE ) . '/models/fixer.php'; | |
| 261 | - | |
| 262 | - $fixer = new Red_Fixer(); | |
| 263 | - $result = $fixer->fix( $fixer->get_status() ); | |
| 264 | - | |
| 265 | - if ( is_wp_error( $result ) ) { | |
| 266 | - $this->fixit_failed = $result; | |
| 267 | - } | |
| 268 | - } | |
| 269 | - } | |
| 270 | - | |
| 271 | - private function set_rest_api( $api ) { | |
| 272 | - if ( $api >= 0 && $api <= REDIRECTION_API_POST ) { | |
| 273 | - red_set_options( array( 'rest_api' => intval( $api, 10 ) ) ); | |
| 274 | - } | |
| 275 | - } | |
| 276 | - | |
| 277 | - private function get_preload_data() { | |
| 278 | - if ( $this->get_menu_page() === 'support' ) { | |
| 279 | - include_once dirname( REDIRECTION_FILE ) . '/models/fixer.php'; | |
| 280 | - | |
| 281 | - $fixer = new Red_Fixer(); | |
| 282 | - | |
| 283 | - return array( | |
| 284 | - 'pluginStatus' => $fixer->get_json(), | |
| 285 | - ); | |
| 286 | - } | |
| 287 | - | |
| 288 | - return array(); | |
| 289 | - } | |
| 290 | - | |
| 291 | - private function add_help_tab() { | |
| 292 | - /* translators: URL */ | |
| 293 | - $content = sprintf( __( 'You can find full documentation about using Redirection on the <a href="%s" target="_blank">redirection.me</a> support site.', 'redirection' ), 'https://redirection.me/support/?utm_source=redirection&utm_medium=plugin&utm_campaign=context-help' ); | |
| 294 | - $title = __( 'Redirection Support', 'redirection' ); | |
| 295 | - | |
| 296 | - $current_screen = get_current_screen(); | |
| 297 | - $current_screen->add_help_tab( array( | |
| 298 | - 'id' => 'redirection', | |
| 299 | - 'title' => 'Redirection', | |
| 300 | - 'content' => "<h2>$title</h2><p>$content</p>", | |
| 301 | - ) ); | |
| 302 | - } | |
| 303 | - | |
| 304 | 132 | private function get_per_page() { |
| 305 | 133 | $per_page = intval( get_user_meta( get_current_user_id(), 'redirection_log_per_page', true ), 10 ); |
| 306 | 134 | |
| 307 | - return $per_page > 0 ? max( 5, min( $per_page, RED_MAX_PER_PAGE ) ) : RED_DEFAULT_PER_PAGE; | |
| 135 | + return $per_page > 0 ? $per_page : RED_DEFAULT_PER_PAGE; | |
| 308 | 136 | } |
| 309 | 137 | |
| 310 | 138 | private function get_i18n_data() { |
| 311 | - $locale = get_locale(); | |
| 139 | + $i18n_json = REDIRECTION_FILE . 'locale/json/redirection-' . get_locale() . '.json'; | |
| 312 | 140 | |
| 313 | - // WP 4.7 | |
| 314 | - if ( function_exists( 'get_user_locale' ) ) { | |
| 315 | - $locale = get_user_locale(); | |
| 316 | - } | |
| 317 | - | |
| 318 | - $i18n_json = dirname( REDIRECTION_FILE ) . '/locale/json/redirection-' . $locale . '.json'; | |
| 319 | - | |
| 320 | 141 | if ( is_file( $i18n_json ) && is_readable( $i18n_json ) ) { |
| 321 | 142 | $locale_data = @file_get_contents( $i18n_json ); |
| 322 | 143 | |
| 323 | 144 | if ( $locale_data ) { |
| 324 | - return json_decode( $locale_data ); | |
| 145 | + return $locale_data; | |
| 325 | 146 | } |
| 326 | 147 | } |
| 327 | 148 | |
| 328 | 149 | // Return empty if we have nothing to return so it doesn't fail when parsed in JS |
| 329 | - return array(); | |
| 150 | + return '{}'; | |
| 330 | 151 | } |
| 331 | 152 | |
| 332 | - public function admin_menu() { | |
| 333 | - $hook = add_management_page( 'Redirection', 'Redirection', $this->get_access_role(), basename( REDIRECTION_FILE ), [ $this, 'admin_screen' ] ); | |
| 334 | - add_action( 'load-' . $hook, [ $this, 'redirection_head' ] ); | |
| 153 | + function admin_menu() { | |
| 154 | + add_management_page( 'Redirection', 'Redirection', apply_filters( 'redirection_role', 'administrator' ), basename( REDIRECTION_FILE ), array( &$this, 'admin_screen' ) ); | |
| 335 | 155 | } |
| 336 | 156 | |
| 337 | - public function get_access_role() { | |
| 338 | - return apply_filters( 'redirection_role', 'manage_options' ); | |
| 339 | - } | |
| 157 | + function admin_screen() { | |
| 158 | + Redirection_Admin::update(); | |
| 340 | 159 | |
| 341 | - private function check_minimum_wp() { | |
| 342 | - $wp_version = get_bloginfo( 'version' ); | |
| 343 | - | |
| 344 | - if ( version_compare( $wp_version, REDIRECTION_MIN_WP, '<' ) ) { | |
| 345 | - return false; | |
| 346 | - } | |
| 347 | - | |
| 348 | - return true; | |
| 349 | - } | |
| 350 | - | |
| 351 | - public function set_default_group( $id, $redirect ) { | |
| 352 | - red_set_options( array( 'last_group_id' => $redirect->get_group_id() ) ); | |
| 353 | - } | |
| 354 | - | |
| 355 | - public function admin_screen() { | |
| 356 | - if ( ! $this->user_has_access() ) { | |
| 357 | - die( 'You do not have sufficient permissions to access this page.' ); | |
| 358 | - } | |
| 359 | - | |
| 360 | - if ( $this->check_minimum_wp() === false ) { | |
| 361 | - return $this->show_minimum_wordpress(); | |
| 362 | - } | |
| 363 | - | |
| 364 | - if ( $this->fixit_failed ) { | |
| 365 | - $this->show_fixit_failed(); | |
| 366 | - } | |
| 367 | - | |
| 368 | - Red_Flusher::schedule(); | |
| 369 | - | |
| 370 | - $this->show_main(); | |
| 371 | - } | |
| 372 | - | |
| 373 | - private function show_fixit_failed() { | |
| 374 | - ?> | |
| 375 | - <div class="notice notice-error"> | |
| 376 | - <h1><?php echo esc_html( $this->fixit_failed->get_error_message() ); ?></h1> | |
| 377 | - <p><?php echo esc_html( $this->fixit_failed->get_error_data() ); ?></p> | |
| 378 | - </div> | |
| 379 | - <?php | |
| 380 | - } | |
| 381 | - | |
| 382 | - private function show_minimum_wordpress() { | |
| 383 | - /* translators: 1: Expected WordPress version, 2: Actual WordPress version */ | |
| 384 | - $wp_requirement = sprintf( __( 'Redirection requires WordPress v%1$1s, you are using v%2$2s - please update your WordPress', 'redirection' ), REDIRECTION_MIN_WP, $wp_version ); | |
| 385 | - ?> | |
| 386 | - <div class="react-error"> | |
| 387 | - <h1><?php esc_html_e( 'Unable to load Redirection', 'redirection' ); ?></h1> | |
| 388 | - <p><?php echo esc_html( $wp_requirement ); ?></p> | |
| 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" /> | |
| 389 | 165 | </div> |
| 390 | - <?php | |
| 391 | - } | |
| 166 | + <noscript>Please enable JavaScript</noscript> | |
| 167 | +</div> | |
| 392 | 168 | |
| 393 | - private function show_load_fail() { | |
| 394 | - ?> | |
| 395 | - <div class="react-error" style="display: none"> | |
| 396 | - <h1><?php esc_html_e( 'Unable to load Redirection ☹️', 'redirection' ); ?> v<?php echo esc_html( REDIRECTION_VERSION ); ?></h1> | |
| 397 | - <p><?php esc_html_e( "This may be caused by another plugin - look at your browser's error console for more details.", 'redirection' ); ?></p> | |
| 398 | - <p><?php esc_html_e( 'If you are using a page caching plugin or service (CloudFlare, OVH, etc) then you can also try clearing that cache.', 'redirection' ); ?></p> | |
| 399 | - <p><?php _e( 'Also check if your browser is able to load <code>redirection.js</code>:', 'redirection' ); ?></p> | |
| 400 | - <p><code><?php echo esc_html( plugin_dir_url( REDIRECTION_FILE ) . 'redirection.js?ver=' . urlencode( REDIRECTION_VERSION ) . '-' . urlencode( REDIRECTION_BUILD ) ); ?></code></p> | |
| 401 | - <p><?php esc_html_e( 'Please note that Redirection requires the WordPress REST API to be enabled. If you have disabled this then you won\'t be able to use Redirection', 'redirection' ); ?></p> | |
| 402 | - <p><?php _e( 'Please see the <a href="https://redirection.me/support/problems/">list of common problems</a>.', 'redirection' ); ?></p> | |
| 403 | - <p><?php esc_html_e( 'If you think Redirection is at fault then create an issue.', 'redirection' ); ?></p> | |
| 404 | - <p class="versions"><?php _e( '<code>Redirectioni10n</code> is not defined. This usually means another plugin is blocking Redirection from loading. Please disable all plugins and try again.', 'redirection' ); ?></p> | |
| 405 | - <p> | |
| 406 | - <a class="button-primary" target="_blank" href="https://github.com/johngodley/redirection/issues/new?title=Problem%20starting%20Redirection%20<?php echo esc_attr( REDIRECTION_VERSION ); ?>"> | |
| 407 | - <?php esc_html_e( 'Create Issue', 'redirection' ); ?> | |
| 408 | - </a> | |
| 409 | - </p> | |
| 410 | - </div> | |
| 411 | - <?php | |
| 169 | +<script> | |
| 170 | + addLoadEvent( function() { | |
| 171 | + redirection.show( 'react-ui' ); | |
| 172 | + } ); | |
| 173 | +</script> | |
| 174 | +<?php | |
| 412 | 175 | } |
| 413 | 176 | |
| 414 | - private function show_main() { | |
| 415 | - ?> | |
| 416 | - <div id="react-modal"></div> | |
| 417 | - <div id="react-ui"> | |
| 418 | - <div class="react-loading"> | |
| 419 | - <h1><?php esc_html_e( 'Loading, please wait...', 'redirection' ); ?></h1> | |
| 420 | - | |
| 421 | - <span class="react-loading-spinner"></span> | |
| 422 | - </div> | |
| 423 | - <noscript><?php esc_html_e( 'Please enable JavaScript', 'redirection' ); ?></noscript> | |
| 424 | - | |
| 425 | - <?php $this->show_load_fail(); ?> | |
| 426 | - </div> | |
| 427 | - | |
| 428 | - <script> | |
| 429 | - var prevError = window.onerror; | |
| 430 | - var errors = []; | |
| 431 | - var timeout = 0; | |
| 432 | - var timer = setInterval( function() { | |
| 433 | - if ( isRedirectionLoaded() ) { | |
| 434 | - resetAll(); | |
| 435 | - } else if ( errors.length > 0 || timeout++ === 5 ) { | |
| 436 | - showError(); | |
| 437 | - } | |
| 438 | - }, 5000 ); | |
| 439 | - | |
| 440 | - function isRedirectionLoaded() { | |
| 441 | - return typeof redirection !== 'undefined'; | |
| 442 | - } | |
| 443 | - | |
| 444 | - function showError() { | |
| 445 | - var errorText = ""; | |
| 446 | - | |
| 447 | - if ( errors.length > 0 ) { | |
| 448 | - errorText = "```\n" + errors.join( ',' ) + "\n```\n\n"; | |
| 449 | - } | |
| 450 | - | |
| 451 | - resetAll(); | |
| 452 | - document.querySelector( '.react-loading' ).style.display = 'none'; | |
| 453 | - document.querySelector( '.react-error' ).style.display = 'block'; | |
| 454 | - | |
| 455 | - if ( typeof Redirectioni10n !== 'undefined' ) { | |
| 456 | - document.querySelector( '.versions' ).innerHTML = Redirectioni10n.versions.replace( /\n/g, '<br />' ); | |
| 457 | - document.querySelector( '.react-error .button-primary' ).href += '&body=' + encodeURIComponent( errorText ) + encodeURIComponent( Redirectioni10n.versions ); | |
| 458 | - } | |
| 459 | - } | |
| 460 | - | |
| 461 | - function resetAll() { | |
| 462 | - clearInterval( timer ); | |
| 463 | - window.onerror = prevError; | |
| 464 | - } | |
| 465 | - | |
| 466 | - window.onerror = function( error, url, line ) { | |
| 467 | - console.error( error ); | |
| 468 | - errors.push( error + ' ' + url + ' ' + line ); | |
| 469 | - }; | |
| 470 | - </script> | |
| 471 | - <?php | |
| 472 | - } | |
| 473 | - | |
| 474 | 177 | private function user_has_access() { |
| 475 | - return current_user_can( $this->get_access_role() ); | |
| 178 | + return current_user_can( apply_filters( 'redirection_role', 'administrator' ) ); | |
| 476 | 179 | } |
| 477 | 180 | |
| 478 | - private function inject() { | |
| 479 | - if ( isset( $_GET['page'] ) && $this->get_menu_page() !== 'redirects' && $_GET['page'] === 'redirection.php' ) { | |
| 480 | - $this->try_export_logs(); | |
| 481 | - $this->try_export_redirects(); | |
| 482 | - $this->try_export_rss(); | |
| 181 | + function inject() { | |
| 182 | + if ( isset( $_GET['page'] ) && isset( $_GET['sub'] ) && $_GET['page'] === 'redirection.php' ) { | |
| 183 | + $this->tryExportLogs(); | |
| 184 | + $this->tryExportRedirects(); | |
| 185 | + $this->tryExportRSS(); | |
| 483 | 186 | } |
| 484 | 187 | } |
| 485 | 188 | |
| 486 | - private function get_menu_page() { | |
| 487 | - if ( isset( $_GET['sub'] ) && in_array( $_GET['sub'], array( 'group', '404s', 'log', 'io', 'options', 'support', true ) ) ) { | |
| 488 | - return $_GET['sub']; | |
| 489 | - } | |
| 490 | - | |
| 491 | - return 'redirects'; | |
| 492 | - } | |
| 493 | - | |
| 494 | - public function try_export_rss() { | |
| 495 | - if ( isset( $_GET['token'] ) && $this->get_menu_page() === 'rss' ) { | |
| 189 | + function tryExportRSS() { | |
| 190 | + if ( isset( $_GET['token'] ) && $_GET['sub'] === 'rss' ) { | |
| 496 | 191 | $options = red_get_options(); |
| 497 | 192 | |
| 498 | - if ( $_GET['token'] === $options['token'] && ! empty( $options['token'] ) ) { | |
| 193 | + if ( $_GET['token'] === $options['token'] && !empty( $options['token'] ) ) { | |
| 499 | 194 | $items = Red_Item::get_all_for_module( intval( $_GET['module'] ) ); |
| 500 | 195 | |
| 501 | 196 | $exporter = Red_FileIO::create( 'rss' ); |
| 502 | 197 | $exporter->force_download(); |
| @@ -505,11 +200,11 @@ | ||
| 505 | 200 | } |
| 506 | 201 | } |
| 507 | 202 | } |
| 508 | 203 | |
| 509 | - private function try_export_logs() { | |
| 204 | + private function tryExportLogs() { | |
| 510 | 205 | if ( $this->user_has_access() && isset( $_POST['export-csv'] ) && check_admin_referer( 'wp_rest' ) ) { |
| 511 | - if ( $this->get_menu_page() === 'log' ) { | |
| 206 | + if ( isset( $_GET['sub'] ) && $_GET['sub'] === 'log' ) { | |
| 512 | 207 | RE_Log::export_to_csv(); |
| 513 | 208 | } else { |
| 514 | 209 | RE_404::export_to_csv(); |
| 515 | 210 | } |
| @@ -517,10 +212,10 @@ | ||
| 517 | 212 | die(); |
| 518 | 213 | } |
| 519 | 214 | } |
| 520 | 215 | |
| 521 | - private function try_export_redirects() { | |
| 522 | - if ( $this->user_has_access() && $_GET['sub'] === 'io' && isset( $_GET['exporter'] ) && isset( $_GET['export'] ) && check_admin_referer( 'wp_rest' ) ) { | |
| 216 | + private function tryExportRedirects() { | |
| 217 | + if ( $this->user_has_access() && $_GET['sub'] === 'modules' && isset( $_GET['exporter'] ) && isset( $_GET['export'] ) ) { | |
| 523 | 218 | $export = Red_FileIO::export( $_GET['export'], $_GET['exporter'] ); |
| 524 | 219 | |
| 525 | 220 | if ( $export !== false ) { |
| 526 | 221 | $export['exporter']->force_download(); |