Make WordPress Core

Changeset 34100


Ignore:
Timestamp:
09/14/2015 12:02:05 AM (8 years ago)
Author:
wonderboymusic
Message:

Check if the $post_type passed to get_post_type_object() is a scalar value. Non-scalars were producing PHP warnings.

Adds unit tests.

Props Kloon.
Fixes #30013.

Location:
trunk
Files:
2 edited

Legend:

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

    r34085 r34100  
    836836    global $wp_post_types;
    837837
    838     if ( empty($wp_post_types[$post_type]) )
     838    if ( ! is_scalar( $post_type ) || empty( $wp_post_types[ $post_type ] ) ) {
    839839        return null;
    840 
    841     return $wp_post_types[$post_type];
     840    }
     841
     842    return $wp_post_types[ $post_type ];
    842843}
    843844
  • trunk/tests/phpunit/tests/post/types.php

    r33776 r34100  
    123123    }
    124124
     125
     126    /**
     127     * @ticket 30013
     128     */
     129    public function test_get_post_type_object_with_non_scalar_values() {
     130        $this->assertFalse( post_type_exists( 'foo' ) );
     131
     132        register_post_type( 'foo' );
     133
     134        $this->assertTrue( post_type_exists( 'foo' ) );
     135
     136        $this->assertNotNull( get_post_type_object( 'foo' ) );
     137        $this->assertNull( get_post_type_object( array() ) );
     138        $this->assertNull( get_post_type_object( array( 'foo' ) ) );
     139        $this->assertNull( get_post_type_object( new stdClass ) );
     140
     141        _unregister_post_type( 'foo' );
     142
     143        $this->assertFalse( post_type_exists( 'foo' ) );
     144    }
    125145}
Note: See TracChangeset for help on using the changeset viewer.