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

 All Classes Namespaces Files Functions Variables Typedefs Pages
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  var $meta_query = false;
213 
221  var $date_query = false;
222 
231  function query( $query_vars ) {
232  global $wpdb;
233 
234  $defaults = array(
235  'author_email' => '',
236  'ID' => '',
237  'karma' => '',
238  'number' => '',
239  'offset' => '',
240  'orderby' => '',
241  'order' => 'DESC',
242  'parent' => '',
243  'post_ID' => '',
244  'post_id' => 0,
245  'post_author' => '',
246  'post_name' => '',
247  'post_parent' => '',
248  'post_status' => '',
249  'post_type' => '',
250  'status' => '',
251  'type' => '',
252  'user_id' => '',
253  'search' => '',
254  'count' => false,
255  'meta_key' => '',
256  'meta_value' => '',
257  'meta_query' => '',
258  'date_query' => null, // See WP_Date_Query
259  );
260 
261  $groupby = '';
262 
263  $this->query_vars = wp_parse_args( $query_vars, $defaults );
264 
265  // Parse meta query
266  $this->meta_query = new WP_Meta_Query();
267  $this->meta_query->parse_query_vars( $this->query_vars );
268 
276  do_action_ref_array( 'pre_get_comments', array( &$this ) );
277  extract( $this->query_vars, EXTR_SKIP );
278 
279  // $args can be whatever, only use the args defined in defaults to compute the key
280  $key = md5( serialize( compact(array_keys($defaults)) ) );
281  $last_changed = wp_cache_get( 'last_changed', 'comment' );
282  if ( ! $last_changed ) {
283  $last_changed = microtime();
284  wp_cache_set( 'last_changed', $last_changed, 'comment' );
285  }
286  $cache_key = "get_comments:$key:$last_changed";
287 
288  if ( $cache = wp_cache_get( $cache_key, 'comment' ) )
289  return $cache;
290 
292 
293  if ( 'hold' == $status )
294  $approved = "comment_approved = '0'";
295  elseif ( 'approve' == $status )
296  $approved = "comment_approved = '1'";
297  elseif ( ! empty( $status ) && 'all' != $status )
298  $approved = $wpdb->prepare( "comment_approved = %s", $status );
299  else
300  $approved = "( comment_approved = '0' OR comment_approved = '1' )";
301 
302  $order = ( 'ASC' == strtoupper($order) ) ? 'ASC' : 'DESC';
303 
304  if ( ! empty( $orderby ) ) {
305  $ordersby = is_array($orderby) ? $orderby : preg_split('/[,\s]/', $orderby);
306  $allowed_keys = array(
307  'comment_agent',
308  'comment_approved',
309  'comment_author',
310  'comment_author_email',
311  'comment_author_IP',
312  'comment_author_url',
313  'comment_content',
314  'comment_date',
315  'comment_date_gmt',
316  'comment_ID',
317  'comment_karma',
318  'comment_parent',
319  'comment_post_ID',
320  'comment_type',
321  'user_id',
322  );
323  if ( ! empty( $this->query_vars['meta_key'] ) ) {
324  $allowed_keys[] = $this->query_vars['meta_key'];
325  $allowed_keys[] = 'meta_value';
326  $allowed_keys[] = 'meta_value_num';
327  }
328  $ordersby = array_intersect( $ordersby, $allowed_keys );
329  foreach ( $ordersby as $key => $value ) {
330  if ( $value == $this->query_vars['meta_key'] || $value == 'meta_value' ) {
331  $ordersby[ $key ] = "$wpdb->commentmeta.meta_value";
332  } elseif ( $value == 'meta_value_num' ) {
333  $ordersby[ $key ] = "$wpdb->commentmeta.meta_value+0";
334  }
335  }
336  $orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby);
337  } else {
338  $orderby = 'comment_date_gmt';
339  }
340 
341  $number = absint($number);
342  $offset = absint($offset);
343 
344  if ( !empty($number) ) {
345  if ( $offset )
346  $limits = 'LIMIT ' . $offset . ',' . $number;
347  else
348  $limits = 'LIMIT ' . $number;
349  } else {
350  $limits = '';
351  }
352 
353  if ( $count )
354  $fields = 'COUNT(*)';
355  else
356  $fields = '*';
357 
358  $join = '';
359  $where = $approved;
360 
361  if ( ! empty($post_id) )
362  $where .= $wpdb->prepare( ' AND comment_post_ID = %d', $post_id );
363  if ( '' !== $author_email )
364  $where .= $wpdb->prepare( ' AND comment_author_email = %s', $author_email );
365  if ( '' !== $karma )
366  $where .= $wpdb->prepare( ' AND comment_karma = %d', $karma );
367  if ( 'comment' == $type ) {
368  $where .= " AND comment_type = ''";
369  } elseif( 'pings' == $type ) {
370  $where .= ' AND comment_type IN ("pingback", "trackback")';
371  } elseif ( ! empty( $type ) ) {
372  $where .= $wpdb->prepare( ' AND comment_type = %s', $type );
373  }
374  if ( '' !== $parent )
375  $where .= $wpdb->prepare( ' AND comment_parent = %d', $parent );
376 
377  if ( is_array( $user_id ) ) {
378  $where .= ' AND user_id IN (' . implode( ',', array_map( 'absint', $user_id ) ) . ')';
379  } elseif ( '' !== $user_id ) {
380  $where .= $wpdb->prepare( ' AND user_id = %d', $user_id );
381  }
382 
383  if ( '' !== $search )
384  $where .= $this->get_search_sql( $search, array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' ) );
385 
386  $post_fields = array_filter( compact( array( 'post_author', 'post_name', 'post_parent', 'post_status', 'post_type', ) ) );
387  if ( ! empty( $post_fields ) ) {
388  $join = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
389  foreach( $post_fields as $field_name => $field_value )
390  $where .= $wpdb->prepare( " AND {$wpdb->posts}.{$field_name} = %s", $field_value );
391  }
392 
393  if ( ! empty( $this->meta_query->queries ) ) {
394  $clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this );
395  $join .= $clauses['join'];
396  $where .= $clauses['where'];
397  $groupby = "{$wpdb->comments}.comment_ID";
398  }
399 
400  if ( ! empty( $date_query ) && is_array( $date_query ) ) {
401  $date_query_object = new WP_Date_Query( $date_query, 'comment_date' );
402  $where .= $date_query_object->get_sql();
403  }
404 
405  $pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits', 'groupby' );
414  $clauses = apply_filters_ref_array( 'comments_clauses', array( compact( $pieces ), &$this ) );
415  foreach ( $pieces as $piece )
416  $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
417 
418  if ( $groupby )
419  $groupby = 'GROUP BY ' . $groupby;
420 
421  $query = "SELECT $fields FROM $wpdb->comments $join WHERE $where $groupby ORDER BY $orderby $order $limits";
422 
423  if ( $count )
424  return $wpdb->get_var( $query );
425 
426  $comments = $wpdb->get_results( $query );
435  $comments = apply_filters_ref_array( 'the_comments', array( $comments, &$this ) );
436 
437  wp_cache_add( $cache_key, $comments, 'comment' );
438 
439  return $comments;
440  }
441 
452  function get_search_sql( $string, $cols ) {
453  $string = esc_sql( like_escape( $string ) );
454 
455  $searches = array();
456  foreach ( $cols as $col )
457  $searches[] = "$col LIKE '%$string%'";
458 
459  return ' AND (' . implode(' OR ', $searches) . ')';
460  }
461 }
462 
474  $status = array(
475  'hold' => __('Unapproved'),
476  /* translators: comment status */
477  'approve' => _x('Approved', 'adjective'),
478  /* translators: comment status */
479  'spam' => _x('Spam', 'adjective'),
480  );
481 
482  return $status;
483 }
484 
495 function get_lastcommentmodified($timezone = 'server') {
496  global $wpdb;
497  static $cache_lastcommentmodified = array();
498 
499  if ( isset($cache_lastcommentmodified[$timezone]) )
500  return $cache_lastcommentmodified[$timezone];
501 
502  $add_seconds_server = date('Z');
503 
504  switch ( strtolower($timezone)) {
505  case 'gmt':
506  $lastcommentmodified = $wpdb->get_var("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1");
507  break;
508  case 'blog':
509  $lastcommentmodified = $wpdb->get_var("SELECT comment_date FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1");
510  break;
511  case 'server':
512  $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));
513  break;
514  }
515 
516  $cache_lastcommentmodified[$timezone] = $lastcommentmodified;
517 
518  return $lastcommentmodified;
519 }
520 
534 function get_comment_count( $post_id = 0 ) {
535  global $wpdb;
536 
537  $post_id = (int) $post_id;
538 
539  $where = '';
540  if ( $post_id > 0 ) {
541  $where = $wpdb->prepare("WHERE comment_post_ID = %d", $post_id);
542  }
543 
544  $totals = (array) $wpdb->get_results("
545  SELECT comment_approved, COUNT( * ) AS total
546  FROM {$wpdb->comments}
547  {$where}
548  GROUP BY comment_approved
549  ", ARRAY_A);
550 
551  $comment_count = array(
552  "approved" => 0,
553  "awaiting_moderation" => 0,
554  "spam" => 0,
555  "total_comments" => 0
556  );
557 
558  foreach ( $totals as $row ) {
559  switch ( $row['comment_approved'] ) {
560  case 'spam':
561  $comment_count['spam'] = $row['total'];
562  $comment_count["total_comments"] += $row['total'];
563  break;
564  case 1:
565  $comment_count['approved'] = $row['total'];
566  $comment_count['total_comments'] += $row['total'];
567  break;
568  case 0:
569  $comment_count['awaiting_moderation'] = $row['total'];
570  $comment_count['total_comments'] += $row['total'];
571  break;
572  default:
573  break;
574  }
575  }
576 
577  return $comment_count;
578 }
579 
580 //
581 // Comment meta functions
582 //
583 
597 function add_comment_meta($comment_id, $meta_key, $meta_value, $unique = false) {
598  return add_metadata('comment', $comment_id, $meta_key, $meta_value, $unique);
599 }
600 
617 function delete_comment_meta($comment_id, $meta_key, $meta_value = '') {
618  return delete_metadata('comment', $comment_id, $meta_key, $meta_value);
619 }
620 
634 function get_comment_meta($comment_id, $key = '', $single = false) {
635  return get_metadata('comment', $comment_id, $key, $single);
636 }
637 
656 function update_comment_meta($comment_id, $meta_key, $meta_value, $prev_value = '') {
657  return update_metadata('comment', $comment_id, $meta_key, $meta_value, $prev_value);
658 }
659 
669 function wp_set_comment_cookies($comment, $user) {
670  if ( $user->exists() )
671  return;
672 
680  $comment_cookie_lifetime = apply_filters( 'comment_cookie_lifetime', 30000000 );
681  setcookie('comment_author_' . COOKIEHASH, $comment->comment_author, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN);
682  setcookie('comment_author_email_' . COOKIEHASH, $comment->comment_author_email, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN);
683  setcookie('comment_author_url_' . COOKIEHASH, esc_url($comment->comment_author_url), time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN);
684 }
685 
695  if ( isset( $_COOKIE['comment_author_' . COOKIEHASH] ) ) {
706  $comment_author = apply_filters( 'pre_comment_author_name', $_COOKIE['comment_author_' . COOKIEHASH] );
709  $_COOKIE['comment_author_' . COOKIEHASH] = $comment_author;
710  }
711 
712  if ( isset( $_COOKIE['comment_author_email_' . COOKIEHASH] ) ) {
723  $comment_author_email = apply_filters( 'pre_comment_author_email', $_COOKIE['comment_author_email_' . COOKIEHASH] );
726  $_COOKIE['comment_author_email_'.COOKIEHASH] = $comment_author_email;
727  }
728 
729  if ( isset( $_COOKIE['comment_author_url_' . COOKIEHASH] ) ) {
740  $comment_author_url = apply_filters( 'pre_comment_author_url', $_COOKIE['comment_author_url_' . COOKIEHASH] );
742  $_COOKIE['comment_author_url_'.COOKIEHASH] = $comment_author_url;
743  }
744 }
745 
755 function wp_allow_comment($commentdata) {
756  global $wpdb;
757  extract($commentdata, EXTR_SKIP);
758 
759  // Simple duplicate check
760  // expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content)
761  $dupe = $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = %s AND comment_approved != 'trash' AND ( comment_author = %s ", wp_unslash( $comment_post_ID ), wp_unslash( $comment_parent ), wp_unslash( $comment_author ) );
762  if ( $comment_author_email )
763  $dupe .= $wpdb->prepare( "OR comment_author_email = %s ", wp_unslash( $comment_author_email ) );
764  $dupe .= $wpdb->prepare( ") AND comment_content = %s LIMIT 1", wp_unslash( $comment_content ) );
765  if ( $wpdb->get_var($dupe) ) {
773  do_action( 'comment_duplicate_trigger', $commentdata );
774  if ( defined('DOING_AJAX') )
775  die( __('Duplicate comment detected; it looks as though you&#8217;ve already said that!') );
776 
777  wp_die( __('Duplicate comment detected; it looks as though you&#8217;ve already said that!') );
778  }
779 
791  do_action( 'check_comment_flood', $comment_author_IP, $comment_author_email, $comment_date_gmt );
792 
793  if ( ! empty( $user_id ) ) {
794  $user = get_userdata( $user_id );
795  $post_author = $wpdb->get_var($wpdb->prepare("SELECT post_author FROM $wpdb->posts WHERE ID = %d LIMIT 1", $comment_post_ID));
796  }
797 
798  if ( isset( $user ) && ( $user_id == $post_author || $user->has_cap( 'moderate_comments' ) ) ) {
799  // The author and the admins get respect.
800  $approved = 1;
801  } else {
802  // Everyone else's comments will be checked.
804  $approved = 1;
805  else
806  $approved = 0;
808  $approved = 'spam';
809  }
810 
819  $approved = apply_filters( 'pre_comment_approved', $approved, $commentdata );
820  return $approved;
821 }
822 
836 function check_comment_flood_db( $ip, $email, $date ) {
837  global $wpdb;
838  if ( current_user_can( 'manage_options' ) )
839  return; // don't throttle admins
840  $hour_ago = gmdate( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS );
841  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 ) ) ) {
842  $time_lastcomment = mysql2date('U', $lasttime, false);
843  $time_newcomment = mysql2date('U', $date, false);
853  $flood_die = apply_filters( 'comment_flood_filter', false, $time_lastcomment, $time_newcomment );
854  if ( $flood_die ) {
863  do_action( 'comment_flood_trigger', $time_lastcomment, $time_newcomment );
864 
865  if ( defined('DOING_AJAX') )
866  die( __('You are posting comments too quickly. Slow down.') );
867 
868  wp_die( __('You are posting comments too quickly. Slow down.'), '', array('response' => 403) );
869  }
870  }
871 }
872 
882  $comments_by_type = array('comment' => array(), 'trackback' => array(), 'pingback' => array(), 'pings' => array());
883  $count = count($comments);
884  for ( $i = 0; $i < $count; $i++ ) {
885  $type = $comments[$i]->comment_type;
886  if ( empty($type) )
887  $type = 'comment';
888  $comments_by_type[$type][] = &$comments[$i];
889  if ( 'trackback' == $type || 'pingback' == $type )
890  $comments_by_type['pings'][] = &$comments[$i];
891  }
892 
893  return $comments_by_type;
894 }
895 
908 function get_comment_pages_count( $comments = null, $per_page = null, $threaded = null ) {
909  global $wp_query;
910 
911  if ( null === $comments && null === $per_page && null === $threaded && !empty($wp_query->max_num_comment_pages) )
912  return $wp_query->max_num_comment_pages;
913 
914  if ( ( ! $comments || ! is_array( $comments ) ) && ! empty( $wp_query->comments ) )
915  $comments = $wp_query->comments;
916 
917  if ( empty($comments) )
918  return 0;
919 
920  if ( ! get_option( 'page_comments' ) )
921  return 1;
922 
923  if ( !isset($per_page) )
924  $per_page = (int) get_query_var('comments_per_page');
925  if ( 0 === $per_page )
926  $per_page = (int) get_option('comments_per_page');
927  if ( 0 === $per_page )
928  return 1;
929 
930  if ( !isset($threaded) )
931  $threaded = get_option('thread_comments');
932 
933  if ( $threaded ) {
934  $walker = new Walker_Comment;
935  $count = ceil( $walker->get_number_of_root_elements( $comments ) / $per_page );
936  } else {
937  $count = ceil( count( $comments ) / $per_page );
938  }
939 
940  return $count;
941 }
942 
955 function get_page_of_comment( $comment_ID, $args = array() ) {
956  global $wpdb;
957 
958  if ( !$comment = get_comment( $comment_ID ) )
959  return;
960 
961  $defaults = array( 'type' => 'all', 'page' => '', 'per_page' => '', 'max_depth' => '' );
962  $args = wp_parse_args( $args, $defaults );
963 
964  if ( '' === $args['per_page'] && get_option('page_comments') )
965  $args['per_page'] = get_query_var('comments_per_page');
966  if ( empty($args['per_page']) ) {
967  $args['per_page'] = 0;
968  $args['page'] = 0;
969  }
970  if ( $args['per_page'] < 1 )
971  return 1;
972 
973  if ( '' === $args['max_depth'] ) {
974  if ( get_option('thread_comments') )
975  $args['max_depth'] = get_option('thread_comments_depth');
976  else
977  $args['max_depth'] = -1;
978  }
979 
980  // Find this comment's top level parent if threading is enabled
981  if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent )
982  return get_page_of_comment( $comment->comment_parent, $args );
983 
984  $allowedtypes = array(
985  'comment' => '',
986  'pingback' => 'pingback',
987  'trackback' => 'trackback',
988  );
989 
990  $comtypewhere = ( 'all' != $args['type'] && isset($allowedtypes[$args['type']]) ) ? " AND comment_type = '" . $allowedtypes[$args['type']] . "'" : '';
991 
992  // Count comments older than this one
993  $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 ) );
994 
995  // No older comments? Then it's page #1.
996  if ( 0 == $oldercoms )
997  return 1;
998 
999  // Divide comments older than this one by comments per page to get this comment's page number
1000  return ceil( ( $oldercoms + 1 ) / $args['per_page'] );
1001 }
1002 
1016 function wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent) {
1029  do_action( 'wp_blacklist_check', $author, $email, $url, $comment, $user_ip, $user_agent );
1030 
1031  $mod_keys = trim( get_option('blacklist_keys') );
1032  if ( '' == $mod_keys )
1033  return false; // If moderation keys are empty
1034  $words = explode("\n", $mod_keys );
1035 
1036  foreach ( (array) $words as $word ) {
1037  $word = trim($word);
1038 
1039  // Skip empty lines
1040  if ( empty($word) ) { continue; }
1041 
1042  // Do some escaping magic so that '#' chars in the
1043  // spam words don't break things:
1044  $word = preg_quote($word, '#');
1045 
1046  $pattern = "#$word#i";
1047  if (
1048  preg_match($pattern, $author)
1049  || preg_match($pattern, $email)
1050  || preg_match($pattern, $url)
1051  || preg_match($pattern, $comment)
1052  || preg_match($pattern, $user_ip)
1053  || preg_match($pattern, $user_agent)
1054  )
1055  return true;
1056  }
1057  return false;
1058 }
1059 
1076 function wp_count_comments( $post_id = 0 ) {
1077  global $wpdb;
1078 
1079  $post_id = (int) $post_id;
1080 
1089  $stats = apply_filters( 'wp_count_comments', array(), $post_id );
1090  if ( !empty($stats) )
1091  return $stats;
1092 
1093  $count = wp_cache_get("comments-{$post_id}", 'counts');
1094 
1095  if ( false !== $count )
1096  return $count;
1097 
1098  $where = '';
1099  if ( $post_id > 0 )
1100  $where = $wpdb->prepare( "WHERE comment_post_ID = %d", $post_id );
1101 
1102  $count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} {$where} GROUP BY comment_approved", ARRAY_A );
1103 
1104  $total = 0;
1105  $approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed');
1106  foreach ( (array) $count as $row ) {
1107  // Don't count post-trashed toward totals
1108  if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] )
1109  $total += $row['num_comments'];
1110  if ( isset( $approved[$row['comment_approved']] ) )
1111  $stats[$approved[$row['comment_approved']]] = $row['num_comments'];
1112  }
1113 
1114  $stats['total_comments'] = $total;
1115  foreach ( $approved as $key ) {
1116  if ( empty($stats[$key]) )
1117  $stats[$key] = 0;
1118  }
1119 
1120  $stats = (object) $stats;
1121  wp_cache_set("comments-{$post_id}", $stats, 'counts');
1122 
1123  return $stats;
1124 }
1125 
1143 function wp_delete_comment($comment_id, $force_delete = false) {
1144  global $wpdb;
1145  if (!$comment = get_comment($comment_id))
1146  return false;
1147 
1148  if ( !$force_delete && EMPTY_TRASH_DAYS && !in_array( wp_get_comment_status($comment_id), array( 'trash', 'spam' ) ) )
1149  return wp_trash_comment($comment_id);
1150 
1158  do_action( 'delete_comment', $comment_id );
1159 
1160  // Move children up a level.
1161  $children = $wpdb->get_col( $wpdb->prepare("SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment_id) );
1162  if ( !empty($children) ) {
1163  $wpdb->update($wpdb->comments, array('comment_parent' => $comment->comment_parent), array('comment_parent' => $comment_id));
1164  clean_comment_cache($children);
1165  }
1166 
1167  // Delete metadata
1168  $meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->commentmeta WHERE comment_id = %d", $comment_id ) );
1169  foreach ( $meta_ids as $mid )
1170  delete_metadata_by_mid( 'comment', $mid );
1171 
1172  if ( ! $wpdb->delete( $wpdb->comments, array( 'comment_ID' => $comment_id ) ) )
1173  return false;
1174 
1182  do_action( 'deleted_comment', $comment_id );
1183 
1184  $post_id = $comment->comment_post_ID;
1185  if ( $post_id && $comment->comment_approved == 1 )
1187 
1188  clean_comment_cache($comment_id);
1189 
1198  do_action( 'wp_set_comment_status', $comment_id, 'delete' );
1199  wp_transition_comment_status('delete', $comment->comment_approved, $comment);
1200  return true;
1201 }
1202 
1215 function wp_trash_comment($comment_id) {
1216  if ( !EMPTY_TRASH_DAYS )
1217  return wp_delete_comment($comment_id, true);
1218 
1219  if ( !$comment = get_comment($comment_id) )
1220  return false;
1221 
1229  do_action( 'trash_comment', $comment_id );
1230 
1231  if ( wp_set_comment_status($comment_id, 'trash') ) {
1232  add_comment_meta($comment_id, '_wp_trash_meta_status', $comment->comment_approved);
1233  add_comment_meta($comment_id, '_wp_trash_meta_time', time() );
1234 
1242  do_action( 'trashed_comment', $comment_id );
1243  return true;
1244  }
1245 
1246  return false;
1247 }
1248 
1257 function wp_untrash_comment($comment_id) {
1258  if ( ! (int)$comment_id )
1259  return false;
1260 
1268  do_action( 'untrash_comment', $comment_id );
1269 
1270  $status = (string) get_comment_meta($comment_id, '_wp_trash_meta_status', true);
1271  if ( empty($status) )
1272  $status = '0';
1273 
1274  if ( wp_set_comment_status($comment_id, $status) ) {
1275  delete_comment_meta($comment_id, '_wp_trash_meta_time');
1276  delete_comment_meta($comment_id, '_wp_trash_meta_status');
1284  do_action( 'untrashed_comment', $comment_id );
1285  return true;
1286  }
1287 
1288  return false;
1289 }
1290 
1299 function wp_spam_comment($comment_id) {
1300  if ( !$comment = get_comment($comment_id) )
1301  return false;
1302 
1310  do_action( 'spam_comment', $comment_id );
1311 
1312  if ( wp_set_comment_status($comment_id, 'spam') ) {
1313  add_comment_meta($comment_id, '_wp_trash_meta_status', $comment->comment_approved);
1321  do_action( 'spammed_comment', $comment_id );
1322  return true;
1323  }
1324 
1325  return false;
1326 }
1327 
1336 function wp_unspam_comment($comment_id) {
1337  if ( ! (int)$comment_id )
1338  return false;
1339 
1347  do_action( 'unspam_comment', $comment_id );
1348 
1349  $status = (string) get_comment_meta($comment_id, '_wp_trash_meta_status', true);
1350  if ( empty($status) )
1351  $status = '0';
1352 
1353  if ( wp_set_comment_status($comment_id, $status) ) {
1354  delete_comment_meta($comment_id, '_wp_trash_meta_status');
1362  do_action( 'unspammed_comment', $comment_id );
1363  return true;
1364  }
1365 
1366  return false;
1367 }
1368 
1377 function wp_get_comment_status($comment_id) {
1378  $comment = get_comment($comment_id);
1379  if ( !$comment )
1380  return false;
1381 
1382  $approved = $comment->comment_approved;
1383 
1384  if ( $approved == null )
1385  return false;
1386  elseif ( $approved == '1' )
1387  return 'approved';
1388  elseif ( $approved == '0' )
1389  return 'unapproved';
1390  elseif ( $approved == 'spam' )
1391  return 'spam';
1392  elseif ( $approved == 'trash' )
1393  return 'trash';
1394  else
1395  return false;
1396 }
1397 
1418 function wp_transition_comment_status($new_status, $old_status, $comment) {
1419  /*
1420  * Translate raw statuses to human readable formats for the hooks.
1421  * This is not a complete list of comment status, it's only the ones
1422  * that need to be renamed
1423  */
1424  $comment_statuses = array(
1425  0 => 'unapproved',
1426  'hold' => 'unapproved', // wp_set_comment_status() uses "hold"
1427  1 => 'approved',
1428  'approve' => 'approved', // wp_set_comment_status() uses "approve"
1429  );
1430  if ( isset($comment_statuses[$new_status]) ) $new_status = $comment_statuses[$new_status];
1431  if ( isset($comment_statuses[$old_status]) ) $old_status = $comment_statuses[$old_status];
1432 
1433  // Call the hooks
1434  if ( $new_status != $old_status ) {
1444  do_action( 'transition_comment_status', $new_status, $old_status, $comment );
1455  do_action( "comment_{$old_status}_to_{$new_status}", $comment );
1456  }
1471  do_action( "comment_{$new_status}_{$comment->comment_type}", $comment->comment_ID, $comment );
1472 }
1473 
1487  // Cookies should already be sanitized.
1488 
1489  $comment_author = '';
1490  if ( isset($_COOKIE['comment_author_'.COOKIEHASH]) )
1491  $comment_author = $_COOKIE['comment_author_'.COOKIEHASH];
1492 
1493  $comment_author_email = '';
1494  if ( isset($_COOKIE['comment_author_email_'.COOKIEHASH]) )
1495  $comment_author_email = $_COOKIE['comment_author_email_'.COOKIEHASH];
1496 
1497  $comment_author_url = '';
1498  if ( isset($_COOKIE['comment_author_url_'.COOKIEHASH]) )
1499  $comment_author_url = $_COOKIE['comment_author_url_'.COOKIEHASH];
1500 
1510  return apply_filters( 'wp_get_current_commenter', compact('comment_author', 'comment_author_email', 'comment_author_url') );
1511 }
1512 
1525 function wp_insert_comment($commentdata) {
1526  global $wpdb;
1527  extract(wp_unslash($commentdata), EXTR_SKIP);
1528 
1529  if ( ! isset($comment_author_IP) )
1530  $comment_author_IP = '';
1531  if ( ! isset($comment_date) )
1532  $comment_date = current_time('mysql');
1533  if ( ! isset($comment_date_gmt) )
1534  $comment_date_gmt = get_gmt_from_date($comment_date);
1535  if ( ! isset($comment_parent) )
1536  $comment_parent = 0;
1537  if ( ! isset($comment_approved) )
1538  $comment_approved = 1;
1539  if ( ! isset($comment_karma) )
1540  $comment_karma = 0;
1541  if ( ! isset($user_id) )
1542  $user_id = 0;
1543  if ( ! isset($comment_type) )
1544  $comment_type = '';
1545 
1546  $data = 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');
1547  $wpdb->insert($wpdb->comments, $data);
1548 
1549  $id = (int) $wpdb->insert_id;
1550 
1551  if ( $comment_approved == 1 )
1553 
1554  $comment = get_comment($id);
1555 
1564  do_action( 'wp_insert_comment', $id, $comment );
1565 
1566  wp_cache_set( 'last_changed', microtime(), 'comment' );
1567 
1568  return $id;
1569 }
1570 
1583 function wp_filter_comment($commentdata) {
1584  if ( isset( $commentdata['user_ID'] ) ) {
1595  $commentdata['user_id'] = apply_filters( 'pre_user_id', $commentdata['user_ID'] );
1596  } elseif ( isset( $commentdata['user_id'] ) ) {
1598  $commentdata['user_id'] = apply_filters( 'pre_user_id', $commentdata['user_id'] );
1599  }
1600 
1608  $commentdata['comment_agent'] = apply_filters( 'pre_comment_user_agent', ( isset( $commentdata['comment_agent'] ) ? $commentdata['comment_agent'] : '' ) );
1610  $commentdata['comment_author'] = apply_filters( 'pre_comment_author_name', $commentdata['comment_author'] );
1618  $commentdata['comment_content'] = apply_filters( 'pre_comment_content', $commentdata['comment_content'] );
1626  $commentdata['comment_author_IP'] = apply_filters( 'pre_comment_user_ip', $commentdata['comment_author_IP'] );
1628  $commentdata['comment_author_url'] = apply_filters( 'pre_comment_author_url', $commentdata['comment_author_url'] );
1630  $commentdata['comment_author_email'] = apply_filters( 'pre_comment_author_email', $commentdata['comment_author_email'] );
1631  $commentdata['filtered'] = true;
1632  return $commentdata;
1633 }
1634 
1645 function wp_throttle_comment_flood($block, $time_lastcomment, $time_newcomment) {
1646  if ( $block ) // a plugin has already blocked... we'll let that decision stand
1647  return $block;
1648  if ( ($time_newcomment - $time_lastcomment) < 15 )
1649  return true;
1650  return false;
1651 }
1652 
1669 function wp_new_comment( $commentdata ) {
1677  $commentdata = apply_filters( 'preprocess_comment', $commentdata );
1678 
1679  $commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID'];
1680  if ( isset($commentdata['user_ID']) )
1681  $commentdata['user_id'] = $commentdata['user_ID'] = (int) $commentdata['user_ID'];
1682  elseif ( isset($commentdata['user_id']) )
1683  $commentdata['user_id'] = (int) $commentdata['user_id'];
1684 
1685  $commentdata['comment_parent'] = isset($commentdata['comment_parent']) ? absint($commentdata['comment_parent']) : 0;
1686  $parent_status = ( 0 < $commentdata['comment_parent'] ) ? wp_get_comment_status($commentdata['comment_parent']) : '';
1687  $commentdata['comment_parent'] = ( 'approved' == $parent_status || 'unapproved' == $parent_status ) ? $commentdata['comment_parent'] : 0;
1688 
1689  $commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR'] );
1690  $commentdata['comment_agent'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? substr( $_SERVER['HTTP_USER_AGENT'], 0, 254 ) : '';
1691 
1692  $commentdata['comment_date'] = current_time('mysql');
1693  $commentdata['comment_date_gmt'] = current_time('mysql', 1);
1694 
1695  $commentdata = wp_filter_comment($commentdata);
1696 
1697  $commentdata['comment_approved'] = wp_allow_comment($commentdata);
1698 
1699  $comment_ID = wp_insert_comment($commentdata);
1700 
1709  do_action( 'comment_post', $comment_ID, $commentdata['comment_approved'] );
1710 
1711  if ( 'spam' !== $commentdata['comment_approved'] ) { // If it's spam save it silently for later crunching
1712  if ( '0' == $commentdata['comment_approved'] ) {
1713  wp_notify_moderator( $comment_ID );
1714  }
1715 
1716  // wp_notify_postauthor() checks if notifying the author of their own comment.
1717  // By default, it won't, but filters can override this.
1718  if ( get_option( 'comments_notify' ) && $commentdata['comment_approved'] ) {
1719  wp_notify_postauthor( $comment_ID );
1720  }
1721  }
1722 
1723  return $comment_ID;
1724 }
1725 
1740 function wp_set_comment_status($comment_id, $comment_status, $wp_error = false) {
1741  global $wpdb;
1742 
1743  $status = '0';
1744  switch ( $comment_status ) {
1745  case 'hold':
1746  case '0':
1747  $status = '0';
1748  break;
1749  case 'approve':
1750  case '1':
1751  $status = '1';
1752  if ( get_option('comments_notify') ) {
1753  wp_notify_postauthor( $comment_id );
1754  }
1755  break;
1756  case 'spam':
1757  $status = 'spam';
1758  break;
1759  case 'trash':
1760  $status = 'trash';
1761  break;
1762  default:
1763  return false;
1764  }
1765 
1766  $comment_old = clone get_comment($comment_id);
1767 
1768  if ( !$wpdb->update( $wpdb->comments, array('comment_approved' => $status), array('comment_ID' => $comment_id) ) ) {
1769  if ( $wp_error )
1770  return new WP_Error('db_update_error', __('Could not update comment status'), $wpdb->last_error);
1771  else
1772  return false;
1773  }
1774 
1775  clean_comment_cache($comment_id);
1776 
1777  $comment = get_comment($comment_id);
1778 
1790  do_action( 'wp_set_comment_status', $comment_id, $comment_status );
1791  wp_transition_comment_status($comment_status, $comment_old->comment_approved, $comment);
1792 
1793  wp_update_comment_count($comment->comment_post_ID);
1794 
1795  return true;
1796 }
1797 
1810 function wp_update_comment($commentarr) {
1811  global $wpdb;
1812 
1813  // First, get all of the original fields
1814  $comment = get_comment($commentarr['comment_ID'], ARRAY_A);
1815  if ( empty( $comment ) )
1816  return 0;
1817 
1818  // Escape data pulled from DB.
1819  $comment = wp_slash($comment);
1820 
1821  $old_status = $comment['comment_approved'];
1822 
1823  // Merge old and new fields with new fields overwriting old ones.
1824  $commentarr = array_merge($comment, $commentarr);
1825 
1826  $commentarr = wp_filter_comment( $commentarr );
1827 
1828  // Now extract the merged array.
1829  extract(wp_unslash($commentarr), EXTR_SKIP);
1830 
1838  $comment_content = apply_filters( 'comment_save_pre', $comment_content );
1839 
1840  $comment_date_gmt = get_gmt_from_date($comment_date);
1841 
1842  if ( !isset($comment_approved) )
1843  $comment_approved = 1;
1844  else if ( 'hold' == $comment_approved )
1845  $comment_approved = 0;
1846  else if ( 'approve' == $comment_approved )
1847  $comment_approved = 1;
1848 
1849  $data = compact( 'comment_content', 'comment_author', 'comment_author_email', 'comment_approved', 'comment_karma', 'comment_author_url', 'comment_date', 'comment_date_gmt', 'comment_parent' );
1850  $rval = $wpdb->update( $wpdb->comments, $data, compact( 'comment_ID' ) );
1851 
1852  clean_comment_cache($comment_ID);
1863  do_action( 'edit_comment', $comment_ID );
1864  $comment = get_comment($comment_ID);
1865  wp_transition_comment_status($comment->comment_approved, $old_status, $comment);
1866  return $rval;
1867 }
1868 
1884  static $_defer = false;
1885 
1886  if ( is_bool($defer) ) {
1887  $_defer = $defer;
1888  // flush any deferred counts
1889  if ( !$defer )
1890  wp_update_comment_count( null, true );
1891  }
1892 
1893  return $_defer;
1894 }
1895 
1914 function wp_update_comment_count($post_id, $do_deferred=false) {
1915  static $_deferred = array();
1916 
1917  if ( $do_deferred ) {
1918  $_deferred = array_unique($_deferred);
1919  foreach ( $_deferred as $i => $_post_id ) {
1920  wp_update_comment_count_now($_post_id);
1921  unset( $_deferred[$i] );
1922  }
1923  }
1924 
1925  if ( wp_defer_comment_counting() ) {
1926  $_deferred[] = $post_id;
1927  return true;
1928  }
1929  elseif ( $post_id ) {
1931  }
1932 
1933 }
1934 
1945  global $wpdb;
1946  $post_id = (int) $post_id;
1947  if ( !$post_id )
1948  return false;
1949  if ( !$post = get_post($post_id) )
1950  return false;
1951 
1952  $old = (int) $post->comment_count;
1953  $new = (int) $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id) );
1954  $wpdb->update( $wpdb->posts, array('comment_count' => $new), array('ID' => $post_id) );
1955 
1957 
1967  do_action( 'wp_update_comment_count', $post_id, $new, $old );
1969  do_action( 'edit_post', $post_id, $post );
1970 
1971  return true;
1972 }
1973 
1974 //
1975 // Ping and trackback functions.
1976 //
1977 
1991 function discover_pingback_server_uri( $url, $deprecated = '' ) {
1992  if ( !empty( $deprecated ) )
1993  _deprecated_argument( __FUNCTION__, '2.7' );
1994 
1995  $pingback_str_dquote = 'rel="pingback"';
1996  $pingback_str_squote = 'rel=\'pingback\'';
1997 
1999  $parsed_url = parse_url($url);
2000 
2001  if ( ! isset( $parsed_url['host'] ) ) // Not an URL. This should never happen.
2002  return false;
2003 
2004  //Do not search for a pingback server on our own uploads
2005  $uploads_dir = wp_upload_dir();
2006  if ( 0 === strpos($url, $uploads_dir['baseurl']) )
2007  return false;
2008 
2009  $response = wp_safe_remote_head( $url, array( 'timeout' => 2, 'httpversion' => '1.0' ) );
2010 
2011  if ( is_wp_error( $response ) )
2012  return false;
2013 
2014  if ( wp_remote_retrieve_header( $response, 'x-pingback' ) )
2015  return wp_remote_retrieve_header( $response, 'x-pingback' );
2016 
2017  // Not an (x)html, sgml, or xml page, no use going further.
2018  if ( preg_match('#(image|audio|video|model)/#is', wp_remote_retrieve_header( $response, 'content-type' )) )
2019  return false;
2020 
2021  // Now do a GET since we're going to look in the html headers (and we're sure it's not a binary file)
2022  $response = wp_safe_remote_get( $url, array( 'timeout' => 2, 'httpversion' => '1.0' ) );
2023 
2024  if ( is_wp_error( $response ) )
2025  return false;
2026 
2027  $contents = wp_remote_retrieve_body( $response );
2028 
2029  $pingback_link_offset_dquote = strpos($contents, $pingback_str_dquote);
2030  $pingback_link_offset_squote = strpos($contents, $pingback_str_squote);
2031  if ( $pingback_link_offset_dquote || $pingback_link_offset_squote ) {
2032  $quote = ($pingback_link_offset_dquote) ? '"' : '\'';
2033  $pingback_link_offset = ($quote=='"') ? $pingback_link_offset_dquote : $pingback_link_offset_squote;
2034  $pingback_href_pos = @strpos($contents, 'href=', $pingback_link_offset);
2035  $pingback_href_start = $pingback_href_pos+6;
2036  $pingback_href_end = @strpos($contents, $quote, $pingback_href_start);
2037  $pingback_server_url_len = $pingback_href_end - $pingback_href_start;
2038  $pingback_server_url = substr($contents, $pingback_href_start, $pingback_server_url_len);
2039 
2040  // We may find rel="pingback" but an incomplete pingback URL
2041  if ( $pingback_server_url_len > 0 ) { // We got it!
2042  return $pingback_server_url;
2043  }
2044  }
2045 
2046  return false;
2047 }
2048 
2055 function do_all_pings() {
2056  global $wpdb;
2057 
2058  // Do pingbacks
2059  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")) {
2060  delete_metadata_by_mid( 'post', $ping->meta_id );
2061  pingback( $ping->post_content, $ping->ID );
2062  }
2063 
2064  // Do Enclosures
2065  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")) {
2066  delete_metadata_by_mid( 'post', $enclosure->meta_id );
2067  do_enclose( $enclosure->post_content, $enclosure->ID );
2068  }
2069 
2070  // Do Trackbacks
2071  $trackbacks = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE to_ping <> '' AND post_status = 'publish'");
2072  if ( is_array($trackbacks) )
2073  foreach ( $trackbacks as $trackback )
2074  do_trackbacks($trackback);
2075 
2076  //Do Update Services/Generic Pings
2077  generic_ping();
2078 }
2079 
2089  global $wpdb;
2090 
2091  $post = get_post( $post_id );
2092  $to_ping = get_to_ping($post_id);
2093  $pinged = get_pung($post_id);
2094  if ( empty($to_ping) ) {
2095  $wpdb->update($wpdb->posts, array('to_ping' => ''), array('ID' => $post_id) );
2096  return;
2097  }
2098 
2099  if ( empty($post->post_excerpt) ) {
2101  $excerpt = apply_filters( 'the_content', $post->post_content, $post->ID );
2102  } else {
2104  $excerpt = apply_filters( 'the_excerpt', $post->post_excerpt );
2105  }
2106 
2107  $excerpt = str_replace(']]>', ']]&gt;', $excerpt);
2108  $excerpt = wp_html_excerpt($excerpt, 252, '&#8230;');
2109 
2111  $post_title = apply_filters( 'the_title', $post->post_title, $post->ID );
2112  $post_title = strip_tags($post_title);
2113 
2114  if ( $to_ping ) {
2115  foreach ( (array) $to_ping as $tb_ping ) {
2116  $tb_ping = trim($tb_ping);
2117  if ( !in_array($tb_ping, $pinged) ) {
2118  trackback($tb_ping, $post_title, $excerpt, $post_id);
2119  $pinged[] = $tb_ping;
2120  } else {
2121  $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, '')) WHERE ID = %d", $tb_ping, $post_id) );
2122  }
2123  }
2124  }
2125 }
2126 
2135 function generic_ping($post_id = 0) {
2136  $services = get_option('ping_sites');
2137 
2138  $services = explode("\n", $services);
2139  foreach ( (array) $services as $service ) {
2140  $service = trim($service);
2141  if ( '' != $service )
2142  weblog_ping($service);
2143  }
2144 
2145  return $post_id;
2146 }
2147 
2159  global $wp_version;
2160  include_once(ABSPATH . WPINC . '/class-IXR.php');
2161  include_once(ABSPATH . WPINC . '/class-wp-http-ixr-client.php');
2162 
2163  // original code by Mort (http://mort.mine.nu:8080)
2164  $post_links = array();
2165 
2166  $pung = get_pung($post_ID);
2167 
2168  // Step 1
2169  // Parsing the post, external links (if any) are stored in the $post_links array
2170  $post_links_temp = wp_extract_urls( $content );
2171 
2172  // Step 2.
2173  // Walking thru the links array
2174  // first we get rid of links pointing to sites, not to specific files
2175  // Example:
2176  // http://dummy-weblog.org
2177  // http://dummy-weblog.org/
2178  // http://dummy-weblog.org/post.php
2179  // We don't wanna ping first and second types, even if they have a valid <link/>
2180 
2181  foreach ( (array) $post_links_temp as $link_test ) :
2182  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
2183  && !is_local_attachment($link_test) ) : // Also, let's never ping local attachments.
2184  if ( $test = @parse_url($link_test) ) {
2185  if ( isset($test['query']) )
2186  $post_links[] = $link_test;
2187  elseif ( isset( $test['path'] ) && ( $test['path'] != '/' ) && ( $test['path'] != '' ) )
2188  $post_links[] = $link_test;
2189  }
2190  endif;
2191  endforeach;
2192 
2193  $post_links = array_unique( $post_links );
2203  do_action_ref_array( 'pre_ping', array( &$post_links, &$pung, $post_ID ) );
2204 
2205  foreach ( (array) $post_links as $pagelinkedto ) {
2206  $pingback_server_url = discover_pingback_server_uri( $pagelinkedto );
2207 
2208  if ( $pingback_server_url ) {
2209  @ set_time_limit( 60 );
2210  // Now, the RPC call
2211  $pagelinkedfrom = get_permalink($post_ID);
2212 
2213  // using a timeout of 3 seconds should be enough to cover slow servers
2214  $client = new WP_HTTP_IXR_Client($pingback_server_url);
2215  $client->timeout = 3;
2228  $client->useragent = apply_filters( 'pingback_useragent', $client->useragent . ' -- WordPress/' . $wp_version, $client->useragent, $pingback_server_url, $pagelinkedto, $pagelinkedfrom );
2229  // when set to true, this outputs debug messages by itself
2230  $client->debug = false;
2231 
2232  if ( $client->query('pingback.ping', $pagelinkedfrom, $pagelinkedto) || ( isset($client->error->code) && 48 == $client->error->code ) ) // Already registered
2233  add_ping( $post_ID, $pagelinkedto );
2234  }
2235  }
2236 }
2237 
2246 function privacy_ping_filter($sites) {
2247  if ( '0' != get_option('blog_public') )
2248  return $sites;
2249  else
2250  return '';
2251 }
2252 
2267 function trackback($trackback_url, $title, $excerpt, $ID) {
2268  global $wpdb;
2269 
2270  if ( empty($trackback_url) )
2271  return;
2272 
2273  $options = array();
2274  $options['timeout'] = 4;
2275  $options['body'] = array(
2276  'title' => $title,
2277  'url' => get_permalink($ID),
2278  'blog_name' => get_option('blogname'),
2279  'excerpt' => $excerpt
2280  );
2281 
2282  $response = wp_safe_remote_post( $trackback_url, $options );
2283 
2284  if ( is_wp_error( $response ) )
2285  return;
2286 
2287  $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', %s) WHERE ID = %d", $trackback_url, $ID) );
2288  return $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, '')) WHERE ID = %d", $trackback_url, $ID) );
2289 }
2290 
2301 function weblog_ping($server = '', $path = '') {
2302  global $wp_version;
2303  include_once(ABSPATH . WPINC . '/class-IXR.php');
2304  include_once(ABSPATH . WPINC . '/class-wp-http-ixr-client.php');
2305 
2306  // using a timeout of 3 seconds should be enough to cover slow servers
2307  $client = new WP_HTTP_IXR_Client($server, ((!strlen(trim($path)) || ('/' == $path)) ? false : $path));
2308  $client->timeout = 3;
2309  $client->useragent .= ' -- WordPress/'.$wp_version;
2310 
2311  // when set to true, this outputs debug messages by itself
2312  $client->debug = false;
2313  $home = trailingslashit( home_url() );
2314  if ( !$client->query('weblogUpdates.extendedPing', get_option('blogname'), $home, get_bloginfo('rss2_url') ) ) // then try a normal ping
2315  $client->query('weblogUpdates.ping', get_option('blogname'), $home);
2316 }
2317 
2327 function pingback_ping_source_uri( $source_uri ) {
2328  return (string) wp_http_validate_url( $source_uri );
2329 }
2330 
2343 function xmlrpc_pingback_error( $ixr_error ) {
2344  if ( $ixr_error->code === 48 )
2345  return $ixr_error;
2346  return new IXR_Error( 0, '' );
2347 }
2348 
2349 //
2350 // Cache
2351 //
2352 
2360 function clean_comment_cache($ids) {
2361  foreach ( (array) $ids as $id )
2362  wp_cache_delete($id, 'comment');
2363 
2364  wp_cache_set( 'last_changed', microtime(), 'comment' );
2365 }
2366 
2379  foreach ( (array) $comments as $comment )
2380  wp_cache_add($comment->comment_ID, $comment, 'comment');
2381 }
2382 
2383 //
2384 // Internal
2385 //
2386 
2397 function _close_comments_for_old_posts( $posts, $query ) {
2398  if ( empty( $posts ) || ! $query->is_singular() || ! get_option( 'close_comments_for_old_posts' ) )
2399  return $posts;
2400 
2408  $post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) );
2409  if ( ! in_array( $posts[0]->post_type, $post_types ) )
2410  return $posts;
2411 
2412  $days_old = (int) get_option( 'close_comments_days_old' );
2413  if ( ! $days_old )
2414  return $posts;
2415 
2416  if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( $days_old * DAY_IN_SECONDS ) ) {
2417  $posts[0]->comment_status = 'closed';
2418  $posts[0]->ping_status = 'closed';
2419  }
2420 
2421  return $posts;
2422 }
2423 
2435  if ( ! $open )
2436  return $open;
2437 
2438  if ( !get_option('close_comments_for_old_posts') )
2439  return $open;
2440 
2441  $days_old = (int) get_option('close_comments_days_old');
2442  if ( !$days_old )
2443  return $open;
2444 
2445  $post = get_post($post_id);
2446 
2448  $post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) );
2449  if ( ! in_array( $post->post_type, $post_types ) )
2450  return $open;
2451 
2452  if ( time() - strtotime( $post->post_date_gmt ) > ( $days_old * DAY_IN_SECONDS ) )
2453  return false;
2454 
2455  return $open;
2456 }
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:2327
$content
Definition: press-this.php:377
wp_remote_retrieve_body(&$response)
Definition: http.php:290
apply_filters_ref_array($tag, $args)
Definition: plugin.php:217
like_escape($text)
endif
Definition: about.php:247
$user
delete_comment_meta($comment_id, $meta_key, $meta_value= '')
Definition: comment.php:617
do_action($tag, $arg= '')
Definition: plugin.php:427
wp_trash_comment($comment_id)
Definition: comment.php:1215
$user_id
Definition: user-edit.php:14
$_COOKIE
Definition: xmlrpc.php:16
$post_title
Definition: revision.php:76
wp_get_current_commenter()
Definition: comment.php:1486
s &</h2 > if($message) $_SERVER['REQUEST_URI']
Definition: edit-tags.php:311
get_approved_comments($post_id)
Definition: comment.php:117
add_ping($post_id, $uri)
Definition: post.php:3706
$post
Definition: post.php:26
wp_die($message= '', $title= '', $args=array())
Definition: functions.php:2243
_close_comments_for_old_posts($posts, $query)
Definition: comment.php:2397
$post_id
wp_delete_comment($comment_id, $force_delete=false)
Definition: comment.php:1143
privacy_ping_filter($sites)
Definition: comment.php:2246
const ABSPATH
get_option()
trackback($trackback_url, $title, $excerpt, $ID)
Definition: comment.php:2267
wp_remote_retrieve_header(&$response, $header)
Definition: http.php:238
$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')
get_pung($post_id)
Definition: post.php:3772
sanitize_comment_cookies()
Definition: comment.php:694
update_comment_meta($comment_id, $meta_key, $meta_value, $prev_value= '')
Definition: comment.php:656
wp_slash($value)
wp_extract_urls($content)
Definition: functions.php:445
COOKIE_DOMAIN
Definition: wp-login.php:427
check_comment_flood_db($ip, $email, $date)
Definition: comment.php:836
delete_metadata_by_mid($meta_type, $meta_id)
Definition: meta.php:650
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:473
do_action_ref_array()
$path
Definition: wp-load.php:53
is_wp_error($thing)
esc_attr()
wp_unspam_comment($comment_id)
Definition: comment.php:1336
generic_ping($post_id=0)
Definition: comment.php:2135
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) add_meta_box('postimagediv' __('Featured Image')
$out
wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent)
Definition: comment.php:1016
wp_cache_set($key, $data, $group= '', $expire=0)
Definition: cache.php:177
get_lastcommentmodified($timezone= 'server')
Definition: comment.php:495
get_comment_count($post_id=0)
Definition: comment.php:534
wp_set_comment_status($comment_id, $comment_status, $wp_error=false)
Definition: comment.php:1740
const ARRAY_A
Definition: wp-db.php:31
get_metadata($meta_type, $object_id, $meta_key= '', $single=false)
Definition: meta.php:425
if(empty($post->comment_status)) $status
get_post($post=null, $output=OBJECT, $filter= 'raw')
Definition: post.php:414
$title
Definition: about.php:16
separate_comments(&$comments)
Definition: comment.php:881
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:44
wp_safe_remote_get($url, $args=array())
Definition: http.php:67
wp_filter_comment($commentdata)
Definition: comment.php:1583
comment_ID()
get_comment_meta($comment_id, $key= '', $single=false)
Definition: comment.php:634
weblog_ping($server= '', $path= '')
Definition: comment.php:2301
$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:66
get_to_ping($post_id)
Definition: post.php:3798
$comments
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:471
delete_metadata($meta_type, $object_id, $meta_key, $meta_value= '', $delete_all=false)
Definition: meta.php:283
$excerpt
const WPINC
wp_count_comments($post_id=0)
Definition: comment.php:1076
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:1883
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:1645
clean_comment_cache($ids)
Definition: comment.php:2360
clean_post_cache($post)
Definition: post.php:5290
get_comment_pages_count($comments=null, $per_page=null, $threaded=null)
Definition: comment.php:908
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:1377
wp_untrash_comment($comment_id)
Definition: comment.php:1257
$ID
get_page_of_comment($comment_ID, $args=array())
Definition: comment.php:955
_close_comments_for_old_post($open, $post_id)
Definition: comment.php:2434
wp_parse_args($args, $defaults= '')
Definition: functions.php:2750
wp_html_excerpt($str, $count, $more=null)
get_comments($args= '')
Definition: comment.php:194
_deprecated_argument($function, $version, $message=null)
Definition: functions.php:3170
is_local_attachment($url)
Definition: post.php:4373
get_search_sql($string, $cols)
Definition: comment.php:452
wp_set_comment_cookies($comment, $user)
Definition: comment.php:669
discover_pingback_server_uri($url, $deprecated= '')
Definition: comment.php:1991
update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_value= '')
Definition: meta.php:136
query($query_vars)
Definition: comment.php:231
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:1669
global $wp_version
wp_spam_comment($comment_id)
Definition: comment.php:1299
current_time($type, $gmt=0)
Definition: functions.php:60
wp_update_comment($commentarr)
Definition: comment.php:1810
esc_sql($data)
do_all_pings()
Definition: comment.php:2055
get_query_var($var, $default= '')
Definition: query.php:25
esc_url($url, $protocols=null, $_context= 'display')
do_trackbacks($post_id)
Definition: comment.php:2088
$comment_author
add_comment_meta($comment_id, $meta_key, $meta_value, $unique=false)
Definition: comment.php:597
xmlrpc_pingback_error($ixr_error)
Definition: comment.php:2343
wp_http_validate_url($url)
Definition: http.php:453
wp_insert_comment($commentdata)
Definition: comment.php:1525
if(!wp_next_scheduled('wp_scheduled_delete')&&!defined('WP_INSTALLING')) wp_schedule_event(time()
Definition: admin.php:82
wp_allow_comment($commentdata)
Definition: comment.php:755
pingback($content, $post_ID)
Definition: comment.php:2158
wp_upload_dir($time=null)
Definition: functions.php:1638
$comment_content
get_gmt_from_date($string, $format= 'Y-m-d H:i:s')
if(!empty($_GET['s'])) if(!empty($selection)) $url
Definition: press-this.php:105
wp_update_comment_count_now($post_id)
Definition: comment.php:1944
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:1418
update_comment_cache($comments)
Definition: comment.php:2378
$GLOBALS['wp_embed']
wp_update_comment_count($post_id, $do_deferred=false)
Definition: comment.php:1914
current_user_can($capability)
endforeach
Definition: export.php:189