From db3a7133f2893413ff4dde81dbb1c504b24ab2f3 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Sat, 12 Sep 2015 18:53:56 +0000 Subject: [PATCH] In `wp_insert_post()`, when setting `$post_author`, use `isset()` instead of `! empty()` to allow `0` to be passed as the value for `$post_author`. Adds unit tests. Props ericdaams, wonderboymusic. Fixes #32585. git-svn-id: https://develop.svn.wordpress.org/trunk@34085 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/post-functions.php | 2 +- tests/phpunit/tests/post.php | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/post-functions.php b/src/wp-includes/post-functions.php index 1d6ae6fc39..05663968de 100644 --- a/src/wp-includes/post-functions.php +++ b/src/wp-includes/post-functions.php @@ -3042,7 +3042,7 @@ function wp_insert_post( $postarr, $wp_error = false ) { // These variables are needed by compact() later. $post_content_filtered = $postarr['post_content_filtered']; - $post_author = empty( $postarr['post_author'] ) ? $user_id : $postarr['post_author']; + $post_author = isset( $postarr['post_author'] ) ? $postarr['post_author'] : $user_id; $ping_status = empty( $postarr['ping_status'] ) ? get_default_comment_status( $post_type, 'pingback' ) : $postarr['ping_status']; $to_ping = isset( $postarr['to_ping'] ) ? sanitize_trackback_urls( $postarr['to_ping'] ) : ''; $pinged = isset( $postarr['pinged'] ) ? $postarr['pinged'] : ''; diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php index d6ce0309e6..b3d7b0a73a 100644 --- a/tests/phpunit/tests/post.php +++ b/tests/phpunit/tests/post.php @@ -1200,4 +1200,22 @@ class Tests_Post extends WP_UnitTestCase { // Teardown wp_set_current_user( $old_uid ); } + + /** + * @ticket 32585 + */ + public function test_wp_insert_post_author_zero() { + $post_id = $this->factory->post->create( array( 'post_author' => 0 ) ); + + $this->assertEquals( 0, get_post( $post_id )->post_author ); + } + + /** + * @ticket 32585 + */ + public function test_wp_insert_post_author_null() { + $post_id = $this->factory->post->create( array( 'post_author' => null ) ); + + $this->assertEquals( $this->author_id, get_post( $post_id )->post_author ); + } }