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

comment.php
Go to the documentation of this file.
1 <?php
39 function check_comment($author, $email, $url, $comment, $user_ip, $user_agent, $comment_type) {
40  global $wpdb;
41 
42  // If manual moderation is enabled, skip all checks and return false.
43  if ( 1 == get_option('comment_moderation') )
44  return false;
45 
47  $comment = apply_filters( 'comment_text', $comment );
48 
49  // Check for the number of external links if a max allowed number is set.
50  if ( $max_links = get_option( 'comment_max_links' ) ) {
51  $num_links = preg_match_all( '/<a [^>]*href/i', $comment, $out );
52 
61  $num_links = apply_filters( 'comment_max_links_url', $num_links, $url );
62 
63  /*
64  * If the number of links in the comment exceeds the allowed amount,
65  * fail the check by returning false.
66  */
67  if ( $num_links >= $max_links )
68  return false;
69  }
70 
71  $mod_keys = trim(get_option('moderation_keys'));
72 
73  // If moderation 'keys' (keywords) are set, process them.
74  if ( !empty($mod_keys) ) {
75  $words = explode("\n", $mod_keys );
76 
77  foreach ( (array) $words as $word) {
78  $word = trim($word);
79 
80  // Skip empty lines.
81  if ( empty($word) )
82  continue;
83 
84  /*
85  * Do some escaping magic so that '#' (number of) characters in the spam
86  * words don't break things:
87  */
88  $word = preg_quote($word, '#');
89 
90  /*
91  * Check the comment fields for moderation keywords. If any are found,
92  * fail the check for the given field by returning false.
93  */
94  $pattern = "#$word#i";
95  if ( preg_match($pattern, $author) ) return false;
96  if ( preg_match($pattern, $email) ) return false;
97  if ( preg_match($pattern, $url) ) return false;
98  if ( preg_match($pattern, $comment) ) return false;
99  if ( preg_match($pattern, $user_ip) ) return false;
100  if ( preg_match($pattern, $user_agent) ) return false;
101  }
102  }
103 
104  /*
105  * Check if the option to approve comments by previously-approved authors is enabled.
106  *
107  * If it is enabled, check whether the comment author has a previously-approved comment,
108  * as well as whether there are any moderation keywords (if set) present in the author
109  * email address. If both checks pass, return true. Otherwise, return false.
110  */
111  if ( 1 == get_option('comment_whitelist')) {
112  if ( 'trackback' != $comment_type && 'pingback' != $comment_type && $author != '' && $email != '' ) {
113  // expected_slashed ($author, $email)
114  $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");
115  if ( ( 1 == $ok_to_comment ) &&
116  ( empty($mod_keys) || false === strpos( $email, $mod_keys) ) )
117  return true;
118  else
119  return false;
120  } else {
121  return false;
122  }
123  }
124  return true;
125 }
126 
139 function get_approved_comments( $post_id, $args = array() ) {
140  if ( ! $post_id ) {
141  return array();
142  }
143 
144  $defaults = array(
145  'status' => 1,
146  'post_id' => $post_id,
147  'order' => 'ASC',
148  );
149  $r = wp_parse_args( $args, $defaults );
150 
151  $query = new WP_Comment_Query;
152  return $query->query( $r );
153 }
154 
171 function get_comment(&$comment, $output = OBJECT) {
172  global $wpdb;
173 
174  if ( empty($comment) ) {
175  if ( isset($GLOBALS['comment']) )
176  $_comment = & $GLOBALS['comment'];
177  else
178  $_comment = null;
179  } elseif ( is_object($comment) ) {
180  wp_cache_add($comment->comment_ID, $comment, 'comment');
181  $_comment = $comment;
182  } else {
183  if ( isset($GLOBALS['comment']) && ($GLOBALS['comment']->comment_ID == $comment) ) {
184  $_comment = & $GLOBALS['comment'];
185  } elseif ( ! $_comment = wp_cache_get($comment, 'comment') ) {
186  $_comment = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment));
187  if ( ! $_comment )
188  return null;
189  wp_cache_add($_comment->comment_ID, $_comment, 'comment');
190  }
191  }
192 
200  $_comment = apply_filters( 'get_comment', $_comment );
201 
202  if ( $output == OBJECT ) {
203  return $_comment;
204  } elseif ( $output == ARRAY_A ) {
205  $__comment = get_object_vars($_comment);
206  return $__comment;
207  } elseif ( $output == ARRAY_N ) {
208  $__comment = array_values(get_object_vars($_comment));
209  return $__comment;
210  } else {
211  return $_comment;
212  }
213 }
214 
226 function get_comments( $args = '' ) {
227  $query = new WP_Comment_Query;
228  return $query->query( $args );
229 }
230 
246  public $request;
247 
255  public $meta_query = false;
256 
264  public $date_query = false;
265 
273  public $query_vars;
274 
283 
291  public $comments;
292 
303  public function __call( $name, $arguments ) {
304  if ( 'get_search_sql' === $name ) {
305  return call_user_func_array( array( $this, $name ), $arguments );
306  }
307  return false;
308  }
309 
382  public function __construct( $query = '' ) {
383  $this->query_var_defaults = array(
384  'author_email' => '',
385  'author__in' => '',
386  'author__not_in' => '',
387  'include_unapproved' => '',
388  'fields' => '',
389  'ID' => '',
390  'comment__in' => '',
391  'comment__not_in' => '',
392  'karma' => '',
393  'number' => '',
394  'offset' => '',
395  'orderby' => '',
396  'order' => 'DESC',
397  'parent' => '',
398  'post_author__in' => '',
399  'post_author__not_in' => '',
400  'post_ID' => '',
401  'post_id' => 0,
402  'post__in' => '',
403  'post__not_in' => '',
404  'post_author' => '',
405  'post_name' => '',
406  'post_parent' => '',
407  'post_status' => '',
408  'post_type' => '',
409  'status' => 'all',
410  'type' => '',
411  'type__in' => '',
412  'type__not_in' => '',
413  'user_id' => '',
414  'search' => '',
415  'count' => false,
416  'meta_key' => '',
417  'meta_value' => '',
418  'meta_query' => '',
419  'date_query' => null, // See WP_Date_Query
420  );
421 
422  if ( ! empty( $query ) ) {
423  $this->query( $query );
424  }
425  }
426 
436  public function parse_query( $query = '' ) {
437  if ( empty( $query ) ) {
438  $query = $this->query_vars;
439  }
440 
441  $this->query_vars = wp_parse_args( $query, $this->query_var_defaults );
442  do_action_ref_array( 'parse_comment_query', array( &$this ) );
443  }
444 
459  public function query( $query ) {
460  $this->query_vars = wp_parse_args( $query );
461  return $this->get_comments();
462  }
463 
474  public function get_comments() {
475  global $wpdb;
476 
477  $groupby = '';
478 
479  $this->parse_query();
480 
481  // Parse meta query
482  $this->meta_query = new WP_Meta_Query();
483  $this->meta_query->parse_query_vars( $this->query_vars );
484 
492  do_action_ref_array( 'pre_get_comments', array( &$this ) );
493 
494  // Reparse query vars, in case they were modified in a 'pre_get_comments' callback.
495  $this->meta_query->parse_query_vars( $this->query_vars );
496  if ( ! empty( $this->meta_query->queries ) ) {
497  $meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this );
498  }
499 
500  // $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
501  $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) );
502  $last_changed = wp_cache_get( 'last_changed', 'comment' );
503  if ( ! $last_changed ) {
504  $last_changed = microtime();
505  wp_cache_set( 'last_changed', $last_changed, 'comment' );
506  }
507  $cache_key = "get_comments:$key:$last_changed";
508 
509  if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
510  $this->comments = $cache;
511  return $this->comments;
512  }
513 
514  $where = array();
515 
516  // Assemble clauses related to 'comment_approved'.
517  $approved_clauses = array();
518 
519  // 'status' accepts an array or a comma-separated string.
520  $status_clauses = array();
521  $statuses = $this->query_vars['status'];
522  if ( ! is_array( $statuses ) ) {
523  $statuses = preg_split( '/[\s,]+/', $statuses );
524  }
525 
526  // 'any' overrides other statuses.
527  if ( ! in_array( 'any', $statuses ) ) {
528  foreach ( $statuses as $status ) {
529  switch ( $status ) {
530  case 'hold' :
531  $status_clauses[] = "comment_approved = '0'";
532  break;
533 
534  case 'approve' :
535  $status_clauses[] = "comment_approved = '1'";
536  break;
537 
538  case 'all' :
539  case '' :
540  $status_clauses[] = "( comment_approved = '0' OR comment_approved = '1' )";
541  break;
542 
543  default :
544  $status_clauses[] = $wpdb->prepare( "comment_approved = %s", $status );
545  break;
546  }
547  }
548 
549  if ( ! empty( $status_clauses ) ) {
550  $approved_clauses[] = '( ' . implode( ' OR ', $status_clauses ) . ' )';
551  }
552  }
553 
554  // User IDs or emails whose unapproved comments are included, regardless of $status.
555  if ( ! empty( $this->query_vars['include_unapproved'] ) ) {
556  $include_unapproved = $this->query_vars['include_unapproved'];
557 
558  // Accepts arrays or comma-separated strings.
559  if ( ! is_array( $include_unapproved ) ) {
560  $include_unapproved = preg_split( '/[\s,]+/', $include_unapproved );
561  }
562 
563  $unapproved_ids = $unapproved_emails = array();
564  foreach ( $include_unapproved as $unapproved_identifier ) {
565  // Numeric values are assumed to be user ids.
566  if ( is_numeric( $unapproved_identifier ) ) {
567  $approved_clauses[] = $wpdb->prepare( "( user_id = %d AND comment_approved = '0' )", $unapproved_identifier );
568 
569  // Otherwise we match against email addresses.
570  } else {
571  $approved_clauses[] = $wpdb->prepare( "( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier );
572  }
573  }
574  }
575 
576  // Collapse comment_approved clauses into a single OR-separated clause.
577  if ( ! empty( $approved_clauses ) ) {
578  if ( 1 === count( $approved_clauses ) ) {
579  $where[] = $approved_clauses[0];
580  } else {
581  $where[] = '( ' . implode( ' OR ', $approved_clauses ) . ' )';
582  }
583  }
584 
585  $order = ( 'ASC' == strtoupper( $this->query_vars['order'] ) ) ? 'ASC' : 'DESC';
586 
587  // Disable ORDER BY with 'none', an empty array, or boolean false.
588  if ( in_array( $this->query_vars['orderby'], array( 'none', array(), false ), true ) ) {
589  $orderby = '';
590  } elseif ( ! empty( $this->query_vars['orderby'] ) ) {
591  $ordersby = is_array( $this->query_vars['orderby'] ) ?
592  $this->query_vars['orderby'] :
593  preg_split( '/[,\s]/', $this->query_vars['orderby'] );
594 
595  $orderby_array = array();
596  $found_orderby_comment_ID = false;
597  foreach ( $ordersby as $_key => $_value ) {
598  if ( ! $_value ) {
599  continue;
600  }
601 
602  if ( is_int( $_key ) ) {
603  $_orderby = $_value;
604  $_order = $order;
605  } else {
606  $_orderby = $_key;
607  $_order = $_value;
608  }
609 
610  if ( ! $found_orderby_comment_ID && 'comment_ID' === $_orderby ) {
611  $found_orderby_comment_ID = true;
612  }
613 
614  $parsed = $this->parse_orderby( $_orderby );
615 
616  if ( ! $parsed ) {
617  continue;
618  }
619 
620  $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order );
621  }
622 
623  // If no valid clauses were found, order by comment_date_gmt.
624  if ( empty( $orderby_array ) ) {
625  $orderby_array[] = "$wpdb->comments.comment_date_gmt $order";
626  }
627 
628  // To ensure determinate sorting, always include a comment_ID clause.
629  if ( ! $found_orderby_comment_ID ) {
630  $comment_ID_order = '';
631 
632  // Inherit order from comment_date or comment_date_gmt, if available.
633  foreach ( $orderby_array as $orderby_clause ) {
634  if ( preg_match( '/comment_date(?:_gmt)*\ (ASC|DESC)/', $orderby_clause, $match ) ) {
635  $comment_ID_order = $match[1];
636  break;
637  }
638  }
639 
640  // If no date-related order is available, use the date from the first available clause.
641  if ( ! $comment_ID_order ) {
642  foreach ( $orderby_array as $orderby_clause ) {
643  if ( false !== strpos( 'ASC', $orderby_clause ) ) {
644  $comment_ID_order = 'ASC';
645  } else {
646  $comment_ID_order = 'DESC';
647  }
648 
649  break;
650  }
651  }
652 
653  // Default to DESC.
654  if ( ! $comment_ID_order ) {
655  $comment_ID_order = 'DESC';
656  }
657 
658  $orderby_array[] = "$wpdb->comments.comment_ID $comment_ID_order";
659  }
660 
661  $orderby = implode( ', ', $orderby_array );
662  } else {
663  $orderby = "$wpdb->comments.comment_date_gmt $order";
664  }
665 
666  $number = absint( $this->query_vars['number'] );
667  $offset = absint( $this->query_vars['offset'] );
668 
669  if ( ! empty( $number ) ) {
670  if ( $offset ) {
671  $limits = 'LIMIT ' . $offset . ',' . $number;
672  } else {
673  $limits = 'LIMIT ' . $number;
674  }
675  } else {
676  $limits = '';
677  }
678 
679  if ( $this->query_vars['count'] ) {
680  $fields = 'COUNT(*)';
681  } else {
682  switch ( strtolower( $this->query_vars['fields'] ) ) {
683  case 'ids':
684  $fields = "$wpdb->comments.comment_ID";
685  break;
686  default:
687  $fields = "*";
688  break;
689  }
690  }
691 
692  $join = '';
693 
694  $post_id = absint( $this->query_vars['post_id'] );
695  if ( ! empty( $post_id ) ) {
696  $where[] = $wpdb->prepare( 'comment_post_ID = %d', $post_id );
697  }
698 
699  // Parse comment IDs for an IN clause.
700  if ( ! empty( $this->query_vars['comment__in'] ) ) {
701  $where[] = "$wpdb->comments.comment_ID IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__in'] ) ) . ' )';
702  }
703 
704  // Parse comment IDs for a NOT IN clause.
705  if ( ! empty( $this->query_vars['comment__not_in'] ) ) {
706  $where[] = "$wpdb->comments.comment_ID NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__not_in'] ) ) . ' )';
707  }
708 
709  // Parse comment post IDs for an IN clause.
710  if ( ! empty( $this->query_vars['post__in'] ) ) {
711  $where[] = 'comment_post_ID IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__in'] ) ) . ' )';
712  }
713 
714  // Parse comment post IDs for a NOT IN clause.
715  if ( ! empty( $this->query_vars['post__not_in'] ) ) {
716  $where[] = 'comment_post_ID NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__not_in'] ) ) . ' )';
717  }
718 
719  if ( '' !== $this->query_vars['author_email'] ) {
720  $where[] = $wpdb->prepare( 'comment_author_email = %s', $this->query_vars['author_email'] );
721  }
722 
723  if ( '' !== $this->query_vars['karma'] ) {
724  $where[] = $wpdb->prepare( 'comment_karma = %d', $this->query_vars['karma'] );
725  }
726 
727  // Filtering by comment_type: 'type', 'type__in', 'type__not_in'.
728  $raw_types = array(
729  'IN' => array_merge( (array) $this->query_vars['type'], (array) $this->query_vars['type__in'] ),
730  'NOT IN' => (array) $this->query_vars['type__not_in'],
731  );
732 
733  $comment_types = array();
734  foreach ( $raw_types as $operator => $_raw_types ) {
735  $_raw_types = array_unique( $_raw_types );
736 
737  foreach ( $_raw_types as $type ) {
738  switch ( $type ) {
739  // An empty translates to 'all', for backward compatibility
740  case '':
741  case 'all' :
742  break;
743 
744  case 'comment':
745  case 'comments':
746  $comment_types[ $operator ][] = "''";
747  break;
748 
749  case 'pings':
750  $comment_types[ $operator ][] = "'pingback'";
751  $comment_types[ $operator ][] = "'trackback'";
752  break;
753 
754  default:
755  $comment_types[ $operator ][] = $wpdb->prepare( '%s', $type );
756  break;
757  }
758  }
759 
760  if ( ! empty( $comment_types[ $operator ] ) ) {
761  $types_sql = implode( ', ', $comment_types[ $operator ] );
762  $where[] = "comment_type $operator ($types_sql)";
763  }
764  }
765 
766  if ( '' !== $this->query_vars['parent'] ) {
767  $where[] = $wpdb->prepare( 'comment_parent = %d', $this->query_vars['parent'] );
768  }
769 
770  if ( is_array( $this->query_vars['user_id'] ) ) {
771  $where[] = 'user_id IN (' . implode( ',', array_map( 'absint', $this->query_vars['user_id'] ) ) . ')';
772  } elseif ( '' !== $this->query_vars['user_id'] ) {
773  $where[] = $wpdb->prepare( 'user_id = %d', $this->query_vars['user_id'] );
774  }
775 
776  if ( '' !== $this->query_vars['search'] ) {
777  $search_sql = $this->get_search_sql(
778  $this->query_vars['search'],
779  array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' )
780  );
781 
782  // Strip leading 'AND'.
783  $where[] = preg_replace( '/^\s*AND\s*/', '', $search_sql );
784  }
785 
786  // If any post-related query vars are passed, join the posts table.
787  $join_posts_table = false;
788  $plucked = wp_array_slice_assoc( $this->query_vars, array( 'post_author', 'post_name', 'post_parent', 'post_status', 'post_type' ) );
789  $post_fields = array_filter( $plucked );
790 
791  if ( ! empty( $post_fields ) ) {
792  $join_posts_table = true;
793  foreach ( $post_fields as $field_name => $field_value ) {
794  // $field_value may be an array.
795  $esses = array_fill( 0, count( (array) $field_value ), '%s' );
796  $where[] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value );
797  }
798  }
799 
800  // Comment author IDs for an IN clause.
801  if ( ! empty( $this->query_vars['author__in'] ) ) {
802  $where[] = 'user_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__in'] ) ) . ' )';
803  }
804 
805  // Comment author IDs for a NOT IN clause.
806  if ( ! empty( $this->query_vars['author__not_in'] ) ) {
807  $where[] = 'user_id NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__not_in'] ) ) . ' )';
808  }
809 
810  // Post author IDs for an IN clause.
811  if ( ! empty( $this->query_vars['post_author__in'] ) ) {
812  $join_posts_table = true;
813  $where[] = 'post_author IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__in'] ) ) . ' )';
814  }
815 
816  // Post author IDs for a NOT IN clause.
817  if ( ! empty( $this->query_vars['post_author__not_in'] ) ) {
818  $join_posts_table = true;
819  $where[] = 'post_author NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__not_in'] ) ) . ' )';
820  }
821 
822  if ( $join_posts_table ) {
823  $join = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
824  }
825 
826  if ( ! empty( $meta_query_clauses ) ) {
827  $join .= $meta_query_clauses['join'];
828 
829  // Strip leading 'AND'.
830  $where[] = preg_replace( '/^\s*AND\s*/', '', $meta_query_clauses['where'] );
831 
832  if ( ! $this->query_vars['count'] ) {
833  $groupby = "{$wpdb->comments}.comment_ID";
834  }
835  }
836 
837  $date_query = $this->query_vars['date_query'];
838  if ( ! empty( $date_query ) && is_array( $date_query ) ) {
839  $date_query_object = new WP_Date_Query( $date_query, 'comment_date' );
840  $where[] = preg_replace( '/^\s*AND\s*/', '', $date_query_object->get_sql() );
841  }
842 
843  $where = implode( ' AND ', $where );
844 
845  $pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' );
854  $clauses = apply_filters_ref_array( 'comments_clauses', array( compact( $pieces ), &$this ) );
855 
856  $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : '';
857  $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : '';
858  $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : '';
859  $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : '';
860  $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : '';
861  $groupby = isset( $clauses[ 'groupby' ] ) ? $clauses[ 'groupby' ] : '';
862 
863  if ( $where ) {
864  $where = 'WHERE ' . $where;
865  }
866 
867  if ( $groupby ) {
868  $groupby = 'GROUP BY ' . $groupby;
869  }
870 
871  if ( $orderby ) {
872  $orderby = "ORDER BY $orderby";
873  }
874 
875  $this->request = "SELECT $fields FROM $wpdb->comments $join $where $groupby $orderby $limits";
876 
877  if ( $this->query_vars['count'] ) {
878  return $wpdb->get_var( $this->request );
879  }
880 
881  if ( 'ids' == $this->query_vars['fields'] ) {
882  $this->comments = $wpdb->get_col( $this->request );
883  return array_map( 'intval', $this->comments );
884  }
885 
886  $results = $wpdb->get_results( $this->request );
895  $comments = apply_filters_ref_array( 'the_comments', array( $results, &$this ) );
896 
897  wp_cache_add( $cache_key, $comments, 'comment' );
898  if ( '*' === $fields ) {
900  }
901 
902  $this->comments = $comments;
903  return $this->comments;
904  }
905 
918  protected function get_search_sql( $string, $cols ) {
919  global $wpdb;
920 
921  $like = '%' . $wpdb->esc_like( $string ) . '%';
922 
923  $searches = array();
924  foreach ( $cols as $col ) {
925  $searches[] = $wpdb->prepare( "$col LIKE %s", $like );
926  }
927 
928  return ' AND (' . implode(' OR ', $searches) . ')';
929  }
930 
942  protected function parse_orderby( $orderby ) {
943  global $wpdb;
944 
945  $allowed_keys = array(
946  'comment_agent',
947  'comment_approved',
948  'comment_author',
949  'comment_author_email',
950  'comment_author_IP',
951  'comment_author_url',
952  'comment_content',
953  'comment_date',
954  'comment_date_gmt',
955  'comment_ID',
956  'comment_karma',
957  'comment_parent',
958  'comment_post_ID',
959  'comment_type',
960  'user_id',
961  );
962 
963  if ( ! empty( $this->query_vars['meta_key'] ) ) {
964  $allowed_keys[] = $this->query_vars['meta_key'];
965  $allowed_keys[] = 'meta_value';
966  $allowed_keys[] = 'meta_value_num';
967  }
968 
969  $meta_query_clauses = $this->meta_query->get_clauses();
970  if ( $meta_query_clauses ) {
971  $allowed_keys = array_merge( $allowed_keys, array_keys( $meta_query_clauses ) );
972  }
973 
974  $parsed = false;
975  if ( $orderby == $this->query_vars['meta_key'] || $orderby == 'meta_value' ) {
976  $parsed = "$wpdb->commentmeta.meta_value";
977  } elseif ( $orderby == 'meta_value_num' ) {
978  $parsed = "$wpdb->commentmeta.meta_value+0";
979  } elseif ( in_array( $orderby, $allowed_keys ) ) {
980 
981  if ( isset( $meta_query_clauses[ $orderby ] ) ) {
982  $meta_clause = $meta_query_clauses[ $orderby ];
983  $parsed = sprintf( "CAST(%s.meta_value AS %s)", esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) );
984  } else {
985  $parsed = "$wpdb->comments.$orderby";
986  }
987  }
988 
989  return $parsed;
990  }
991 
1001  protected function parse_order( $order ) {
1002  if ( ! is_string( $order ) || empty( $order ) ) {
1003  return 'DESC';
1004  }
1005 
1006  if ( 'ASC' === strtoupper( $order ) ) {
1007  return 'ASC';
1008  } else {
1009  return 'DESC';
1010  }
1011  }
1012 }
1013 
1025  $status = array(
1026  'hold' => __('Unapproved'),
1027  /* translators: comment status */
1028  'approve' => _x('Approved', 'adjective'),
1029  /* translators: comment status */
1030  'spam' => _x('Spam', 'adjective'),
1031  );
1032 
1033  return $status;
1034 }
1035 
1045 function get_default_comment_status( $post_type = 'post', $comment_type = 'comment' ) {
1046  switch ( $comment_type ) {
1047  case 'pingback' :
1048  case 'trackback' :
1049  $supports = 'trackbacks';
1050  $option = 'ping';
1051  break;
1052  default :
1053  $supports = 'comments';
1054  $option = 'comment';
1055  }
1056 
1057  // Set the status.
1058  if ( 'page' === $post_type ) {
1059  $status = 'closed';
1060  } elseif ( post_type_supports( $post_type, $supports ) ) {
1061  $status = get_option( "default_{$option}_status" );
1062  } else {
1063  $status = 'closed';
1064  }
1065 
1076  return apply_filters( 'get_default_comment_status' , $status, $post_type, $comment_type );
1077 }
1078 
1091 function get_lastcommentmodified($timezone = 'server') {
1092  global $wpdb;
1093  static $cache_lastcommentmodified = array();
1094 
1095  if ( isset($cache_lastcommentmodified[$timezone]) )
1096  return $cache_lastcommentmodified[$timezone];
1097 
1098  $add_seconds_server = date('Z');
1099 
1100  switch ( strtolower($timezone)) {
1101  case 'gmt':
1102  $lastcommentmodified = $wpdb->get_var("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1");
1103  break;
1104  case 'blog':
1105  $lastcommentmodified = $wpdb->get_var("SELECT comment_date FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1");
1106  break;
1107  case 'server':
1108  $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));
1109  break;
1110  }
1111 
1112  $cache_lastcommentmodified[$timezone] = $lastcommentmodified;
1113 
1114  return $lastcommentmodified;
1115 }
1116 
1131 function get_comment_count( $post_id = 0 ) {
1132  global $wpdb;
1133 
1134  $post_id = (int) $post_id;
1135 
1136  $where = '';
1137  if ( $post_id > 0 ) {
1138  $where = $wpdb->prepare("WHERE comment_post_ID = %d", $post_id);
1139  }
1140 
1141  $totals = (array) $wpdb->get_results("
1142  SELECT comment_approved, COUNT( * ) AS total
1143  FROM {$wpdb->comments}
1144  {$where}
1145  GROUP BY comment_approved
1146  ", ARRAY_A);
1147 
1148  $comment_count = array(
1149  "approved" => 0,
1150  "awaiting_moderation" => 0,
1151  "spam" => 0,
1152  "total_comments" => 0
1153  );
1154 
1155  foreach ( $totals as $row ) {
1156  switch ( $row['comment_approved'] ) {
1157  case 'spam':
1158  $comment_count['spam'] = $row['total'];
1159  $comment_count["total_comments"] += $row['total'];
1160  break;
1161  case 1:
1162  $comment_count['approved'] = $row['total'];
1163  $comment_count['total_comments'] += $row['total'];
1164  break;
1165  case 0:
1166  $comment_count['awaiting_moderation'] = $row['total'];
1167  $comment_count['total_comments'] += $row['total'];
1168  break;
1169  default:
1170  break;
1171  }
1172  }
1173 
1174  return $comment_count;
1175 }
1176 
1177 //
1178 // Comment meta functions
1179 //
1180 
1193 function add_comment_meta($comment_id, $meta_key, $meta_value, $unique = false) {
1194  return add_metadata('comment', $comment_id, $meta_key, $meta_value, $unique);
1195 }
1196 
1212 function delete_comment_meta($comment_id, $meta_key, $meta_value = '') {
1213  return delete_metadata('comment', $comment_id, $meta_key, $meta_value);
1214 }
1215 
1228 function get_comment_meta($comment_id, $key = '', $single = false) {
1229  return get_metadata('comment', $comment_id, $key, $single);
1230 }
1231 
1249 function update_comment_meta($comment_id, $meta_key, $meta_value, $prev_value = '') {
1250  return update_metadata('comment', $comment_id, $meta_key, $meta_value, $prev_value);
1251 }
1252 
1263  if ( $user->exists() )
1264  return;
1265 
1273  $comment_cookie_lifetime = apply_filters( 'comment_cookie_lifetime', 30000000 );
1274  $secure = ( 'https' === parse_url( home_url(), PHP_URL_SCHEME ) );
1275  setcookie( 'comment_author_' . COOKIEHASH, $comment->comment_author, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
1276  setcookie( 'comment_author_email_' . COOKIEHASH, $comment->comment_author_email, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
1277  setcookie( 'comment_author_url_' . COOKIEHASH, esc_url($comment->comment_author_url), time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
1278 }
1279 
1289  if ( isset( $_COOKIE['comment_author_' . COOKIEHASH] ) ) {
1300  $comment_author = apply_filters( 'pre_comment_author_name', $_COOKIE['comment_author_' . COOKIEHASH] );
1303  $_COOKIE['comment_author_' . COOKIEHASH] = $comment_author;
1304  }
1305 
1306  if ( isset( $_COOKIE['comment_author_email_' . COOKIEHASH] ) ) {
1317  $comment_author_email = apply_filters( 'pre_comment_author_email', $_COOKIE['comment_author_email_' . COOKIEHASH] );
1320  $_COOKIE['comment_author_email_'.COOKIEHASH] = $comment_author_email;
1321  }
1322 
1323  if ( isset( $_COOKIE['comment_author_url_' . COOKIEHASH] ) ) {
1334  $comment_author_url = apply_filters( 'pre_comment_author_url', $_COOKIE['comment_author_url_' . COOKIEHASH] );
1336  $_COOKIE['comment_author_url_'.COOKIEHASH] = $comment_author_url;
1337  }
1338 }
1339 
1351  global $wpdb;
1352 
1353  // Simple duplicate check
1354  // expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content)
1355  $dupe = $wpdb->prepare(
1356  "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = %s AND comment_approved != 'trash' AND ( comment_author = %s ",
1357  wp_unslash( $commentdata['comment_post_ID'] ),
1358  wp_unslash( $commentdata['comment_parent'] ),
1359  wp_unslash( $commentdata['comment_author'] )
1360  );
1361  if ( $commentdata['comment_author_email'] ) {
1362  $dupe .= $wpdb->prepare(
1363  "OR comment_author_email = %s ",
1364  wp_unslash( $commentdata['comment_author_email'] )
1365  );
1366  }
1367  $dupe .= $wpdb->prepare(
1368  ") AND comment_content = %s LIMIT 1",
1369  wp_unslash( $commentdata['comment_content'] )
1370  );
1371  if ( $wpdb->get_var( $dupe ) ) {
1379  do_action( 'comment_duplicate_trigger', $commentdata );
1380  if ( defined( 'DOING_AJAX' ) ) {
1381  die( __('Duplicate comment detected; it looks as though you&#8217;ve already said that!') );
1382  }
1383  wp_die( __( 'Duplicate comment detected; it looks as though you&#8217;ve already said that!' ), 409 );
1384  }
1385 
1397  do_action(
1398  'check_comment_flood',
1399  $commentdata['comment_author_IP'],
1400  $commentdata['comment_author_email'],
1401  $commentdata['comment_date_gmt']
1402  );
1403 
1404  if ( ! empty( $commentdata['user_id'] ) ) {
1405  $user = get_userdata( $commentdata['user_id'] );
1406  $post_author = $wpdb->get_var( $wpdb->prepare(
1407  "SELECT post_author FROM $wpdb->posts WHERE ID = %d LIMIT 1",
1408  $commentdata['comment_post_ID']
1409  ) );
1410  }
1411 
1412  if ( isset( $user ) && ( $commentdata['user_id'] == $post_author || $user->has_cap( 'moderate_comments' ) ) ) {
1413  // The author and the admins get respect.
1414  $approved = 1;
1415  } else {
1416  // Everyone else's comments will be checked.
1417  if ( check_comment(
1418  $commentdata['comment_author'],
1419  $commentdata['comment_author_email'],
1420  $commentdata['comment_author_url'],
1421  $commentdata['comment_content'],
1422  $commentdata['comment_author_IP'],
1423  $commentdata['comment_agent'],
1424  $commentdata['comment_type']
1425  ) ) {
1426  $approved = 1;
1427  } else {
1428  $approved = 0;
1429  }
1430 
1431  if ( wp_blacklist_check(
1432  $commentdata['comment_author'],
1433  $commentdata['comment_author_email'],
1434  $commentdata['comment_author_url'],
1435  $commentdata['comment_content'],
1436  $commentdata['comment_author_IP'],
1437  $commentdata['comment_agent']
1438  ) ) {
1439  $approved = 'spam';
1440  }
1441  }
1442 
1451  $approved = apply_filters( 'pre_comment_approved', $approved, $commentdata );
1452  return $approved;
1453 }
1454 
1469 function check_comment_flood_db( $ip, $email, $date ) {
1470  global $wpdb;
1471  if ( current_user_can( 'manage_options' ) )
1472  return; // don't throttle admins
1473  $hour_ago = gmdate( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS );
1474  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 ) ) ) {
1475  $time_lastcomment = mysql2date('U', $lasttime, false);
1476  $time_newcomment = mysql2date('U', $date, false);
1486  $flood_die = apply_filters( 'comment_flood_filter', false, $time_lastcomment, $time_newcomment );
1487  if ( $flood_die ) {
1496  do_action( 'comment_flood_trigger', $time_lastcomment, $time_newcomment );
1497 
1498  if ( defined('DOING_AJAX') )
1499  die( __('You are posting comments too quickly. Slow down.') );
1500 
1501  wp_die( __( 'You are posting comments too quickly. Slow down.' ), 429 );
1502  }
1503  }
1504 }
1505 
1515  $comments_by_type = array('comment' => array(), 'trackback' => array(), 'pingback' => array(), 'pings' => array());
1516  $count = count($comments);
1517  for ( $i = 0; $i < $count; $i++ ) {
1518  $type = $comments[$i]->comment_type;
1519  if ( empty($type) )
1520  $type = 'comment';
1521  $comments_by_type[$type][] = &$comments[$i];
1522  if ( 'trackback' == $type || 'pingback' == $type )
1523  $comments_by_type['pings'][] = &$comments[$i];
1524  }
1525 
1526  return $comments_by_type;
1527 }
1528 
1543 function get_comment_pages_count( $comments = null, $per_page = null, $threaded = null ) {
1544  global $wp_query;
1545 
1546  if ( null === $comments && null === $per_page && null === $threaded && !empty($wp_query->max_num_comment_pages) )
1547  return $wp_query->max_num_comment_pages;
1548 
1549  if ( ( ! $comments || ! is_array( $comments ) ) && ! empty( $wp_query->comments ) )
1550  $comments = $wp_query->comments;
1551 
1552  if ( empty($comments) )
1553  return 0;
1554 
1555  if ( ! get_option( 'page_comments' ) )
1556  return 1;
1557 
1558  if ( !isset($per_page) )
1559  $per_page = (int) get_query_var('comments_per_page');
1560  if ( 0 === $per_page )
1561  $per_page = (int) get_option('comments_per_page');
1562  if ( 0 === $per_page )
1563  return 1;
1564 
1565  if ( !isset($threaded) )
1566  $threaded = get_option('thread_comments');
1567 
1568  if ( $threaded ) {
1569  $walker = new Walker_Comment;
1570  $count = ceil( $walker->get_number_of_root_elements( $comments ) / $per_page );
1571  } else {
1572  $count = ceil( count( $comments ) / $per_page );
1573  }
1574 
1575  return $count;
1576 }
1577 
1589 function get_page_of_comment( $comment_ID, $args = array() ) {
1590  global $wpdb;
1591 
1592  if ( !$comment = get_comment( $comment_ID ) )
1593  return;
1594 
1595  $defaults = array( 'type' => 'all', 'page' => '', 'per_page' => '', 'max_depth' => '' );
1596  $args = wp_parse_args( $args, $defaults );
1597 
1598  if ( '' === $args['per_page'] && get_option('page_comments') )
1599  $args['per_page'] = get_query_var('comments_per_page');
1600  if ( empty($args['per_page']) ) {
1601  $args['per_page'] = 0;
1602  $args['page'] = 0;
1603  }
1604  if ( $args['per_page'] < 1 )
1605  return 1;
1606 
1607  if ( '' === $args['max_depth'] ) {
1608  if ( get_option('thread_comments') )
1609  $args['max_depth'] = get_option('thread_comments_depth');
1610  else
1611  $args['max_depth'] = -1;
1612  }
1613 
1614  // Find this comment's top level parent if threading is enabled
1615  if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent )
1616  return get_page_of_comment( $comment->comment_parent, $args );
1617 
1618  $allowedtypes = array(
1619  'comment' => '',
1620  'pingback' => 'pingback',
1621  'trackback' => 'trackback',
1622  );
1623 
1624  $comtypewhere = ( 'all' != $args['type'] && isset($allowedtypes[$args['type']]) ) ? " AND comment_type = '" . $allowedtypes[$args['type']] . "'" : '';
1625 
1626  // Count comments older than this one
1627  $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 ) );
1628 
1629  // No older comments? Then it's page #1.
1630  if ( 0 == $oldercoms )
1631  return 1;
1632 
1633  // Divide comments older than this one by comments per page to get this comment's page number
1634  return ceil( ( $oldercoms + 1 ) / $args['per_page'] );
1635 }
1636 
1650 function wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent) {
1663  do_action( 'wp_blacklist_check', $author, $email, $url, $comment, $user_ip, $user_agent );
1664 
1665  $mod_keys = trim( get_option('blacklist_keys') );
1666  if ( '' == $mod_keys )
1667  return false; // If moderation keys are empty
1668  $words = explode("\n", $mod_keys );
1669 
1670  foreach ( (array) $words as $word ) {
1671  $word = trim($word);
1672 
1673  // Skip empty lines
1674  if ( empty($word) ) { continue; }
1675 
1676  // Do some escaping magic so that '#' chars in the
1677  // spam words don't break things:
1678  $word = preg_quote($word, '#');
1679 
1680  $pattern = "#$word#i";
1681  if (
1682  preg_match($pattern, $author)
1683  || preg_match($pattern, $email)
1684  || preg_match($pattern, $url)
1685  || preg_match($pattern, $comment)
1686  || preg_match($pattern, $user_ip)
1687  || preg_match($pattern, $user_agent)
1688  )
1689  return true;
1690  }
1691  return false;
1692 }
1693 
1712 function wp_count_comments( $post_id = 0 ) {
1713  global $wpdb;
1714 
1715  $post_id = (int) $post_id;
1716 
1725  $stats = apply_filters( 'wp_count_comments', array(), $post_id );
1726  if ( !empty($stats) )
1727  return $stats;
1728 
1729  $count = wp_cache_get("comments-{$post_id}", 'counts');
1730 
1731  if ( false !== $count )
1732  return $count;
1733 
1734  $where = '';
1735  if ( $post_id > 0 )
1736  $where = $wpdb->prepare( "WHERE comment_post_ID = %d", $post_id );
1737 
1738  $count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} {$where} GROUP BY comment_approved", ARRAY_A );
1739 
1740  $total = 0;
1741  $approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed');
1742  foreach ( (array) $count as $row ) {
1743  // Don't count post-trashed toward totals
1744  if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] )
1745  $total += $row['num_comments'];
1746  if ( isset( $approved[$row['comment_approved']] ) )
1747  $stats[$approved[$row['comment_approved']]] = $row['num_comments'];
1748  }
1749 
1750  $stats['total_comments'] = $total;
1751  foreach ( $approved as $key ) {
1752  if ( empty($stats[$key]) )
1753  $stats[$key] = 0;
1754  }
1755 
1756  $stats = (object) $stats;
1757  wp_cache_set("comments-{$post_id}", $stats, 'counts');
1758 
1759  return $stats;
1760 }
1761 
1779 function wp_delete_comment($comment_id, $force_delete = false) {
1780  global $wpdb;
1782  return false;
1783 
1784  if ( !$force_delete && EMPTY_TRASH_DAYS && !in_array( wp_get_comment_status($comment_id), array( 'trash', 'spam' ) ) )
1785  return wp_trash_comment($comment_id);
1786 
1794  do_action( 'delete_comment', $comment_id );
1795 
1796  // Move children up a level.
1797  $children = $wpdb->get_col( $wpdb->prepare("SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment_id) );
1798  if ( !empty($children) ) {
1799  $wpdb->update($wpdb->comments, array('comment_parent' => $comment->comment_parent), array('comment_parent' => $comment_id));
1800  clean_comment_cache($children);
1801  }
1802 
1803  // Delete metadata
1804  $meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->commentmeta WHERE comment_id = %d", $comment_id ) );
1805  foreach ( $meta_ids as $mid )
1806  delete_metadata_by_mid( 'comment', $mid );
1807 
1808  if ( ! $wpdb->delete( $wpdb->comments, array( 'comment_ID' => $comment_id ) ) )
1809  return false;
1810 
1818  do_action( 'deleted_comment', $comment_id );
1819 
1820  $post_id = $comment->comment_post_ID;
1821  if ( $post_id && $comment->comment_approved == 1 )
1823 
1825 
1827  do_action( 'wp_set_comment_status', $comment_id, 'delete' );
1828 
1829  wp_transition_comment_status('delete', $comment->comment_approved, $comment);
1830  return true;
1831 }
1832 
1844  if ( !EMPTY_TRASH_DAYS )
1845  return wp_delete_comment($comment_id, true);
1846 
1847  if ( !$comment = get_comment($comment_id) )
1848  return false;
1849 
1857  do_action( 'trash_comment', $comment_id );
1858 
1859  if ( wp_set_comment_status($comment_id, 'trash') ) {
1860  add_comment_meta($comment_id, '_wp_trash_meta_status', $comment->comment_approved);
1861  add_comment_meta($comment_id, '_wp_trash_meta_time', time() );
1862 
1870  do_action( 'trashed_comment', $comment_id );
1871  return true;
1872  }
1873 
1874  return false;
1875 }
1876 
1886  if ( ! (int)$comment_id )
1887  return false;
1888 
1896  do_action( 'untrash_comment', $comment_id );
1897 
1898  $status = (string) get_comment_meta($comment_id, '_wp_trash_meta_status', true);
1899  if ( empty($status) )
1900  $status = '0';
1901 
1902  if ( wp_set_comment_status($comment_id, $status) ) {
1903  delete_comment_meta($comment_id, '_wp_trash_meta_time');
1904  delete_comment_meta($comment_id, '_wp_trash_meta_status');
1912  do_action( 'untrashed_comment', $comment_id );
1913  return true;
1914  }
1915 
1916  return false;
1917 }
1918 
1928  if ( !$comment = get_comment($comment_id) )
1929  return false;
1930 
1938  do_action( 'spam_comment', $comment_id );
1939 
1940  if ( wp_set_comment_status($comment_id, 'spam') ) {
1941  add_comment_meta($comment_id, '_wp_trash_meta_status', $comment->comment_approved);
1949  do_action( 'spammed_comment', $comment_id );
1950  return true;
1951  }
1952 
1953  return false;
1954 }
1955 
1965  if ( ! (int)$comment_id )
1966  return false;
1967 
1975  do_action( 'unspam_comment', $comment_id );
1976 
1977  $status = (string) get_comment_meta($comment_id, '_wp_trash_meta_status', true);
1978  if ( empty($status) )
1979  $status = '0';
1980 
1981  if ( wp_set_comment_status($comment_id, $status) ) {
1982  delete_comment_meta($comment_id, '_wp_trash_meta_status');
1990  do_action( 'unspammed_comment', $comment_id );
1991  return true;
1992  }
1993 
1994  return false;
1995 }
1996 
2007  if ( !$comment )
2008  return false;
2009 
2010  $approved = $comment->comment_approved;
2011 
2012  if ( $approved == null )
2013  return false;
2014  elseif ( $approved == '1' )
2015  return 'approved';
2016  elseif ( $approved == '0' )
2017  return 'unapproved';
2018  elseif ( $approved == 'spam' )
2019  return 'spam';
2020  elseif ( $approved == 'trash' )
2021  return 'trash';
2022  else
2023  return false;
2024 }
2025 
2046 function wp_transition_comment_status($new_status, $old_status, $comment) {
2047  /*
2048  * Translate raw statuses to human readable formats for the hooks.
2049  * This is not a complete list of comment status, it's only the ones
2050  * that need to be renamed
2051  */
2052  $comment_statuses = array(
2053  0 => 'unapproved',
2054  'hold' => 'unapproved', // wp_set_comment_status() uses "hold"
2055  1 => 'approved',
2056  'approve' => 'approved', // wp_set_comment_status() uses "approve"
2057  );
2058  if ( isset($comment_statuses[$new_status]) ) $new_status = $comment_statuses[$new_status];
2059  if ( isset($comment_statuses[$old_status]) ) $old_status = $comment_statuses[$old_status];
2060 
2061  // Call the hooks
2062  if ( $new_status != $old_status ) {
2072  do_action( 'transition_comment_status', $new_status, $old_status, $comment );
2083  do_action( "comment_{$old_status}_to_{$new_status}", $comment );
2084  }
2099  do_action( "comment_{$new_status}_{$comment->comment_type}", $comment->comment_ID, $comment );
2100 }
2101 
2115  // Cookies should already be sanitized.
2116 
2117  $comment_author = '';
2118  if ( isset($_COOKIE['comment_author_'.COOKIEHASH]) )
2119  $comment_author = $_COOKIE['comment_author_'.COOKIEHASH];
2120 
2121  $comment_author_email = '';
2122  if ( isset($_COOKIE['comment_author_email_'.COOKIEHASH]) )
2123  $comment_author_email = $_COOKIE['comment_author_email_'.COOKIEHASH];
2124 
2125  $comment_author_url = '';
2126  if ( isset($_COOKIE['comment_author_url_'.COOKIEHASH]) )
2127  $comment_author_url = $_COOKIE['comment_author_url_'.COOKIEHASH];
2128 
2138  return apply_filters( 'wp_get_current_commenter', compact('comment_author', 'comment_author_email', 'comment_author_url') );
2139 }
2140 
2174  global $wpdb;
2175  $data = wp_unslash( $commentdata );
2176 
2177  $comment_author = ! isset( $data['comment_author'] ) ? '' : $data['comment_author'];
2178  $comment_author_email = ! isset( $data['comment_author_email'] ) ? '' : $data['comment_author_email'];
2179  $comment_author_url = ! isset( $data['comment_author_url'] ) ? '' : $data['comment_author_url'];
2180  $comment_author_IP = ! isset( $data['comment_author_IP'] ) ? '' : $data['comment_author_IP'];
2181 
2182  $comment_date = ! isset( $data['comment_date'] ) ? current_time( 'mysql' ) : $data['comment_date'];
2183  $comment_date_gmt = ! isset( $data['comment_date_gmt'] ) ? get_gmt_from_date( $comment_date ) : $data['comment_date_gmt'];
2184 
2185  $comment_post_ID = ! isset( $data['comment_post_ID'] ) ? '' : $data['comment_post_ID'];
2186  $comment_content = ! isset( $data['comment_content'] ) ? '' : $data['comment_content'];
2187  $comment_karma = ! isset( $data['comment_karma'] ) ? 0 : $data['comment_karma'];
2188  $comment_approved = ! isset( $data['comment_approved'] ) ? 1 : $data['comment_approved'];
2189  $comment_agent = ! isset( $data['comment_agent'] ) ? '' : $data['comment_agent'];
2190  $comment_type = ! isset( $data['comment_type'] ) ? '' : $data['comment_type'];
2191  $comment_parent = ! isset( $data['comment_parent'] ) ? 0 : $data['comment_parent'];
2192 
2193  $user_id = ! isset( $data['user_id'] ) ? 0 : $data['user_id'];
2194 
2195  $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' );
2196  if ( ! $wpdb->insert( $wpdb->comments, $compacted ) ) {
2197  return false;
2198  }
2199 
2200  $id = (int) $wpdb->insert_id;
2201 
2202  if ( $comment_approved == 1 ) {
2204  }
2205  $comment = get_comment( $id );
2206 
2215  do_action( 'wp_insert_comment', $id, $comment );
2216 
2217  wp_cache_set( 'last_changed', microtime(), 'comment' );
2218 
2219  return $id;
2220 }
2221 
2235  if ( isset( $commentdata['user_ID'] ) ) {
2246  $commentdata['user_id'] = apply_filters( 'pre_user_id', $commentdata['user_ID'] );
2247  } elseif ( isset( $commentdata['user_id'] ) ) {
2249  $commentdata['user_id'] = apply_filters( 'pre_user_id', $commentdata['user_id'] );
2250  }
2251 
2259  $commentdata['comment_agent'] = apply_filters( 'pre_comment_user_agent', ( isset( $commentdata['comment_agent'] ) ? $commentdata['comment_agent'] : '' ) );
2261  $commentdata['comment_author'] = apply_filters( 'pre_comment_author_name', $commentdata['comment_author'] );
2269  $commentdata['comment_content'] = apply_filters( 'pre_comment_content', $commentdata['comment_content'] );
2277  $commentdata['comment_author_IP'] = apply_filters( 'pre_comment_user_ip', $commentdata['comment_author_IP'] );
2279  $commentdata['comment_author_url'] = apply_filters( 'pre_comment_author_url', $commentdata['comment_author_url'] );
2281  $commentdata['comment_author_email'] = apply_filters( 'pre_comment_author_email', $commentdata['comment_author_email'] );
2282  $commentdata['filtered'] = true;
2283  return $commentdata;
2284 }
2285 
2296 function wp_throttle_comment_flood($block, $time_lastcomment, $time_newcomment) {
2297  if ( $block ) // a plugin has already blocked... we'll let that decision stand
2298  return $block;
2299  if ( ($time_newcomment - $time_lastcomment) < 15 )
2300  return true;
2301  return false;
2302 }
2303 
2328  global $wpdb;
2329 
2330  if ( isset( $commentdata['user_ID'] ) ) {
2331  $commentdata['user_id'] = $commentdata['user_ID'] = (int) $commentdata['user_ID'];
2332  }
2333 
2334  $prefiltered_user_id = ( isset( $commentdata['user_id'] ) ) ? (int) $commentdata['user_id'] : 0;
2335 
2343  $commentdata = apply_filters( 'preprocess_comment', $commentdata );
2344 
2345  $commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID'];
2346  if ( isset( $commentdata['user_ID'] ) && $prefiltered_user_id !== (int) $commentdata['user_ID'] ) {
2347  $commentdata['user_id'] = $commentdata['user_ID'] = (int) $commentdata['user_ID'];
2348  } elseif ( isset( $commentdata['user_id'] ) ) {
2349  $commentdata['user_id'] = (int) $commentdata['user_id'];
2350  }
2351 
2352  $commentdata['comment_parent'] = isset($commentdata['comment_parent']) ? absint($commentdata['comment_parent']) : 0;
2353  $parent_status = ( 0 < $commentdata['comment_parent'] ) ? wp_get_comment_status($commentdata['comment_parent']) : '';
2354  $commentdata['comment_parent'] = ( 'approved' == $parent_status || 'unapproved' == $parent_status ) ? $commentdata['comment_parent'] : 0;
2355 
2356  if ( ! isset( $commentdata['comment_author_IP'] ) ) {
2357  $commentdata['comment_author_IP'] = $_SERVER['REMOTE_ADDR'];
2358  }
2359  $commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '', $commentdata['comment_author_IP'] );
2360 
2361  if ( ! isset( $commentdata['comment_agent'] ) ) {
2362  $commentdata['comment_agent'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT']: '';
2363  }
2364  $commentdata['comment_agent'] = substr( $commentdata['comment_agent'], 0, 254 );
2365 
2366  if ( empty( $commentdata['comment_date'] ) ) {
2367  $commentdata['comment_date'] = current_time('mysql');
2368  }
2369 
2370  if ( empty( $commentdata['comment_date_gmt'] ) ) {
2371  $commentdata['comment_date_gmt'] = current_time( 'mysql', 1 );
2372  }
2373 
2375 
2376  $commentdata['comment_approved'] = wp_allow_comment($commentdata);
2377 
2378  $comment_ID = wp_insert_comment($commentdata);
2379  if ( ! $comment_ID ) {
2380  $fields = array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content' );
2381 
2382  foreach( $fields as $field ) {
2383  if ( isset( $commentdata[ $field ] ) ) {
2384  $commentdata[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->comments, $field, $commentdata[ $field ] );
2385  }
2386  }
2387 
2389 
2390  $commentdata['comment_approved'] = wp_allow_comment( $commentdata );
2391 
2392  $comment_ID = wp_insert_comment( $commentdata );
2393  if ( ! $comment_ID ) {
2394  return false;
2395  }
2396  }
2397 
2406  do_action( 'comment_post', $comment_ID, $commentdata['comment_approved'] );
2407 
2408  if ( 'spam' !== $commentdata['comment_approved'] ) { // If it's spam save it silently for later crunching
2409  if ( '0' == $commentdata['comment_approved'] ) {
2410  wp_notify_moderator( $comment_ID );
2411  }
2412 
2413  // wp_notify_postauthor() checks if notifying the author of their own comment.
2414  // By default, it won't, but filters can override this.
2415  if ( get_option( 'comments_notify' ) && $commentdata['comment_approved'] ) {
2416  wp_notify_postauthor( $comment_ID );
2417  }
2418  }
2419 
2420  return $comment_ID;
2421 }
2422 
2438 function wp_set_comment_status($comment_id, $comment_status, $wp_error = false) {
2439  global $wpdb;
2440 
2441  switch ( $comment_status ) {
2442  case 'hold':
2443  case '0':
2444  $status = '0';
2445  break;
2446  case 'approve':
2447  case '1':
2448  $status = '1';
2449  if ( get_option('comments_notify') ) {
2450  wp_notify_postauthor( $comment_id );
2451  }
2452  break;
2453  case 'spam':
2454  $status = 'spam';
2455  break;
2456  case 'trash':
2457  $status = 'trash';
2458  break;
2459  default:
2460  return false;
2461  }
2462 
2463  $comment_old = clone get_comment($comment_id);
2464 
2465  if ( !$wpdb->update( $wpdb->comments, array('comment_approved' => $status), array('comment_ID' => $comment_id) ) ) {
2466  if ( $wp_error )
2467  return new WP_Error('db_update_error', __('Could not update comment status'), $wpdb->last_error);
2468  else
2469  return false;
2470  }
2471 
2473 
2475 
2486  do_action( 'wp_set_comment_status', $comment_id, $comment_status );
2487 
2488  wp_transition_comment_status($comment_status, $comment_old->comment_approved, $comment);
2489 
2490  wp_update_comment_count($comment->comment_post_ID);
2491 
2492  return true;
2493 }
2494 
2507 function wp_update_comment($commentarr) {
2508  global $wpdb;
2509 
2510  // First, get all of the original fields
2511  $comment = get_comment($commentarr['comment_ID'], ARRAY_A);
2512  if ( empty( $comment ) ) {
2513  return 0;
2514  }
2515 
2516  // Make sure that the comment post ID is valid (if specified).
2517  if ( isset( $commentarr['comment_post_ID'] ) && ! get_post( $commentarr['comment_post_ID'] ) ) {
2518  return 0;
2519  }
2520 
2521  // Escape data pulled from DB.
2523 
2524  $old_status = $comment['comment_approved'];
2525 
2526  // Merge old and new fields with new fields overwriting old ones.
2527  $commentarr = array_merge($comment, $commentarr);
2528 
2529  $commentarr = wp_filter_comment( $commentarr );
2530 
2531  // Now extract the merged array.
2532  $data = wp_unslash( $commentarr );
2533 
2541  $data['comment_content'] = apply_filters( 'comment_save_pre', $data['comment_content'] );
2542 
2543  $data['comment_date_gmt'] = get_gmt_from_date( $data['comment_date'] );
2544 
2545  if ( ! isset( $data['comment_approved'] ) ) {
2546  $data['comment_approved'] = 1;
2547  } elseif ( 'hold' == $data['comment_approved'] ) {
2548  $data['comment_approved'] = 0;
2549  } elseif ( 'approve' == $data['comment_approved'] ) {
2550  $data['comment_approved'] = 1;
2551  }
2552 
2553  $comment_ID = $data['comment_ID'];
2554  $comment_post_ID = $data['comment_post_ID'];
2555  $keys = array( 'comment_post_ID', 'comment_content', 'comment_author', 'comment_author_email', 'comment_approved', 'comment_karma', 'comment_author_url', 'comment_date', 'comment_date_gmt', 'comment_type', 'comment_parent', 'user_id' );
2556  $data = wp_array_slice_assoc( $data, $keys );
2557  $rval = $wpdb->update( $wpdb->comments, $data, compact( 'comment_ID' ) );
2558 
2559  clean_comment_cache( $comment_ID );
2570  do_action( 'edit_comment', $comment_ID );
2571  $comment = get_comment($comment_ID);
2572  wp_transition_comment_status($comment->comment_approved, $old_status, $comment);
2573  return $rval;
2574 }
2575 
2591  static $_defer = false;
2592 
2593  if ( is_bool($defer) ) {
2594  $_defer = $defer;
2595  // flush any deferred counts
2596  if ( !$defer )
2597  wp_update_comment_count( null, true );
2598  }
2599 
2600  return $_defer;
2601 }
2602 
2623 function wp_update_comment_count($post_id, $do_deferred=false) {
2624  static $_deferred = array();
2625 
2626  if ( $do_deferred ) {
2627  $_deferred = array_unique($_deferred);
2628  foreach ( $_deferred as $i => $_post_id ) {
2629  wp_update_comment_count_now($_post_id);
2630  unset( $_deferred[$i] );
2631  }
2632  }
2633 
2634  if ( wp_defer_comment_counting() ) {
2635  $_deferred[] = $post_id;
2636  return true;
2637  }
2638  elseif ( $post_id ) {
2640  }
2641 
2642 }
2643 
2655  global $wpdb;
2656  $post_id = (int) $post_id;
2657  if ( !$post_id )
2658  return false;
2659  if ( !$post = get_post($post_id) )
2660  return false;
2661 
2662  $old = (int) $post->comment_count;
2663  $new = (int) $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id) );
2664  $wpdb->update( $wpdb->posts, array('comment_count' => $new), array('ID' => $post_id) );
2665 
2667 
2677  do_action( 'wp_update_comment_count', $post_id, $new, $old );
2679  do_action( 'edit_post', $post_id, $post );
2680 
2681  return true;
2682 }
2683 
2684 //
2685 // Ping and trackback functions.
2686 //
2687 
2701 function discover_pingback_server_uri( $url, $deprecated = '' ) {
2702  if ( !empty( $deprecated ) )
2703  _deprecated_argument( __FUNCTION__, '2.7' );
2704 
2705  $pingback_str_dquote = 'rel="pingback"';
2706  $pingback_str_squote = 'rel=\'pingback\'';
2707 
2709  $parsed_url = parse_url($url);
2710 
2711  if ( ! isset( $parsed_url['host'] ) ) // Not an URL. This should never happen.
2712  return false;
2713 
2714  //Do not search for a pingback server on our own uploads
2715  $uploads_dir = wp_upload_dir();
2716  if ( 0 === strpos($url, $uploads_dir['baseurl']) )
2717  return false;
2718 
2719  $response = wp_safe_remote_head( $url, array( 'timeout' => 2, 'httpversion' => '1.0' ) );
2720 
2721  if ( is_wp_error( $response ) )
2722  return false;
2723 
2724  if ( wp_remote_retrieve_header( $response, 'x-pingback' ) )
2725  return wp_remote_retrieve_header( $response, 'x-pingback' );
2726 
2727  // Not an (x)html, sgml, or xml page, no use going further.
2728  if ( preg_match('#(image|audio|video|model)/#is', wp_remote_retrieve_header( $response, 'content-type' )) )
2729  return false;
2730 
2731  // Now do a GET since we're going to look in the html headers (and we're sure it's not a binary file)
2732  $response = wp_safe_remote_get( $url, array( 'timeout' => 2, 'httpversion' => '1.0' ) );
2733 
2734  if ( is_wp_error( $response ) )
2735  return false;
2736 
2737  $contents = wp_remote_retrieve_body( $response );
2738 
2739  $pingback_link_offset_dquote = strpos($contents, $pingback_str_dquote);
2740  $pingback_link_offset_squote = strpos($contents, $pingback_str_squote);
2741  if ( $pingback_link_offset_dquote || $pingback_link_offset_squote ) {
2742  $quote = ($pingback_link_offset_dquote) ? '"' : '\'';
2743  $pingback_link_offset = ($quote=='"') ? $pingback_link_offset_dquote : $pingback_link_offset_squote;
2744  $pingback_href_pos = @strpos($contents, 'href=', $pingback_link_offset);
2745  $pingback_href_start = $pingback_href_pos+6;
2746  $pingback_href_end = @strpos($contents, $quote, $pingback_href_start);
2747  $pingback_server_url_len = $pingback_href_end - $pingback_href_start;
2748  $pingback_server_url = substr($contents, $pingback_href_start, $pingback_server_url_len);
2749 
2750  // We may find rel="pingback" but an incomplete pingback URL
2751  if ( $pingback_server_url_len > 0 ) { // We got it!
2752  return $pingback_server_url;
2753  }
2754  }
2755 
2756  return false;
2757 }
2758 
2766 function do_all_pings() {
2767  global $wpdb;
2768 
2769  // Do pingbacks
2770  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")) {
2771  delete_metadata_by_mid( 'post', $ping->meta_id );
2772  pingback( $ping->post_content, $ping->ID );
2773  }
2774 
2775  // Do Enclosures
2776  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")) {
2777  delete_metadata_by_mid( 'post', $enclosure->meta_id );
2778  do_enclose( $enclosure->post_content, $enclosure->ID );
2779  }
2780 
2781  // Do Trackbacks
2782  $trackbacks = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE to_ping <> '' AND post_status = 'publish'");
2783  if ( is_array($trackbacks) )
2784  foreach ( $trackbacks as $trackback )
2785  do_trackbacks($trackback);
2786 
2787  //Do Update Services/Generic Pings
2788  generic_ping();
2789 }
2790 
2801  global $wpdb;
2802 
2803  $post = get_post( $post_id );
2804  $to_ping = get_to_ping($post_id);
2805  $pinged = get_pung($post_id);
2806  if ( empty($to_ping) ) {
2807  $wpdb->update($wpdb->posts, array('to_ping' => ''), array('ID' => $post_id) );
2808  return;
2809  }
2810 
2811  if ( empty($post->post_excerpt) ) {
2813  $excerpt = apply_filters( 'the_content', $post->post_content, $post->ID );
2814  } else {
2816  $excerpt = apply_filters( 'the_excerpt', $post->post_excerpt );
2817  }
2818 
2819  $excerpt = str_replace(']]>', ']]&gt;', $excerpt);
2820  $excerpt = wp_html_excerpt($excerpt, 252, '&#8230;');
2821 
2823  $post_title = apply_filters( 'the_title', $post->post_title, $post->ID );
2824  $post_title = strip_tags($post_title);
2825 
2826  if ( $to_ping ) {
2827  foreach ( (array) $to_ping as $tb_ping ) {
2828  $tb_ping = trim($tb_ping);
2829  if ( !in_array($tb_ping, $pinged) ) {
2830  trackback($tb_ping, $post_title, $excerpt, $post_id);
2831  $pinged[] = $tb_ping;
2832  } else {
2833  $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, '')) WHERE ID = %d", $tb_ping, $post_id) );
2834  }
2835  }
2836  }
2837 }
2838 
2847 function generic_ping( $post_id = 0 ) {
2848  $services = get_option('ping_sites');
2849 
2850  $services = explode("\n", $services);
2851  foreach ( (array) $services as $service ) {
2852  $service = trim($service);
2853  if ( '' != $service )
2854  weblog_ping($service);
2855  }
2856 
2857  return $post_id;
2858 }
2859 
2871  global $wp_version;
2872  include_once(ABSPATH . WPINC . '/class-IXR.php');
2873  include_once(ABSPATH . WPINC . '/class-wp-http-ixr-client.php');
2874 
2875  // original code by Mort (http://mort.mine.nu:8080)
2876  $post_links = array();
2877 
2878  $pung = get_pung($post_ID);
2879 
2880  // Step 1
2881  // Parsing the post, external links (if any) are stored in the $post_links array
2882  $post_links_temp = wp_extract_urls( $content );
2883 
2884  // Step 2.
2885  // Walking thru the links array
2886  // first we get rid of links pointing to sites, not to specific files
2887  // Example:
2888  // http://dummy-weblog.org
2889  // http://dummy-weblog.org/
2890  // http://dummy-weblog.org/post.php
2891  // We don't wanna ping first and second types, even if they have a valid <link/>
2892 
2893  foreach ( (array) $post_links_temp as $link_test ) :
2894  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
2895  && !is_local_attachment($link_test) ) : // Also, let's never ping local attachments.
2896  if ( $test = @parse_url($link_test) ) {
2897  if ( isset($test['query']) )
2898  $post_links[] = $link_test;
2899  elseif ( isset( $test['path'] ) && ( $test['path'] != '/' ) && ( $test['path'] != '' ) )
2900  $post_links[] = $link_test;
2901  }
2902  endif;
2903  endforeach;
2904 
2905  $post_links = array_unique( $post_links );
2915  do_action_ref_array( 'pre_ping', array( &$post_links, &$pung, $post_ID ) );
2916 
2917  foreach ( (array) $post_links as $pagelinkedto ) {
2918  $pingback_server_url = discover_pingback_server_uri( $pagelinkedto );
2919 
2920  if ( $pingback_server_url ) {
2921  @ set_time_limit( 60 );
2922  // Now, the RPC call
2923  $pagelinkedfrom = get_permalink($post_ID);
2924 
2925  // using a timeout of 3 seconds should be enough to cover slow servers
2926  $client = new WP_HTTP_IXR_Client($pingback_server_url);
2927  $client->timeout = 3;
2940  $client->useragent = apply_filters( 'pingback_useragent', $client->useragent . ' -- WordPress/' . $wp_version, $client->useragent, $pingback_server_url, $pagelinkedto, $pagelinkedfrom );
2941  // when set to true, this outputs debug messages by itself
2942  $client->debug = false;
2943 
2944  if ( $client->query('pingback.ping', $pagelinkedfrom, $pagelinkedto) || ( isset($client->error->code) && 48 == $client->error->code ) ) // Already registered
2945  add_ping( $post_ID, $pagelinkedto );
2946  }
2947  }
2948 }
2949 
2958 function privacy_ping_filter($sites) {
2959  if ( '0' != get_option('blog_public') )
2960  return $sites;
2961  else
2962  return '';
2963 }
2964 
2980 function trackback($trackback_url, $title, $excerpt, $ID) {
2981  global $wpdb;
2982 
2983  if ( empty($trackback_url) )
2984  return;
2985 
2986  $options = array();
2987  $options['timeout'] = 4;
2988  $options['body'] = array(
2989  'title' => $title,
2990  'url' => get_permalink($ID),
2991  'blog_name' => get_option('blogname'),
2992  'excerpt' => $excerpt
2993  );
2994 
2995  $response = wp_safe_remote_post( $trackback_url, $options );
2996 
2997  if ( is_wp_error( $response ) )
2998  return;
2999 
3000  $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', %s) WHERE ID = %d", $trackback_url, $ID) );
3001  return $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, '')) WHERE ID = %d", $trackback_url, $ID) );
3002 }
3003 
3014 function weblog_ping($server = '', $path = '') {
3015  global $wp_version;
3016  include_once(ABSPATH . WPINC . '/class-IXR.php');
3017  include_once(ABSPATH . WPINC . '/class-wp-http-ixr-client.php');
3018 
3019  // using a timeout of 3 seconds should be enough to cover slow servers
3020  $client = new WP_HTTP_IXR_Client($server, ((!strlen(trim($path)) || ('/' == $path)) ? false : $path));
3021  $client->timeout = 3;
3022  $client->useragent .= ' -- WordPress/'.$wp_version;
3023 
3024  // when set to true, this outputs debug messages by itself
3025  $client->debug = false;
3026  $home = trailingslashit( home_url() );
3027  if ( !$client->query('weblogUpdates.extendedPing', get_option('blogname'), $home, get_bloginfo('rss2_url') ) ) // then try a normal ping
3028  $client->query('weblogUpdates.ping', get_option('blogname'), $home);
3029 }
3030 
3040 function pingback_ping_source_uri( $source_uri ) {
3041  return (string) wp_http_validate_url( $source_uri );
3042 }
3043 
3056 function xmlrpc_pingback_error( $ixr_error ) {
3057  if ( $ixr_error->code === 48 )
3058  return $ixr_error;
3059  return new IXR_Error( 0, '' );
3060 }
3061 
3062 //
3063 // Cache
3064 //
3065 
3073 function clean_comment_cache($ids) {
3074  foreach ( (array) $ids as $id )
3075  wp_cache_delete($id, 'comment');
3076 
3077  wp_cache_set( 'last_changed', microtime(), 'comment' );
3078 }
3079 
3092  foreach ( (array) $comments as $comment )
3093  wp_cache_add($comment->comment_ID, $comment, 'comment');
3094 }
3095 
3096 //
3097 // Internal
3098 //
3099 
3110 function _close_comments_for_old_posts( $posts, $query ) {
3111  if ( empty( $posts ) || ! $query->is_singular() || ! get_option( 'close_comments_for_old_posts' ) )
3112  return $posts;
3113 
3121  $post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) );
3122  if ( ! in_array( $posts[0]->post_type, $post_types ) )
3123  return $posts;
3124 
3125  $days_old = (int) get_option( 'close_comments_days_old' );
3126  if ( ! $days_old )
3127  return $posts;
3128 
3129  if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( $days_old * DAY_IN_SECONDS ) ) {
3130  $posts[0]->comment_status = 'closed';
3131  $posts[0]->ping_status = 'closed';
3132  }
3133 
3134  return $posts;
3135 }
3136 
3148  if ( ! $open )
3149  return $open;
3150 
3151  if ( !get_option('close_comments_for_old_posts') )
3152  return $open;
3153 
3154  $days_old = (int) get_option('close_comments_days_old');
3155  if ( !$days_old )
3156  return $open;
3157 
3158  $post = get_post($post_id);
3159 
3161  $post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) );
3162  if ( ! in_array( $post->post_type, $post_types ) )
3163  return $open;
3164 
3165  if ( time() - strtotime( $post->post_date_gmt ) > ( $days_old * DAY_IN_SECONDS ) )
3166  return false;
3167 
3168  return $open;
3169 }
const ARRAY_N
Definition: wp-db.php:36
wp_cache_get($key, $group= '', $force=false, &$found=null)
Definition: cache.php:110
get_default_comment_status($post_type= 'post', $comment_type= 'comment')
Definition: comment.php:1045
pingback_ping_source_uri($source_uri)
Definition: comment.php:3040
apply_filters_ref_array($tag, $args)
Definition: plugin.php:239
endif
Definition: about.php:130
delete_comment_meta($comment_id, $meta_key, $meta_value= '')
Definition: comment.php:1212
do_action($tag, $arg= '')
Definition: plugin.php:460
wp_trash_comment($comment_id)
Definition: comment.php:1843
$user_id
Definition: user-edit.php:14
$_COOKIE
Definition: xmlrpc.php:16
wp_get_current_commenter()
Definition: comment.php:2114
global $post_type
query($query)
Definition: comment.php:459
$keys
Definition: wp-cron.php:61
$GLOBALS['wp_press_this']
if(!$comment_id) $comment
add_ping($post_id, $uri)
Definition: post.php:4094
wp_die($message= '', $title= '', $args=array())
Definition: functions.php:2375
_close_comments_for_old_posts($posts, $query)
Definition: comment.php:3110
$comment_id
$post_id
wp_delete_comment($comment_id, $force_delete=false)
Definition: comment.php:1779
privacy_ping_filter($sites)
Definition: comment.php:2958
const ABSPATH
get_option()
trackback($trackback_url, $title, $excerpt, $ID)
Definition: comment.php:2980
__construct($query= '')
Definition: comment.php:382
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:3104
get_pung($post_id)
Definition: post.php:4160
get_approved_comments($post_id, $args=array())
Definition: comment.php:139
sanitize_comment_cookies()
Definition: comment.php:1288
update_comment_meta($comment_id, $meta_key, $meta_value, $prev_value= '')
Definition: comment.php:1249
wp_slash($value)
wp_extract_urls($content)
Definition: functions.php:466
COOKIE_DOMAIN
Definition: wp-login.php:445
parse_orderby($orderby)
Definition: comment.php:942
check_comment_flood_db($ip, $email, $date)
Definition: comment.php:1469
delete_metadata_by_mid($meta_type, $meta_id)
Definition: meta.php:700
wp_remote_retrieve_header($response, $header)
Definition: http.php:233
check_comment($author, $email, $url, $comment, $user_ip, $user_agent, $comment_type)
Definition: comment.php:39
if(isset($_REQUEST['post_id'])) $id
get_comment_statuses()
Definition: comment.php:1024
do_action_ref_array()
$path
Definition: wp-load.php:56
is_wp_error($thing)
esc_attr()
endforeach
Definition: about.php:132
wp_unspam_comment($comment_id)
Definition: comment.php:1964
generic_ping($post_id=0)
Definition: comment.php:2847
$out
wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent)
Definition: comment.php:1650
wp_cache_set($key, $data, $group= '', $expire=0)
Definition: cache.php:177
const object
Definition: wp-db.php:21
get_lastcommentmodified($timezone= 'server')
Definition: comment.php:1091
get_comment_count($post_id=0)
Definition: comment.php:1131
wp_set_comment_status($comment_id, $comment_status, $wp_error=false)
Definition: comment.php:2438
const ARRAY_A
Definition: wp-db.php:31
get_metadata($meta_type, $object_id, $meta_key= '', $single=false)
Definition: meta.php:459
if(empty($post->comment_status)) $status
get_post($post=null, $output=OBJECT, $filter= 'raw')
Definition: post.php:425
$title
Definition: about.php:19
separate_comments(&$comments)
Definition: comment.php:1514
add_metadata($meta_type, $object_id, $meta_key, $meta_value, $unique=false)
Definition: meta.php:31
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:69
wp_filter_comment($commentdata)
Definition: comment.php:2234
comment_ID()
get_comment_meta($comment_id, $key= '', $single=false)
Definition: comment.php:1228
if(defined( 'RELOCATE')&&RELOCATE) $secure
Definition: wp-login.php:443
weblog_ping($server= '', $path= '')
Definition: comment.php:3014
$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:73
get_to_ping($post_id)
Definition: post.php:4186
$comments
__call($name, $arguments)
Definition: comment.php:303
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:503
delete_metadata($meta_type, $object_id, $meta_key, $meta_value= '', $delete_all=false)
Definition: meta.php:311
$excerpt
const WPINC
wp_count_comments($post_id=0)
Definition: comment.php:1712
wp_safe_remote_post($url, $args=array())
Definition: http.php:90
apply_filters()
get_bloginfo()
wp_safe_remote_head($url, $args=array())
Definition: http.php:111
global $post
wp_defer_comment_counting($defer=null)
Definition: comment.php:2590
trailingslashit($string)
wp_cache_delete($key, $group= '')
Definition: cache.php:75
home_url()
if(get_option('require_name_email')&&!$user->exists()) if( ''==$comment_content) $comment_parent
wp_throttle_comment_flood($block, $time_lastcomment, $time_newcomment)
Definition: comment.php:2296
if(has_action( 'welcome_panel')&&current_user_can( 'edit_theme_options')) $option
Definition: index.php:101
clean_comment_cache($ids)
Definition: comment.php:3073
clean_post_cache($post)
Definition: post.php:5629
$post_title
Definition: image.php:23
get_comment_pages_count($comments=null, $per_page=null, $threaded=null)
Definition: comment.php:1543
parse_order($order)
Definition: comment.php:1001
get_comment(&$comment, $output=OBJECT)
Definition: comment.php:171
wp_parse_id_list($list)
Definition: functions.php:3088
wp_get_comment_status($comment_id)
Definition: comment.php:2005
wp_untrash_comment($comment_id)
Definition: comment.php:1885
$ID
get_page_of_comment($comment_ID, $args=array())
Definition: comment.php:1589
_close_comments_for_old_post($open, $post_id)
Definition: comment.php:3147
wp_parse_args($args, $defaults= '')
Definition: functions.php:3067
wp_html_excerpt($str, $count, $more=null)
get_comments($args= '')
Definition: comment.php:226
_deprecated_argument($function, $version, $message=null)
Definition: functions.php:3547
is_local_attachment($url)
Definition: post.php:4806
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:47
get_search_sql($string, $cols)
Definition: comment.php:918
wp_set_comment_cookies($comment, $user)
Definition: comment.php:1262
discover_pingback_server_uri($url, $deprecated= '')
Definition: comment.php:2701
update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_value= '')
Definition: meta.php:144
$_SERVER['REQUEST_URI']
Definition: edit-tags.php:322
wp_unslash($value)
post_type_supports($post_type, $feature)
Definition: post.php:1815
$comment_author_url
url_to_postid($url)
Definition: rewrite.php:414
$comment_post_ID
wp_new_comment($commentdata)
Definition: comment.php:2327
$commentdata
global $wp_version
wp_spam_comment($comment_id)
Definition: comment.php:1927
current_time($type, $gmt=0)
Definition: functions.php:61
wp_update_comment($commentarr)
Definition: comment.php:2507
esc_sql($data)
do_all_pings()
Definition: comment.php:2766
get_query_var($var, $default= '')
Definition: query.php:25
do_trackbacks($post_id)
Definition: comment.php:2800
$content
Definition: feed-rss2.php:96
$comment_author
wp_remote_retrieve_body($response)
Definition: http.php:285
add_comment_meta($comment_id, $meta_key, $meta_value, $unique=false)
Definition: comment.php:1193
xmlrpc_pingback_error($ixr_error)
Definition: comment.php:3056
wp_http_validate_url($url)
Definition: http.php:450
wp_insert_comment($commentdata)
Definition: comment.php:2173
wp_allow_comment($commentdata)
Definition: comment.php:1350
pingback($content, $post_ID)
Definition: comment.php:2870
if(!defined( 'WP_ADMIN'))
Definition: admin-ajax.php:17
wp_upload_dir($time=null)
Definition: functions.php:1734
$comment_content
get_gmt_from_date($string, $format= 'Y-m-d H:i:s')
wp_update_comment_count_now($post_id)
Definition: comment.php:2654
if(current_theme_supports( 'post-formats')&&post_type_supports($post_type, 'post-formats')) add_meta_box( 'formatdiv' null
global $wpdb
Definition: schema.php:18
parse_query($query= '')
Definition: comment.php:436
wp_transition_comment_status($new_status, $old_status, $comment)
Definition: comment.php:2046
update_comment_cache($comments)
Definition: comment.php:3091
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'if(post_type_supports($post_type, 'excerpt')) add_meta_box('postexcerpt' __('Excerpt')
wp_update_comment_count($post_id, $do_deferred=false)
Definition: comment.php:2623
current_user_can($capability)