From e055e5b80ed20864e2a9292f6a7f767337c4cd7e Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 6 Jan 2016 07:39:29 +0000 Subject: [PATCH] Rewrite: Add a `remove_permastruct()` helper function. It can be used to remove permastructs that were added using `add_permastruct()`. Fixes #35235. git-svn-id: https://develop.svn.wordpress.org/trunk@36181 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-rewrite.php | 12 ++++++ src/wp-includes/rewrite.php | 21 +++++++++- tests/phpunit/tests/rewrite/permastructs.php | 40 ++++++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/tests/rewrite/permastructs.php diff --git a/src/wp-includes/class-wp-rewrite.php b/src/wp-includes/class-wp-rewrite.php index 035bbc3866..e6f768fed1 100644 --- a/src/wp-includes/class-wp-rewrite.php +++ b/src/wp-includes/class-wp-rewrite.php @@ -1744,6 +1744,18 @@ class WP_Rewrite { $this->extra_permastructs[ $name ] = $args; } + /** + * Removes a permalink structure. + * + * @since 4.5.0 + * @access public + * + * @param string $name Name for permalink structure. + */ + public function remove_permastruct( $name ) { + unset( $this->extra_permastructs[ $name ] ); + } + /** * Removes rewrite rules and then recreate rewrite rules. * diff --git a/src/wp-includes/rewrite.php b/src/wp-includes/rewrite.php index 7f3b2ed321..fe6b99adb7 100644 --- a/src/wp-includes/rewrite.php +++ b/src/wp-includes/rewrite.php @@ -178,7 +178,7 @@ function add_rewrite_tag( $tag, $regex, $query = '' ) { * @since 3.0.0 * * @see WP_Rewrite::add_permastruct() - * @global WP_Rewrite $wp_rewrite + * @global WP_Rewrite $wp_rewrite WordPress rewrite component. * * @param string $name Name for permalink structure. * @param string $struct Permalink structure. @@ -197,6 +197,25 @@ function add_permastruct( $name, $struct, $args = array() ) { $wp_rewrite->add_permastruct( $name, $struct, $args ); } +/** + * Remove permalink structure. + * + * Can only be used to remove permastructs that were added using add_permastruct(). + * Built-in permastructs cannot be removed. + * + * @since 4.5.0 + * + * @see WP_Rewrite::remove_permastruct() + * @global WP_Rewrite $wp_rewrite WordPress rewrite component. + * + * @param string $name Name for permalink structure. + */ +function remove_permastruct( $name ) { + global $wp_rewrite; + + $wp_rewrite->remove_permastruct( $name ); +} + /** * Add a new feed type like /atom1/. * diff --git a/tests/phpunit/tests/rewrite/permastructs.php b/tests/phpunit/tests/rewrite/permastructs.php new file mode 100644 index 0000000000..34133987fc --- /dev/null +++ b/tests/phpunit/tests/rewrite/permastructs.php @@ -0,0 +1,40 @@ +set_permalink_structure( '/%postname%/' ); + } + + public function test_add_permastruct( ) { + global $wp_rewrite; + + add_permastruct( 'foo', 'bar/%foo%' ); + $this->assertEqualSets( array( + 'with_front' => true, + 'ep_mask' => EP_NONE, + 'paged' => true, + 'feed' => true, + 'walk_dirs' => true, + 'endpoints' => true, + 'forcomments' => false, + 'struct' => '/bar/%foo%', + ), $wp_rewrite->extra_permastructs['foo'] ); + } + + public function test_remove_permastruct( ) { + global $wp_rewrite; + + add_permastruct( 'foo', 'bar/%foo%' ); + $this->assertInternalType( 'array', $wp_rewrite->extra_permastructs['foo'] ); + $this->assertSame( '/bar/%foo%', $wp_rewrite->extra_permastructs['foo']['struct'] ); + + remove_permastruct( 'foo' ); + $this->assertFalse( isset( $wp_rewrite->extra_permastructs['foo'] ) ); + } +}