From e50296de48bf131ce0a69b9b906486e8afa8f1f5 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 5 Jan 2016 20:56:10 +0000 Subject: [PATCH] Query: Add a `WP::remove_query_var()` helper function. This makes cleaning up public query vars easier. Fixes #35234. git-svn-id: https://develop.svn.wordpress.org/trunk@36177 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp.php | 11 +++++++++++ tests/phpunit/tests/wp.php | 37 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 tests/phpunit/tests/wp.php diff --git a/src/wp-includes/class-wp.php b/src/wp-includes/class-wp.php index f2de5f4143..14beeaa338 100644 --- a/src/wp-includes/class-wp.php +++ b/src/wp-includes/class-wp.php @@ -95,6 +95,17 @@ class WP { $this->public_query_vars[] = $qv; } + /** + * Remove name from list of public query variables. + * + * @since 4.5.0 + * + * @param string $name Query variable name. + */ + public function remove_query_var( $name ) { + $this->public_query_vars = array_diff( $this->public_query_vars, array( $name ) ); + } + /** * Set the value of a query variable. * diff --git a/tests/phpunit/tests/wp.php b/tests/phpunit/tests/wp.php new file mode 100644 index 0000000000..df5cdfcf37 --- /dev/null +++ b/tests/phpunit/tests/wp.php @@ -0,0 +1,37 @@ +wp = new WP(); + } + + public function test_add_query_var() { + $public_qv_count = count( $this->wp->public_query_vars ); + + $this->wp->add_query_var( 'test' ); + $this->wp->add_query_var( 'test2' ); + $this->wp->add_query_var( 'test' ); + + $this->assertSame( $public_qv_count + 2, count( $this->wp->public_query_vars ) ); + $this->assertTrue( in_array( 'test', $this->wp->public_query_vars ) ); + $this->assertTrue( in_array( 'test2', $this->wp->public_query_vars ) ); + } + + public function test_remove_query_var() { + $public_qv_count = count( $this->wp->public_query_vars ); + + $this->wp->add_query_var( 'test' ); + $this->assertTrue( in_array( 'test', $this->wp->public_query_vars ) ); + $this->wp->remove_query_var( 'test' ); + + $this->assertSame( $public_qv_count, count( $this->wp->public_query_vars ) ); + } +}