Make WordPress Core


Ignore:
Timestamp:
09/30/2015 01:10:03 AM (9 years ago)
Author:
wonderboymusic
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-rewrite.php

    r34492 r34708  
    15111511     *
    15121512     * @since 2.1.0
    1513      * @access public
    1514      *
    1515      * @param string $regex    Regular expression to match against request.
    1516      * @param string $redirect URL regex redirects to when regex matches request.
    1517      * @param string $after    Optional, default is bottom. Location to place rule.
    1518      */
    1519     public function add_rule($regex, $redirect, $after = 'bottom') {
    1520         //get everything up to the first ?
    1521         $index = (strpos($redirect, '?') === false ? strlen($redirect) : strpos($redirect, '?'));
    1522         $front = substr($redirect, 0, $index);
    1523         if ( $front != $this->index ) { //it doesn't redirect to WP's index.php
    1524             $this->add_external_rule($regex, $redirect);
     1513     * @since 4.4.0 Array support was added to the `$redirect` parameter.
     1514     * @access public
     1515     *
     1516     * @param string       $regex    Regular expression to match against request.
     1517     * @param string|array $redirect URL regex redirects to when regex matches request, or array
     1518     *                               of query vars and values.
     1519     * @param string       $after    Optional, default is bottom. Location to place rule.
     1520     */
     1521    public function add_rule( $regex, $redirect, $after = 'bottom' ) {
     1522        if ( is_array( $redirect ) ) {
     1523            $external = false;
     1524            $redirect = add_query_arg( $redirect, 'index.php' );
    15251525        } else {
    1526             if ( 'bottom' == $after)
    1527                 $this->extra_rules = array_merge($this->extra_rules, array($regex => $redirect));
    1528             else
    1529                 $this->extra_rules_top = array_merge($this->extra_rules_top, array($regex => $redirect));
    1530             //$this->extra_rules[$regex] = $redirect;
     1526            $index = false === strpos( $redirect, '?' ) ? strlen( $redirect ) : strpos( $redirect, '?' );
     1527            $front = substr( $redirect, 0, $index );
     1528
     1529            $external = $front != $this->index;
     1530        }
     1531
     1532        // "external" = it doesn't redirect to index.php
     1533        if ( $external ) {
     1534            $this->add_external_rule( $regex, $redirect );
     1535        } else {
     1536            if ( 'bottom' == $after ) {
     1537                $this->extra_rules = array_merge( $this->extra_rules, array( $regex => $redirect ) );
     1538            } else {
     1539                $this->extra_rules_top = array_merge( $this->extra_rules_top, array( $regex => $redirect ) );
     1540            }
    15311541        }
    15321542    }
Note: See TracChangeset for help on using the changeset viewer.