Make WordPress Core

Ticket #21364: 21364.diff

File 21364.diff, 2.3 KB (added by DrewAPicture, 10 years ago)

+ unit test

  • src/wp-includes/user.php

     
    250250 * Number of posts user has written.
    251251 *
    252252 * @since 3.0.0
     253 * @since 4.0.0 Added $post_type parameter.
    253254 *
    254255 * @global wpdb $wpdb WordPress database object for queries.
    255256 *
    256  * @param int $userid User ID.
    257  * @return int Amount of posts user has written.
     257 * @param int    $userid    User ID.
     258 * @param string $post_type Optional. Post type to count the number of posts for. Default 'post'.
     259 * @return int Number of posts the user has written in this post type.
    258260 */
    259 function count_user_posts($userid) {
     261function count_user_posts( $userid, $post_type = 'post' ) {
    260262        global $wpdb;
    261263
    262         $where = get_posts_by_author_sql('post', true, $userid);
     264        $where = get_posts_by_author_sql( $post_type, true, $userid );
    263265
    264266        $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
    265267
     
    267269         * Filter the number of posts a user has written.
    268270         *
    269271         * @since 2.7.0
     272         * @since 4.0.0 Added $post_type parameter.
    270273         *
    271          * @param int $count  The user's post count.
    272          * @param int $userid User ID.
     274         * @param int    $count     The user's post count.
     275         * @param int    $userid    User ID.
     276         * @param string $post_type Post type to count the number of posts for.
    273277         */
    274         return apply_filters( 'get_usernumposts', $count, $userid );
     278        return apply_filters( 'get_usernumposts', $count, $userid, $post_type );
    275279}
    276280
    277281/**
  • tests/phpunit/tests/user.php

     
    616616        }
    617617
    618618        /**
     619         * @ticket 21364
     620         */
     621        function test_count_user_posts() {
     622                $post_type = rand_str( 10 );
     623                register_post_type( $post_type );
     624
     625                $user_id = $this->factory->user->create( array( 'role' => 'author' ) );
     626
     627                wp_set_current_user( $user_id );
     628                $this->factory->post->create_many( 15, array(
     629                        'post_author' => $user_id,
     630                        'post_type'   => $post_type
     631                ) );
     632
     633                $count = count_user_posts( $user_id, $post_type );
     634                $this->assertEquals( 15, $count );
     635        }
     636
     637        /**
    619638         * @ticket 22858
    620639         */
    621640        function test_wp_update_user_on_nonexistent_users() {