PluginProbe
Redirection / 2.7
Redirection v2.7
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
← All changes | modules/wordpress.php +29 -122 4.22.7 View file →
@@ -3,9 +3,8 @@
3 3 class WordPress_Module extends Red_Module {
4 4 const MODULE_ID = 1;
5 5
6 6 private $matched = false;
7 - private $can_log = true;
8 7
9 8 public function get_id() {
10 9 return self::MODULE_ID;
11 10 }
@@ -15,90 +14,19 @@
15 14 }
16 15
17 16 public function start() {
18 17 // 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' ) );
18 + add_action( 'init', array( &$this, 'init' ) );
19 + add_action( 'send_headers', array( &$this, 'send_headers' ) );
20 + add_filter( 'permalink_redirect_skip', array( &$this, 'permalink_redirect_skip' ) );
21 + add_filter( 'wp_redirect', array( &$this, 'wp_redirect' ), 1, 2 );
22 + add_action( 'template_redirect', array( &$this, 'template_redirect' ) );
27 23
28 24 // Remove WordPress 2.3 redirection
29 25 remove_action( 'template_redirect', 'wp_old_slug_redirect' );
26 + remove_action( 'edit_form_advanced', 'wp_remember_old_slug' );
30 27 }
31 28
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 - }
40 -
41 - return $redirect_url;
42 - }
43 -
44 - public function template_redirect() {
45 - if ( ! is_404() || $this->matched ) {
46 - return;
47 - }
48 -
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 - }
59 - }
60 -
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 - public function redirection_do_nothing() {
83 - $this->can_log = false;
84 - return true;
85 - }
86 -
87 - public function redirection_visit( $redirect, $url, $target ) {
88 - $redirect->visit( $url, $target );
89 - }
90 -
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 29 public function init() {
102 30 $url = apply_filters( 'redirection_url_source', Redirection_Request::get_request_url() );
103 31
104 32 // Make sure we don't try and redirect something essential
@@ -104,9 +32,9 @@
104 32 // Make sure we don't try and redirect something essential
105 33 if ( $url && ! $this->protected_url( $url ) && $this->matched === false ) {
106 34 do_action( 'redirection_first', $url, $this );
107 35
108 - $redirects = Red_Item::get_for_url( $url );
36 + $redirects = Red_Item::get_for_url( $url, 'wp' );
109 37
110 38 foreach ( (array) $redirects as $item ) {
111 39 if ( $item->matches( $url ) ) {
112 40 $this->matched = $item;
@@ -114,43 +42,35 @@
114 42 }
115 43 }
116 44
117 45 do_action( 'redirection_last', $url, $this );
118 -
119 - if ( ! $this->matched ) {
120 - // Keep them for later
121 - $this->redirects = $redirects;
122 - }
123 46 }
124 47 }
125 48
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 49 private function protected_url( $url ) {
130 - $rest = wp_parse_url( red_get_rest_api() );
131 - $rest_api = $rest['path'] . ( isset( $rest['query'] ) ? '?' . $rest['query'] : '' );
50 + return false;
51 + }
132 52
133 - if ( substr( $url, 0, strlen( $rest_api ) ) === $rest_api ) {
134 - // Never redirect the REST API
135 - return true;
53 + public function template_redirect() {
54 + if ( is_404() ) {
55 + $options = red_get_options();
56 +
57 + if ( isset( $options['expire_404'] ) && $options['expire_404'] >= 0 ) {
58 + RE_404::create( Redirection_Request::get_request_url(), Redirection_Request::get_user_agent(), Redirection_Request::get_ip(), Redirection_Request::get_referrer() );
59 + }
136 60 }
137 -
138 - return false;
139 61 }
140 62
141 63 public function status_header( $status ) {
142 64 // Fix for incorrect headers sent when using FastCGI/IIS
143 - if ( substr( php_sapi_name(), 0, 3 ) === 'cgi' ) {
65 + if ( substr( php_sapi_name(), 0, 3 ) === 'cgi' )
144 66 return str_replace( 'HTTP/1.1', 'Status:', $status );
145 - }
146 -
147 67 return $status;
148 68 }
149 69
150 70 public function send_headers( $obj ) {
151 - if ( ! empty( $this->matched ) && $this->matched->action->get_code() === 410 ) {
152 - add_filter( 'status_header', array( $this, 'set_header_410' ) );
71 + if ( ! empty( $this->matched ) && $this->matched->match->action_code === '410' ) {
72 + add_filter( 'status_header', array( &$this, 'set_header_410' ) );
153 73 }
154 74 }
155 75
156 76 public function set_header_410() {
@@ -163,14 +83,13 @@
163 83 if ( $is_IIS ) {
164 84 header( "Refresh: 0;url=$url" );
165 85 return $url;
166 86 }
167 -
168 - if ( $status === 301 && php_sapi_name() === 'cgi-fcgi' ) {
87 + elseif ( $status === 301 && php_sapi_name() === 'cgi-fcgi' ) {
169 88 $servers_to_check = array( 'lighttpd', 'nginx' );
170 89
171 90 foreach ( $servers_to_check as $name ) {
172 - if ( isset( $_SERVER['SERVER_SOFTWARE'] ) && stripos( $_SERVER['SERVER_SOFTWARE'], $name ) !== false ) {
91 + if ( stripos( $_SERVER['SERVER_SOFTWARE'], $name ) !== false ) {
173 92 status_header( $status );
174 93 header( "Location: $url" );
175 94 exit( 0 );
176 95 }
@@ -175,29 +94,14 @@
175 94 exit( 0 );
176 95 }
177 96 }
178 97 }
179 -
180 - if ( intval( $status, 10 ) === 307 ) {
98 + elseif ( $status == 307) {
181 99 status_header( $status );
182 - nocache_headers();
100 + header( "Cache-Control: no-cache, must-revalidate, max-age=0" );
101 + header( "Expires: Sat, 26 Jul 1997 05:00:00 GMT" );
183 102 return $url;
184 103 }
185 -
186 - $options = red_get_options();
187 -
188 - // Do we need to set the cache header?
189 - if ( ! headers_sent() && isset( $options['redirect_cache'] ) && $options['redirect_cache'] !== 0 && intval( $status, 10 ) === 301 ) {
190 - if ( $options['redirect_cache'] === -1 ) {
191 - // No cache - just use WP function
192 - nocache_headers();
193 - } else {
194 - // Custom cache
195 - header( 'Expires: ' . gmdate( 'D, d M Y H:i:s T', time() + $options['redirect_cache'] * 60 * 60 ) );
196 - header( 'Cache-Control: max-age=' . $options['redirect_cache'] * 60 * 60 );
197 - }
198 - }
199 -
200 104 status_header( $status );
201 105 return $url;
202 106 }
203 107
@@ -210,8 +114,11 @@
210 114
211 115 protected function flush_module() {
212 116 }
213 117
214 - public function reset() {
215 - $this->can_log = true;
118 + public function permalink_redirect_skip( $skip ) {
119 + // only want this if we've matched using redirection
120 + if ( $this->matched )
121 + $skip[] = $_SERVER['REQUEST_URI'];
122 + return $skip;
216 123 }
217 124 }