@@ -35,6 +35,121 @@ public function tear_down() {
3535 parent ::tear_down ();
3636 }
3737
38+
39+ /**
40+ * Gets permalink bases that must not be confused with rewrite tags.
41+ *
42+ * @return string[] Permalink bases.
43+ */
44+ private function get_percent_encoded_bases () {
45+ return array (
46+ 'Lucky%20Dip ' ,
47+ 'this%20is%20a%20test ' ,
48+ '%E6%BC%A2%E8%AA%9E ' ,
49+ '%e6%bc%a2%e8%aa%9e ' ,
50+ '100%25 ' ,
51+ '100% ' ,
52+ 'Lucky%20Dip/More%20Posts ' ,
53+ 'topics ' ,
54+ );
55+ }
56+
57+ /**
58+ * @ticket 45819
59+ */
60+ public function test_generate_rewrite_rules_with_percent_encoded_bases () {
61+ global $ wp_rewrite ;
62+
63+ $ rewrite = clone $ wp_rewrite ;
64+ $ rewrite ->matches = 'matches ' ;
65+ $ rewrite ->add_rewrite_tag ( '%test.tag% ' , '([^/]+) ' , 'test_tag= ' );
66+
67+ foreach ( $ this ->get_percent_encoded_bases () as $ base ) {
68+ $ rules = $ rewrite ->generate_rewrite_rules ( $ base . '/%category% ' , EP_NONE , false , false , false , false , false );
69+ $ this ->assertSame ( 'index.php?category_name=$matches[1] ' , $ rules [ $ base . '/(.+?)/?$ ' ], $ base );
70+ }
71+
72+ $ rules = $ rewrite ->generate_rewrite_rules ( 'Lucky%20Dip/%test.tag%/more%20posts/%author% ' , EP_NONE , true , true , false , true , false );
73+ $ this ->assertSame ( 'index.php?test_tag=$matches[1]&author_name=$matches[2] ' , $ rules ['Lucky%20Dip/([^/]+)/more%20posts/([^/]+)/?$ ' ] );
74+ $ this ->assertSame ( 'index.php?test_tag=$matches[1]&author_name=$matches[2]&paged=$matches[3] ' , $ rules ['Lucky%20Dip/([^/]+)/more%20posts/([^/]+)/page/?([0-9]{1,})/?$ ' ] );
75+ $ this ->assertSame ( 'index.php?test_tag=$matches[1]&author_name=$matches[2]&feed=$matches[3] ' , $ rules ['Lucky%20Dip/([^/]+)/more%20posts/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$ ' ] );
76+
77+ $ rewrite ->rewritecode = array ();
78+ $ this ->assertSame ( array (), $ rewrite ->generate_rewrite_rules ( 'Lucky%20Dip ' , EP_NONE , false , false , false , false , false ) );
79+ }
80+
81+ /**
82+ * @ticket 45819
83+ */
84+ public function test_percent_encoded_bases_preserve_archive_and_random_queries () {
85+ global $ wp , $ wp_rewrite , $ wp_taxonomies ;
86+
87+ $ original_rewrite = clone $ wp_rewrite ;
88+ $ original_taxonomies = $ wp_taxonomies ;
89+ $ original_options = array ();
90+ foreach ( array ( 'permalink_structure ' , 'category_base ' , 'tag_base ' , 'wp_random_base ' ) as $ option ) {
91+ $ original_options [ $ option ] = get_option ( $ option );
92+ }
93+ $ stop_redirect = static function () {
94+ throw new WP_Tests_Exception ( 'Random content redirect. ' );
95+ };
96+
97+ try {
98+ $ this ->set_permalink_structure ( '/%postname%/ ' );
99+ $ terms = array ();
100+ $ post_id = self ::factory ()->post ->create ();
101+ foreach ( array ( 'category ' , 'post_tag ' ) as $ taxonomy ) {
102+ $ terms [ $ taxonomy ] = self ::factory ()->term ->create_and_get ( array ( 'taxonomy ' => $ taxonomy ) );
103+ wp_set_object_terms ( $ post_id , array ( $ terms [ $ taxonomy ]->term_id ), $ taxonomy );
104+ }
105+
106+ foreach ( $ this ->get_percent_encoded_bases () as $ base ) {
107+ update_option ( 'category_base ' , $ base );
108+ update_option ( 'tag_base ' , 'tags/ ' . $ base );
109+ $ wp_rewrite ->init ();
110+ $ wp_rewrite ->extra_rules_top = $ original_rewrite ->extra_rules_top ;
111+ create_initial_taxonomies ();
112+ flush_rewrite_rules ( false );
113+
114+ foreach ( $ terms as $ term ) {
115+ $ this ->go_to ( get_term_link ( $ term ) );
116+ $ this ->assertFalse ( is_404 (), $ base );
117+ $ this ->assertSame ( $ term ->term_id , get_queried_object_id (), $ base );
118+ }
119+ }
120+
121+ update_option ( 'category_base ' , '' );
122+ update_option ( 'tag_base ' , '' );
123+ $ wp_rewrite ->set_random_base ( 'Lucky Dip ' );
124+ $ wp_rewrite ->extra_rules_top = $ original_rewrite ->extra_rules_top ;
125+ create_initial_taxonomies ();
126+ flush_rewrite_rules ( false );
127+ add_filter ( 'wp_redirect ' , $ stop_redirect );
128+
129+ foreach ( array (
130+ 'author/admin/ ' => array ( 'author_name ' , 'admin ' ),
131+ 'category/missing/ ' => array ( 'category_name ' , 'missing ' ),
132+ 'tag/missing/ ' => array ( 'tag ' , 'missing ' ),
133+ ) as $ path => $ expected ) {
134+ try {
135+ $ this ->go_to ( home_url ( '/Lucky%20Dip/ ' . $ path ) );
136+ } catch ( WP_Tests_Exception $ e ) {
137+ $ this ->assertSame ( 'Random content redirect. ' , $ e ->getMessage () );
138+ }
139+ $ this ->assertSame ( '1 ' , $ wp ->query_vars ['random ' ], $ path );
140+ $ this ->assertArrayHasKey ( $ expected [0 ], $ wp ->query_vars , $ path );
141+ $ this ->assertSame ( $ expected [1 ], $ wp ->query_vars [ $ expected [0 ] ], $ path );
142+ }
143+ } finally {
144+ remove_filter ( 'wp_redirect ' , $ stop_redirect );
145+ foreach ( $ original_options as $ option => $ value ) {
146+ update_option ( $ option , $ value );
147+ }
148+ $ wp_rewrite = $ original_rewrite ;
149+ $ wp_taxonomies = $ original_taxonomies ;
150+ }
151+ }
152+
38153 /**
39154 * @ticket 16840
40155 */
0 commit comments