Make WordPress Core

Ticket #31168: 31168.5.diff

File 31168.5.diff, 6.0 KB (added by rachelbaker, 10 years ago)
  • src/wp-includes/post.php

     
    31273127
    31283128        $user_id = get_current_user_id();
    31293129
    3130         $defaults = array('post_status' => 'draft', 'post_type' => 'post', 'post_author' => $user_id,
    3131                 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0,
    3132                 'menu_order' => 0, 'to_ping' =>  '', 'pinged' => '', 'post_password' => '',
    3133                 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0,
    3134                 'post_content' => '', 'post_title' => '', 'context' => '');
     3130        $defaults = array(
     3131                'post_author' => $user_id,
     3132                'post_content' => '',
     3133                'post_content_filtered' => '',
     3134                'post_title' => '',
     3135                'post_excerpt' => '',
     3136                'post_status' => 'draft',
     3137                'post_type' => 'post',
     3138                'comment_status' => '',
     3139                'ping_status' => '',
     3140                'post_password' => '',
     3141                'to_ping' =>  '',
     3142                'pinged' => '',
     3143                'post_parent' => 0,
     3144                'menu_order' => 0,
     3145                'guid' => '',
     3146                'import_id' => 0,
     3147                'context' => '',
     3148        );
    31353149
    31363150        $postarr = wp_parse_args($postarr, $defaults);
    31373151
     
    33023316                }
    33033317        }
    33043318
     3319        // Comment status.
    33053320        if ( empty( $postarr['comment_status'] ) ) {
    33063321                if ( $update ) {
    33073322                        $comment_status = 'closed';
    33083323                } else {
    3309                         $comment_status = get_option('default_comment_status');
     3324                        $comment_status = get_default_comment_status( $post_type );
    33103325                }
    33113326        } else {
    33123327                $comment_status = $postarr['comment_status'];
     
    33153330        // These variables are needed by compact() later.
    33163331        $post_content_filtered = $postarr['post_content_filtered'];
    33173332        $post_author = empty( $postarr['post_author'] ) ? $user_id : $postarr['post_author'];
    3318         $ping_status = empty( $postarr['ping_status'] ) ? get_option( 'default_ping_status' ) : $postarr['ping_status'];
     3333        $ping_status = empty( $postarr['ping_status'] ) ? get_default_comment_status( $post_type, 'pingback' ) : $postarr['ping_status'];
    33193334        $to_ping = isset( $postarr['to_ping'] ) ? sanitize_trackback_urls( $postarr['to_ping'] ) : '';
    33203335        $pinged = isset( $postarr['pinged'] ) ? $postarr['pinged'] : '';
    33213336        $import_id = isset( $postarr['import_id'] ) ? $postarr['import_id'] : 0;
     
    40564071}
    40574072
    40584073//
    4059 // Trackback and ping functions
     4074// Comment, trackback, and pingback functions.
    40604075//
    40614076
    40624077/**
     4078 * Get the default comment status for a post type.
     4079 *
     4080 * @since 4.3.0
     4081 *
     4082 * @param  string $post_type    Optional. Post type. Default 'post'.
     4083 * @param  string $comment_type Optional. Comment type. Default ''.
     4084 * @return string Expected return value is 'open' or 'closed'.
     4085 */
     4086function get_default_comment_status( $post_type = 'post', $comment_type = '' ) {
     4087        if ( empty( $comment_type ) ) {
     4088                $comment_type = 'comment';
     4089        }
     4090
     4091        switch ( $comment_type ) {
     4092                case 'pingback' :
     4093                case 'trackback' :
     4094                        $supports = 'trackbacks';
     4095                        $option = 'ping';
     4096                        break;
     4097                default :
     4098                        $supports = 'comments';
     4099                        $option = 'comment';
     4100        }
     4101
     4102        // Set the status.
     4103        if ( 'page' === $post_type ) {
     4104                $status = 'closed';
     4105        } elseif ( post_type_supports( $post_type, $supports ) ) {
     4106                $status = get_option( "default_{$option}_status" );
     4107        } else {
     4108                $status = 'closed';
     4109        }
     4110
     4111        /**
     4112         * Filter the default comment status for the given post_type.
     4113         *
     4114         * @since 4.3.0
     4115         *
     4116         * @param string $status       Default status for the given post_type,
     4117         *                             either 'open' or 'closed'.
     4118         * @param string $comment_type Type of comment. Default is `comment`.
     4119         */
     4120        return apply_filters( "get_{$post_type}_default_comment_status", $status, $comment_type );
     4121}
     4122
     4123/**
    40634124 * Add a URL to those already pinged.
    40644125 *
    40654126 * @since 1.5.0
  • tests/phpunit/tests/post.php

     
    950950                        $this->assertEquals( $value, $post->$field );
    951951                }
    952952        }
     953
     954        /**
     955         * @ticket 31168
     956         */
     957        function test_wp_insert_post_default_comment_ping_status_open() {
     958                $post_id = $this->factory->post->create( array(
     959                        'post_author' => $this->author_id,
     960                        'post_status' => 'public',
     961                        'post_content' => rand_str(),
     962                        'post_title' => rand_str(),
     963                ) );
     964                $post = get_post( $post_id );
     965
     966                $this->assertEquals( 'open', $post->comment_status );
     967                $this->assertEquals( 'open', $post->ping_status );
     968        }
     969
     970        /**
     971         * @ticket 31168
     972         */
     973        function test_wp_insert_post_page_default_comment_ping_status_closed() {
     974                $post_id = $this->factory->post->create( array(
     975                        'post_author' => $this->author_id,
     976                        'post_status' => 'public',
     977                        'post_content' => rand_str(),
     978                        'post_title' => rand_str(),
     979                        'post_type' => 'page',
     980                ) );
     981                $post = get_post( $post_id );
     982
     983                $this->assertEquals( 'closed', $post->comment_status );
     984                $this->assertEquals( 'closed', $post->ping_status );
     985        }
     986
     987        /**
     988         * @ticket 31168
     989         */
     990        function test_wp_insert_post_cpt_default_comment_ping_status_open() {
     991                $post_type = rand_str(20);
     992                register_post_type( $post_type, array( 'supports' => array( 'comments', 'trackbacks' ) ) );
     993                $post_id = $this->factory->post->create( array(
     994                        'post_author' => $this->author_id,
     995                        'post_status' => 'public',
     996                        'post_content' => rand_str(),
     997                        'post_title' => rand_str(),
     998                        'post_type' => $post_type,
     999                ) );
     1000                $post = get_post( $post_id );
     1001
     1002                $this->assertEquals( 'open', $post->comment_status );
     1003                $this->assertEquals( 'open', $post->ping_status );
     1004                _unregister_post_type( $post_type );
     1005        }
     1006
     1007        /**
     1008         * @ticket 31168
     1009         */
     1010        function test_wp_insert_post_cpt_default_comment_ping_status_closed() {
     1011                $post_type = rand_str(20);
     1012                register_post_type( $post_type );
     1013                $post_id = $this->factory->post->create( array(
     1014                        'post_author' => $this->author_id,
     1015                        'post_status' => 'public',
     1016                        'post_content' => rand_str(),
     1017                        'post_title' => rand_str(),
     1018                        'post_type' => $post_type,
     1019                ) );
     1020                $post = get_post( $post_id );
     1021
     1022                $this->assertEquals( 'closed', $post->comment_status );
     1023                $this->assertEquals( 'closed', $post->ping_status );
     1024                _unregister_post_type( $post_type );
     1025        }
    9531026}