Changeset 17742
- Timestamp:
- 04/28/2011 11:27:39 AM (14 years ago)
- Location:
- trunk/wp-includes
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/post.php
r17698 r17742 4048 4048 * 4049 4049 * 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 that4051 * can be added to a WHERE clause; this SQL is constructed to allow all4050 * 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 4052 4052 * published posts, and all private posts to which the user has access. 4053 4053 * 4054 * It also allows plugins that define their own post type to control the cap by4055 * using the hook 'pub_priv_sql_capability'. The plugin is expected to return4056 * the capability the user must have to read the private post type.4057 *4058 4054 * @since 2.2.0 4059 4055 * 4060 4056 * @uses $user_ID 4061 * @uses apply_filters() Call 'pub_priv_sql_capability' filter for plugins with different post types.4062 4057 * 4063 4058 * @param string $post_type currently only supports 'post' or 'page'. 4064 4059 * @return string SQL code that can be added to a where clause. 4065 4060 */ 4066 function get_private_posts_cap_sql( $post_type) {4067 return get_posts_by_author_sql( $post_type, FALSE);4061 function get_private_posts_cap_sql( $post_type ) { 4062 return get_posts_by_author_sql( $post_type, false ); 4068 4063 } 4069 4064 … … 4071 4066 * Retrieve the post SQL based on capability, author, and type. 4072 4067 * 4073 * See abovefor full description.4068 * @see get_private_posts_cap_sql() for full description. 4074 4069 * 4075 4070 * @since 3.0.0 4076 * @param string $post_type currently only supports 'post' or 'page'.4071 * @param string $post_type Post type. 4077 4072 * @param bool $full Optional. Returns a full WHERE statement instead of just an 'andalso' term. 4078 4073 * @param int $post_author Optional. Query posts having a single author ID. 4079 4074 * @return string SQL WHERE code that can be added to a query. 4080 4075 */ 4081 function get_posts_by_author_sql( $post_type, $full = TRUE, $post_author = NULL) {4076 function get_posts_by_author_sql( $post_type, $full = true, $post_author = null ) { 4082 4077 global $user_ID, $wpdb; 4083 4078 4084 4079 // 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 ); 4105 4091 } 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 ); 4107 4093 } 4108 4094 } else { … … 4112 4098 $sql .= "(post_status = 'publish'"; 4113 4099 4114 if ( current_user_can($cap)) {4100 if ( current_user_can( $cap ) ) { 4115 4101 // Does the user have the capability to view private posts? Guess so. 4116 4102 $sql .= " OR post_status = 'private'"; 4117 } elseif ( is_user_logged_in()) {4103 } elseif ( is_user_logged_in() ) { 4118 4104 // Users can view their own private posts. 4119 4105 $id = (int) $user_ID; 4120 if ( is_null($post_author) || !$full) {4106 if ( null === $post_author || ! $full ) { 4121 4107 $sql .= " OR post_status = 'private' AND post_author = $id"; 4122 } elseif ( $id == (int)$post_author) {4108 } elseif ( $id == (int) $post_author ) { 4123 4109 $sql .= " OR post_status = 'private'"; 4124 4110 } // else none -
trunk/wp-includes/user.php
r17699 r17742 166 166 * 167 167 * @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. 169 170 * @return array Amount of posts each user has written. 170 171 */ 171 function count_many_users_posts($users ) {172 function count_many_users_posts($users, $post_type = 'post' ) { 172 173 global $wpdb; 173 174 174 175 $count = array(); 175 if ( ! is_array($users) || empty( $users ) )176 if ( empty( $users ) || ! is_array( $users ) ) 176 177 return $count; 177 178 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 ); 180 181 181 182 $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.