wordpress  4.0.1
About: WordPress is a blogging system that has evolved to a full content management system.
  Fossies Dox: wordpress-4.0.1.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

comment.php
Go to the documentation of this file.
1 <?php
41 function check_comment($author, $email, $url, $comment, $user_ip, $user_agent, $comment_type) {
42  global $wpdb;
43 
44  if ( 1 == get_option('comment_moderation') )
45  return false; // If moderation is set to manual
46 
48  $comment = apply_filters( 'comment_text', $comment );
49 
50  // Check # of external links
51  if ( $max_links = get_option( 'comment_max_links' ) ) {
52  $num_links = preg_match_all( '/<a [^>]*href/i', $comment, $out );
61  $num_links = apply_filters( 'comment_max_links_url', $num_links, $url );
62  if ( $num_links >= $max_links )
63  return false;
64  }
65 
66  $mod_keys = trim(get_option('moderation_keys'));
67  if ( !empty($mod_keys) ) {
68  $words = explode("\n", $mod_keys );
69 
70  foreach ( (array) $words as $word) {
71  $word = trim($word);
72 
73  // Skip empty lines
74  if ( empty($word) )
75  continue;
76 
77  // Do some escaping magic so that '#' chars in the
78  // spam words don't break things:
79  $word = preg_quote($word, '#');
80 
81  $pattern = "#$word#i";
82  if ( preg_match($pattern, $author) ) return false;
83  if ( preg_match($pattern, $email) ) return false;
84  if ( preg_match($pattern, $url) ) return false;
85  if ( preg_match($pattern, $comment) ) return false;
86  if ( preg_match($pattern, $user_ip) ) return false;
87  if ( preg_match($pattern, $user_agent) ) return false;
88  }
89  }
90 
91  // Comment whitelisting:
92  if ( 1 == get_option('comment_whitelist')) {
93  if ( 'trackback' != $comment_type && 'pingback' != $comment_type && $author != '' && $email != '' ) {
94  // expected_slashed ($author, $email)
95  $ok_to_comment = $wpdb->get_var("SELECT comment_approved FROM $wpdb->comments WHERE comment_author = '$author' AND comment_author_email = '$email' and comment_approved = '1' LIMIT 1");
96  if ( ( 1 == $ok_to_comment ) &&
97  ( empty($mod_keys) || false === strpos( $email, $mod_keys) ) )
98  return true;
99  else
100  return false;
101  } else {
102  return false;
103  }
104  }
105  return true;
106 }
107 
118  global $wpdb;
119  return $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' ORDER BY comment_date", $post_id));
120 }
121 
136 function get_comment(&$comment, $output = OBJECT) {
137  global $wpdb;
138 
139  if ( empty($comment) ) {
140  if ( isset($GLOBALS['comment']) )
141  $_comment = & $GLOBALS['comment'];
142  else
143  $_comment = null;
144  } elseif ( is_object($comment) ) {
145  wp_cache_add($comment->comment_ID, $comment, 'comment');
146  $_comment = $comment;
147  } else {
148  if ( isset($GLOBALS['comment']) && ($GLOBALS['comment']->comment_ID == $comment) ) {
149  $_comment = & $GLOBALS['comment'];
150  } elseif ( ! $_comment = wp_cache_get($comment, 'comment') ) {
151  $_comment = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment));
152  if ( ! $_comment )
153  return null;
154  wp_cache_add($_comment->comment_ID, $_comment, 'comment');
155  }
156  }
157 
165  $_comment = apply_filters( 'get_comment', $_comment );
166 
167  if ( $output == OBJECT ) {
168  return $_comment;
169  } elseif ( $output == ARRAY_A ) {
170  $__comment = get_object_vars($_comment);
171  return $__comment;
172  } elseif ( $output == ARRAY_N ) {
173  $__comment = array_values(get_object_vars($_comment));
174  return $__comment;
175  } else {
176  return $_comment;
177  }
178 }
179 
194 function get_comments( $args = '' ) {
195  $query = new WP_Comment_Query;
196  return $query->query( $args );
197 }
198 
212  public $meta_query = false;
213 
221  public $date_query = false;
222 
233  public function __call( $name, $arguments ) {
234  return call_user_func_array( array( $this, $name ), $arguments );
235  }
236 
245  public function query( $query_vars ) {
246  global $wpdb;
247 
248  $defaults = array(
249  'author_email' => '',
250  'fields' => '',
251  'ID' => '',
252  'karma' => '',
253  'number' => '',
254  'offset' => '',
255  'orderby' => '',
256  'order' => 'DESC',
257  'parent' => '',
258  'post_ID' => '',
259  'post_id' => 0,
260  'post_author' => '',
261  'post_name' => '',
262  'post_parent' => '',
263  'post_status' => '',
264  'post_type' => '',
265  'status' => '',
266  'type' => '',
267  'user_id' => '',
268  'search' => '',
269  'count' => false,
270  'meta_key' => '',
271  'meta_value' => '',
272  'meta_query' => '',
273  'date_query' => null, // See WP_Date_Query
274  );
275 
276  $groupby = '';
277 
278  $this->query_vars = wp_parse_args( $query_vars, $defaults );
279 
280  // Parse meta query
281  $this->meta_query = new WP_Meta_Query();
282  $this->meta_query->parse_query_vars( $this->query_vars );
283 
291  do_action_ref_array( 'pre_get_comments', array( &$this ) );
292 
293  // $args can be whatever, only use the args defined in defaults to compute the key
294  $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $defaults ) ) ) );
295  $last_changed = wp_cache_get( 'last_changed', 'comment' );
296  if ( ! $last_changed ) {
297  $last_changed = microtime();
298  wp_cache_set( 'last_changed', $last_changed, 'comment' );
299  }
300  $cache_key = "get_comments:$key:$last_changed";
301 
302  if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
303  return $cache;
304  }
305 
306  $status = $this->query_vars['status'];
307  if ( 'hold' == $status ) {
308  $approved = "comment_approved = '0'";
309  } elseif ( 'approve' == $status ) {
310  $approved = "comment_approved = '1'";
311  } elseif ( ! empty( $status ) && 'all' != $status ) {
312  $approved = $wpdb->prepare( "comment_approved = %s", $status );
313  } else {
314  $approved = "( comment_approved = '0' OR comment_approved = '1' )";
315  }
316  $order = ( 'ASC' == strtoupper( $this->query_vars['order'] ) ) ? 'ASC' : 'DESC';
317 
318  if ( ! empty( $this->query_vars['orderby'] ) ) {
319  $ordersby = is_array( $this->query_vars['orderby'] ) ?
320  $this->query_vars['orderby'] :
321  preg_split( '/[,\s]/', $this->query_vars['orderby'] );
322 
323  $allowed_keys = array(
324  'comment_agent',
325  'comment_approved',
326  'comment_author',
327  'comment_author_email',
328  'comment_author_IP',
329  'comment_author_url',
330  'comment_content',
331  'comment_date',
332  'comment_date_gmt',
333  'comment_ID',
334  'comment_karma',
335  'comment_parent',
336  'comment_post_ID',
337  'comment_type',
338  'user_id',
339  );
340  if ( ! empty( $this->query_vars['meta_key'] ) ) {
341  $allowed_keys[] = $this->query_vars['meta_key'];
342  $allowed_keys[] = 'meta_value';
343  $allowed_keys[] = 'meta_value_num';
344  }
345  $ordersby = array_intersect( $ordersby, $allowed_keys );
346  foreach ( $ordersby as $key => $value ) {
347  if ( $value == $this->query_vars['meta_key'] || $value == 'meta_value' ) {
348  $ordersby[ $key ] = "$wpdb->commentmeta.meta_value";
349  } elseif ( $value == 'meta_value_num' ) {
350  $ordersby[ $key ] = "$wpdb->commentmeta.meta_value+0";
351  }
352  }
353  $orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby);
354  } else {
355  $orderby = 'comment_date_gmt';
356  }
357 
358  $number = absint( $this->query_vars['number'] );
359  $offset = absint( $this->query_vars['offset'] );
360 
361  if ( ! empty( $number ) ) {
362  if ( $offset ) {
363  $limits = 'LIMIT ' . $offset . ',' . $number;
364  } else {
365  $limits = 'LIMIT ' . $number;
366  }
367  } else {
368  $limits = '';
369  }
370 
371  if ( $this->query_vars['count'] ) {
372  $fields = 'COUNT(*)';
373  } else {
374  switch ( strtolower( $this->query_vars['fields'] ) ) {
375  case 'ids':
376  $fields = "$wpdb->comments.comment_ID";
377  break;
378  default:
379  $fields = "*";
380  break;
381  }
382  }
383 
384  $join = '';
385  $where = $approved;
386 
387  $post_id = absint( $this->query_vars['post_id'] );
388  if ( ! empty( $post_id ) ) {
389  $where .= $wpdb->prepare( ' AND comment_post_ID = %d', $post_id );
390  }
391 
392  if ( '' !== $this->query_vars['author_email'] ) {
393  $where .= $wpdb->prepare( ' AND comment_author_email = %s', $this->query_vars['author_email'] );
394  }
395 
396  if ( '' !== $this->query_vars['karma'] ) {
397  $where .= $wpdb->prepare( ' AND comment_karma = %d', $this->query_vars['karma'] );
398  }
399 
400  if ( 'comment' == $this->query_vars['type'] ) {
401  $where .= " AND comment_type = ''";
402  } elseif( 'pings' == $this->query_vars['type'] ) {
403  $where .= ' AND comment_type IN ("pingback", "trackback")';
404  } elseif ( ! empty( $this->query_vars['type'] ) ) {
405  $where .= $wpdb->prepare( ' AND comment_type = %s', $this->query_vars['type'] );
406  }
407 
408  if ( '' !== $this->query_vars['parent'] ) {
409  $where .= $wpdb->prepare( ' AND comment_parent = %d', $this->query_vars['parent'] );
410  }
411 
412  if ( is_array( $this->query_vars['user_id'] ) ) {
413  $where .= ' AND user_id IN (' . implode( ',', array_map( 'absint', $this->query_vars['user_id'] ) ) . ')';
414  } elseif ( '' !== $this->query_vars['user_id'] ) {
415  $where .= $wpdb->prepare( ' AND user_id = %d', $this->query_vars['user_id'] );
416  }
417 
418  if ( '' !== $this->query_vars['search'] ) {
419  $where .= $this->get_search_sql(
420  $this->query_vars['search'],
421  array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' )
422  );
423  }
424 
425  $plucked = wp_array_slice_assoc( $this->query_vars, array( 'post_author', 'post_name', 'post_parent', 'post_status', 'post_type' ) );
426  $post_fields = array_filter( $plucked );
427 
428  if ( ! empty( $post_fields ) ) {
429  $join = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
430  foreach( $post_fields as $field_name => $field_value )
431  $where .= $wpdb->prepare( " AND {$wpdb->posts}.{$field_name} = %s", $field_value );
432  }
433 
434  if ( ! empty( $this->meta_query->queries ) ) {
435  $clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this );
436  $join .= $clauses['join'];
437  $where .= $clauses['where'];
438  $groupby = "{$wpdb->comments}.comment_ID";
439  }
440 
441  $date_query = $this->query_vars['date_query'];
442  if ( ! empty( $date_query ) && is_array( $date_query ) ) {
443  $date_query_object = new WP_Date_Query( $date_query, 'comment_date' );
444  $where .= $date_query_object->get_sql();
445  }
446 
447  $pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits', 'groupby' );
456  $clauses = apply_filters_ref_array( 'comments_clauses', array( compact( $pieces ), &$this ) );
457 
458  $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : '';
459  $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : '';
460  $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : '';
461  $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : '';
462  $order = isset( $clauses[ 'order' ] ) ? $clauses[ 'order' ] : '';
463  $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : '';
464  $groupby = isset( $clauses[ 'groupby' ] ) ? $clauses[ 'groupby' ] : '';
465 
466  if ( $groupby ) {
467  $groupby = 'GROUP BY ' . $groupby;
468  }
469  $query = "SELECT $fields FROM $wpdb->comments $join WHERE $where $groupby ORDER BY $orderby $order $limits";
470 
471  if ( $this->query_vars['count'] ) {
472  return $wpdb->get_var( $query );
473  }
474 
475  if ( 'ids' == $this->query_vars['fields'] ) {
476  $this->comments = $wpdb->get_col( $query );
477  return array_map( 'intval', $this->comments );
478  }
479 
480  $results = $wpdb->get_results( $query );
489  $comments = apply_filters_ref_array( 'the_comments', array( $results, &$this ) );
490 
491  wp_cache_add( $cache_key, $comments, 'comment' );
492 
493  return $comments;
494  }
495 
506  protected function get_search_sql( $string, $cols ) {
507  global $wpdb;
508 
509  $like = '%' . $wpdb->esc_like( $string ) . '%';
510 
511  $searches = array();
512  foreach ( $cols as $col ) {
513  $searches[] = $wpdb->prepare( "$col LIKE %s", $like );
514  }
515 
516  return ' AND (' . implode(' OR ', $searches) . ')';
517  }
518 }
519 
531  $status = array(
532  'hold' => __('Unapproved'),
533  /* translators: comment status */
534  'approve' => _x('Approved', 'adjective'),
535  /* translators: comment status */
536  'spam' => _x('Spam', 'adjective'),
537  );
538 
539  return $status;
540 }
541 
552 function get_lastcommentmodified($timezone = 'server') {
553  global $wpdb;
554  static $cache_lastcommentmodified = array();
555 
556  if ( isset($cache_lastcommentmodified[$timezone]) )
557  return $cache_lastcommentmodified[$timezone];
558 
559  $add_seconds_server = date('Z');
560 
561  switch ( strtolower($timezone)) {
562  case 'gmt':
563  $lastcommentmodified = $wpdb->get_var("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1");
564  break;
565  case 'blog':
566  $lastcommentmodified = $wpdb->get_var("SELECT comment_date FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1");
567  break;
568  case 'server':
569  $lastcommentmodified = $wpdb->get_var($wpdb->prepare("SELECT DATE_ADD(comment_date_gmt, INTERVAL %s SECOND) FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1", $add_seconds_server));
570  break;
571  }
572 
573  $cache_lastcommentmodified[$timezone] = $lastcommentmodified;
574 
575  return $lastcommentmodified;
576 }
577 
591 function get_comment_count( $post_id = 0 ) {
592  global $wpdb;
593 
594  $post_id = (int) $post_id;
595 
596  $where = '';
597  if ( $post_id > 0 ) {
598  $where = $wpdb->prepare("WHERE comment_post_ID = %d", $post_id);
599  }
600 
601  $totals = (array) $wpdb->get_results("
602  SELECT comment_approved, COUNT( * ) AS total
603  FROM {$wpdb->comments}
604  {$where}
605  GROUP BY comment_approved
606  ", ARRAY_A);
607 
608  $comment_count = array(
609  "approved" => 0,
610  "awaiting_moderation" => 0,
611  "spam" => 0,
612  "total_comments" => 0
613  );
614 
615  foreach ( $totals as $row ) {
616  switch ( $row['comment_approved'] ) {
617  case 'spam':
618  $comment_count['spam'] = $row['total'];
619  $comment_count["total_comments"] += $row['total'];
620  break;
621  case 1:
622  $comment_count['approved'] = $row['total'];
623  $comment_count['total_comments'] += $row['total'];
624  break;
625  case 0:
626  $comment_count['awaiting_moderation'] = $row['total'];
627  $comment_count['total_comments'] += $row['total'];
628  break;
629  default:
630  break;
631  }
632  }
633 
634  return $comment_count;
635 }
636 
637 //
638 // Comment meta functions
639 //
640 
654 function add_comment_meta($comment_id, $meta_key, $meta_value, $unique = false) {
655  return add_metadata('comment', $comment_id, $meta_key, $meta_value, $unique);
656 }
657 
674 function delete_comment_meta($comment_id, $meta_key, $meta_value = '') {
675  return delete_metadata('comment', $comment_id, $meta_key, $meta_value);
676 }
677 
691 function get_comment_meta($comment_id, $key = '', $single = false) {
692  return get_metadata('comment', $comment_id, $key, $single);
693 }
694 
713 function update_comment_meta($comment_id, $meta_key, $meta_value, $prev_value = '') {
714  return update_metadata('comment', $comment_id, $meta_key, $meta_value, $prev_value);
715 }
716 
726 function wp_set_comment_cookies($comment, $user) {
727  if ( $user->exists() )
728  return;
729 
737  $comment_cookie_lifetime = apply_filters( 'comment_cookie_lifetime', 30000000 );
738  $secure = ( 'https' === parse_url( home_url(), PHP_URL_SCHEME ) );
739  setcookie( 'comment_author_' . COOKIEHASH, $comment->comment_author, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
740  setcookie( 'comment_author_email_' . COOKIEHASH, $comment->comment_author_email, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
741  setcookie( 'comment_author_url_' . COOKIEHASH, esc_url($comment->comment_author_url), time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
742 }
743 
753  if ( isset( $_COOKIE['comment_author_' . COOKIEHASH] ) ) {
764  $comment_author = apply_filters( 'pre_comment_author_name', $_COOKIE['comment_author_' . COOKIEHASH] );
767  $_COOKIE['comment_author_' . COOKIEHASH] = $comment_author;
768  }
769 
770  if ( isset( $_COOKIE['comment_author_email_' . COOKIEHASH] ) ) {
781  $comment_author_email = apply_filters( 'pre_comment_author_email', $_COOKIE['comment_author_email_' . COOKIEHASH] );
784  $_COOKIE['comment_author_email_'.COOKIEHASH] = $comment_author_email;
785  }
786 
787  if ( isset( $_COOKIE['comment_author_url_' . COOKIEHASH] ) ) {
798  $comment_author_url = apply_filters( 'pre_comment_author_url', $_COOKIE['comment_author_url_' . COOKIEHASH] );
800  $_COOKIE['comment_author_url_'.COOKIEHASH] = $comment_author_url;
801  }
802 }
803 
813 function wp_allow_comment( $commentdata ) {
814  global $wpdb;
815 
816  // Simple duplicate check
817  // expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content)
818  $dupe = $wpdb->prepare(
819  "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = %s AND comment_approved != 'trash' AND ( comment_author = %s ",
820  wp_unslash( $commentdata['comment_post_ID'] ),
821  wp_unslash( $commentdata['comment_parent'] ),
822  wp_unslash( $commentdata['comment_author'] )
823  );
824  if ( $commentdata['comment_author_email'] ) {
825  $dupe .= $wpdb->prepare(
826  "OR comment_author_email = %s ",
827  wp_unslash( $commentdata['comment_author_email'] )
828  );
829  }
830  $dupe .= $wpdb->prepare(
831  ") AND comment_content = %s LIMIT 1",
832  wp_unslash( $commentdata['comment_content'] )
833  );
834  if ( $wpdb->get_var( $dupe ) ) {
842  do_action( 'comment_duplicate_trigger', $commentdata );
843  if ( defined( 'DOING_AJAX' ) ) {
844  die( __('Duplicate comment detected; it looks as though you&#8217;ve already said that!') );
845  }
846  wp_die( __('Duplicate comment detected; it looks as though you&#8217;ve already said that!') );
847  }
848 
860  do_action(
861  'check_comment_flood',
862  $commentdata['comment_author_IP'],
863  $commentdata['comment_author_email'],
864  $commentdata['comment_date_gmt']
865  );
866 
867  if ( ! empty( $commentdata['user_id'] ) ) {
868  $user = get_userdata( $commentdata['user_id'] );
869  $post_author = $wpdb->get_var( $wpdb->prepare(
870  "SELECT post_author FROM $wpdb->posts WHERE ID = %d LIMIT 1",
871  $commentdata['comment_post_ID']
872  ) );
873  }
874 
875  if ( isset( $user ) && ( $commentdata['user_id'] == $post_author || $user->has_cap( 'moderate_comments' ) ) ) {
876  // The author and the admins get respect.
877  $approved = 1;
878  } else {
879  // Everyone else's comments will be checked.
880  if ( check_comment(
881  $commentdata['comment_author'],
882  $commentdata['comment_author_email'],
883  $commentdata['comment_author_url'],
884  $commentdata['comment_content'],
885  $commentdata['comment_author_IP'],
886  $commentdata['comment_agent'],
887  $commentdata['comment_type']
888  ) ) {
889  $approved = 1;
890  } else {
891  $approved = 0;
892  }
893 
894  if ( wp_blacklist_check(
895  $commentdata['comment_author'],
896  $commentdata['comment_author_email'],
897  $commentdata['comment_author_url'],
898  $commentdata['comment_content'],
899  $commentdata['comment_author_IP'],
900  $commentdata['comment_agent']
901  ) ) {
902  $approved = 'spam';
903  }
904  }
905 
914  $approved = apply_filters( 'pre_comment_approved', $approved, $commentdata );
915  return $approved;
916 }
917 
931 function check_comment_flood_db( $ip, $email, $date ) {
932  global $wpdb;
933  if ( current_user_can( 'manage_options' ) )
934  return; // don't throttle admins
935  $hour_ago = gmdate( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS );
936  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 ) ) ) {
937  $time_lastcomment = mysql2date('U', $lasttime, false);
938  $time_newcomment = mysql2date('U', $date, false);
948  $flood_die = apply_filters( 'comment_flood_filter', false, $time_lastcomment, $time_newcomment );
949  if ( $flood_die ) {
958  do_action( 'comment_flood_trigger', $time_lastcomment, $time_newcomment );
959 
960  if ( defined('DOING_AJAX') )
961  die( __('You are posting comments too quickly. Slow down.') );
962 
963  wp_die( __('You are posting comments too quickly. Slow down.'), '', array('response' => 403) );
964  }
965  }
966 }
967 
977  $comments_by_type = array('comment' => array(), 'trackback' => array(), 'pingback' => array(), 'pings' => array());
978  $count = count($comments);
979  for ( $i = 0; $i < $count; $i++ ) {
980  $type = $comments[$i]->comment_type;
981  if ( empty($type) )
982  $type = 'comment';
983  $comments_by_type[$type][] = &$comments[$i];
984  if ( 'trackback' == $type || 'pingback' == $type )
985  $comments_by_type['pings'][] = &$comments[$i];
986  }
987 
988  return $comments_by_type;
989 }
990 
1003 function get_comment_pages_count( $comments = null, $per_page = null, $threaded = null ) {
1004  global $wp_query;
1005 
1006  if ( null === $comments && null === $per_page && null === $threaded && !empty($wp_query->max_num_comment_pages) )
1007  return $wp_query->max_num_comment_pages;
1008 
1009  if ( ( ! $comments || ! is_array( $comments ) ) && ! empty( $wp_query->comments ) )
1010  $comments = $wp_query->comments;
1011 
1012  if ( empty($comments) )
1013  return 0;
1014 
1015  if ( ! get_option( 'page_comments' ) )
1016  return 1;
1017 
1018  if ( !isset($per_page) )
1019  $per_page = (int) get_query_var('comments_per_page');
1020  if ( 0 === $per_page )
1021  $per_page = (int) get_option('comments_per_page');
1022  if ( 0 === $per_page )
1023  return 1;
1024 
1025  if ( !isset($threaded) )
1026  $threaded = get_option('thread_comments');
1027 
1028  if ( $threaded ) {
1029  $walker = new Walker_Comment;
1030  $count = ceil( $walker->get_number_of_root_elements( $comments ) / $per_page );
1031  } else {
1032  $count = ceil( count( $comments ) / $per_page );
1033  }
1034 
1035  return $count;
1036 }
1037 
1050 function get_page_of_comment( $comment_ID, $args = array() ) {
1051  global $wpdb;
1052 
1053  if ( !$comment = get_comment( $comment_ID ) )
1054  return;
1055 
1056  $defaults = array( 'type' => 'all', 'page' => '', 'per_page' => '', 'max_depth' => '' );
1057  $args = wp_parse_args( $args, $defaults );
1058 
1059  if ( '' === $args['per_page'] && get_option('page_comments') )
1060  $args['per_page'] = get_query_var('comments_per_page');
1061  if ( empty($args['per_page']) ) {
1062  $args['per_page'] = 0;
1063  $args['page'] = 0;
1064  }
1065  if ( $args['per_page'] < 1 )
1066  return 1;
1067 
1068  if ( '' === $args['max_depth'] ) {
1069  if ( get_option('thread_comments') )
1070  $args['max_depth'] = get_option('thread_comments_depth');
1071  else
1072  $args['max_depth'] = -1;
1073  }
1074 
1075  // Find this comment's top level parent if threading is enabled
1076  if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent )
1077  return get_page_of_comment( $comment->comment_parent, $args );
1078 
1079  $allowedtypes = array(
1080  'comment' => '',
1081  'pingback' => 'pingback',
1082  'trackback' => 'trackback',
1083  );
1084 
1085  $comtypewhere = ( 'all' != $args['type'] && isset($allowedtypes[$args['type']]) ) ? " AND comment_type = '" . $allowedtypes[$args['type']] . "'" : '';
1086 
1087  // Count comments older than this one
1088  $oldercoms = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = 0 AND comment_approved = '1' AND comment_date_gmt < '%s'" . $comtypewhere, $comment->comment_post_ID, $comment->comment_date_gmt ) );
1089 
1090  // No older comments? Then it's page #1.
1091  if ( 0 == $oldercoms )
1092  return 1;
1093 
1094  // Divide comments older than this one by comments per page to get this comment's page number
1095  return ceil( ( $oldercoms + 1 ) / $args['per_page'] );
1096 }
1097 
1111 function wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent) {
1124  do_action( 'wp_blacklist_check', $author, $email, $url, $comment, $user_ip, $user_agent );
1125 
1126  $mod_keys = trim( get_option('blacklist_keys') );
1127  if ( '' == $mod_keys )
1128  return false; // If moderation keys are empty
1129  $words = explode("\n", $mod_keys );
1130 
1131  foreach ( (array) $words as $word ) {
1132  $word = trim($word);
1133 
1134  // Skip empty lines
1135  if ( empty($word) ) { continue; }
1136 
1137  // Do some escaping magic so that '#' chars in the
1138  // spam words don't break things:
1139  $word = preg_quote($word, '#');
1140 
1141  $pattern = "#$word#i";
1142  if (
1143  preg_match($pattern, $author)
1144  || preg_match($pattern, $email)
1145  || preg_match($pattern, $url)
1146  || preg_match($pattern, $comment)
1147  || preg_match($pattern, $user_ip)
1148  || preg_match($pattern, $user_agent)
1149  )
1150  return true;
1151  }
1152  return false;
1153 }
1154 
1171 function wp_count_comments( $post_id = 0 ) {
1172  global $wpdb;
1173 
1174  $post_id = (int) $post_id;
1175 
1184  $stats = apply_filters( 'wp_count_comments', array(), $post_id );
1185  if ( !empty($stats) )
1186  return $stats;
1187 
1188  $count = wp_cache_get("comments-{$post_id}", 'counts');
1189 
1190  if ( false !== $count )
1191  return $count;
1192 
1193  $where = '';
1194  if ( $post_id > 0 )
1195  $where = $wpdb->prepare( "WHERE comment_post_ID = %d", $post_id );
1196 
1197  $count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} {$where} GROUP BY comment_approved", ARRAY_A );
1198 
1199  $total = 0;
1200  $approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed');
1201  foreach ( (array) $count as $row ) {
1202  // Don't count post-trashed toward totals
1203  if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] )
1204  $total += $row['num_comments'];
1205  if ( isset( $approved[$row['comment_approved']] ) )
1206  $stats[$approved[$row['comment_approved']]] = $row['num_comments'];
1207  }
1208 
1209  $stats['total_comments'] = $total;
1210  foreach ( $approved as $key ) {
1211  if ( empty($stats[$key]) )
1212  $stats[$key] = 0;
1213  }
1214 
1215  $stats = (object) $stats;
1216  wp_cache_set("comments-{$post_id}", $stats, 'counts');
1217 
1218  return $stats;
1219 }
1220 
1238 function wp_delete_comment($comment_id, $force_delete = false) {
1239  global $wpdb;
1240  if (!$comment = get_comment($comment_id))
1241  return false;
1242 
1243  if ( !$force_delete && EMPTY_TRASH_DAYS && !in_array( wp_get_comment_status($comment_id), array( 'trash', 'spam' ) ) )
1244  return wp_trash_comment($comment_id);
1245 
1253  do_action( 'delete_comment', $comment_id );
1254 
1255  // Move children up a level.
1256  $children = $wpdb->get_col( $wpdb->prepare("SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment_id) );
1257  if ( !empty($children) ) {
1258  $wpdb->update($wpdb->comments, array('comment_parent' => $comment->comment_parent), array('comment_parent' => $comment_id));
1259  clean_comment_cache($children);
1260  }
1261 
1262  // Delete metadata
1263  $meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->commentmeta WHERE comment_id = %d", $comment_id ) );
1264  foreach ( $meta_ids as $mid )
1265  delete_metadata_by_mid( 'comment', $mid );
1266 
1267  if ( ! $wpdb->delete( $wpdb->comments, array( 'comment_ID' => $comment_id ) ) )
1268  return false;
1269 
1277  do_action( 'deleted_comment', $comment_id );
1278 
1279  $post_id = $comment->comment_post_ID;
1280  if ( $post_id && $comment->comment_approved == 1 )
1282 
1283  clean_comment_cache($comment_id);
1284 
1286  do_action( 'wp_set_comment_status', $comment_id, 'delete' );
1287 
1288  wp_transition_comment_status('delete', $comment->comment_approved, $comment);
1289  return true;
1290 }
1291 
1304 function wp_trash_comment($comment_id) {
1305  if ( !EMPTY_TRASH_DAYS )
1306  return wp_delete_comment($comment_id, true);
1307 
1308  if ( !$comment = get_comment($comment_id) )
1309  return false;
1310 
1318  do_action( 'trash_comment', $comment_id );
1319 
1320  if ( wp_set_comment_status($comment_id, 'trash') ) {
1321  add_comment_meta($comment_id, '_wp_trash_meta_status', $comment->comment_approved);
1322  add_comment_meta($comment_id, '_wp_trash_meta_time', time() );
1323 
1331  do_action( 'trashed_comment', $comment_id );
1332  return true;
1333  }
1334 
1335  return false;
1336 }
1337 
1346 function wp_untrash_comment($comment_id) {
1347  if ( ! (int)$comment_id )
1348  return false;
1349 
1357  do_action( 'untrash_comment', $comment_id );
1358 
1359  $status = (string) get_comment_meta($comment_id, '_wp_trash_meta_status', true);
1360  if ( empty($status) )
1361  $status = '0';
1362 
1363  if ( wp_set_comment_status($comment_id, $status) ) {
1364  delete_comment_meta($comment_id, '_wp_trash_meta_time');
1365  delete_comment_meta($comment_id, '_wp_trash_meta_status');
1373  do_action( 'untrashed_comment', $comment_id );
1374  return true;
1375  }
1376 
1377  return false;
1378 }
1379 
1388 function wp_spam_comment($comment_id) {
1389  if ( !$comment = get_comment($comment_id) )
1390  return false;
1391 
1399  do_action( 'spam_comment', $comment_id );
1400 
1401  if ( wp_set_comment_status($comment_id, 'spam') ) {
1402  add_comment_meta($comment_id, '_wp_trash_meta_status', $comment->comment_approved);
1410  do_action( 'spammed_comment', $comment_id );
1411  return true;
1412  }
1413 
1414  return false;
1415 }
1416 
1425 function wp_unspam_comment($comment_id) {
1426  if ( ! (int)$comment_id )
1427  return false;
1428 
1436  do_action( 'unspam_comment', $comment_id );
1437 
1438  $status = (string) get_comment_meta($comment_id, '_wp_trash_meta_status', true);
1439  if ( empty($status) )
1440  $status = '0';
1441 
1442  if ( wp_set_comment_status($comment_id, $status) ) {
1443  delete_comment_meta($comment_id, '_wp_trash_meta_status');
1451  do_action( 'unspammed_comment', $comment_id );
1452  return true;
1453  }
1454 
1455  return false;
1456 }
1457 
1466 function wp_get_comment_status($comment_id) {
1467  $comment = get_comment($comment_id);
1468  if ( !$comment )
1469  return false;
1470 
1471  $approved = $comment->comment_approved;
1472 
1473  if ( $approved == null )
1474  return false;
1475  elseif ( $approved == '1' )
1476  return 'approved';
1477  elseif ( $approved == '0' )
1478  return 'unapproved';
1479  elseif ( $approved == 'spam' )
1480  return 'spam';
1481  elseif ( $approved == 'trash' )
1482  return 'trash';
1483  else
1484  return false;
1485 }
1486 
1507 function wp_transition_comment_status($new_status, $old_status, $comment) {
1508  /*
1509  * Translate raw statuses to human readable formats for the hooks.
1510  * This is not a complete list of comment status, it's only the ones
1511  * that need to be renamed
1512  */
1513  $comment_statuses = array(
1514  0 => 'unapproved',
1515  'hold' => 'unapproved', // wp_set_comment_status() uses "hold"
1516  1 => 'approved',
1517  'approve' => 'approved', // wp_set_comment_status() uses "approve"
1518  );
1519  if ( isset($comment_statuses[$new_status]) ) $new_status = $comment_statuses[$new_status];
1520  if ( isset($comment_statuses[$old_status]) ) $old_status = $comment_statuses[$old_status];
1521 
1522  // Call the hooks
1523  if ( $new_status != $old_status ) {
1533  do_action( 'transition_comment_status', $new_status, $old_status, $comment );
1544  do_action( "comment_{$old_status}_to_{$new_status}", $comment );
1545  }
1560  do_action( "comment_{$new_status}_{$comment->comment_type}", $comment->comment_ID, $comment );
1561 }
1562 
1576  // Cookies should already be sanitized.
1577 
1578  $comment_author = '';
1579  if ( isset($_COOKIE['comment_author_'.COOKIEHASH]) )
1580  $comment_author = $_COOKIE['comment_author_'.COOKIEHASH];
1581 
1582  $comment_author_email = '';
1583  if ( isset($_COOKIE['comment_author_email_'.COOKIEHASH]) )
1584  $comment_author_email = $_COOKIE['comment_author_email_'.COOKIEHASH];
1585 
1586  $comment_author_url = '';
1587  if ( isset($_COOKIE['comment_author_url_'.COOKIEHASH]) )
1588  $comment_author_url = $_COOKIE['comment_author_url_'.COOKIEHASH];
1589 
1599  return apply_filters( 'wp_get_current_commenter', compact('comment_author', 'comment_author_email', 'comment_author_url') );
1600 }
1601 
1614 function wp_insert_comment( $commentdata ) {
1615  global $wpdb;
1616  $data = wp_unslash( $commentdata );
1617 
1618  $comment_author = ! isset( $data['comment_author'] ) ? '' : $data['comment_author'];
1619  $comment_author_email = ! isset( $data['comment_author_email'] ) ? '' : $data['comment_author_email'];
1620  $comment_author_url = ! isset( $data['comment_author_url'] ) ? '' : $data['comment_author_url'];
1621  $comment_author_IP = ! isset( $data['comment_author_IP'] ) ? '' : $data['comment_author_IP'];
1622 
1623  $comment_date = ! isset( $data['comment_date'] ) ? current_time( 'mysql' ) : $data['comment_date'];
1624  $comment_date_gmt = ! isset( $data['comment_date_gmt'] ) ? get_gmt_from_date( $comment_date ) : $data['comment_date_gmt'];
1625 
1626  $comment_post_ID = ! isset( $data['comment_post_ID'] ) ? '' : $data['comment_post_ID'];
1627  $comment_content = ! isset( $data['comment_content'] ) ? '' : $data['comment_content'];
1628  $comment_karma = ! isset( $data['comment_karma'] ) ? 0 : $data['comment_karma'];
1629  $comment_approved = ! isset( $data['comment_approved'] ) ? 1 : $data['comment_approved'];
1630  $comment_agent = ! isset( $data['comment_agent'] ) ? '' : $data['comment_agent'];
1631  $comment_type = ! isset( $data['comment_type'] ) ? '' : $data['comment_type'];
1632  $comment_parent = ! isset( $data['comment_parent'] ) ? 0 : $data['comment_parent'];
1633 
1634  $user_id = ! isset( $data['user_id'] ) ? 0 : $data['user_id'];
1635 
1636  $compacted = compact( 'comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_date', 'comment_date_gmt', 'comment_content', 'comment_karma', 'comment_approved', 'comment_agent', 'comment_type', 'comment_parent', 'user_id' );
1637  if ( ! $wpdb->insert( $wpdb->comments, $compacted ) ) {
1638  return false;
1639  }
1640 
1641  $id = (int) $wpdb->insert_id;
1642 
1643  if ( $comment_approved == 1 ) {
1645  }
1646  $comment = get_comment( $id );
1647 
1656  do_action( 'wp_insert_comment', $id, $comment );
1657 
1658  wp_cache_set( 'last_changed', microtime(), 'comment' );
1659 
1660  return $id;
1661 }
1662 
1675 function wp_filter_comment($commentdata) {
1676  if ( isset( $commentdata['user_ID'] ) ) {
1687  $commentdata['user_id'] = apply_filters( 'pre_user_id', $commentdata['user_ID'] );
1688  } elseif ( isset( $commentdata['user_id'] ) ) {
1690  $commentdata['user_id'] = apply_filters( 'pre_user_id', $commentdata['user_id'] );
1691  }
1692 
1700  $commentdata['comment_agent'] = apply_filters( 'pre_comment_user_agent', ( isset( $commentdata['comment_agent'] ) ? $commentdata['comment_agent'] : '' ) );
1702  $commentdata['comment_author'] = apply_filters( 'pre_comment_author_name', $commentdata['comment_author'] );
1710  $commentdata['comment_content'] = apply_filters( 'pre_comment_content', $commentdata['comment_content'] );
1718  $commentdata['comment_author_IP'] = apply_filters( 'pre_comment_user_ip', $commentdata['comment_author_IP'] );
1720  $commentdata['comment_author_url'] = apply_filters( 'pre_comment_author_url', $commentdata['comment_author_url'] );
1722  $commentdata['comment_author_email'] = apply_filters( 'pre_comment_author_email', $commentdata['comment_author_email'] );
1723  $commentdata['filtered'] = true;
1724  return $commentdata;
1725 }
1726 
1737 function wp_throttle_comment_flood($block, $time_lastcomment, $time_newcomment) {
1738  if ( $block ) // a plugin has already blocked... we'll let that decision stand
1739  return $block;
1740  if ( ($time_newcomment - $time_lastcomment) < 15 )
1741  return true;
1742  return false;
1743 }
1744 
1761 function wp_new_comment( $commentdata ) {
1762  if ( isset( $commentdata['user_ID'] ) ) {
1763  $commentdata['user_id'] = $commentdata['user_ID'] = (int) $commentdata['user_ID'];
1764  }
1765 
1766  $prefiltered_user_id = ( isset( $commentdata['user_id'] ) ) ? (int) $commentdata['user_id'] : 0;
1767 
1775  $commentdata = apply_filters( 'preprocess_comment', $commentdata );
1776 
1777  $commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID'];
1778  if ( isset( $commentdata['user_ID'] ) && $prefiltered_user_id !== (int) $commentdata['user_ID'] ) {
1779  $commentdata['user_id'] = $commentdata['user_ID'] = (int) $commentdata['user_ID'];
1780  } elseif ( isset( $commentdata['user_id'] ) ) {
1781  $commentdata['user_id'] = (int) $commentdata['user_id'];
1782  }
1783 
1784  $commentdata['comment_parent'] = isset($commentdata['comment_parent']) ? absint($commentdata['comment_parent']) : 0;
1785  $parent_status = ( 0 < $commentdata['comment_parent'] ) ? wp_get_comment_status($commentdata['comment_parent']) : '';
1786  $commentdata['comment_parent'] = ( 'approved' == $parent_status || 'unapproved' == $parent_status ) ? $commentdata['comment_parent'] : 0;
1787 
1788  $commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR'] );
1789  $commentdata['comment_agent'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? substr( $_SERVER['HTTP_USER_AGENT'], 0, 254 ) : '';
1790 
1791  $commentdata['comment_date'] = current_time('mysql');
1792  $commentdata['comment_date_gmt'] = current_time('mysql', 1);
1793 
1794  $commentdata = wp_filter_comment($commentdata);
1795 
1796  $commentdata['comment_approved'] = wp_allow_comment($commentdata);
1797 
1798  $comment_ID = wp_insert_comment($commentdata);
1799  if ( ! $comment_ID ) {
1800  return false;
1801  }
1802 
1811  do_action( 'comment_post', $comment_ID, $commentdata['comment_approved'] );
1812 
1813  if ( 'spam' !== $commentdata['comment_approved'] ) { // If it's spam save it silently for later crunching
1814  if ( '0' == $commentdata['comment_approved'] ) {
1815  wp_notify_moderator( $comment_ID );
1816  }
1817 
1818  // wp_notify_postauthor() checks if notifying the author of their own comment.
1819  // By default, it won't, but filters can override this.
1820  if ( get_option( 'comments_notify' ) && $commentdata['comment_approved'] ) {
1821  wp_notify_postauthor( $comment_ID );
1822  }
1823  }
1824 
1825  return $comment_ID;
1826 }
1827 
1842 function wp_set_comment_status($comment_id, $comment_status, $wp_error = false) {
1843  global $wpdb;
1844 
1845  switch ( $comment_status ) {
1846  case 'hold':
1847  case '0':
1848  $status = '0';
1849  break;
1850  case 'approve':
1851  case '1':
1852  $status = '1';
1853  if ( get_option('comments_notify') ) {
1854  wp_notify_postauthor( $comment_id );
1855  }
1856  break;
1857  case 'spam':
1858  $status = 'spam';
1859  break;
1860  case 'trash':
1861  $status = 'trash';
1862  break;
1863  default:
1864  return false;
1865  }
1866 
1867  $comment_old = clone get_comment($comment_id);
1868 
1869  if ( !$wpdb->update( $wpdb->comments, array('comment_approved' => $status), array('comment_ID' => $comment_id) ) ) {
1870  if ( $wp_error )
1871  return new WP_Error('db_update_error', __('Could not update comment status'), $wpdb->last_error);
1872  else
1873  return false;
1874  }
1875 
1876  clean_comment_cache($comment_id);
1877 
1878  $comment = get_comment($comment_id);
1879 
1890  do_action( 'wp_set_comment_status', $comment_id, $comment_status );
1891 
1892  wp_transition_comment_status($comment_status, $comment_old->comment_approved, $comment);
1893 
1894  wp_update_comment_count($comment->comment_post_ID);
1895 
1896  return true;
1897 }
1898 
1911 function wp_update_comment($commentarr) {
1912  global $wpdb;
1913 
1914  // First, get all of the original fields
1915  $comment = get_comment($commentarr['comment_ID'], ARRAY_A);
1916  if ( empty( $comment ) ) {
1917  return 0;
1918  }
1919  // Escape data pulled from DB.
1920  $comment = wp_slash($comment);
1921 
1922  $old_status = $comment['comment_approved'];
1923 
1924  // Merge old and new fields with new fields overwriting old ones.
1925  $commentarr = array_merge($comment, $commentarr);
1926 
1927  $commentarr = wp_filter_comment( $commentarr );
1928 
1929  // Now extract the merged array.
1930  $data = wp_unslash( $commentarr );
1931 
1939  $data['comment_content'] = apply_filters( 'comment_save_pre', $data['comment_content'] );
1940 
1941  $data['comment_date_gmt'] = get_gmt_from_date( $data['comment_date'] );
1942 
1943  if ( ! isset( $data['comment_approved'] ) ) {
1944  $data['comment_approved'] = 1;
1945  } else if ( 'hold' == $data['comment_approved'] ) {
1946  $data['comment_approved'] = 0;
1947  } else if ( 'approve' == $data['comment_approved'] ) {
1948  $data['comment_approved'] = 1;
1949  }
1950 
1951  $comment_ID = $data['comment_ID'];
1952  $comment_post_ID = $data['comment_post_ID'];
1953  $keys = array( 'comment_content', 'comment_author', 'comment_author_email', 'comment_approved', 'comment_karma', 'comment_author_url', 'comment_date', 'comment_date_gmt', 'comment_parent' );
1954  $data = wp_array_slice_assoc( $data, $keys );
1955  $rval = $wpdb->update( $wpdb->comments, $data, compact( 'comment_ID' ) );
1956 
1957  clean_comment_cache( $comment_ID );
1968  do_action( 'edit_comment', $comment_ID );
1969  $comment = get_comment($comment_ID);
1970  wp_transition_comment_status($comment->comment_approved, $old_status, $comment);
1971  return $rval;
1972 }
1973 
1989  static $_defer = false;
1990 
1991  if ( is_bool($defer) ) {
1992  $_defer = $defer;
1993  // flush any deferred counts
1994  if ( !$defer )
1995  wp_update_comment_count( null, true );
1996  }
1997 
1998  return $_defer;
1999 }
2000 
2019 function wp_update_comment_count($post_id, $do_deferred=false) {
2020  static $_deferred = array();
2021 
2022  if ( $do_deferred ) {
2023  $_deferred = array_unique($_deferred);
2024  foreach ( $_deferred as $i => $_post_id ) {
2025  wp_update_comment_count_now($_post_id);
2026  unset( $_deferred[$i] );
2027  }
2028  }
2029 
2030  if ( wp_defer_comment_counting() ) {
2031  $_deferred[] = $post_id;
2032  return true;
2033  }
2034  elseif ( $post_id ) {
2036  }
2037 
2038 }
2039 
2050  global $wpdb;
2051  $post_id = (int) $post_id;
2052  if ( !$post_id )
2053  return false;
2054  if ( !$post = get_post($post_id) )
2055  return false;
2056 
2057  $old = (int) $post->comment_count;
2058  $new = (int) $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id) );
2059  $wpdb->update( $wpdb->posts, array('comment_count' => $new), array('ID' => $post_id) );
2060 
2062 
2072  do_action( 'wp_update_comment_count', $post_id, $new, $old );
2074  do_action( 'edit_post', $post_id, $post );
2075 
2076  return true;
2077 }
2078 
2079 //
2080 // Ping and trackback functions.
2081 //
2082 
2096 function discover_pingback_server_uri( $url, $deprecated = '' ) {
2097  if ( !empty( $deprecated ) )
2098  _deprecated_argument( __FUNCTION__, '2.7' );
2099 
2100  $pingback_str_dquote = 'rel="pingback"';
2101  $pingback_str_squote = 'rel=\'pingback\'';
2102 
2104  $parsed_url = parse_url($url);
2105 
2106  if ( ! isset( $parsed_url['host'] ) ) // Not an URL. This should never happen.
2107  return false;
2108 
2109  //Do not search for a pingback server on our own uploads
2110  $uploads_dir = wp_upload_dir();
2111  if ( 0 === strpos($url, $uploads_dir['baseurl']) )
2112  return false;
2113 
2114  $response = wp_safe_remote_head( $url, array( 'timeout' => 2, 'httpversion' => '1.0' ) );
2115 
2116  if ( is_wp_error( $response ) )
2117  return false;
2118 
2119  if ( wp_remote_retrieve_header( $response, 'x-pingback' ) )
2120  return wp_remote_retrieve_header( $response, 'x-pingback' );
2121 
2122  // Not an (x)html, sgml, or xml page, no use going further.
2123  if ( preg_match('#(image|audio|video|model)/#is', wp_remote_retrieve_header( $response, 'content-type' )) )
2124  return false;
2125 
2126  // Now do a GET since we're going to look in the html headers (and we're sure it's not a binary file)
2127  $response = wp_safe_remote_get( $url, array( 'timeout' => 2, 'httpversion' => '1.0' ) );
2128 
2129  if ( is_wp_error( $response ) )
2130  return false;
2131 
2132  $contents = wp_remote_retrieve_body( $response );
2133 
2134  $pingback_link_offset_dquote = strpos($contents, $pingback_str_dquote);
2135  $pingback_link_offset_squote = strpos($contents, $pingback_str_squote);
2136  if ( $pingback_link_offset_dquote || $pingback_link_offset_squote ) {
2137  $quote = ($pingback_link_offset_dquote) ? '"' : '\'';
2138  $pingback_link_offset = ($quote=='"') ? $pingback_link_offset_dquote : $pingback_link_offset_squote;
2139  $pingback_href_pos = @strpos($contents, 'href=', $pingback_link_offset);
2140  $pingback_href_start = $pingback_href_pos+6;
2141  $pingback_href_end = @strpos($contents, $quote, $pingback_href_start);
2142  $pingback_server_url_len = $pingback_href_end - $pingback_href_start;
2143  $pingback_server_url = substr($contents, $pingback_href_start, $pingback_server_url_len);
2144 
2145  // We may find rel="pingback" but an incomplete pingback URL
2146  if ( $pingback_server_url_len > 0 ) { // We got it!
2147  return $pingback_server_url;
2148  }
2149  }
2150 
2151  return false;
2152 }
2153 
2160 function do_all_pings() {
2161  global $wpdb;
2162 
2163  // Do pingbacks
2164  while ($ping = $wpdb->get_row("SELECT ID, post_content, meta_id FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_pingme' LIMIT 1")) {
2165  delete_metadata_by_mid( 'post', $ping->meta_id );
2166  pingback( $ping->post_content, $ping->ID );
2167  }
2168 
2169  // Do Enclosures
2170  while ($enclosure = $wpdb->get_row("SELECT ID, post_content, meta_id FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_encloseme' LIMIT 1")) {
2171  delete_metadata_by_mid( 'post', $enclosure->meta_id );
2172  do_enclose( $enclosure->post_content, $enclosure->ID );
2173  }
2174 
2175  // Do Trackbacks
2176  $trackbacks = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE to_ping <> '' AND post_status = 'publish'");
2177  if ( is_array($trackbacks) )
2178  foreach ( $trackbacks as $trackback )
2179  do_trackbacks($trackback);
2180 
2181  //Do Update Services/Generic Pings
2182  generic_ping();
2183 }
2184 
2194  global $wpdb;
2195 
2196  $post = get_post( $post_id );
2197  $to_ping = get_to_ping($post_id);
2198  $pinged = get_pung($post_id);
2199  if ( empty($to_ping) ) {
2200  $wpdb->update($wpdb->posts, array('to_ping' => ''), array('ID' => $post_id) );
2201  return;
2202  }
2203 
2204  if ( empty($post->post_excerpt) ) {
2206  $excerpt = apply_filters( 'the_content', $post->post_content, $post->ID );
2207  } else {
2209  $excerpt = apply_filters( 'the_excerpt', $post->post_excerpt );
2210  }
2211 
2212  $excerpt = str_replace(']]>', ']]&gt;', $excerpt);
2213  $excerpt = wp_html_excerpt($excerpt, 252, '&#8230;');
2214 
2216  $post_title = apply_filters( 'the_title', $post->post_title, $post->ID );
2217  $post_title = strip_tags($post_title);
2218 
2219  if ( $to_ping ) {
2220  foreach ( (array) $to_ping as $tb_ping ) {
2221  $tb_ping = trim($tb_ping);
2222  if ( !in_array($tb_ping, $pinged) ) {
2223  trackback($tb_ping, $post_title, $excerpt, $post_id);
2224  $pinged[] = $tb_ping;
2225  } else {
2226  $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, '')) WHERE ID = %d", $tb_ping, $post_id) );
2227  }
2228  }
2229  }
2230 }
2231 
2240 function generic_ping( $post_id = 0 ) {
2241  $services = get_option('ping_sites');
2242 
2243  $services = explode("\n", $services);
2244  foreach ( (array) $services as $service ) {
2245  $service = trim($service);
2246  if ( '' != $service )
2247  weblog_ping($service);
2248  }
2249 
2250  return $post_id;
2251 }
2252 
2264  global $wp_version;
2265  include_once(ABSPATH . WPINC . '/class-IXR.php');
2266  include_once(ABSPATH . WPINC . '/class-wp-http-ixr-client.php');
2267 
2268  // original code by Mort (http://mort.mine.nu:8080)
2269  $post_links = array();
2270 
2271  $pung = get_pung($post_ID);
2272 
2273  // Step 1
2274  // Parsing the post, external links (if any) are stored in the $post_links array
2275  $post_links_temp = wp_extract_urls( $content );
2276 
2277  // Step 2.
2278  // Walking thru the links array
2279  // first we get rid of links pointing to sites, not to specific files
2280  // Example:
2281  // http://dummy-weblog.org
2282  // http://dummy-weblog.org/
2283  // http://dummy-weblog.org/post.php
2284  // We don't wanna ping first and second types, even if they have a valid <link/>
2285 
2286  foreach ( (array) $post_links_temp as $link_test ) :
2287  if ( !in_array($link_test, $pung) && (url_to_postid($link_test) != $post_ID) // If we haven't pung it already and it isn't a link to itself
2288  && !is_local_attachment($link_test) ) : // Also, let's never ping local attachments.
2289  if ( $test = @parse_url($link_test) ) {
2290  if ( isset($test['query']) )
2291  $post_links[] = $link_test;
2292  elseif ( isset( $test['path'] ) && ( $test['path'] != '/' ) && ( $test['path'] != '' ) )
2293  $post_links[] = $link_test;
2294  }
2295  endif;
2296  endforeach;
2297 
2298  $post_links = array_unique( $post_links );
2308  do_action_ref_array( 'pre_ping', array( &$post_links, &$pung, $post_ID ) );
2309 
2310  foreach ( (array) $post_links as $pagelinkedto ) {
2311  $pingback_server_url = discover_pingback_server_uri( $pagelinkedto );
2312 
2313  if ( $pingback_server_url ) {
2314  @ set_time_limit( 60 );
2315  // Now, the RPC call
2316  $pagelinkedfrom = get_permalink($post_ID);
2317 
2318  // using a timeout of 3 seconds should be enough to cover slow servers
2319  $client = new WP_HTTP_IXR_Client($pingback_server_url);
2320  $client->timeout = 3;
2333  $client->useragent = apply_filters( 'pingback_useragent', $client->useragent . ' -- WordPress/' . $wp_version, $client->useragent, $pingback_server_url, $pagelinkedto, $pagelinkedfrom );
2334  // when set to true, this outputs debug messages by itself
2335  $client->debug = false;
2336 
2337  if ( $client->query('pingback.ping', $pagelinkedfrom, $pagelinkedto) || ( isset($client->error->code) && 48 == $client->error->code ) ) // Already registered
2338  add_ping( $post_ID, $pagelinkedto );
2339  }
2340  }
2341 }
2342 
2351 function privacy_ping_filter($sites) {
2352  if ( '0' != get_option('blog_public') )
2353  return $sites;
2354  else
2355  return '';
2356 }
2357 
2372 function trackback($trackback_url, $title, $excerpt, $ID) {
2373  global $wpdb;
2374 
2375  if ( empty($trackback_url) )
2376  return;
2377 
2378  $options = array();
2379  $options['timeout'] = 4;
2380  $options['body'] = array(
2381  'title' => $title,
2382  'url' => get_permalink($ID),
2383  'blog_name' => get_option('blogname'),
2384  'excerpt' => $excerpt
2385  );
2386 
2387  $response = wp_safe_remote_post( $trackback_url, $options );
2388 
2389  if ( is_wp_error( $response ) )
2390  return;
2391 
2392  $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', %s) WHERE ID = %d", $trackback_url, $ID) );
2393  return $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, '')) WHERE ID = %d", $trackback_url, $ID) );
2394 }
2395 
2406 function weblog_ping($server = '', $path = '') {
2407  global $wp_version;
2408  include_once(ABSPATH . WPINC . '/class-IXR.php');
2409  include_once(ABSPATH . WPINC . '/class-wp-http-ixr-client.php');
2410 
2411  // using a timeout of 3 seconds should be enough to cover slow servers
2412  $client = new WP_HTTP_IXR_Client($server, ((!strlen(trim($path)) || ('/' == $path)) ? false : $path));
2413  $client->timeout = 3;
2414  $client->useragent .= ' -- WordPress/'.$wp_version;
2415 
2416  // when set to true, this outputs debug messages by itself
2417  $client->debug = false;
2418  $home = trailingslashit( home_url() );
2419  if ( !$client->query('weblogUpdates.extendedPing', get_option('blogname'), $home, get_bloginfo('rss2_url') ) ) // then try a normal ping
2420  $client->query('weblogUpdates.ping', get_option('blogname'), $home);
2421 }
2422 
2432 function pingback_ping_source_uri( $source_uri ) {
2433  return (string) wp_http_validate_url( $source_uri );
2434 }
2435 
2448 function xmlrpc_pingback_error( $ixr_error ) {
2449  if ( $ixr_error->code === 48 )
2450  return $ixr_error;
2451  return new IXR_Error( 0, '' );
2452 }
2453 
2454 //
2455 // Cache
2456 //
2457 
2465 function clean_comment_cache($ids) {
2466  foreach ( (array) $ids as $id )
2467  wp_cache_delete($id, 'comment');
2468 
2469  wp_cache_set( 'last_changed', microtime(), 'comment' );
2470 }
2471 
2484  foreach ( (array) $comments as $comment )
2485  wp_cache_add($comment->comment_ID, $comment, 'comment');
2486 }
2487 
2488 //
2489 // Internal
2490 //
2491 
2502 function _close_comments_for_old_posts( $posts, $query ) {
2503  if ( empty( $posts ) || ! $query->is_singular() || ! get_option( 'close_comments_for_old_posts' ) )
2504  return $posts;
2505 
2513  $post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) );
2514  if ( ! in_array( $posts[0]->post_type, $post_types ) )
2515  return $posts;
2516 
2517  $days_old = (int) get_option( 'close_comments_days_old' );
2518  if ( ! $days_old )
2519  return $posts;
2520 
2521  if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( $days_old * DAY_IN_SECONDS ) ) {
2522  $posts[0]->comment_status = 'closed';
2523  $posts[0]->ping_status = 'closed';
2524  }
2525 
2526  return $posts;
2527 }
2528 
2540  if ( ! $open )
2541  return $open;
2542 
2543  if ( !get_option('close_comments_for_old_posts') )
2544  return $open;
2545 
2546  $days_old = (int) get_option('close_comments_days_old');
2547  if ( !$days_old )
2548  return $open;
2549 
2550  $post = get_post($post_id);
2551 
2553  $post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) );
2554  if ( ! in_array( $post->post_type, $post_types ) )
2555  return $open;
2556 
2557  if ( time() - strtotime( $post->post_date_gmt ) > ( $days_old * DAY_IN_SECONDS ) )
2558  return false;
2559 
2560  return $open;
2561 }
const ARRAY_N
Definition: wp-db.php:36
wp_cache_get($key, $group= '', $force=false, &$found=null)
Definition: cache.php:110
pingback_ping_source_uri($source_uri)
Definition: comment.php:2432
$content
Definition: press-this.php:378
apply_filters_ref_array($tag, $args)
Definition: plugin.php:240
endif
Definition: about.php:166
$user
delete_comment_meta($comment_id, $meta_key, $meta_value= '')
Definition: comment.php:674
do_action($tag, $arg= '')
Definition: plugin.php:462
wp_trash_comment($comment_id)
Definition: comment.php:1304
$user_id
Definition: user-edit.php:14
$_COOKIE
Definition: xmlrpc.php:16
wp_get_current_commenter()
Definition: comment.php:1575
s &</h2 > if($message) $_SERVER['REQUEST_URI']
Definition: edit-tags.php:305
$keys
Definition: wp-cron.php:52
get_approved_comments($post_id)
Definition: comment.php:117
add_ping($post_id, $uri)
Definition: post.php:3981
$post
Definition: post.php:26
wp_die($message= '', $title= '', $args=array())
Definition: functions.php:2345
_close_comments_for_old_posts($posts, $query)
Definition: comment.php:2502
$post_id
wp_delete_comment($comment_id, $force_delete=false)
Definition: comment.php:1238
privacy_ping_filter($sites)
Definition: comment.php:2351
const ABSPATH
get_option()
trackback($trackback_url, $title, $excerpt, $ID)
Definition: comment.php:2372
$comment_type
const OBJECT
Definition: wp-db.php:20
if(current_theme_supports( 'post-formats')&&post_type_supports($post_type, 'post-formats')) add_meta_box( 'formatdiv' _x( 'Format', 'post format')
wp_array_slice_assoc($array, $keys)
Definition: functions.php:2900
get_pung($post_id)
Definition: post.php:4048
sanitize_comment_cookies()
Definition: comment.php:752
update_comment_meta($comment_id, $meta_key, $meta_value, $prev_value= '')
Definition: comment.php:713
wp_slash($value)
wp_extract_urls($content)
Definition: functions.php:458
COOKIE_DOMAIN
Definition: wp-login.php:432
check_comment_flood_db($ip, $email, $date)
Definition: comment.php:931
delete_metadata_by_mid($meta_type, $meta_id)
Definition: meta.php:680
wp_remote_retrieve_header($response, $header)
Definition: http.php:231
check_comment($author, $email, $url, $comment, $user_ip, $user_agent, $comment_type)
Definition: comment.php:41
if(isset($_REQUEST['post_id'])) $id
get_comment_statuses()
Definition: comment.php:530
do_action_ref_array()
$path
Definition: wp-load.php:48
is_wp_error($thing)
esc_attr()
wp_unspam_comment($comment_id)
Definition: comment.php:1425
generic_ping($post_id=0)
Definition: comment.php:2240
if(current_theme_supports( 'post-formats')&&post_type_supports($post_type, 'post-formats')) add_meta_box( 'formatdiv'foreach(get_object_taxonomies($post) as $tax_name) if($thumbnail_support &&current_user_can( 'upload_files')) add_meta_box('postimagediv' __('Featured Image')
$out
wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent)
Definition: comment.php:1111
wp_cache_set($key, $data, $group= '', $expire=0)
Definition: cache.php:177
get_lastcommentmodified($timezone= 'server')
Definition: comment.php:552
get_comment_count($post_id=0)
Definition: comment.php:591
wp_set_comment_status($comment_id, $comment_status, $wp_error=false)
Definition: comment.php:1842
const ARRAY_A
Definition: wp-db.php:31
get_metadata($meta_type, $object_id, $meta_key= '', $single=false)
Definition: meta.php:440
if(empty($post->comment_status)) $status
get_post($post=null, $output=OBJECT, $filter= 'raw')
Definition: post.php:417
$title
Definition: about.php:19
separate_comments(&$comments)
Definition: comment.php:976
add_metadata($meta_type, $object_id, $meta_key, $meta_value, $unique=false)
Definition: meta.php:29
if(get_option('db_upgraded')) elseif(get_option('db_version')!=$wp_db_version &&empty($_POST))
Definition: admin.php:45
$url
Definition: customize.php:20
wp_safe_remote_get($url, $args=array())
Definition: http.php:67
wp_filter_comment($commentdata)
Definition: comment.php:1675
comment_ID()
get_comment_meta($comment_id, $key= '', $single=false)
Definition: comment.php:691
if(defined( 'RELOCATE')&&RELOCATE) $secure
Definition: wp-login.php:430
weblog_ping($server= '', $path= '')
Definition: comment.php:2406
$comment_author_email
wp_cache_add($key, $data, $group= '', $expire=0)
Definition: cache.php:24
mysql2date($format, $date, $translate=true)
Definition: functions.php:26
if(!empty($_GET['action'])&&in_array($_GET['action'], $core_actions_get)) add_action( 'wp_ajax_'.$_GET['action'] str_replace( '-', '_', $_GET['action'])
Definition: admin-ajax.php:69
get_to_ping($post_id)
Definition: post.php:4075
$comments
__call($name, $arguments)
Definition: comment.php:233
uh WP_Comments_List_Table bulk comments delete_all pagegen_timestamp comment_status pagegen_timestamp delete delete_comments delete_comments action action action2 ids absint
do_enclose($content, $post_ID)
Definition: functions.php:495
delete_metadata($meta_type, $object_id, $meta_key, $meta_value= '', $delete_all=false)
Definition: meta.php:293
$excerpt
const WPINC
wp_count_comments($post_id=0)
Definition: comment.php:1171
wp_safe_remote_post($url, $args=array())
Definition: http.php:88
apply_filters()
get_bloginfo()
wp_safe_remote_head($url, $args=array())
Definition: http.php:109
wp_defer_comment_counting($defer=null)
Definition: comment.php:1988
trailingslashit($string)
wp_cache_delete($key, $group= '')
Definition: cache.php:75
home_url()
wp_throttle_comment_flood($block, $time_lastcomment, $time_newcomment)
Definition: comment.php:1737
clean_comment_cache($ids)
Definition: comment.php:2465
clean_post_cache($post)
Definition: post.php:5444
$post_title
Definition: image.php:23
get_comment_pages_count($comments=null, $per_page=null, $threaded=null)
Definition: comment.php:1003
get_comment(&$comment, $output=OBJECT)
Definition: comment.php:136
const object
Definition: wp-db.php:21
wp_get_comment_status($comment_id)
Definition: comment.php:1466
wp_untrash_comment($comment_id)
Definition: comment.php:1346
$ID
get_page_of_comment($comment_ID, $args=array())
Definition: comment.php:1050
_close_comments_for_old_post($open, $post_id)
Definition: comment.php:2539
wp_parse_args($args, $defaults= '')
Definition: functions.php:2863
wp_html_excerpt($str, $count, $more=null)
get_comments($args= '')
Definition: comment.php:194
_deprecated_argument($function, $version, $message=null)
Definition: functions.php:3295
is_local_attachment($url)
Definition: post.php:4683
s content in order to import it into another installation or platform The export file will be an XML file format called WXR comments
Definition: export.php:49
get_search_sql($string, $cols)
Definition: comment.php:506
wp_set_comment_cookies($comment, $user)
Definition: comment.php:726
discover_pingback_server_uri($url, $deprecated= '')
Definition: comment.php:2096
update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_value= '')
Definition: meta.php:141
query($query_vars)
Definition: comment.php:245
wp_unslash($value)
$comment_author_url
url_to_postid($url)
Definition: rewrite.php:289
$comment_post_ID
$cols
Definition: my-sites.php:100
wp_new_comment($commentdata)
Definition: comment.php:1761
global $wp_version
wp_spam_comment($comment_id)
Definition: comment.php:1388
current_time($type, $gmt=0)
Definition: functions.php:61
wp_update_comment($commentarr)
Definition: comment.php:1911
do_all_pings()
Definition: comment.php:2160
get_query_var($var, $default= '')
Definition: query.php:25
esc_url($url, $protocols=null, $_context= 'display')
do_trackbacks($post_id)
Definition: comment.php:2193
$comment_author
wp_remote_retrieve_body($response)
Definition: http.php:283
add_comment_meta($comment_id, $meta_key, $meta_value, $unique=false)
Definition: comment.php:654
xmlrpc_pingback_error($ixr_error)
Definition: comment.php:2448
wp_http_validate_url($url)
Definition: http.php:446
wp_insert_comment($commentdata)
Definition: comment.php:1614
wp_allow_comment($commentdata)
Definition: comment.php:813
pingback($content, $post_ID)
Definition: comment.php:2263
if(!defined( 'WP_ADMIN'))
Definition: admin-ajax.php:17
wp_upload_dir($time=null)
Definition: functions.php:1718
$comment_content
get_gmt_from_date($string, $format= 'Y-m-d H:i:s')
wp_update_comment_count_now($post_id)
Definition: comment.php:2049
if(current_theme_supports( 'post-formats')&&post_type_supports($post_type, 'post-formats')) add_meta_box( 'formatdiv' null
global $wpdb
Definition: schema.php:12
wp_transition_comment_status($new_status, $old_status, $comment)
Definition: comment.php:1507
update_comment_cache($comments)
Definition: comment.php:2483
$GLOBALS['wp_embed']
wp_update_comment_count($post_id, $do_deferred=false)
Definition: comment.php:2019
current_user_can($capability)
endforeach
Definition: export.php:189
$name
Definition: options.php:241