PluginProbe
Redirection / 3.3
Redirection v3.3
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
redirection / modules / wordpress.php

wordpress.php in Redirection 3.3, at modules/wordpress.php

184 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class WordPress_Module extends Red_Module {
4 const MODULE_ID = 1;
5
6 private $matched = false;
7 private $can_log = true;
8
9 public function get_id() {
10 return self::MODULE_ID;
11 }
12
13 public function get_name() {
14 return 'WordPress';
15 }
16
17 public function start() {
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' ) );
27
28 // Remove WordPress 2.3 redirection
29 remove_action( 'template_redirect', 'wp_old_slug_redirect' );
30 }
31
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() ) {
46 $options = red_get_options();
47
48 if ( isset( $options['expire_404'] ) && $options['expire_404'] >= 0 && apply_filters( 'redirection_log_404', $this->can_log ) ) {
49 RE_404::create( Redirection_Request::get_request_url(), Redirection_Request::get_user_agent(), Redirection_Request::get_ip(), Redirection_Request::get_referrer() );
50 }
51 }
52 }
53
54 public function redirection_do_nothing() {
55 $this->can_log = false;
56 return false;
57 }
58
59 public function redirection_visit( $redirect, $url, $target ) {
60 $redirect->visit( $url, $target );
61 }
62
63 public function force_https() {
64 $options = red_get_options();
65
66 if ( $options['https'] && ! is_ssl() ) {
67 wp_safe_redirect( 'https://' . Redirection_Request::get_server_name(), 301 );
68 die();
69 }
70 }
71
72 public function init() {
73 $url = apply_filters( 'redirection_url_source', Redirection_Request::get_request_url() );
74
75 // Make sure we don't try and redirect something essential
76 if ( $url && ! $this->protected_url( $url ) && $this->matched === false ) {
77 do_action( 'redirection_first', $url, $this );
78
79 $redirects = Red_Item::get_for_url( $url, 'wp' );
80
81 foreach ( (array) $redirects as $item ) {
82 if ( $item->matches( $url ) ) {
83 $this->matched = $item;
84 break;
85 }
86 }
87
88 do_action( 'redirection_last', $url, $this );
89 }
90 }
91
92 /**
93 * Protect certain URLs from being redirected. Note we don't need to protect wp-admin, as this code doesn't run there
94 */
95 private function protected_url( $url ) {
96 $rest = parse_url( red_get_rest_api() );
97 $rest_api = $rest['path'] . ( isset( $rest['query'] ) ? '?' . $rest['query'] : '' );
98
99 if ( substr( $url, 0, strlen( $rest_api ) ) === $rest_api ) {
100 // Never redirect the REST API
101 return true;
102 }
103
104 return false;
105 }
106
107 public function status_header( $status ) {
108 // Fix for incorrect headers sent when using FastCGI/IIS
109 if ( substr( php_sapi_name(), 0, 3 ) === 'cgi' ) {
110 return str_replace( 'HTTP/1.1', 'Status:', $status );
111 }
112
113 return $status;
114 }
115
116 public function send_headers( $obj ) {
117 if ( ! empty( $this->matched ) && $this->matched->match->action_code === '410' ) {
118 add_filter( 'status_header', array( $this, 'set_header_410' ) );
119 }
120 }
121
122 public function set_header_410() {
123 return 'HTTP/1.1 410 Gone';
124 }
125
126 public function wp_redirect( $url, $status ) {
127 global $wp_version, $is_IIS;
128
129 if ( $is_IIS ) {
130 header( "Refresh: 0;url=$url" );
131 return $url;
132 }
133
134 if ( $status === 301 && php_sapi_name() === 'cgi-fcgi' ) {
135 $servers_to_check = array( 'lighttpd', 'nginx' );
136
137 foreach ( $servers_to_check as $name ) {
138 if ( stripos( $_SERVER['SERVER_SOFTWARE'], $name ) !== false ) {
139 status_header( $status );
140 header( "Location: $url" );
141 exit( 0 );
142 }
143 }
144 }
145
146 if ( intval( $status, 10 ) === 307 ) {
147 status_header( $status );
148 nocache_headers();
149 return $url;
150 }
151
152 $options = red_get_options();
153
154 // Do we need to set the cache header?
155 if ( ! headers_sent() && isset( $options['redirect_cache'] ) && $options['redirect_cache'] !== 0 && intval( $status, 10 ) === 301 ) {
156 if ( $options['redirect_cache'] === -1 ) {
157 // No cache - just use WP function
158 nocache_headers();
159 } else {
160 // Custom cache
161 header( 'Expires: ' . gmdate( 'D, d M Y H:i:s T', time() + $options['redirect_cache'] * 60 * 60 ) );
162 header( 'Cache-Control: max-age=' . $options['redirect_cache'] * 60 * 60 );
163 }
164 }
165
166 status_header( $status );
167 return $url;
168 }
169
170 public function update( array $data ) {
171 return false;
172 }
173
174 protected function load( $options ) {
175 }
176
177 protected function flush_module() {
178 }
179
180 public function reset() {
181 $this->can_log = true;
182 }
183 }
184