Make WordPress Core

Changeset 34708

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.

Location:
trunk
Files:
3 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     * @
     1514     *
     1515     *
     1516     * @param string request.
     1517     * @param string
     1518     *
     1519   
     1520   
     1521   
     1522       
     1523       
     1524            $);
    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    }
  • trunk/src/wp-includes/rewrite-functions.php

    r34566 r34708  
    88
    99/**
    10  * Add a straight rewrite rule.
     10 * Add a straight rewrite rule.
    1111 *
    1212 * @since 2.1.0
    13  *
    14  * @global WP_Rewrite $wp_rewrite
    15  *
    16  * @param string $regex    Regular Expression to match request against.
    17  * @param string $redirect Page to redirect to.
    18  * @param string $after    Optional, default is 'bottom'. Where to add rule, can also be 'top'.
    19  */
    20 function add_rewrite_rule($regex, $redirect, $after = 'bottom') {
    21     global $wp_rewrite;
    22     $wp_rewrite->add_rule($regex, $redirect, $after);
     13 * @since 4.4.0 Array support was added to the `$redirect` parameter.
     14 *
     15 * @global WP_Rewrite $wp_rewrite WordPress Rewrite Component.
     16 *
     17 * @param string       $regex    Regular Expression to match request against.
     18 * @param string|array $redirect Page to redirect to, or array of query vars and values.
     19 * @param string       $after    Optional, default is 'bottom'. Where to add rule, can also be 'top'.
     20 */
     21function add_rewrite_rule( $regex, $redirect, $after = 'bottom' ) {
     22    global $wp_rewrite;
     23
     24    $wp_rewrite->add_rule( $regex, $redirect, $after );
    2325}
    2426
  • trunk/tests/phpunit/tests/rewrite.php

    r34215 r34708  
    3232    }
    3333
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
    3491    function test_url_to_postid() {
    3592
Note: See TracChangeset for help on using the changeset viewer.