Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/wp-includes/class-wp-rewrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -1844,7 +1844,13 @@ public function add_permastruct( $name, $struct, $args = array() ) {
$struct = $this->root . $struct;
}

$args['struct'] = $struct;
$args['struct'] = preg_replace_callback(
'/(?:%[0-9A-F]{2})+/',
static function ( $matches ) {
return rawurldecode( $matches[0] );
},
$struct
);

$this->extra_permastructs[ $name ] = $args;
}
Expand Down
38 changes: 38 additions & 0 deletions tests/phpunit/tests/rewrite/permastructs.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ public function set_up() {
$this->set_permalink_structure( '/%postname%/' );
}

public function tear_down() {
remove_permastruct( 'wptests_cert' );
unregister_taxonomy( 'wptests_cert' );

parent::tear_down();
}

public function test_add_permastruct() {
global $wp_rewrite;

Expand All @@ -31,6 +38,7 @@ public function test_add_permastruct() {
),
$wp_rewrite->extra_permastructs['foo']
);
remove_permastruct( 'foo' );
}

public function test_remove_permastruct() {
Expand All @@ -43,4 +51,34 @@ public function test_remove_permastruct() {
remove_permastruct( 'foo' );
$this->assertArrayNotHasKey( 'foo', $wp_rewrite->extra_permastructs );
}

/**
* Tests that a URL-encoded Unicode taxonomy slug is decoded in the permastruct and
* that the generated rewrite rules correctly map the taxonomy query var to $matches[1].
*
* @ticket 41791
*/
public function test_add_permastruct_with_url_encoded_unicode_slug() {
global $wp_rewrite;

register_taxonomy(
'wptests_cert',
'post',
array(
'rewrite' => array(
'slug' => urlencode( 'Сертификат' ),
),
)
);

$stored_struct = $wp_rewrite->extra_permastructs['wptests_cert']['struct'];

// The struct must store decoded Unicode, not percent-encoded sequences.
$this->assertStringContainsString( 'Сертификат', $stored_struct );
$this->assertStringNotContainsString( urlencode( 'Сертификат' ), $stored_struct );

// The generated rules must map the taxonomy var to $matches[1], not a shifted index.
$rules = $wp_rewrite->generate_rewrite_rules( $stored_struct );
$this->assertContains( 'index.php?wptests_cert=$matches[1]', array_values( $rules ) );
}
}
Loading