From efdce1da5139e767c79665fa498936abcf2c1791 Mon Sep 17 00:00:00 2001 From: Konstantin Obenland Date: Thu, 2 Jul 2015 00:21:28 +0000 Subject: [PATCH] Turn of comments for pages by default. Pages are static content, for which comments are not expected out of the box. Props valendesigns, rachelbaker. Fixes #31168. git-svn-id: https://develop.svn.wordpress.org/trunk@33041 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/post.php | 73 ++++++++++++++++++++++++++++++++---- tests/phpunit/tests/post.php | 73 ++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index a26efec2a8..52600b847e 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -3127,11 +3127,25 @@ function wp_insert_post( $postarr, $wp_error = false ) { $user_id = get_current_user_id(); - $defaults = array('post_status' => 'draft', 'post_type' => 'post', 'post_author' => $user_id, - 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0, - 'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '', - 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0, - 'post_content' => '', 'post_title' => '', 'context' => ''); + $defaults = array( + 'post_author' => $user_id, + 'post_content' => '', + 'post_content_filtered' => '', + 'post_title' => '', + 'post_excerpt' => '', + 'post_status' => 'draft', + 'post_type' => 'post', + 'comment_status' => '', + 'ping_status' => '', + 'post_password' => '', + 'to_ping' => '', + 'pinged' => '', + 'post_parent' => 0, + 'menu_order' => 0, + 'guid' => '', + 'import_id' => 0, + 'context' => '', + ); $postarr = wp_parse_args($postarr, $defaults); @@ -3302,11 +3316,12 @@ function wp_insert_post( $postarr, $wp_error = false ) { } } + // Comment status. if ( empty( $postarr['comment_status'] ) ) { if ( $update ) { $comment_status = 'closed'; } else { - $comment_status = get_option('default_comment_status'); + $comment_status = get_default_comment_status( $post_type ); } } else { $comment_status = $postarr['comment_status']; @@ -3315,7 +3330,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']; - $ping_status = empty( $postarr['ping_status'] ) ? get_option( 'default_ping_status' ) : $postarr['ping_status']; + $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'] : ''; $import_id = isset( $postarr['import_id'] ) ? $postarr['import_id'] : 0; @@ -4056,9 +4071,51 @@ function wp_transition_post_status( $new_status, $old_status, $post ) { } // -// Trackback and ping functions +// Comment, trackback, and pingback functions. // +/** + * Get the default comment status for a post type. + * + * @since 4.3.0 + * + * @param string $post_type Optional. Post type. Default 'post'. + * @param string $comment_type Optional. Comment type. Default 'comment'. + * @return string Expected return value is 'open' or 'closed'. + */ +function get_default_comment_status( $post_type = 'post', $comment_type = 'comment' ) { + switch ( $comment_type ) { + case 'pingback' : + case 'trackback' : + $supports = 'trackbacks'; + $option = 'ping'; + break; + default : + $supports = 'comments'; + $option = 'comment'; + } + + // Set the status. + if ( 'page' === $post_type ) { + $status = 'closed'; + } elseif ( post_type_supports( $post_type, $supports ) ) { + $status = get_option( "default_{$option}_status" ); + } else { + $status = 'closed'; + } + + /** + * Filter the default comment status for the given post type. + * + * @since 4.3.0 + * + * @param string $status Default status for the given post type, + * either 'open' or 'closed'. + * @param string $comment_type Type of comment. Default is `comment`. + */ + return apply_filters( "get_{$post_type}_default_comment_status", $status, $comment_type ); +} + /** * Add a URL to those already pinged. * diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php index 2c80735b80..86810b9a38 100644 --- a/tests/phpunit/tests/post.php +++ b/tests/phpunit/tests/post.php @@ -950,4 +950,77 @@ class Tests_Post extends WP_UnitTestCase { $this->assertEquals( $value, $post->$field ); } } + + /** + * @ticket 31168 + */ + function test_wp_insert_post_default_comment_ping_status_open() { + $post_id = $this->factory->post->create( array( + 'post_author' => $this->author_id, + 'post_status' => 'public', + 'post_content' => rand_str(), + 'post_title' => rand_str(), + ) ); + $post = get_post( $post_id ); + + $this->assertEquals( 'open', $post->comment_status ); + $this->assertEquals( 'open', $post->ping_status ); + } + + /** + * @ticket 31168 + */ + function test_wp_insert_post_page_default_comment_ping_status_closed() { + $post_id = $this->factory->post->create( array( + 'post_author' => $this->author_id, + 'post_status' => 'public', + 'post_content' => rand_str(), + 'post_title' => rand_str(), + 'post_type' => 'page', + ) ); + $post = get_post( $post_id ); + + $this->assertEquals( 'closed', $post->comment_status ); + $this->assertEquals( 'closed', $post->ping_status ); + } + + /** + * @ticket 31168 + */ + function test_wp_insert_post_cpt_default_comment_ping_status_open() { + $post_type = rand_str(20); + register_post_type( $post_type, array( 'supports' => array( 'comments', 'trackbacks' ) ) ); + $post_id = $this->factory->post->create( array( + 'post_author' => $this->author_id, + 'post_status' => 'public', + 'post_content' => rand_str(), + 'post_title' => rand_str(), + 'post_type' => $post_type, + ) ); + $post = get_post( $post_id ); + + $this->assertEquals( 'open', $post->comment_status ); + $this->assertEquals( 'open', $post->ping_status ); + _unregister_post_type( $post_type ); + } + + /** + * @ticket 31168 + */ + function test_wp_insert_post_cpt_default_comment_ping_status_closed() { + $post_type = rand_str(20); + register_post_type( $post_type ); + $post_id = $this->factory->post->create( array( + 'post_author' => $this->author_id, + 'post_status' => 'public', + 'post_content' => rand_str(), + 'post_title' => rand_str(), + 'post_type' => $post_type, + ) ); + $post = get_post( $post_id ); + + $this->assertEquals( 'closed', $post->comment_status ); + $this->assertEquals( 'closed', $post->ping_status ); + _unregister_post_type( $post_type ); + } }