WordPress.org

Make WordPress Core

Changeset 17742


Ignore:
Timestamp:
04/28/2011 11:27:39 AM (7 years ago)
Author:
nacin
Message:

Use the post type object in get_posts_by_author_sql() and add a post type parameter to count_many_users_posts(). The formerly somewhat useless and now totally useless pub_priv_sql_capability filter is considered deprecated. fixes #17220.

Location:
trunk/wp-includes
Files:
2 edited

Legend:

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

    r17698 r17742  
    40484048 *
    40494049 * This function provides a standardized way to appropriately select on the
    4050  * post_status of posts/pages. The function will return a piece of SQL code that
    4051  * can be added to a WHERE clause; this SQL is constructed to allow all
     4050 * post_status of a post type. The function will return a piece of SQL code
     4051 * that can be added to a WHERE clause; this SQL is constructed to allow all
    40524052 * published posts, and all private posts to which the user has access.
    40534053 *
    4054  * It also allows plugins that define their own post type to control the cap by
    4055  * using the hook 'pub_priv_sql_capability'. The plugin is expected to return
    4056  * the capability the user must have to read the private post type.
    4057  *
    40584054 * @since 2.2.0
    40594055 *
    40604056 * @uses $user_ID
    4061  * @uses apply_filters() Call 'pub_priv_sql_capability' filter for plugins with different post types.
    40624057 *
    40634058 * @param string $post_type currently only supports 'post' or 'page'.
    40644059 * @return string SQL code that can be added to a where clause.
    40654060 */
    4066 function get_private_posts_cap_sql($post_type) {
    4067     return get_posts_by_author_sql($post_type, FALSE);
     4061function get_private_posts_cap_sql( $post_type ) {
     4062    return get_posts_by_author_sql( $post_type, false );
    40684063}
    40694064
     
    40714066 * Retrieve the post SQL based on capability, author, and type.
    40724067 *
    4073  * See above for full description.
     4068 * @see get_private_posts_cap_sql() for full description.
    40744069 *
    40754070 * @since 3.0.0
    4076  * @param string $post_type currently only supports 'post' or 'page'.
     4071 * @param string $post_type Post type.
    40774072 * @param bool $full Optional.  Returns a full WHERE statement instead of just an 'andalso' term.
    40784073 * @param int $post_author Optional.  Query posts having a single author ID.
    40794074 * @return string SQL WHERE code that can be added to a query.
    40804075 */
    4081 function get_posts_by_author_sql($post_type, $full = TRUE, $post_author = NULL) {
     4076function get_posts_by_author_sql( $post_type, $full = true, $post_author = null ) {
    40824077    global $user_ID, $wpdb;
    40834078
    40844079    // Private posts
    4085     if ($post_type == 'post') {
    4086         $cap = 'read_private_posts';
    4087     // Private pages
    4088     } elseif ($post_type == 'page') {
    4089         $cap = 'read_private_pages';
    4090     // Dunno what it is, maybe plugins have their own post type?
    4091     } else {
    4092         $cap = '';
    4093         $cap = apply_filters('pub_priv_sql_capability', $cap);
    4094 
    4095         if (empty($cap)) {
    4096             // We don't know what it is, filters don't change anything,
    4097             // so set the SQL up to return nothing.
    4098             return ' 1 = 0 ';
    4099         }
    4100     }
    4101 
    4102     if ($full) {
    4103         if (is_null($post_author)) {
    4104             $sql = $wpdb->prepare('WHERE post_type = %s AND ', $post_type);
     4080    $post_type_obj = get_post_type_object( $post_type );
     4081    if ( ! $post_type_obj )
     4082        return ' 1 = 0 ';
     4083
     4084    // This hook is deprecated. Why you'd want to use it, I dunno.
     4085    if ( ! $cap = apply_filters( 'pub_priv_sql_capability', '' ) )
     4086        $cap = $post_type_obj->cap->read_private_posts;
     4087
     4088    if ( $full ) {
     4089        if ( null === $post_author ) {
     4090            $sql = $wpdb->prepare( 'WHERE post_type = %s AND ', $post_type );
    41054091        } else {
    4106             $sql = $wpdb->prepare('WHERE post_author = %d AND post_type = %s AND ', $post_author, $post_type);
     4092            $sql = $wpdb->prepare( 'WHERE post_author = %d AND post_type = %s AND ', $post_author, $post_type );
    41074093        }
    41084094    } else {
     
    41124098    $sql .= "(post_status = 'publish'";
    41134099
    4114     if (current_user_can($cap)) {
     4100    if ( current_user_can( $cap ) ) {
    41154101        // Does the user have the capability to view private posts? Guess so.
    41164102        $sql .= " OR post_status = 'private'";
    4117     } elseif (is_user_logged_in()) {
     4103    } elseif ( is_user_logged_in() ) {
    41184104        // Users can view their own private posts.
    41194105        $id = (int) $user_ID;
    4120         if (is_null($post_author) || !$full) {
     4106        if ( null === $post_author || ! $full ) {
    41214107            $sql .= " OR post_status = 'private' AND post_author = $id";
    4122         } elseif ($id == (int)$post_author) {
     4108        } elseif ( $id == (int) $post_author ) {
    41234109            $sql .= " OR post_status = 'private'";
    41244110        } // else none
  • trunk/wp-includes/user.php

    r17699 r17742  
    166166 *
    167167 * @since 3.0.0
    168  * @param array $users User ID number list.
     168 * @param array $user_ids Array of user IDs.
     169 * @param string|array $post_type Optional. Post type to check. Defaults to post.
    169170 * @return array Amount of posts each user has written.
    170171 */
    171 function count_many_users_posts($users) {
     172function count_many_users_posts($users, $post_type = 'post' ) {
    172173    global $wpdb;
    173174
    174175    $count = array();
    175     if ( ! is_array($users) || empty( $users ) )
     176    if ( empty( $users ) || ! is_array( $users ) )
    176177        return $count;
    177178
    178     $userlist = implode( ',', $users );
    179     $where = get_posts_by_author_sql( 'post' );
     179    $userlist = implode( ',', array_map( 'absint', $users ) );
     180    $where = get_posts_by_author_sql( $post_type );
    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.