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
This commit is contained in:
Jb Audras
2022-08-09 10:21:32 +00:00
parent 11cc06cd22
commit 60493f3968
2 changed files with 36 additions and 1 deletions

View File

@@ -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 ] );
}
/**

View File

@@ -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' ) );