PluginProbe
Redirection / 3.0.1
Redirection v3.0.1
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.0.1, at modules/wordpress.php

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