PluginProbe
Redirection / 2.4.2
Redirection v2.4.2
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.2, at modules/wordpress.php

160 lines 4.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
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 foreach ( $servers_to_check as $name ) {
97 if ( stripos( $_SERVER['SERVER_SOFTWARE'], $name ) !== false ) {
98 status_header( $status );
99 header( "Location: $url" );
100 exit( 0 );
101 }
102 }
103 }
104
105 status_header( $status );
106 return $url;
107 }
108
109 public function update( $data ) {
110 return false;
111 }
112
113 protected function load( $options ) {
114 }
115
116 protected function flush_module() {
117 }
118
119 public function permalink_redirect_skip( $skip ) {
120 // only want this if we've matched using redirection
121 if ( $this->matched )
122 $skip[] = $_SERVER['REQUEST_URI'];
123 return $skip;
124 }
125
126 public function get_name() {
127 return __( 'WordPress', 'redirection' );
128 }
129
130 public function get_description() {
131 return __( 'WordPress-powered redirects. This requires no further configuration, and you can track hits.', 'redirection' );
132 }
133
134 private function get_url() {
135 if ( isset( $_SERVER['REQUEST_URI'] ) )
136 return $_SERVER['REQUEST_URI'];
137 return '';
138 }
139
140 private function get_user_agent() {
141 if ( isset( $_SERVER['HTTP_USER_AGENT'] ) )
142 return $_SERVER['HTTP_USER_AGENT'];
143 return false;
144 }
145
146 private function get_referrer() {
147 if ( isset( $_SERVER['HTTP_REFERER'] ) )
148 return $_SERVER['HTTP_REFERER'];
149 return false;
150 }
151
152 private function get_ip() {
153 if ( isset( $_SERVER['REMOTE_ADDR'] ) )
154 return $_SERVER['REMOTE_ADDR'];
155 elseif ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
156 return $_SERVER['HTTP_X_FORWARDED_FOR'];
157 return '';
158 }
159 }
160