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