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

174 lines 4.9 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 private function protected_url( $url ) {
75 return false;
76 }
77
78 public function template_redirect() {
79 if ( is_404() ) {
80 $options = red_get_options();
81
82 if ( isset( $options['expire_404'] ) && $options['expire_404'] >= 0 && apply_filters( 'redirection_log_404', $this->can_log ) ) {
83 RE_404::create( Redirection_Request::get_request_url(), Redirection_Request::get_user_agent(), Redirection_Request::get_ip(), Redirection_Request::get_referrer() );
84 }
85 }
86 }
87
88 public function status_header( $status ) {
89 // Fix for incorrect headers sent when using FastCGI/IIS
90 if ( substr( php_sapi_name(), 0, 3 ) === 'cgi' ) {
91 return str_replace( 'HTTP/1.1', 'Status:', $status );
92 }
93
94 return $status;
95 }
96
97 public function send_headers( $obj ) {
98 if ( ! empty( $this->matched ) && $this->matched->match->action_code === '410' ) {
99 add_filter( 'status_header', array( $this, 'set_header_410' ) );
100 }
101 }
102
103 public function set_header_410() {
104 return 'HTTP/1.1 410 Gone';
105 }
106
107 public function wp_redirect( $url, $status ) {
108 global $wp_version, $is_IIS;
109
110 if ( $is_IIS ) {
111 header( "Refresh: 0;url=$url" );
112 return $url;
113 }
114
115 if ( $status === 301 && php_sapi_name() === 'cgi-fcgi' ) {
116 $servers_to_check = array( 'lighttpd', 'nginx' );
117
118 foreach ( $servers_to_check as $name ) {
119 if ( stripos( $_SERVER['SERVER_SOFTWARE'], $name ) !== false ) {
120 status_header( $status );
121 header( "Location: $url" );
122 exit( 0 );
123 }
124 }
125 }
126
127 if ( intval( $status, 10 ) === 307 ) {
128 status_header( $status );
129 nocache_headers();
130 return $url;
131 }
132
133 $options = red_get_options();
134
135 // Do we need to set the cache header?
136 if ( ! headers_sent() && isset( $options['redirect_cache'] ) && $options['redirect_cache'] !== 0 && intval( $status, 10 ) === 301 ) {
137 if ( $options['redirect_cache'] === -1 ) {
138 // No cache - just use WP function
139 nocache_headers();
140 } else {
141 // Custom cache
142 header( 'Expires: ' . gmdate( 'D, d M Y H:i:s T', time() + $options['redirect_cache'] * 60 * 60 ) );
143 header( 'Cache-Control: max-age=' . $options['redirect_cache'] * 60 * 60 );
144 }
145 }
146
147 status_header( $status );
148 return $url;
149 }
150
151 public function update( array $data ) {
152 return false;
153 }
154
155 protected function load( $options ) {
156 }
157
158 protected function flush_module() {
159 }
160
161 public function permalink_redirect_skip( $skip ) {
162 // only want this if we've matched using redirection
163 if ( $this->matched ) {
164 $skip[] = $_SERVER['REQUEST_URI'];
165 }
166
167 return $skip;
168 }
169
170 public function reset() {
171 $this->can_log = true;
172 }
173 }
174