From 5d0bc760409e546b100b213c4cc2e6e0b6f7ce68 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 14 Sep 2015 00:02:05 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@34100 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/post-functions.php | 5 +++-- tests/phpunit/tests/post/types.php | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/post-functions.php b/src/wp-includes/post-functions.php index 05663968de..1c410d7674 100644 --- a/src/wp-includes/post-functions.php +++ b/src/wp-includes/post-functions.php @@ -835,10 +835,11 @@ function get_post_type( $post = null ) { function get_post_type_object( $post_type ) { global $wp_post_types; - if ( empty($wp_post_types[$post_type]) ) + if ( ! is_scalar( $post_type ) || empty( $wp_post_types[ $post_type ] ) ) { return null; + } - return $wp_post_types[$post_type]; + return $wp_post_types[ $post_type ]; } /** diff --git a/tests/phpunit/tests/post/types.php b/tests/phpunit/tests/post/types.php index e1ead02003..5a1be694d8 100644 --- a/tests/phpunit/tests/post/types.php +++ b/tests/phpunit/tests/post/types.php @@ -122,4 +122,24 @@ class Tests_Post_Types extends WP_UnitTestCase { return $labels; } + + /** + * @ticket 30013 + */ + public function test_get_post_type_object_with_non_scalar_values() { + $this->assertFalse( post_type_exists( 'foo' ) ); + + register_post_type( 'foo' ); + + $this->assertTrue( post_type_exists( 'foo' ) ); + + $this->assertNotNull( get_post_type_object( 'foo' ) ); + $this->assertNull( get_post_type_object( array() ) ); + $this->assertNull( get_post_type_object( array( 'foo' ) ) ); + $this->assertNull( get_post_type_object( new stdClass ) ); + + _unregister_post_type( 'foo' ); + + $this->assertFalse( post_type_exists( 'foo' ) ); + } }