Make WordPress Core

Changeset 34708


Ignore:
Timestamp:
09/30/2015 01:10:03 AM (11 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         * @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        }
  • trunk/src/wp-includes/rewrite-functions.php

    r34566 r34708  
    88
    99/**
    10  * Add a straight rewrite rule.
     10 * Adds 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         * @ticket 16840
     36         */
     37        public function test_add_rule() {
     38                global $wp_rewrite;
     39
     40                $pattern  = 'path/to/rewrite/([^/]+)/?$';
     41                $redirect = 'index.php?test_var1=$matches[1]&test_var2=1';
     42
     43                $wp_rewrite->add_rule( $pattern, $redirect );
     44
     45                $wp_rewrite->flush_rules();
     46
     47                $rewrite_rules = $wp_rewrite->rewrite_rules();
     48
     49                $this->assertSame( $redirect, $rewrite_rules[ $pattern ] );
     50        }
     51
     52        /**
     53         * @ticket 16840
     54         */
     55        public function test_add_rule_redirect_array() {
     56                global $wp_rewrite;
     57
     58                $pattern  = 'path/to/rewrite/([^/]+)/?$';
     59                $redirect = 'index.php?test_var1=$matches[1]&test_var2=1';
     60
     61                $wp_rewrite->add_rule( $pattern, array(
     62                        'test_var1' => '$matches[1]',
     63                        'test_var2' => '1'
     64                ) );
     65
     66                $wp_rewrite->flush_rules();
     67
     68                $rewrite_rules = $wp_rewrite->rewrite_rules();
     69
     70                $this->assertSame( $redirect, $rewrite_rules[ $pattern ] );
     71        }
     72
     73        /**
     74         * @ticket 16840
     75         */
     76        public function test_add_rule_top() {
     77                global $wp_rewrite;
     78
     79                $pattern  = 'path/to/rewrite/([^/]+)/?$';
     80                $redirect = 'index.php?test_var1=$matches[1]&test_var2=1';
     81
     82                $wp_rewrite->add_rule( $pattern, $redirect, 'top' );
     83
     84                $wp_rewrite->flush_rules();
     85
     86                $extra_rules_top = $wp_rewrite->extra_rules_top;
     87
     88                $this->assertContains( $redirect, $extra_rules_top[ $pattern ] );
     89        }
     90
    3491        function test_url_to_postid() {
    3592
Note: See TracChangeset for help on using the changeset viewer.