Ticket #32243: 32243.diff
File 32243.diff, 3.9 KB (added by , 10 years ago) |
---|
-
post.php
5330 5330 * 5331 5331 * @see get_private_posts_cap_sql() 5332 5332 * 5333 * @param string $post_type Post type.5333 * @param array $post_types Post type(s). 5334 5334 * @param bool $full Optional. Returns a full WHERE statement instead of just 5335 5335 * an 'andalso' term. Default true. 5336 5336 * @param int $post_author Optional. Query posts having a single author ID. Default null. … … 5338 5338 * $current_user. Default false. 5339 5339 * @return string SQL WHERE code that can be added to a query. 5340 5340 */ 5341 function get_posts_by_author_sql( $post_type , $full = true, $post_author = null, $public_only = false ) {5341 function get_posts_by_author_sql( $post_types, $full = true, $post_author = null, $public_only = false ) { 5342 5342 global $wpdb; 5343 5343 5344 5344 // Private posts. 5345 $post_type_obj = get_post_type_object( $post_type ); 5346 if ( ! $post_type_obj ) 5347 return $full ? 'WHERE 1 = 0' : ' 1 = 0 '; 5348 5345 $cap_final = true; 5346 foreach ($post_types as $post_type) { 5347 $post_type_obj = get_post_type_object( $post_type ); 5348 if ( ! $post_type_obj ) 5349 return $full ? 'WHERE 1 = 0' : ' 1 = 0 '; 5350 $cap_final = $cap_final && current_user_can( $post_type_obj->cap->read_private_posts ); 5351 } 5352 5349 5353 /** 5350 5354 * Filter the capability to read private posts for a custom post type 5351 5355 * when generating SQL for getting posts by author. … … 5355 5359 * 5356 5360 * @param string $cap Capability. 5357 5361 */ 5358 if ( ! $cap = apply_filters( 'pub_priv_sql_capability', '' ) ) { 5359 $cap = $post_type_obj->cap->read_private_posts; 5360 } 5362 if ( ! $cap = apply_filters( 'pub_priv_sql_capability', '' ) ) $cap = $cap_final; 5361 5363 5362 $sql = $wpdb->prepare( 'post_type = %s', $post_type ); 5364 $post_type_sql = array_fill(0, count($post_types), '%s'); 5365 $where = '(post_type = '.implode(' OR post_type = ', $post_type_sql).')'; 5366 $sql = $wpdb->prepare($where, $post_types); 5363 5367 5364 5368 if ( null !== $post_author ) { 5365 5369 $sql .= $wpdb->prepare( ' AND post_author = %d', $post_author ); … … 5368 5372 // Only need to check the cap if $public_only is false. 5369 5373 $post_status_sql = "post_status = 'publish'"; 5370 5374 if ( false === $public_only ) { 5371 if ( current_user_can( $cap )) {5375 if ( $cap ) { 5372 5376 // Does the user have the capability to view private posts? Guess so. 5373 5377 $post_status_sql .= " OR post_status = 'private'"; 5374 5378 } elseif ( is_user_logged_in() ) { -
user.php
254 254 * 255 255 * @global wpdb $wpdb WordPress database object for queries. 256 256 * 257 * @param int $useridUser ID.258 * @param string $post_type Optional. Post typeto count the number of posts for. Default 'post'.257 * @param int $userid User ID. 258 * @param array/string $post_types Optional. Post type(s) to count the number of posts for. Default 'post'. 259 259 * @return int Number of posts the user has written in this post type. 260 260 */ 261 function count_user_posts( $userid, $post_type = 'post' ) {261 function count_user_posts( $userid, $post_types = 'post' ) { 262 262 global $wpdb; 263 264 if ( is_string( $post_types ) ) $post_types = explode(',', $post_types); 263 265 264 $where = get_posts_by_author_sql( $post_type , true, $userid );266 $where = get_posts_by_author_sql( $post_types, true, $userid ); 265 267 266 268 $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" ); 267 269 … … 273 275 * 274 276 * @param int $count The user's post count. 275 277 * @param int $userid User ID. 276 * @param string $post_type Post typeto count the number of posts for.278 * @param array $post_types Post types to count the number of posts for. 277 279 */ 278 return apply_filters( 'get_usernumposts', $count, $userid, $post_type );280 return apply_filters( 'get_usernumposts', $count, $userid, $post_types ); 279 281 } 280 282 281 283 /**