initial
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Return an error to the client, and trigger the WordPress error page
|
||||
*/
|
||||
class Error_Action extends Red_Action {
|
||||
/**
|
||||
* Set WordPress to show the error page
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run() {
|
||||
wp_reset_query();
|
||||
|
||||
// Set the query to be a 404
|
||||
set_query_var( 'is_404', true );
|
||||
|
||||
// Return the 404 page
|
||||
add_filter( 'template_include', [ $this, 'template_include' ] );
|
||||
|
||||
// Clear any posts if this is actually a valid URL
|
||||
add_filter( 'pre_handle_404', [ $this, 'pre_handle_404' ] );
|
||||
|
||||
// Ensure the appropriate http code is returned
|
||||
add_action( 'wp', [ $this, 'wp' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Output selected HTTP code, as well as redirection header
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function wp() {
|
||||
status_header( $this->code );
|
||||
nocache_headers();
|
||||
|
||||
global $wp_version;
|
||||
|
||||
if ( version_compare( $wp_version, '5.1', '<' ) ) {
|
||||
header( 'X-Redirect-Agent: redirection' );
|
||||
} else {
|
||||
header( 'X-Redirect-By: redirection' );
|
||||
}
|
||||
}
|
||||
|
||||
public function pre_handle_404() {
|
||||
global $wp_query;
|
||||
|
||||
// Page comments plugin interferes with this
|
||||
$wp_query->posts = [];
|
||||
return false;
|
||||
}
|
||||
|
||||
public function template_include() {
|
||||
return get_404_template();
|
||||
}
|
||||
|
||||
public function name() {
|
||||
return __( 'Error (404)', 'redirection' );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* The 'do nothing' action. This really does nothing, and is used to short-circuit Redirection so that it doesn't trigger other redirects.
|
||||
*/
|
||||
class Nothing_Action extends Red_Action {
|
||||
/**
|
||||
* Issue an action when nothing happens. This stops further processing.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run() {
|
||||
do_action( 'redirection_do_nothing', $this->get_target() );
|
||||
}
|
||||
|
||||
public function name() {
|
||||
return __( 'Do nothing (ignore)', 'redirection' );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/url.php';
|
||||
|
||||
/**
|
||||
* A 'pass through' action. Matches a rewrite rather than a redirect, and uses PHP to fetch data from a remote URL.
|
||||
*/
|
||||
class Pass_Action extends Url_Action {
|
||||
/**
|
||||
* Process an external passthrough - a URL that lives external to this server.
|
||||
*
|
||||
* @param string $url Target URL.
|
||||
* @return void
|
||||
*/
|
||||
public function process_external( $url ) {
|
||||
// This is entirely at the user's risk. The $url is set by the user
|
||||
// phpcs:ignore
|
||||
echo wp_remote_fopen( $url );
|
||||
}
|
||||
|
||||
/**
|
||||
* Process an internal passthrough - a URL that lives on the same server. Here we change the request URI and continue without making a remote request.
|
||||
*
|
||||
* @param string $target Target URL.
|
||||
* @return void
|
||||
*/
|
||||
public function process_internal( $target ) {
|
||||
// Another URL on the server
|
||||
$pos = strpos( $target, '?' );
|
||||
$_SERVER['REQUEST_URI'] = $target;
|
||||
$_SERVER['PATH_INFO'] = $target;
|
||||
|
||||
if ( $pos ) {
|
||||
$_SERVER['QUERY_STRING'] = substr( $target, $pos + 1 );
|
||||
$_SERVER['PATH_INFO'] = $target;
|
||||
|
||||
// Take the query params in the target and make them the params for this request
|
||||
parse_str( $_SERVER['QUERY_STRING'], $_GET );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Is a URL external?
|
||||
*
|
||||
* @param string $target URL to test.
|
||||
* @return boolean
|
||||
*/
|
||||
public function is_external( $target ) {
|
||||
return substr( $target, 0, 7 ) === 'http://' || substr( $target, 0, 8 ) === 'https://';
|
||||
}
|
||||
|
||||
/**
|
||||
* Pass the data from the target
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run() {
|
||||
// External target
|
||||
$target = $this->get_target();
|
||||
if ( $target === null ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( $this->is_external( $target ) ) {
|
||||
// Pass on to an external request, echo the results, and then stop
|
||||
$this->process_external( $target );
|
||||
exit();
|
||||
}
|
||||
|
||||
// Change the request and carry on
|
||||
$this->process_internal( $target );
|
||||
}
|
||||
|
||||
public function name() {
|
||||
return __( 'Pass-through', 'redirection' );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/url.php';
|
||||
|
||||
/**
|
||||
* URL action - redirect to a URL
|
||||
*/
|
||||
class Random_Action extends Url_Action {
|
||||
/**
|
||||
* Get a random URL
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
private function get_random_url() {
|
||||
// Pick a random WordPress page
|
||||
global $wpdb;
|
||||
|
||||
$id = $wpdb->get_var( "SELECT ID FROM {$wpdb->prefix}posts WHERE post_status='publish' AND post_password='' AND post_type='post' ORDER BY RAND() LIMIT 0,1" );
|
||||
if ( $id ) {
|
||||
$url = get_permalink( $id );
|
||||
|
||||
if ( $url ) {
|
||||
return $url;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run this action. May not return from this function.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run() {
|
||||
$target = $this->get_random_url();
|
||||
|
||||
if ( $target ) {
|
||||
$this->redirect_to( $target );
|
||||
}
|
||||
}
|
||||
|
||||
public function needs_target() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public function name() {
|
||||
return __( 'Redirect to random post', 'redirection' );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* URL action - redirect to a URL
|
||||
*/
|
||||
class Url_Action extends Red_Action {
|
||||
/**
|
||||
* Redirect to a URL
|
||||
*
|
||||
* @param string $target Target URL.
|
||||
* @return void
|
||||
*/
|
||||
protected function redirect_to( $target ) {
|
||||
// This is a known redirect, possibly extenal
|
||||
// phpcs:ignore
|
||||
$redirect = wp_redirect( $target, $this->get_code(), 'redirection' );
|
||||
|
||||
if ( $redirect ) {
|
||||
global $wp_version;
|
||||
|
||||
if ( version_compare( $wp_version, '5.1', '<' ) ) {
|
||||
header( 'X-Redirect-Agent: redirection' );
|
||||
}
|
||||
|
||||
die();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run this action. May not return from this function.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run() {
|
||||
$target = $this->get_target();
|
||||
|
||||
if ( $target !== null ) {
|
||||
$this->redirect_to( $target );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Does this action need a target?
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function needs_target() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public function name() {
|
||||
return __( 'Redirect to URL', 'redirection' );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user