PluginProbe
Redirection / 2.10
Redirection v2.10
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 2.10, at modules/wordpress.php

175 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 remove_action( 'edit_form_advanced', 'wp_remember_old_slug' );
31 }
32
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();
37
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 }
41 }
42
43 return $status_header;
44 }
45
46 public function redirection_do_nothing() {
47 $this->can_log = false;
48 return false;
49 }
50
51 public function redirection_visit( $redirect, $url, $target ) {
52 $redirect->visit( $url, $target );
53 }
54
55 public function init() {
56 $url = apply_filters( 'redirection_url_source', Redirection_Request::get_request_url() );
57
58 // Make sure we don't try and redirect something essential
59 if ( $url && ! $this->protected_url( $url ) && $this->matched === false ) {
60 do_action( 'redirection_first', $url, $this );
61
62 $redirects = Red_Item::get_for_url( $url, 'wp' );
63
64 foreach ( (array) $redirects as $item ) {
65 if ( $item->matches( $url ) ) {
66 $this->matched = $item;
67 break;
68 }
69 }
70
71 do_action( 'redirection_last', $url, $this );
72 }
73 }
74
75 private function protected_url( $url ) {
76 return false;
77 }
78
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 }
86 }
87 }
88
89 public function status_header( $status ) {
90 // Fix for incorrect headers sent when using FastCGI/IIS
91 if ( substr( php_sapi_name(), 0, 3 ) === 'cgi' ) {
92 return str_replace( 'HTTP/1.1', 'Status:', $status );
93 }
94
95 return $status;
96 }
97
98 public function send_headers( $obj ) {
99 if ( ! empty( $this->matched ) && $this->matched->match->action_code === '410' ) {
100 add_filter( 'status_header', array( $this, 'set_header_410' ) );
101 }
102 }
103
104 public function set_header_410() {
105 return 'HTTP/1.1 410 Gone';
106 }
107
108 public function wp_redirect( $url, $status ) {
109 global $wp_version, $is_IIS;
110
111 if ( $is_IIS ) {
112 header( "Refresh: 0;url=$url" );
113 return $url;
114 }
115
116 if ( $status === 301 && php_sapi_name() === 'cgi-fcgi' ) {
117 $servers_to_check = array( 'lighttpd', 'nginx' );
118
119 foreach ( $servers_to_check as $name ) {
120 if ( stripos( $_SERVER['SERVER_SOFTWARE'], $name ) !== false ) {
121 status_header( $status );
122 header( "Location: $url" );
123 exit( 0 );
124 }
125 }
126 }
127
128 if ( intval( $status, 10 ) === 307 ) {
129 status_header( $status );
130 nocache_headers();
131 return $url;
132 }
133
134 $options = red_get_options();
135
136 // Do we need to set the cache header?
137 if ( ! headers_sent() && isset( $options['redirect_cache'] ) && $options['redirect_cache'] !== 0 && intval( $status, 10 ) === 301 ) {
138 if ( $options['redirect_cache'] === -1 ) {
139 // No cache - just use WP function
140 nocache_headers();
141 } else {
142 // Custom cache
143 header( 'Expires: ' . gmdate( 'D, d M Y H:i:s T', time() + $options['redirect_cache'] * 60 * 60 ) );
144 header( 'Cache-Control: max-age=' . $options['redirect_cache'] * 60 * 60 );
145 }
146 }
147
148 status_header( $status );
149 return $url;
150 }
151
152 public function update( array $data ) {
153 return false;
154 }
155
156 protected function load( $options ) {
157 }
158
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;
169 }
170
171 public function reset() {
172 $this->can_log = true;
173 }
174 }
175