220 'post_type' => '', |
228 'post_type' => '', |
221 'status' => '', |
229 'status' => '', |
222 'type' => '', |
230 'type' => '', |
223 'user_id' => '', |
231 'user_id' => '', |
224 'search' => '', |
232 'search' => '', |
225 'count' => false |
233 'count' => false, |
|
234 'meta_key' => '', |
|
235 'meta_value' => '', |
|
236 'meta_query' => '', |
226 ); |
237 ); |
227 |
238 |
|
239 $groupby = ''; |
|
240 |
228 $this->query_vars = wp_parse_args( $query_vars, $defaults ); |
241 $this->query_vars = wp_parse_args( $query_vars, $defaults ); |
|
242 |
|
243 // Parse meta query |
|
244 $this->meta_query = new WP_Meta_Query(); |
|
245 $this->meta_query->parse_query_vars( $this->query_vars ); |
|
246 |
229 do_action_ref_array( 'pre_get_comments', array( &$this ) ); |
247 do_action_ref_array( 'pre_get_comments', array( &$this ) ); |
230 extract( $this->query_vars, EXTR_SKIP ); |
248 extract( $this->query_vars, EXTR_SKIP ); |
231 |
249 |
232 // $args can be whatever, only use the args defined in defaults to compute the key |
250 // $args can be whatever, only use the args defined in defaults to compute the key |
233 $key = md5( serialize( compact(array_keys($defaults)) ) ); |
251 $key = md5( serialize( compact(array_keys($defaults)) ) ); |
234 $last_changed = wp_cache_get('last_changed', 'comment'); |
252 $last_changed = wp_cache_get( 'last_changed', 'comment' ); |
235 if ( !$last_changed ) { |
253 if ( ! $last_changed ) |
236 $last_changed = time(); |
254 $last_changed = wp_cache_set( 'last_changed', 1, 'comment' ); |
237 wp_cache_set('last_changed', $last_changed, 'comment'); |
|
238 } |
|
239 $cache_key = "get_comments:$key:$last_changed"; |
255 $cache_key = "get_comments:$key:$last_changed"; |
240 |
256 |
241 if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) { |
257 if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) |
242 return $cache; |
258 return $cache; |
243 } |
|
244 |
259 |
245 $post_id = absint($post_id); |
260 $post_id = absint($post_id); |
246 |
261 |
247 if ( 'hold' == $status ) |
262 if ( 'hold' == $status ) |
248 $approved = "comment_approved = '0'"; |
263 $approved = "comment_approved = '0'"; |
249 elseif ( 'approve' == $status ) |
264 elseif ( 'approve' == $status ) |
250 $approved = "comment_approved = '1'"; |
265 $approved = "comment_approved = '1'"; |
251 elseif ( 'spam' == $status ) |
266 elseif ( ! empty( $status ) && 'all' != $status ) |
252 $approved = "comment_approved = 'spam'"; |
267 $approved = $wpdb->prepare( "comment_approved = %s", $status ); |
253 elseif ( 'trash' == $status ) |
|
254 $approved = "comment_approved = 'trash'"; |
|
255 else |
268 else |
256 $approved = "( comment_approved = '0' OR comment_approved = '1' )"; |
269 $approved = "( comment_approved = '0' OR comment_approved = '1' )"; |
257 |
270 |
258 $order = ( 'ASC' == strtoupper($order) ) ? 'ASC' : 'DESC'; |
271 $order = ( 'ASC' == strtoupper($order) ) ? 'ASC' : 'DESC'; |
259 |
272 |
260 if ( ! empty( $orderby ) ) { |
273 if ( ! empty( $orderby ) ) { |
261 $ordersby = is_array($orderby) ? $orderby : preg_split('/[,\s]/', $orderby); |
274 $ordersby = is_array($orderby) ? $orderby : preg_split('/[,\s]/', $orderby); |
262 $ordersby = array_intersect( |
275 $allowed_keys = array( |
263 $ordersby, |
276 'comment_agent', |
264 array( |
277 'comment_approved', |
265 'comment_agent', |
278 'comment_author', |
266 'comment_approved', |
279 'comment_author_email', |
267 'comment_author', |
280 'comment_author_IP', |
268 'comment_author_email', |
281 'comment_author_url', |
269 'comment_author_IP', |
282 'comment_content', |
270 'comment_author_url', |
283 'comment_date', |
271 'comment_content', |
284 'comment_date_gmt', |
272 'comment_date', |
285 'comment_ID', |
273 'comment_date_gmt', |
286 'comment_karma', |
274 'comment_ID', |
287 'comment_parent', |
275 'comment_karma', |
288 'comment_post_ID', |
276 'comment_parent', |
289 'comment_type', |
277 'comment_post_ID', |
290 'user_id', |
278 'comment_type', |
|
279 'user_id', |
|
280 ) |
|
281 ); |
291 ); |
|
292 if ( ! empty( $this->query_vars['meta_key'] ) ) { |
|
293 $allowed_keys[] = $q['meta_key']; |
|
294 $allowed_keys[] = 'meta_value'; |
|
295 $allowed_keys[] = 'meta_value_num'; |
|
296 } |
|
297 $ordersby = array_intersect( $ordersby, $allowed_keys ); |
|
298 foreach ( $ordersby as $key => $value ) { |
|
299 if ( $value == $q['meta_key'] || $value == 'meta_value' ) { |
|
300 $ordersby[ $key ] = "$wpdb->commentmeta.meta_value"; |
|
301 } elseif ( $value == 'meta_value_num' ) { |
|
302 $ordersby[ $key ] = "$wpdb->commentmeta.meta_value+0"; |
|
303 } |
|
304 } |
282 $orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby); |
305 $orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby); |
283 } else { |
306 } else { |
284 $orderby = 'comment_date_gmt'; |
307 $orderby = 'comment_date_gmt'; |
285 } |
308 } |
286 |
309 |
329 $join = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID"; |
352 $join = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID"; |
330 foreach( $post_fields as $field_name => $field_value ) |
353 foreach( $post_fields as $field_name => $field_value ) |
331 $where .= $wpdb->prepare( " AND {$wpdb->posts}.{$field_name} = %s", $field_value ); |
354 $where .= $wpdb->prepare( " AND {$wpdb->posts}.{$field_name} = %s", $field_value ); |
332 } |
355 } |
333 |
356 |
334 $pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits' ); |
357 if ( ! empty( $this->meta_query->queries ) ) { |
|
358 $clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this ); |
|
359 $join .= $clauses['join']; |
|
360 $where .= $clauses['where']; |
|
361 $groupby = "{$wpdb->comments}.comment_ID"; |
|
362 } |
|
363 |
|
364 $pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits', 'groupby' ); |
335 $clauses = apply_filters_ref_array( 'comments_clauses', array( compact( $pieces ), &$this ) ); |
365 $clauses = apply_filters_ref_array( 'comments_clauses', array( compact( $pieces ), &$this ) ); |
336 foreach ( $pieces as $piece ) |
366 foreach ( $pieces as $piece ) |
337 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : ''; |
367 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : ''; |
338 |
368 |
339 $query = "SELECT $fields FROM $wpdb->comments $join WHERE $where ORDER BY $orderby $order $limits"; |
369 if ( $groupby ) |
|
370 $groupby = 'GROUP BY ' . $groupby; |
|
371 |
|
372 $query = "SELECT $fields FROM $wpdb->comments $join WHERE $where $groupby ORDER BY $orderby $order $limits"; |
340 |
373 |
341 if ( $count ) |
374 if ( $count ) |
342 return $wpdb->get_var( $query ); |
375 return $wpdb->get_var( $query ); |
343 |
376 |
344 $comments = $wpdb->get_results( $query ); |
377 $comments = $wpdb->get_results( $query ); |
397 /** |
430 /** |
398 * The date the last comment was modified. |
431 * The date the last comment was modified. |
399 * |
432 * |
400 * @since 1.5.0 |
433 * @since 1.5.0 |
401 * @uses $wpdb |
434 * @uses $wpdb |
402 * @global array $cache_lastcommentmodified |
|
403 * |
435 * |
404 * @param string $timezone Which timezone to use in reference to 'gmt', 'blog', |
436 * @param string $timezone Which timezone to use in reference to 'gmt', 'blog', |
405 * or 'server' locations. |
437 * or 'server' locations. |
406 * @return string Last comment modified date. |
438 * @return string Last comment modified date. |
407 */ |
439 */ |
408 function get_lastcommentmodified($timezone = 'server') { |
440 function get_lastcommentmodified($timezone = 'server') { |
409 global $cache_lastcommentmodified, $wpdb; |
441 global $wpdb; |
|
442 static $cache_lastcommentmodified = array(); |
410 |
443 |
411 if ( isset($cache_lastcommentmodified[$timezone]) ) |
444 if ( isset($cache_lastcommentmodified[$timezone]) ) |
412 return $cache_lastcommentmodified[$timezone]; |
445 return $cache_lastcommentmodified[$timezone]; |
413 |
446 |
414 $add_seconds_server = date('Z'); |
447 $add_seconds_server = date('Z'); |
634 global $wpdb; |
667 global $wpdb; |
635 extract($commentdata, EXTR_SKIP); |
668 extract($commentdata, EXTR_SKIP); |
636 |
669 |
637 // Simple duplicate check |
670 // Simple duplicate check |
638 // expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content) |
671 // expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content) |
639 $dupe = "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND comment_approved != 'trash' AND ( comment_author = '$comment_author' "; |
672 $dupe = "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND comment_parent = '$comment_parent' AND comment_approved != 'trash' AND ( comment_author = '$comment_author' "; |
640 if ( $comment_author_email ) |
673 if ( $comment_author_email ) |
641 $dupe .= "OR comment_author_email = '$comment_author_email' "; |
674 $dupe .= "OR comment_author_email = '$comment_author_email' "; |
642 $dupe .= ") AND comment_content = '$comment_content' LIMIT 1"; |
675 $dupe .= ") AND comment_content = '$comment_content' LIMIT 1"; |
643 if ( $wpdb->get_var($dupe) ) { |
676 if ( $wpdb->get_var($dupe) ) { |
644 do_action( 'comment_duplicate_trigger', $commentdata ); |
677 do_action( 'comment_duplicate_trigger', $commentdata ); |
648 wp_die( __('Duplicate comment detected; it looks as though you’ve already said that!') ); |
681 wp_die( __('Duplicate comment detected; it looks as though you’ve already said that!') ); |
649 } |
682 } |
650 |
683 |
651 do_action( 'check_comment_flood', $comment_author_IP, $comment_author_email, $comment_date_gmt ); |
684 do_action( 'check_comment_flood', $comment_author_IP, $comment_author_email, $comment_date_gmt ); |
652 |
685 |
653 if ( isset($user_id) && $user_id) { |
686 if ( ! empty( $user_id ) ) { |
654 $userdata = get_userdata($user_id); |
687 $user = get_userdata( $user_id ); |
655 $user = new WP_User($user_id); |
|
656 $post_author = $wpdb->get_var($wpdb->prepare("SELECT post_author FROM $wpdb->posts WHERE ID = %d LIMIT 1", $comment_post_ID)); |
688 $post_author = $wpdb->get_var($wpdb->prepare("SELECT post_author FROM $wpdb->posts WHERE ID = %d LIMIT 1", $comment_post_ID)); |
657 } |
689 } |
658 |
690 |
659 if ( isset($userdata) && ( $user_id == $post_author || $user->has_cap('moderate_comments') ) ) { |
691 if ( isset( $user ) && ( $user_id == $post_author || $user->has_cap( 'moderate_comments' ) ) ) { |
660 // The author and the admins get respect. |
692 // The author and the admins get respect. |
661 $approved = 1; |
693 $approved = 1; |
662 } else { |
694 } else { |
663 // Everyone else's comments will be checked. |
695 // Everyone else's comments will be checked. |
664 if ( check_comment($comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_IP, $comment_agent, $comment_type) ) |
696 if ( check_comment($comment_author, $comment_author_email, $comment_author_url, $comment_content, $comment_author_IP, $comment_agent, $comment_type) ) |
692 */ |
724 */ |
693 function check_comment_flood_db( $ip, $email, $date ) { |
725 function check_comment_flood_db( $ip, $email, $date ) { |
694 global $wpdb; |
726 global $wpdb; |
695 if ( current_user_can( 'manage_options' ) ) |
727 if ( current_user_can( 'manage_options' ) ) |
696 return; // don't throttle admins |
728 return; // don't throttle admins |
697 $hour_ago = gmdate( 'Y-m-d H:i:s', time() - 3600 ); |
729 $hour_ago = gmdate( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS ); |
698 if ( $lasttime = $wpdb->get_var( $wpdb->prepare( "SELECT `comment_date_gmt` FROM `$wpdb->comments` WHERE `comment_date_gmt` >= %s AND ( `comment_author_IP` = %s OR `comment_author_email` = %s ) ORDER BY `comment_date_gmt` DESC LIMIT 1", $hour_ago, $ip, $email ) ) ) { |
730 if ( $lasttime = $wpdb->get_var( $wpdb->prepare( "SELECT `comment_date_gmt` FROM `$wpdb->comments` WHERE `comment_date_gmt` >= %s AND ( `comment_author_IP` = %s OR `comment_author_email` = %s ) ORDER BY `comment_date_gmt` DESC LIMIT 1", $hour_ago, $ip, $email ) ) ) { |
699 $time_lastcomment = mysql2date('U', $lasttime, false); |
731 $time_lastcomment = mysql2date('U', $lasttime, false); |
700 $time_newcomment = mysql2date('U', $date, false); |
732 $time_newcomment = mysql2date('U', $date, false); |
701 $flood_die = apply_filters('comment_flood_filter', false, $time_lastcomment, $time_newcomment); |
733 $flood_die = apply_filters('comment_flood_filter', false, $time_lastcomment, $time_newcomment); |
702 if ( $flood_die ) { |
734 if ( $flood_die ) { |
716 * @since 2.7.0 |
748 * @since 2.7.0 |
717 * |
749 * |
718 * @param array $comments Array of comments |
750 * @param array $comments Array of comments |
719 * @return array Array of comments keyed by comment_type. |
751 * @return array Array of comments keyed by comment_type. |
720 */ |
752 */ |
721 function &separate_comments(&$comments) { |
753 function separate_comments(&$comments) { |
722 $comments_by_type = array('comment' => array(), 'trackback' => array(), 'pingback' => array(), 'pings' => array()); |
754 $comments_by_type = array('comment' => array(), 'trackback' => array(), 'pingback' => array(), 'pings' => array()); |
723 $count = count($comments); |
755 $count = count($comments); |
724 for ( $i = 0; $i < $count; $i++ ) { |
756 for ( $i = 0; $i < $count; $i++ ) { |
725 $type = $comments[$i]->comment_type; |
757 $type = $comments[$i]->comment_type; |
726 if ( empty($type) ) |
758 if ( empty($type) ) |
1477 else if ( 'hold' == $comment_approved ) |
1516 else if ( 'hold' == $comment_approved ) |
1478 $comment_approved = 0; |
1517 $comment_approved = 0; |
1479 else if ( 'approve' == $comment_approved ) |
1518 else if ( 'approve' == $comment_approved ) |
1480 $comment_approved = 1; |
1519 $comment_approved = 1; |
1481 |
1520 |
1482 $data = compact('comment_content', 'comment_author', 'comment_author_email', 'comment_approved', 'comment_karma', 'comment_author_url', 'comment_date', 'comment_date_gmt'); |
1521 $data = compact( 'comment_content', 'comment_author', 'comment_author_email', 'comment_approved', 'comment_karma', 'comment_author_url', 'comment_date', 'comment_date_gmt', 'comment_parent' ); |
1483 $rval = $wpdb->update( $wpdb->comments, $data, compact( 'comment_ID' ) ); |
1522 $rval = $wpdb->update( $wpdb->comments, $data, compact( 'comment_ID' ) ); |
1484 |
1523 |
1485 clean_comment_cache($comment_ID); |
1524 clean_comment_cache($comment_ID); |
1486 wp_update_comment_count($comment_post_ID); |
1525 wp_update_comment_count($comment_post_ID); |
1487 do_action('edit_comment', $comment_ID); |
1526 do_action('edit_comment', $comment_ID); |