Make WordPress Core

Changeset 53869


Ignore:
Timestamp:
08/09/2022 10:21:32 AM (4 years ago)
Author:
audrasjb
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/taxonomy.php

    r53827 r53869  
    338338        global $wp_taxonomies;
    339339
    340         return isset( $wp_taxonomies[ $taxonomy ] );
     340        return is_string( $taxonomy ) && isset( $wp_taxonomies[ $taxonomy ] );
    341341}
    342342
  • trunk/tests/phpunit/tests/taxonomy.php

    r53669 r53869  
    129129        }
    130130
     131        /**
     132         * Tests that `taxonomy_exists()` returns `false` when the `$taxonomy`
     133         * argument is not a string.
     134         *
     135         * @ticket 56338
     136         *
     137         * @covers :taxonomy_exists
     138         *
     139         * @dataProvider data_taxonomy_exists_should_return_false_with_non_string_taxonomy
     140         *
     141         * @param mixed $taxonomy The non-string taxonomy.
     142         */
     143        public function test_taxonomy_exists_should_return_false_with_non_string_taxonomy( $taxonomy ) {
     144                $this->assertFalse( taxonomy_exists( $taxonomy ) );
     145        }
     146
     147        /**
     148         * Data provider with non-string values.
     149         *
     150         * @return array
     151         */
     152        public function data_taxonomy_exists_should_return_false_with_non_string_taxonomy() {
     153                return array(
     154                        'array'        => array( array() ),
     155                        'object'       => array( new stdClass() ),
     156                        'bool (true)'  => array( true ),
     157                        'bool (false)' => array( false ),
     158                        'null'         => array( null ),
     159                        'integer (0)'  => array( 0 ),
     160                        'integer (1)'  => array( 1 ),
     161                        'float (0.0)'  => array( 0.0 ),
     162                        'float (1.1)'  => array( 1.1 ),
     163                );
     164        }
     165
    131166        public function test_is_taxonomy_hierarchical() {
    132167                $this->assertTrue( is_taxonomy_hierarchical( 'category' ) );
Note: See TracChangeset for help on using the changeset viewer.