WordPress.org

Make WordPress Core


Ignore:
Timestamp:
03/03/2010 07:08:30 PM (12 years ago)
Author:
ryan
Message:

Improve user listing performance. Props miqrogroove. see #11914

File:
1 edited

Legend:

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

    r13424 r13576  
    36533653 */
    36543654function get_private_posts_cap_sql($post_type) {
    3655     global $user_ID;
    3656     $cap = '';
     3655    return get_posts_by_author_sql($post_type, FALSE);
     3656}
     3657
     3658/**
     3659 * Retrieve the post SQL based on capability, author, and type.
     3660 *
     3661 * See above for full description.
     3662 *
     3663 * @since 3.0.0
     3664 * @param string $post_type currently only supports 'post' or 'page'.
     3665 * @param bool $full Optional.  Returns a full WHERE statement instead of just an 'andalso' term.
     3666 * @param int $post_author Optional.  Query posts having a single author ID.
     3667 * @return string SQL WHERE code that can be added to a query.
     3668 */
     3669function get_posts_by_author_sql($post_type, $full = TRUE, $post_author = NULL) {
     3670    global $user_ID, $wpdb;
    36573671
    36583672    // Private posts
     
    36643678    // Dunno what it is, maybe plugins have their own post type?
    36653679    } else {
     3680        $cap = '';
    36663681        $cap = apply_filters('pub_priv_sql_capability', $cap);
    36673682
     
    36693684            // We don't know what it is, filters don't change anything,
    36703685            // so set the SQL up to return nothing.
    3671             return '1 = 0';
    3672         }
    3673     }
    3674 
    3675     $sql = '(post_status = \'publish\'';
     3686            return ' 1 = 0 ';
     3687        }
     3688    }
     3689
     3690    if ($full) {
     3691        if (is_null($post_author)) {
     3692            $sql = $wpdb->prepare('WHERE post_type = %s AND ', $post_type);
     3693        } else {
     3694            $sql = $wpdb->prepare('WHERE post_author = %d AND post_type = %s AND ', $post_author, $post_type);
     3695        }
     3696    } else {
     3697        $sql = '';
     3698    }
     3699
     3700    $sql .= "(post_status = 'publish'";
    36763701
    36773702    if (current_user_can($cap)) {
    36783703        // Does the user have the capability to view private posts? Guess so.
    3679         $sql .= ' OR post_status = \'private\'';
     3704        $sql .= " OR post_status = 'private'";
    36803705    } elseif (is_user_logged_in()) {
    36813706        // Users can view their own private posts.
    3682         $sql .= ' OR post_status = \'private\' AND post_author = \'' . $user_ID . '\'';
    3683     }
     3707        $id = (int) $user_ID;
     3708        if (is_null($post_author) || !$full) {
     3709            $sql .= " OR post_status = 'private' AND post_author = $id";
     3710        } elseif ($id == (int)$post_author) {
     3711            $sql .= " OR post_status = 'private'";
     3712        } // else none
     3713    } // else none
    36843714
    36853715    $sql .= ')';
Note: See TracChangeset for help on using the changeset viewer.