Changeset 15723 for trunk/wp-includes/comment.php
- Timestamp:
- 10/04/2010 09:05:31 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/comment.php
r15623 r15723 189 189 */ 190 190 function get_comments( $args = '' ) { 191 global $wpdb; 192 193 $defaults = array( 194 'author_email' => '', 195 'ID' => '', 196 'karma' => '', 197 'number' => '', 198 'offset' => '', 199 'orderby' => '', 200 'order' => 'DESC', 201 'parent' => '', 202 'post_ID' => '', 203 'post_id' => 0, 204 'status' => '', 205 'type' => '', 206 'user_id' => '', 207 'search' => '', 208 'count' => false 209 ); 210 211 $args = wp_parse_args( $args, $defaults ); 212 extract( $args, EXTR_SKIP ); 213 214 // $args can be whatever, only use the args defined in defaults to compute the key 215 $key = md5( serialize( compact(array_keys($defaults)) ) ); 216 $last_changed = wp_cache_get('last_changed', 'comment'); 217 if ( !$last_changed ) { 218 $last_changed = time(); 219 wp_cache_set('last_changed', $last_changed, 'comment'); 220 } 221 $cache_key = "get_comments:$key:$last_changed"; 222 223 if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) { 224 return $cache; 225 } 226 227 $post_id = absint($post_id); 228 229 if ( 'hold' == $status ) 230 $approved = "comment_approved = '0'"; 231 elseif ( 'approve' == $status ) 232 $approved = "comment_approved = '1'"; 233 elseif ( 'spam' == $status ) 234 $approved = "comment_approved = 'spam'"; 235 elseif ( 'trash' == $status ) 236 $approved = "comment_approved = 'trash'"; 237 else 238 $approved = "( comment_approved = '0' OR comment_approved = '1' )"; 239 240 $order = ( 'ASC' == strtoupper($order) ) ? 'ASC' : 'DESC'; 241 242 if ( ! empty( $orderby ) ) { 243 $ordersby = is_array($orderby) ? $orderby : preg_split('/[,\s]/', $orderby); 244 $ordersby = array_intersect( 245 $ordersby, 246 array( 247 'comment_agent', 248 'comment_approved', 249 'comment_author', 250 'comment_author_email', 251 'comment_author_IP', 252 'comment_author_url', 253 'comment_content', 254 'comment_date', 255 'comment_date_gmt', 256 'comment_ID', 257 'comment_karma', 258 'comment_parent', 259 'comment_post_ID', 260 'comment_type', 261 'user_id', 262 ) 191 $query = new WP_Comment_Query; 192 return $query->query( $args ); 193 } 194 195 class WP_Comment_Query extends WP_Object_Query { 196 197 function query( $args ) { 198 global $wpdb; 199 200 $defaults = array( 201 'author_email' => '', 202 'ID' => '', 203 'karma' => '', 204 'number' => '', 205 'offset' => '', 206 'orderby' => '', 207 'order' => 'DESC', 208 'parent' => '', 209 'post_ID' => '', 210 'post_id' => 0, 211 'status' => '', 212 'type' => '', 213 'user_id' => '', 214 'search' => '', 215 'count' => false 263 216 ); 264 $orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby); 265 } else { 266 $orderby = 'comment_date_gmt'; 267 } 268 269 $number = absint($number); 270 $offset = absint($offset); 271 272 if ( !empty($number) ) { 273 if ( $offset ) 274 $limit = 'LIMIT ' . $offset . ',' . $number; 217 218 $args = wp_parse_args( $args, $defaults ); 219 extract( $args, EXTR_SKIP ); 220 221 // $args can be whatever, only use the args defined in defaults to compute the key 222 $key = md5( serialize( compact(array_keys($defaults)) ) ); 223 $last_changed = wp_cache_get('last_changed', 'comment'); 224 if ( !$last_changed ) { 225 $last_changed = time(); 226 wp_cache_set('last_changed', $last_changed, 'comment'); 227 } 228 $cache_key = "get_comments:$key:$last_changed"; 229 230 if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) { 231 return $cache; 232 } 233 234 $post_id = absint($post_id); 235 236 if ( 'hold' == $status ) 237 $approved = "comment_approved = '0'"; 238 elseif ( 'approve' == $status ) 239 $approved = "comment_approved = '1'"; 240 elseif ( 'spam' == $status ) 241 $approved = "comment_approved = 'spam'"; 242 elseif ( 'trash' == $status ) 243 $approved = "comment_approved = 'trash'"; 275 244 else 276 $limit = 'LIMIT ' . $number; 277 } else { 278 $limit = ''; 279 } 280 281 $post_where = "WHERE $approved"; 282 283 if ( ! empty($post_id) ) 284 $post_where .= $wpdb->prepare( ' AND comment_post_ID = %d', $post_id ); 285 if ( '' !== $author_email ) 286 $post_where .= $wpdb->prepare( 'AND comment_author_email = %s', $author_email ); 287 if ( '' !== $karma ) 288 $post_where .= $wpdb->prepare( 'AND comment_karma = %d', $karma ); 289 if ( 'comment' == $type ) 290 $post_where .= " AND comment_type = ''"; 291 elseif ( ! empty( $type ) ) 292 $post_where .= $wpdb->prepare( ' AND comment_type = %s', $type ); 293 if ( '' !== $parent ) 294 $post_where .= $wpdb->prepare( ' AND comment_parent = %d', $parent ); 295 if ( '' !== $user_id ) 296 $post_where .= $wpdb->prepare( ' AND user_id = %d', $user_id ); 297 if ( '' !== $search ) 298 $post_where .= _wp_search_sql($search, array('comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content')); 299 300 if ( $count ) 301 return $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->comments $post_where ORDER BY $orderby $order $limit" ); 302 303 $comments = $wpdb->get_results( "SELECT * FROM $wpdb->comments $post_where ORDER BY $orderby $order $limit" ); 304 305 wp_cache_add( $cache_key, $comments, 'comment' ); 306 307 return $comments; 245 $approved = "( comment_approved = '0' OR comment_approved = '1' )"; 246 247 $order = ( 'ASC' == strtoupper($order) ) ? 'ASC' : 'DESC'; 248 249 if ( ! empty( $orderby ) ) { 250 $ordersby = is_array($orderby) ? $orderby : preg_split('/[,\s]/', $orderby); 251 $ordersby = array_intersect( 252 $ordersby, 253 array( 254 'comment_agent', 255 'comment_approved', 256 'comment_author', 257 'comment_author_email', 258 'comment_author_IP', 259 'comment_author_url', 260 'comment_content', 261 'comment_date', 262 'comment_date_gmt', 263 'comment_ID', 264 'comment_karma', 265 'comment_parent', 266 'comment_post_ID', 267 'comment_type', 268 'user_id', 269 ) 270 ); 271 $orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby); 272 } else { 273 $orderby = 'comment_date_gmt'; 274 } 275 276 $number = absint($number); 277 $offset = absint($offset); 278 279 if ( !empty($number) ) { 280 if ( $offset ) 281 $limit = 'LIMIT ' . $offset . ',' . $number; 282 else 283 $limit = 'LIMIT ' . $number; 284 } else { 285 $limit = ''; 286 } 287 288 $post_where = "WHERE $approved"; 289 290 if ( ! empty($post_id) ) 291 $post_where .= $wpdb->prepare( ' AND comment_post_ID = %d', $post_id ); 292 if ( '' !== $author_email ) 293 $post_where .= $wpdb->prepare( 'AND comment_author_email = %s', $author_email ); 294 if ( '' !== $karma ) 295 $post_where .= $wpdb->prepare( 'AND comment_karma = %d', $karma ); 296 if ( 'comment' == $type ) 297 $post_where .= " AND comment_type = ''"; 298 elseif ( ! empty( $type ) ) 299 $post_where .= $wpdb->prepare( ' AND comment_type = %s', $type ); 300 if ( '' !== $parent ) 301 $post_where .= $wpdb->prepare( ' AND comment_parent = %d', $parent ); 302 if ( '' !== $user_id ) 303 $post_where .= $wpdb->prepare( ' AND user_id = %d', $user_id ); 304 if ( '' !== $search ) 305 $post_where .= $this->get_search_sql( $search, array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' ) ); 306 307 if ( $count ) 308 return $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->comments $post_where ORDER BY $orderby $order $limit" ); 309 310 $comments = $wpdb->get_results( "SELECT * FROM $wpdb->comments $post_where ORDER BY $orderby $order $limit" ); 311 312 wp_cache_add( $cache_key, $comments, 'comment' ); 313 314 return $comments; 315 } 308 316 } 309 317
Note: See TracChangeset
for help on using the changeset viewer.