Rewrite: allow `add_rewrite_rule|WP_Rewrite::add_rule()` to accept an associative array for the value of `$redirect` instead of requiring a query string.
Adds unit tests. Props scribu, DrewAPicture. Fixes #16840. Built from https://develop.svn.wordpress.org/trunk@34708 git-svn-id: http://core.svn.wordpress.org/trunk@34672 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
882c34d555
commit
619063b4e2
|
@ -1510,24 +1510,34 @@ class WP_Rewrite {
|
||||||
* the top of the rules.
|
* the top of the rules.
|
||||||
*
|
*
|
||||||
* @since 2.1.0
|
* @since 2.1.0
|
||||||
|
* @since 4.4.0 Array support was added to the `$redirect` parameter.
|
||||||
* @access public
|
* @access public
|
||||||
*
|
*
|
||||||
* @param string $regex Regular expression to match against request.
|
* @param string $regex Regular expression to match against request.
|
||||||
* @param string $redirect URL regex redirects to when regex matches request.
|
* @param string|array $redirect URL regex redirects to when regex matches request, or array
|
||||||
* @param string $after Optional, default is bottom. Location to place rule.
|
* of query vars and values.
|
||||||
|
* @param string $after Optional, default is bottom. Location to place rule.
|
||||||
*/
|
*/
|
||||||
public function add_rule($regex, $redirect, $after = 'bottom') {
|
public function add_rule( $regex, $redirect, $after = 'bottom' ) {
|
||||||
//get everything up to the first ?
|
if ( is_array( $redirect ) ) {
|
||||||
$index = (strpos($redirect, '?') === false ? strlen($redirect) : strpos($redirect, '?'));
|
$external = false;
|
||||||
$front = substr($redirect, 0, $index);
|
$redirect = add_query_arg( $redirect, 'index.php' );
|
||||||
if ( $front != $this->index ) { //it doesn't redirect to WP's index.php
|
|
||||||
$this->add_external_rule($regex, $redirect);
|
|
||||||
} else {
|
} else {
|
||||||
if ( 'bottom' == $after)
|
$index = false === strpos( $redirect, '?' ) ? strlen( $redirect ) : strpos( $redirect, '?' );
|
||||||
$this->extra_rules = array_merge($this->extra_rules, array($regex => $redirect));
|
$front = substr( $redirect, 0, $index );
|
||||||
else
|
|
||||||
$this->extra_rules_top = array_merge($this->extra_rules_top, array($regex => $redirect));
|
$external = $front != $this->index;
|
||||||
//$this->extra_rules[$regex] = $redirect;
|
}
|
||||||
|
|
||||||
|
// "external" = it doesn't redirect to index.php
|
||||||
|
if ( $external ) {
|
||||||
|
$this->add_external_rule( $regex, $redirect );
|
||||||
|
} else {
|
||||||
|
if ( 'bottom' == $after ) {
|
||||||
|
$this->extra_rules = array_merge( $this->extra_rules, array( $regex => $redirect ) );
|
||||||
|
} else {
|
||||||
|
$this->extra_rules_top = array_merge( $this->extra_rules_top, array( $regex => $redirect ) );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,19 +7,21 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a straight rewrite rule.
|
* Adds a straight rewrite rule.
|
||||||
*
|
*
|
||||||
* @since 2.1.0
|
* @since 2.1.0
|
||||||
|
* @since 4.4.0 Array support was added to the `$redirect` parameter.
|
||||||
*
|
*
|
||||||
* @global WP_Rewrite $wp_rewrite
|
* @global WP_Rewrite $wp_rewrite WordPress Rewrite Component.
|
||||||
*
|
*
|
||||||
* @param string $regex Regular Expression to match request against.
|
* @param string $regex Regular Expression to match request against.
|
||||||
* @param string $redirect Page to redirect to.
|
* @param string|array $redirect Page to redirect to, or array of query vars and values.
|
||||||
* @param string $after Optional, default is 'bottom'. Where to add rule, can also be 'top'.
|
* @param string $after Optional, default is 'bottom'. Where to add rule, can also be 'top'.
|
||||||
*/
|
*/
|
||||||
function add_rewrite_rule($regex, $redirect, $after = 'bottom') {
|
function add_rewrite_rule( $regex, $redirect, $after = 'bottom' ) {
|
||||||
global $wp_rewrite;
|
global $wp_rewrite;
|
||||||
$wp_rewrite->add_rule($regex, $redirect, $after);
|
|
||||||
|
$wp_rewrite->add_rule( $regex, $redirect, $after );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
*
|
*
|
||||||
* @global string $wp_version
|
* @global string $wp_version
|
||||||
*/
|
*/
|
||||||
$wp_version = '4.4-alpha-34707';
|
$wp_version = '4.4-alpha-34708';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||||
|
|
Loading…
Reference in New Issue