Ticket #16840: 16840.2.diff
File 16840.2.diff, 6.2 KB (added by , 9 years ago) |
---|
-
src/wp-includes/class-wp-rewrite.php
1510 1510 * the top of the rules. 1511 1511 * 1512 1512 * @since 2.1.0 1513 * @since 4.4.0 Array support was added to the `$redirect` parameter. 1513 1514 * @access public 1514 1515 * 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. 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. 1518 1520 */ 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); 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' ); 1525 1525 } else { 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 { 1526 1536 if ( 'bottom' == $after) 1527 1537 $this->extra_rules = array_merge($this->extra_rules, array($regex => $redirect)); 1528 1538 else 1529 1539 $this->extra_rules_top = array_merge($this->extra_rules_top, array($regex => $redirect)); 1530 //$this->extra_rules[$regex] = $redirect;1531 1540 } 1532 1541 } 1533 1542 -
src/wp-includes/rewrite-functions.php
7 7 */ 8 8 9 9 /** 10 * Add a straight rewrite rule.10 * Adds a straight rewrite rule. 11 11 * 12 12 * @since 2.1.0 13 * @since 4.4.0 Array support was added to the `$redirect` parameter. 13 14 * 14 * @global WP_Rewrite $wp_rewrite 15 * @global WP_Rewrite $wp_rewrite WordPress Rewrite Component. 15 16 * 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'.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'. 19 20 */ 20 function add_rewrite_rule( $regex, $redirect, $after = 'bottom') {21 function add_rewrite_rule( $regex, $redirect, $after = 'bottom' ) { 21 22 global $wp_rewrite; 22 $wp_rewrite->add_rule($regex, $redirect, $after); 23 24 $wp_rewrite->add_rule( $regex, $redirect, $after ); 23 25 } 24 26 25 27 /** -
tests/phpunit/tests/rewrite/addRewriteRule.php
1 <?php 2 3 /** 4 * @group rewrite 5 */ 6 class Tests_Rewrite_AddRewriteRule extends WP_UnitTestCase { 7 8 9 public function setUp() { 10 global $wp_rewrite; 11 parent::setUp(); 12 13 $wp_rewrite->init(); 14 $wp_rewrite->set_permalink_structure( '/%postname%/' ); 15 $wp_rewrite->flush_rules(); 16 } 17 18 public function tearDown() { 19 global $wp_rewrite; 20 $wp_rewrite->init(); 21 22 parent::tearDown(); 23 } 24 25 /** 26 * @ticket 16840 27 */ 28 public function test_add_rewrite_rule_redirect() { 29 global $wp_rewrite; 30 31 $pattern = 'path/to/rewrite/([^/]+)/?$'; 32 $redirect = 'index.php?test_var1=$matches[1]&test_var2=1'; 33 add_rewrite_rule( $pattern, $redirect ); 34 35 flush_rewrite_rules(); 36 37 $this->assertContains( $redirect, $wp_rewrite->rewrite_rules() ); 38 } 39 40 /** 41 * @ticket 16840 42 */ 43 public function test_add_rewrite_rule_redirect_array() { 44 global $wp_rewrite; 45 46 $pattern = 'path/to/rewrite/([^/]+)/?$'; 47 $redirect = 'index.php?test_var1=$matches[1]&test_var2=1'; 48 49 add_rewrite_rule( $pattern, array( 50 'test_var1' => '$matches[1]', 51 'test_var2' => '1' 52 ) ); 53 54 flush_rewrite_rules(); 55 56 $this->assertContains( $redirect, $wp_rewrite->rewrite_rules() ); 57 } 58 } -
tests/phpunit/tests/rewrite.php
31 31 parent::tearDown(); 32 32 } 33 33 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 $this->assertContains( $redirect, $wp_rewrite->rewrite_rules() ); 48 } 49 50 /** 51 * @ticket 16840 52 */ 53 public function test_add_rule_redirect_array() { 54 global $wp_rewrite; 55 56 $pattern = 'path/to/rewrite/([^/]+)/?$'; 57 $redirect = 'index.php?test_var1=$matches[1]&test_var2=1'; 58 59 $wp_rewrite->add_rule( $pattern, array( 60 'test_var1' => '$matches[1]', 61 'test_var2' => '1' 62 ) ); 63 64 $wp_rewrite->flush_rules(); 65 66 $this->assertContains( $redirect, $wp_rewrite->rewrite_rules() ); 67 } 68 69 /** 70 * @ticket 16840 71 */ 72 public function test_add_rule_top() { 73 global $wp_rewrite; 74 75 $pattern = 'path/to/rewrite/([^/]+)/?$'; 76 $redirect = 'index.php?test_var1=$matches[1]&test_var2=1'; 77 78 $wp_rewrite->add_rule( $pattern, $redirect, 'top' ); 79 80 $wp_rewrite->flush_rules(); 81 82 $this->assertContains( $redirect, $wp_rewrite->extra_rules_top ); 83 } 84 34 85 function test_url_to_postid() { 35 86 36 87 $id = $this->factory->post->create();