From edffcb0bcd6f4d8b02a25c97b47d4d179d3b0f8e Mon Sep 17 00:00:00 2001 From: Konstantin Obenland Date: Thu, 2 Jul 2015 00:22:24 +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. Built from https://develop.svn.wordpress.org/trunk@33041 git-svn-id: http://core.svn.wordpress.org/trunk@33012 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/post.php | 73 ++++++++++++++++++++++++++++++++++++----- wp-includes/version.php | 2 +- 2 files changed, 66 insertions(+), 9 deletions(-) diff --git a/wp-includes/post.php b/wp-includes/post.php index a26efec2a8..52600b847e 100644 --- a/wp-includes/post.php +++ b/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/wp-includes/version.php b/wp-includes/version.php index f96d12450b..8cdbbbfa85 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.3-alpha-33040'; +$wp_version = '4.3-alpha-33041'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.