| 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 = apply_filters( 'redirection_url_source', Redirection_Request::get_request_url() ); |
| 38 |
|
| 39 |
// Make sure we don't try and redirect something essential |
| 40 |
if ( $url && ! $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( Redirection_Request::get_request_url(), Redirection_Request::get_user_agent(), Redirection_Request::get_ip(), Redirection_Request::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 |
elseif ( $status == 307) { |
| 106 |
status_header( $status ); |
| 107 |
header( "Cache-Control: no-cache, must-revalidate, max-age=0" ); |
| 108 |
header( "Expires: Sat, 26 Jul 1997 05:00:00 GMT" ); |
| 109 |
return $url; |
| 110 |
} |
| 111 |
status_header( $status ); |
| 112 |
return $url; |
| 113 |
} |
| 114 |
|
| 115 |
public function update( $data ) { |
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
protected function load( $options ) { |
| 120 |
} |
| 121 |
|
| 122 |
protected function flush_module() { |
| 123 |
} |
| 124 |
|
| 125 |
public function permalink_redirect_skip( $skip ) { |
| 126 |
// only want this if we've matched using redirection |
| 127 |
if ( $this->matched ) |
| 128 |
$skip[] = $_SERVER['REQUEST_URI']; |
| 129 |
return $skip; |
| 130 |
} |
| 131 |
|
| 132 |
public function get_name() { |
| 133 |
return __( 'WordPress', 'redirection' ); |
| 134 |
} |
| 135 |
|
| 136 |
public function get_description() { |
| 137 |
return __( 'WordPress-powered redirects. This requires no further configuration, and you can track hits.', 'redirection' ); |
| 138 |
} |
| 139 |
} |
| 140 |
|