From f4d6d4e66ba5588647c6a7e58c0dcd40b503da6e Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Fri, 13 Feb 2015 16:42:36 +0000 Subject: [PATCH] Return a `WP_Error` if an empty name is provided when registering a post type. Fixes #31134 Props tyxla, MikeHansenMe git-svn-id: https://develop.svn.wordpress.org/trunk@31450 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/post.php | 6 +++--- tests/phpunit/tests/post/types.php | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 3d1227ff9a..1e5d3a27f0 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -1342,9 +1342,9 @@ function register_post_type( $post_type, $args = array() ) { $post_type = sanitize_key( $post_type ); $args->name = $post_type; - if ( strlen( $post_type ) > 20 ) { - _doing_it_wrong( __FUNCTION__, __( 'Post types cannot exceed 20 characters in length' ), '4.0' ); - return new WP_Error( 'post_type_too_long', __( 'Post types cannot exceed 20 characters in length' ) ); + if ( empty( $post_type ) || strlen( $post_type ) > 20 ) { + _doing_it_wrong( __FUNCTION__, __( 'Post type names must be between 1 and 20 characters in length.' ), '4.2' ); + return new WP_Error( 'post_type_length_invalid', __( 'Post type names must be between 1 and 20 characters in length.' ) ); } // If not set, default to the setting for public. diff --git a/tests/phpunit/tests/post/types.php b/tests/phpunit/tests/post/types.php index 0c8d95d298..e1582d5d8d 100644 --- a/tests/phpunit/tests/post/types.php +++ b/tests/phpunit/tests/post/types.php @@ -19,6 +19,26 @@ class Tests_Post_Types extends WP_UnitTestCase { _unregister_post_type( 'foo' ); } + /** + * @ticket 31134 + * + * @expectedIncorrectUsage register_post_type + */ + function test_register_post_type_length_long() { + // post type too long + $this->assertInstanceOf( 'WP_Error', register_post_type( 'abcdefghijklmnopqrstuvwxyz0123456789' ) ); + } + + /** + * @ticket 31134 + * + * @expectedIncorrectUsage register_post_type + */ + function test_register_post_type_length_short() { + // post type too short + $this->assertInstanceOf( 'WP_Error', register_post_type( '' ) ); + } + function test_register_taxonomy_for_object_type() { global $wp_taxonomies;