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

161 lines 3.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
8 public function get_id() {
9 return self::MODULE_ID;
10 }
11
12 public function can_edit_config() {
13 return false;
14 }
15
16 public function render_config() {
17 }
18
19 public function get_config() {
20 return array();
21 }
22
23 public function start() {
24 // Setup the various filters and actions that allow Redirection to happen
25 add_action( 'init', array( &$this, 'init' ) );
26 add_action( 'send_headers', array( &$this, 'send_headers' ) );
27 add_filter( 'permalink_redirect_skip', array( &$this, 'permalink_redirect_skip' ) );
28 add_filter( 'wp_redirect', array( &$this, 'wp_redirect' ), 1, 2 );
29 add_action( 'template_redirect', array( &$this, 'template_redirect' ) );
30
31 // Remove WordPress 2.3 redirection
32 remove_action( 'template_redirect', 'wp_old_slug_redirect' );
33 remove_action( 'edit_form_advanced', 'wp_remember_old_slug' );
34 }
35
36 public function init() {
37 $url = $_SERVER['REQUEST_URI'];
38
39 // Make sure we don't try and redirect something essential
40 if ( ! $this->protected_url( $url ) && $this->matched === false ) {
41 do_action( 'redirection_first', $url, $this );
42
43 $redirects = Red_Item::get_for_url( $url, 'wp' );
44
45 foreach ( (array) $redirects as $item ) {
46 if ( $item->matches( $url ) ) {
47 $this->matched = $item;
48 break;
49 }
50 }
51
52 do_action( 'redirection_last', $url, $this );
53 }
54 }
55
56 private function protected_url( $url ) {
57 return false;
58 }
59
60 public function template_redirect() {
61 if ( is_404() ) {
62 $options = red_get_options();
63
64 if ( isset( $options['expire_404'] ) && $options['expire_404'] >= 0 ) {
65 RE_404::create( $this->get_url(), $this->get_user_agent(), $this->get_ip(), $this->get_referrer() );
66 }
67 }
68 }
69
70 public function status_header( $status ) {
71 // Fix for incorrect headers sent when using FastCGI/IIS
72 if ( substr( php_sapi_name(), 0, 3 ) === 'cgi' )
73 return str_replace( 'HTTP/1.1', 'Status:', $status );
74 return $status;
75 }
76
77 public function send_headers( $obj ) {
78 if ( ! empty( $this->matched ) && $this->matched->match->action_code === '410' ) {
79 add_filter( 'status_header', array( &$this, 'set_header_410' ) );
80 }
81 }
82
83 public function set_header_410() {
84 return 'HTTP/1.1 410 Gone';
85 }
86
87 public function wp_redirect( $url, $status ) {
88 global $wp_version, $is_IIS;
89
90 if ( $is_IIS ) {
91 header( "Refresh: 0;url=$url" );
92 return $url;
93 }
94 elseif ( $status === 301 && php_sapi_name() === 'cgi-fcgi' ) {
95 $servers_to_check = array( 'lighttpd', 'nginx' );
96
97 foreach ( $servers_to_check as $name ) {
98 if ( stripos( $_SERVER['SERVER_SOFTWARE'], $name ) !== false ) {
99 status_header( $status );
100 header( "Location: $url" );
101 exit( 0 );
102 }
103 }
104 }
105
106 status_header( $status );
107 return $url;
108 }
109
110 public function update( $data ) {
111 return false;
112 }
113
114 protected function load( $options ) {
115 }
116
117 protected function flush_module() {
118 }
119
120 public function permalink_redirect_skip( $skip ) {
121 // only want this if we've matched using redirection
122 if ( $this->matched )
123 $skip[] = $_SERVER['REQUEST_URI'];
124 return $skip;
125 }
126
127 public function get_name() {
128 return __( 'WordPress', 'redirection' );
129 }
130
131 public function get_description() {
132 return __( 'WordPress-powered redirects. This requires no further configuration, and you can track hits.', 'redirection' );
133 }
134
135 private function get_url() {
136 if ( isset( $_SERVER['REQUEST_URI'] ) )
137 return $_SERVER['REQUEST_URI'];
138 return '';
139 }
140
141 private function get_user_agent() {
142 if ( isset( $_SERVER['HTTP_USER_AGENT'] ) )
143 return $_SERVER['HTTP_USER_AGENT'];
144 return false;
145 }
146
147 private function get_referrer() {
148 if ( isset( $_SERVER['HTTP_REFERER'] ) )
149 return $_SERVER['HTTP_REFERER'];
150 return false;
151 }
152
153 private function get_ip() {
154 if ( isset( $_SERVER['REMOTE_ADDR'] ) )
155 return $_SERVER['REMOTE_ADDR'];
156 elseif ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
157 return $_SERVER['HTTP_X_FORWARDED_FOR'];
158 return '';
159 }
160 }
161