From 60493f396843e80d19111f6ee1bc7d3c2f5efed3 Mon Sep 17 00:00:00 2001 From: Jb Audras Date: Tue, 9 Aug 2022 10:21:32 +0000 Subject: [PATCH] Taxonomy: Prevent non string taxonomy names generating warnings or errors. This changeset adds an `is_string( $taxonomy )` check to the condition in `taxonomy_exists()`, to ensure `false` is returned when the `$taxonomy` is not a string. Follow-up to [35718]. Props costdev, peterwilsoncc, mukesh27. Fixes #56338. See #56336. git-svn-id: https://develop.svn.wordpress.org/trunk@53869 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/taxonomy.php | 2 +- tests/phpunit/tests/taxonomy.php | 35 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 76a9ada576..6374d73bfa 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -337,7 +337,7 @@ function get_taxonomy( $taxonomy ) { function taxonomy_exists( $taxonomy ) { global $wp_taxonomies; - return isset( $wp_taxonomies[ $taxonomy ] ); + return is_string( $taxonomy ) && isset( $wp_taxonomies[ $taxonomy ] ); } /** diff --git a/tests/phpunit/tests/taxonomy.php b/tests/phpunit/tests/taxonomy.php index 69dc1a8180..a4763fff9c 100644 --- a/tests/phpunit/tests/taxonomy.php +++ b/tests/phpunit/tests/taxonomy.php @@ -128,6 +128,41 @@ class Tests_Taxonomy extends WP_UnitTestCase { $this->assertFalse( taxonomy_exists( null ) ); } + /** + * Tests that `taxonomy_exists()` returns `false` when the `$taxonomy` + * argument is not a string. + * + * @ticket 56338 + * + * @covers :taxonomy_exists + * + * @dataProvider data_taxonomy_exists_should_return_false_with_non_string_taxonomy + * + * @param mixed $taxonomy The non-string taxonomy. + */ + public function test_taxonomy_exists_should_return_false_with_non_string_taxonomy( $taxonomy ) { + $this->assertFalse( taxonomy_exists( $taxonomy ) ); + } + + /** + * Data provider with non-string values. + * + * @return array + */ + public function data_taxonomy_exists_should_return_false_with_non_string_taxonomy() { + return array( + 'array' => array( array() ), + 'object' => array( new stdClass() ), + 'bool (true)' => array( true ), + 'bool (false)' => array( false ), + 'null' => array( null ), + 'integer (0)' => array( 0 ), + 'integer (1)' => array( 1 ), + 'float (0.0)' => array( 0.0 ), + 'float (1.1)' => array( 1.1 ), + ); + } + public function test_is_taxonomy_hierarchical() { $this->assertTrue( is_taxonomy_hierarchical( 'category' ) ); $this->assertFalse( is_taxonomy_hierarchical( 'post_tag' ) );