| @@ -15,74 +15,38 @@ | ||
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | public function start() { |
| 18 | 18 | // Setup the various filters and actions that allow Redirection to happen |
| 19 | - add_action( 'init', array( $this, 'init' ) ); | |
| 20 | - add_action( 'init', array( $this, 'force_https' ) ); | |
| 21 | - add_action( 'send_headers', array( $this, 'send_headers' ) ); | |
| 22 | - add_filter( 'wp_redirect', array( $this, 'wp_redirect' ), 1, 2 ); | |
| 23 | - add_action( 'redirection_visit', array( $this, 'redirection_visit' ), 10, 3 ); | |
| 24 | - add_action( 'redirection_do_nothing', array( $this, 'redirection_do_nothing' ) ); | |
| 25 | - add_filter( 'redirect_canonical', array( $this, 'redirect_canonical' ), 10, 2 ); | |
| 26 | - add_action( 'template_redirect', array( $this, 'template_redirect' ) ); | |
| 19 | + add_action( 'init', array( $this, 'init' ) ); | |
| 20 | + add_action( 'send_headers', array( $this, 'send_headers' ) ); | |
| 21 | + add_filter( 'permalink_redirect_skip', array( $this, 'permalink_redirect_skip' ) ); | |
| 22 | + add_filter( 'wp_redirect', array( $this, 'wp_redirect' ), 1, 2 ); | |
| 23 | + add_action( 'template_redirect', array( $this, 'template_redirect' ) ); | |
| 24 | + //add_filter( 'status_header', array( $this, 'status_header_404' ), 10, 4 ); | |
| 25 | + add_action( 'redirection_visit', array( $this, 'redirection_visit' ), 10, 3 ); | |
| 26 | + add_action( 'redirection_do_nothing', array( $this, 'redirection_do_nothing' ) ); | |
| 27 | 27 | |
| 28 | 28 | // Remove WordPress 2.3 redirection |
| 29 | 29 | remove_action( 'template_redirect', 'wp_old_slug_redirect' ); |
| 30 | + remove_action( 'edit_form_advanced', 'wp_remember_old_slug' ); | |
| 30 | 31 | } |
| 31 | 32 | |
| 32 | - /** | |
| 33 | - * This ensures that a matched URL is not overriddden by WordPress, if the URL happens to be a WordPress URL of some kind | |
| 34 | - * For example: /?author=1 will be redirected to /author/name unless this returns false | |
| 35 | - */ | |
| 36 | - public function redirect_canonical( $redirect_url, $requested_url ) { | |
| 37 | - if ( $this->matched ) { | |
| 38 | - return false; | |
| 39 | - } | |
| 33 | + // Replacement for template_redirect to catch all 404 situations, not just template_redirect | |
| 34 | + public function status_header_404( $status_header, $code, $description, $protocol ) { | |
| 35 | + if ( $code === 404 ) { | |
| 36 | + $options = red_get_options(); | |
| 40 | 37 | |
| 41 | - return $redirect_url; | |
| 42 | - } | |
| 43 | - | |
| 44 | - public function template_redirect() { | |
| 45 | - if ( ! is_404() || $this->matched ) { | |
| 46 | - return; | |
| 38 | + if ( isset( $options['expire_404'] ) && $options['expire_404'] >= 0 && apply_filters( 'redirection_log_404', $this->can_log ) ) { | |
| 39 | + RE_404::create( Redirection_Request::get_request_url(), Redirection_Request::get_user_agent(), Redirection_Request::get_ip(), Redirection_Request::get_referrer() ); | |
| 40 | + } | |
| 47 | 41 | } |
| 48 | 42 | |
| 49 | - if ( $this->match_404_type() ) { | |
| 50 | - // Don't log an intentionally redirected 404 | |
| 51 | - return; | |
| 52 | - } | |
| 53 | - | |
| 54 | - $options = red_get_options(); | |
| 55 | - | |
| 56 | - if ( isset( $options['expire_404'] ) && $options['expire_404'] >= 0 && apply_filters( 'redirection_log_404', $this->can_log ) ) { | |
| 57 | - RE_404::create( Redirection_Request::get_request_url(), Redirection_Request::get_user_agent(), Redirection_Request::get_ip(), Redirection_Request::get_referrer() ); | |
| 58 | - } | |
| 43 | + return $status_header; | |
| 59 | 44 | } |
| 60 | 45 | |
| 61 | - private function match_404_type() { | |
| 62 | - if ( ! property_exists( $this, 'redirects' ) || count( $this->redirects ) === 0 ) { | |
| 63 | - return false; | |
| 64 | - } | |
| 65 | - | |
| 66 | - $page_type = array_values( array_filter( $this->redirects, array( $this, 'only_404' ) ) ); | |
| 67 | - | |
| 68 | - if ( count( $page_type ) > 0 ) { | |
| 69 | - $url = apply_filters( 'redirection_url_source', Redirection_Request::get_request_url() ); | |
| 70 | - $first = $page_type[0]; | |
| 71 | - return $first->matches( $url ); | |
| 72 | - } | |
| 73 | - | |
| 74 | - return false; | |
| 75 | - } | |
| 76 | - | |
| 77 | - private function only_404( $redirect ) { | |
| 78 | - return $redirect->match->get_type() === 'page'; | |
| 79 | - } | |
| 80 | - | |
| 81 | - // Return true to stop further processing of the 'do nothing' | |
| 82 | 46 | public function redirection_do_nothing() { |
| 83 | 47 | $this->can_log = false; |
| 84 | - return true; | |
| 48 | + return false; | |
| 85 | 49 | } |
| 86 | 50 | |
| 87 | 51 | public function redirection_visit( $redirect, $url, $target ) { |
| 88 | 52 | $redirect->visit( $url, $target ); |
| @@ -87,18 +51,8 @@ | ||
| 87 | 51 | public function redirection_visit( $redirect, $url, $target ) { |
| 88 | 52 | $redirect->visit( $url, $target ); |
| 89 | 53 | } |
| 90 | 54 | |
| 91 | - public function force_https() { | |
| 92 | - $options = red_get_options(); | |
| 93 | - | |
| 94 | - if ( $options['https'] && ! is_ssl() ) { | |
| 95 | - $target = rtrim( parse_url( home_url(), PHP_URL_HOST ), '/' ) . esc_url_raw( Redirection_Request::get_request_url() ); | |
| 96 | - wp_safe_redirect( 'https://' . $target, 301 ); | |
| 97 | - die(); | |
| 98 | - } | |
| 99 | - } | |
| 100 | - | |
| 101 | 55 | public function init() { |
| 102 | 56 | $url = apply_filters( 'redirection_url_source', Redirection_Request::get_request_url() ); |
| 103 | 57 | |
| 104 | 58 | // Make sure we don't try and redirect something essential |
| @@ -104,9 +58,9 @@ | ||
| 104 | 58 | // Make sure we don't try and redirect something essential |
| 105 | 59 | if ( $url && ! $this->protected_url( $url ) && $this->matched === false ) { |
| 106 | 60 | do_action( 'redirection_first', $url, $this ); |
| 107 | 61 | |
| 108 | - $redirects = Red_Item::get_for_url( $url ); | |
| 62 | + $redirects = Red_Item::get_for_url( $url, 'wp' ); | |
| 109 | 63 | |
| 110 | 64 | foreach ( (array) $redirects as $item ) { |
| 111 | 65 | if ( $item->matches( $url ) ) { |
| 112 | 66 | $this->matched = $item; |
| @@ -114,29 +68,23 @@ | ||
| 114 | 68 | } |
| 115 | 69 | } |
| 116 | 70 | |
| 117 | 71 | do_action( 'redirection_last', $url, $this ); |
| 118 | - | |
| 119 | - if ( ! $this->matched ) { | |
| 120 | - // Keep them for later | |
| 121 | - $this->redirects = $redirects; | |
| 122 | - } | |
| 123 | 72 | } |
| 124 | 73 | } |
| 125 | 74 | |
| 126 | - /** | |
| 127 | - * Protect certain URLs from being redirected. Note we don't need to protect wp-admin, as this code doesn't run there | |
| 128 | - */ | |
| 129 | 75 | private function protected_url( $url ) { |
| 130 | - $rest = wp_parse_url( red_get_rest_api() ); | |
| 131 | - $rest_api = $rest['path'] . ( isset( $rest['query'] ) ? '?' . $rest['query'] : '' ); | |
| 76 | + return false; | |
| 77 | + } | |
| 132 | 78 | |
| 133 | - if ( substr( $url, 0, strlen( $rest_api ) ) === $rest_api ) { | |
| 134 | - // Never redirect the REST API | |
| 135 | - return true; | |
| 79 | + public function template_redirect() { | |
| 80 | + if ( is_404() ) { | |
| 81 | + $options = red_get_options(); | |
| 82 | + | |
| 83 | + if ( isset( $options['expire_404'] ) && $options['expire_404'] >= 0 && apply_filters( 'redirection_log_404', $this->can_log ) ) { | |
| 84 | + RE_404::create( Redirection_Request::get_request_url(), Redirection_Request::get_user_agent(), Redirection_Request::get_ip(), Redirection_Request::get_referrer() ); | |
| 85 | + } | |
| 136 | 86 | } |
| 137 | - | |
| 138 | - return false; | |
| 139 | 87 | } |
| 140 | 88 | |
| 141 | 89 | public function status_header( $status ) { |
| 142 | 90 | // Fix for incorrect headers sent when using FastCGI/IIS |
| @@ -147,9 +95,9 @@ | ||
| 147 | 95 | return $status; |
| 148 | 96 | } |
| 149 | 97 | |
| 150 | 98 | public function send_headers( $obj ) { |
| 151 | - if ( ! empty( $this->matched ) && $this->matched->action->get_code() === 410 ) { | |
| 99 | + if ( ! empty( $this->matched ) && $this->matched->match->action_code === '410' ) { | |
| 152 | 100 | add_filter( 'status_header', array( $this, 'set_header_410' ) ); |
| 153 | 101 | } |
| 154 | 102 | } |
| 155 | 103 | |
| @@ -168,9 +116,9 @@ | ||
| 168 | 116 | if ( $status === 301 && php_sapi_name() === 'cgi-fcgi' ) { |
| 169 | 117 | $servers_to_check = array( 'lighttpd', 'nginx' ); |
| 170 | 118 | |
| 171 | 119 | foreach ( $servers_to_check as $name ) { |
| 172 | - if ( isset( $_SERVER['SERVER_SOFTWARE'] ) && stripos( $_SERVER['SERVER_SOFTWARE'], $name ) !== false ) { | |
| 120 | + if ( stripos( $_SERVER['SERVER_SOFTWARE'], $name ) !== false ) { | |
| 173 | 121 | status_header( $status ); |
| 174 | 122 | header( "Location: $url" ); |
| 175 | 123 | exit( 0 ); |
| 176 | 124 | } |
| @@ -176,9 +124,9 @@ | ||
| 176 | 124 | } |
| 177 | 125 | } |
| 178 | 126 | } |
| 179 | 127 | |
| 180 | - if ( intval( $status, 10 ) === 307 ) { | |
| 128 | + if ( $status == 307 ) { | |
| 181 | 129 | status_header( $status ); |
| 182 | 130 | nocache_headers(); |
| 183 | 131 | return $url; |
| 184 | 132 | } |
| @@ -185,9 +133,9 @@ | ||
| 185 | 133 | |
| 186 | 134 | $options = red_get_options(); |
| 187 | 135 | |
| 188 | 136 | // Do we need to set the cache header? |
| 189 | - if ( ! headers_sent() && isset( $options['redirect_cache'] ) && $options['redirect_cache'] !== 0 && intval( $status, 10 ) === 301 ) { | |
| 137 | + if ( ! headers_sent() && isset( $options['redirect_cache'] ) && $options['redirect_cache'] !== 0 ) { | |
| 190 | 138 | if ( $options['redirect_cache'] === -1 ) { |
| 191 | 139 | // No cache - just use WP function |
| 192 | 140 | nocache_headers(); |
| 193 | 141 | } else { |
| @@ -208,8 +156,17 @@ | ||
| 208 | 156 | protected function load( $options ) { |
| 209 | 157 | } |
| 210 | 158 | |
| 211 | 159 | protected function flush_module() { |
| 160 | + } | |
| 161 | + | |
| 162 | + public function permalink_redirect_skip( $skip ) { | |
| 163 | + // only want this if we've matched using redirection | |
| 164 | + if ( $this->matched ) { | |
| 165 | + $skip[] = $_SERVER['REQUEST_URI']; | |
| 166 | + } | |
| 167 | + | |
| 168 | + return $skip; | |
| 212 | 169 | } |
| 213 | 170 | |
| 214 | 171 | public function reset() { |
| 215 | 172 | $this->can_log = true; |