WordPress.org

Make WordPress Core

Changeset 22386


Ignore:
Timestamp:
11/05/2012 11:11:25 PM (6 years ago)
Author:
nacin
Message:

Add a $public_only argument to count_many_users_posts() and get_posts_by_author_sql(). Defaults to false, and allows the counts to be returned for only public posts. props ryan, westi. fixes #21431.

Location:
trunk/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/post.php

    r22357 r22386  
    44324432 * @param bool $full Optional. Returns a full WHERE statement instead of just an 'andalso' term.
    44334433 * @param int $post_author Optional. Query posts having a single author ID.
     4434 * @param bool $public_only Optional. Only return public posts. Skips cap checks for $current_user.  Default is false.
    44344435 * @return string SQL WHERE code that can be added to a query.
    44354436 */
    4436 function get_posts_by_author_sql( $post_type, $full = true, $post_author = null ) {
     4437function get_posts_by_author_sql( $post_type, $full = true, $post_author = null, $public_only = false ) {
    44374438    global $user_ID, $wpdb;
    44384439
     
    44584459    $sql .= "(post_status = 'publish'";
    44594460
    4460     if ( current_user_can( $cap ) ) {
    4461         // Does the user have the capability to view private posts? Guess so.
    4462         $sql .= " OR post_status = 'private'";
    4463     } elseif ( is_user_logged_in() ) {
    4464         // Users can view their own private posts.
    4465         $id = (int) $user_ID;
    4466         if ( null === $post_author || ! $full ) {
    4467             $sql .= " OR post_status = 'private' AND post_author = $id";
    4468         } elseif ( $id == (int) $post_author ) {
     4461    // Only need to check the cap if $public_only is false
     4462    if ( false === $public_only ) {
     4463        if ( current_user_can( $cap ) ) {
     4464            // Does the user have the capability to view private posts? Guess so.
    44694465            $sql .= " OR post_status = 'private'";
     4466        } elseif ( is_user_logged_in() ) {
     4467            // Users can view their own private posts.
     4468            $id = (int) $user_ID;
     4469            if ( null === $post_author || ! $full ) {
     4470                $sql .= " OR post_status = 'private' AND post_author = $id";
     4471            } elseif ( $id == (int) $post_author ) {
     4472                $sql .= " OR post_status = 'private'";
     4473            } // else none
    44704474        } // else none
    4471     } // else none
     4475    }
    44724476
    44734477    $sql .= ')';
  • trunk/wp-includes/user.php

    r22248 r22386  
    167167 * @param array $users Array of user IDs.
    168168 * @param string $post_type Optional. Post type to check. Defaults to post.
     169 * @param bool $public_only Optional. Only return counts for public posts.  Defaults to false.
    169170 * @return array Amount of posts each user has written.
    170171 */
    171 function count_many_users_posts( $users, $post_type = 'post' ) {
     172function count_many_users_posts( $users, $post_type = 'post', $public_only = false ) {
    172173    global $wpdb;
    173174
     
    177178
    178179    $userlist = implode( ',', array_map( 'absint', $users ) );
    179     $where = get_posts_by_author_sql( $post_type );
     180    $where = get_posts_by_author_sql( $post_type, true, null, $public_only );
    180181
    181182    $result = $wpdb->get_results( "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author", ARRAY_N );
Note: See TracChangeset for help on using the changeset viewer.