wordpress  4.2.2
About: WordPress is a blogging system that has evolved to a full content management system.
  Fossies Dox: wordpress-4.2.2.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 
170 function get_comment(&$comment, $output = OBJECT) {
171  global $wpdb;
172 
173  if ( empty($comment) ) {
174  if ( isset($GLOBALS['comment']) )
175  $_comment = & $GLOBALS['comment'];
176  else
177  $_comment = null;
178  } elseif ( is_object($comment) ) {
179  wp_cache_add($comment->comment_ID, $comment, 'comment');
180  $_comment = $comment;
181  } else {
182  if ( isset($GLOBALS['comment']) && ($GLOBALS['comment']->comment_ID == $comment) ) {
183  $_comment = & $GLOBALS['comment'];
184  } elseif ( ! $_comment = wp_cache_get($comment, 'comment') ) {
185  $_comment = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment));
186  if ( ! $_comment )
187  return null;
188  wp_cache_add($_comment->comment_ID, $_comment, 'comment');
189  }
190  }
191 
199  $_comment = apply_filters( 'get_comment', $_comment );
200 
201  if ( $output == OBJECT ) {
202  return $_comment;
203  } elseif ( $output == ARRAY_A ) {
204  $__comment = get_object_vars($_comment);
205  return $__comment;
206  } elseif ( $output == ARRAY_N ) {
207  $__comment = array_values(get_object_vars($_comment));
208  return $__comment;
209  } else {
210  return $_comment;
211  }
212 }
213 
225 function get_comments( $args = '' ) {
226  $query = new WP_Comment_Query;
227  return $query->query( $args );
228 }
229 
245  public $request;
246 
254  public $meta_query = false;
255 
263  public $date_query = false;
264 
272  public $query_vars;
273 
282 
290  public $comments;
291 
302  public function __call( $name, $arguments ) {
303  if ( 'get_search_sql' === $name ) {
304  return call_user_func_array( array( $this, $name ), $arguments );
305  }
306  return false;
307  }
308 
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 
485  if ( ! empty( $this->meta_query->queries ) ) {
486  $meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this );
487  }
488 
496  do_action_ref_array( 'pre_get_comments', array( &$this ) );
497 
498  // $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
499  $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) );
500  $last_changed = wp_cache_get( 'last_changed', 'comment' );
501  if ( ! $last_changed ) {
502  $last_changed = microtime();
503  wp_cache_set( 'last_changed', $last_changed, 'comment' );
504  }
505  $cache_key = "get_comments:$key:$last_changed";
506 
507  if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
508  $this->comments = $cache;
509  return $this->comments;
510  }
511 
512  $where = array();
513 
514  // Assemble clauses related to 'comment_approved'.
515  $approved_clauses = array();
516 
517  // 'status' accepts an array or a comma-separated string.
518  $status_clauses = array();
519  $statuses = $this->query_vars['status'];
520  if ( ! is_array( $statuses ) ) {
521  $statuses = preg_split( '/[\s,]+/', $statuses );
522  }
523 
524  // 'any' overrides other statuses.
525  if ( ! in_array( 'any', $statuses ) ) {
526  foreach ( $statuses as $status ) {
527  switch ( $status ) {
528  case 'hold' :
529  $status_clauses[] = "comment_approved = '0'";
530  break;
531 
532  case 'approve' :
533  $status_clauses[] = "comment_approved = '1'";
534  break;
535 
536  case 'all' :
537  case '' :
538  $status_clauses[] = "( comment_approved = '0' OR comment_approved = '1' )";
539  break;
540 
541  default :
542  $status_clauses[] = $wpdb->prepare( "comment_approved = %s", $status );
543  break;
544  }
545  }
546 
547  if ( ! empty( $status_clauses ) ) {
548  $approved_clauses[] = '( ' . implode( ' OR ', $status_clauses ) . ' )';
549  }
550  }
551 
552  // User IDs or emails whose unapproved comments are included, regardless of $status.
553  if ( ! empty( $this->query_vars['include_unapproved'] ) ) {
554  $include_unapproved = $this->query_vars['include_unapproved'];
555 
556  // Accepts arrays or comma-separated strings.
557  if ( ! is_array( $include_unapproved ) ) {
558  $include_unapproved = preg_split( '/[\s,]+/', $include_unapproved );
559  }
560 
561  $unapproved_ids = $unapproved_emails = array();
562  foreach ( $include_unapproved as $unapproved_identifier ) {
563  // Numeric values are assumed to be user ids.
564  if ( is_numeric( $unapproved_identifier ) ) {
565  $approved_clauses[] = $wpdb->prepare( "( user_id = %d AND comment_approved = '0' )", $unapproved_identifier );
566 
567  // Otherwise we match against email addresses.
568  } else {
569  $approved_clauses[] = $wpdb->prepare( "( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier );
570  }
571  }
572  }
573 
574  // Collapse comment_approved clauses into a single OR-separated clause.
575  if ( ! empty( $approved_clauses ) ) {
576  if ( 1 === count( $approved_clauses ) ) {
577  $where[] = $approved_clauses[0];
578  } else {
579  $where[] = '( ' . implode( ' OR ', $approved_clauses ) . ' )';
580  }
581  }
582 
583  $order = ( 'ASC' == strtoupper( $this->query_vars['order'] ) ) ? 'ASC' : 'DESC';
584 
585  // Disable ORDER BY with 'none', an empty array, or boolean false.
586  if ( in_array( $this->query_vars['orderby'], array( 'none', array(), false ), true ) ) {
587  $orderby = '';
588  } elseif ( ! empty( $this->query_vars['orderby'] ) ) {
589  $ordersby = is_array( $this->query_vars['orderby'] ) ?
590  $this->query_vars['orderby'] :
591  preg_split( '/[,\s]/', $this->query_vars['orderby'] );
592 
593  $orderby_array = array();
594  $found_orderby_comment_ID = false;
595  foreach ( $ordersby as $_key => $_value ) {
596  if ( ! $_value ) {
597  continue;
598  }
599 
600  if ( is_int( $_key ) ) {
601  $_orderby = $_value;
602  $_order = $order;
603  } else {
604  $_orderby = $_key;
605  $_order = $_value;
606  }
607 
608  if ( ! $found_orderby_comment_ID && 'comment_ID' === $_orderby ) {
609  $found_orderby_comment_ID = true;
610  }
611 
612  $parsed = $this->parse_orderby( $_orderby );
613 
614  if ( ! $parsed ) {
615  continue;
616  }
617 
618  $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order );
619  }
620 
621  // If no valid clauses were found, order by comment_date_gmt.
622  if ( empty( $orderby_array ) ) {
623  $orderby_array[] = "$wpdb->comments.comment_date_gmt $order";
624  }
625 
626  // To ensure determinate sorting, always include a comment_ID clause.
627  if ( ! $found_orderby_comment_ID ) {
628  $comment_ID_order = '';
629 
630  // Inherit order from comment_date or comment_date_gmt, if available.
631  foreach ( $orderby_array as $orderby_clause ) {
632  if ( preg_match( '/comment_date(?:_gmt)*\ (ASC|DESC)/', $orderby_clause, $match ) ) {
633  $comment_ID_order = $match[1];
634  break;
635  }
636  }
637 
638  // If no date-related order is available, use the date from the first available clause.
639  if ( ! $comment_ID_order ) {
640  foreach ( $orderby_array as $orderby_clause ) {
641  if ( false !== strpos( 'ASC', $orderby_clause ) ) {
642  $comment_ID_order = 'ASC';
643  } else {
644  $comment_ID_order = 'DESC';
645  }
646 
647  break;
648  }
649  }
650 
651  // Default to DESC.
652  if ( ! $comment_ID_order ) {
653  $comment_ID_order = 'DESC';
654  }
655 
656  $orderby_array[] = "$wpdb->comments.comment_ID $comment_ID_order";
657  }
658 
659  $orderby = implode( ', ', $orderby_array );
660  } else {
661  $orderby = "$wpdb->comments.comment_date_gmt $order";
662  }
663 
664  $number = absint( $this->query_vars['number'] );
665  $offset = absint( $this->query_vars['offset'] );
666 
667  if ( ! empty( $number ) ) {
668  if ( $offset ) {
669  $limits = 'LIMIT ' . $offset . ',' . $number;
670  } else {
671  $limits = 'LIMIT ' . $number;
672  }
673  } else {
674  $limits = '';
675  }
676 
677  if ( $this->query_vars['count'] ) {
678  $fields = 'COUNT(*)';
679  } else {
680  switch ( strtolower( $this->query_vars['fields'] ) ) {
681  case 'ids':
682  $fields = "$wpdb->comments.comment_ID";
683  break;
684  default:
685  $fields = "*";
686  break;
687  }
688  }
689 
690  $join = '';
691 
692  $post_id = absint( $this->query_vars['post_id'] );
693  if ( ! empty( $post_id ) ) {
694  $where[] = $wpdb->prepare( 'comment_post_ID = %d', $post_id );
695  }
696 
697  // Parse comment IDs for an IN clause.
698  if ( ! empty( $this->query_vars['comment__in'] ) ) {
699  $where[] = 'comment_ID IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['comment__in'] ) ) . ' )';
700  }
701 
702  // Parse comment IDs for a NOT IN clause.
703  if ( ! empty( $this->query_vars['comment__not_in'] ) ) {
704  $where[] = 'comment_ID NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['comment__not_in'] ) ) . ' )';
705  }
706 
707  // Parse comment post IDs for an IN clause.
708  if ( ! empty( $this->query_vars['post__in'] ) ) {
709  $where[] = 'comment_post_ID IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__in'] ) ) . ' )';
710  }
711 
712  // Parse comment post IDs for a NOT IN clause.
713  if ( ! empty( $this->query_vars['post__not_in'] ) ) {
714  $where[] = 'comment_post_ID NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__not_in'] ) ) . ' )';
715  }
716 
717  if ( '' !== $this->query_vars['author_email'] ) {
718  $where[] = $wpdb->prepare( 'comment_author_email = %s', $this->query_vars['author_email'] );
719  }
720 
721  if ( '' !== $this->query_vars['karma'] ) {
722  $where[] = $wpdb->prepare( 'comment_karma = %d', $this->query_vars['karma'] );
723  }
724 
725  // Filtering by comment_type: 'type', 'type__in', 'type__not_in'.
726  $raw_types = array(
727  'IN' => array_merge( (array) $this->query_vars['type'], (array) $this->query_vars['type__in'] ),
728  'NOT IN' => (array) $this->query_vars['type__not_in'],
729  );
730 
731  $comment_types = array();
732  foreach ( $raw_types as $operator => $_raw_types ) {
733  $_raw_types = array_unique( $_raw_types );
734 
735  foreach ( $_raw_types as $type ) {
736  switch ( $type ) {
737  // An empty translates to 'all', for backward compatibility
738  case '':
739  case 'all' :
740  break;
741 
742  case 'comment':
743  case 'comments':
744  $comment_types[ $operator ][] = "''";
745  break;
746 
747  case 'pings':
748  $comment_types[ $operator ][] = "'pingback'";
749  $comment_types[ $operator ][] = "'trackback'";
750  break;
751 
752  default:
753  $comment_types[ $operator ][] = $wpdb->prepare( '%s', $type );
754  break;
755  }
756  }
757 
758  if ( ! empty( $comment_types[ $operator ] ) ) {
759  $types_sql = implode( ', ', $comment_types[ $operator ] );
760  $where[] = "comment_type $operator ($types_sql)";
761  }
762  }
763 
764  if ( '' !== $this->query_vars['parent'] ) {
765  $where[] = $wpdb->prepare( 'comment_parent = %d', $this->query_vars['parent'] );
766  }
767 
768  if ( is_array( $this->query_vars['user_id'] ) ) {
769  $where[] = 'user_id IN (' . implode( ',', array_map( 'absint', $this->query_vars['user_id'] ) ) . ')';
770  } elseif ( '' !== $this->query_vars['user_id'] ) {
771  $where[] = $wpdb->prepare( 'user_id = %d', $this->query_vars['user_id'] );
772  }
773 
774  if ( '' !== $this->query_vars['search'] ) {
775  $search_sql = $this->get_search_sql(
776  $this->query_vars['search'],
777  array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' )
778  );
779 
780  // Strip leading 'AND'.
781  $where[] = preg_replace( '/^\s*AND\s*/', '', $search_sql );
782  }
783 
784  // If any post-related query vars are passed, join the posts table.
785  $join_posts_table = false;
786  $plucked = wp_array_slice_assoc( $this->query_vars, array( 'post_author', 'post_name', 'post_parent', 'post_status', 'post_type' ) );
787  $post_fields = array_filter( $plucked );
788 
789  if ( ! empty( $post_fields ) ) {
790  $join_posts_table = true;
791  foreach ( $post_fields as $field_name => $field_value ) {
792  // $field_value may be an array.
793  $esses = array_fill( 0, count( (array) $field_value ), '%s' );
794  $where[] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value );
795  }
796  }
797 
798  // Comment author IDs for an IN clause.
799  if ( ! empty( $this->query_vars['author__in'] ) ) {
800  $where[] = 'user_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__in'] ) ) . ' )';
801  }
802 
803  // Comment author IDs for a NOT IN clause.
804  if ( ! empty( $this->query_vars['author__not_in'] ) ) {
805  $where[] = 'user_id NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__not_in'] ) ) . ' )';
806  }
807 
808  // Post author IDs for an IN clause.
809  if ( ! empty( $this->query_vars['post_author__in'] ) ) {
810  $join_posts_table = true;
811  $where[] = 'post_author IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__in'] ) ) . ' )';
812  }
813 
814  // Post author IDs for a NOT IN clause.
815  if ( ! empty( $this->query_vars['post_author__not_in'] ) ) {
816  $join_posts_table = true;
817  $where[] = 'post_author NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__not_in'] ) ) . ' )';
818  }
819 
820  if ( $join_posts_table ) {
821  $join = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
822  }
823 
824  if ( ! empty( $meta_query_clauses ) ) {
825  $join .= $meta_query_clauses['join'];
826 
827  // Strip leading 'AND'.
828  $where[] = preg_replace( '/^\s*AND\s*/', '', $meta_query_clauses['where'] );
829 
830  if ( ! $this->query_vars['count'] ) {
831  $groupby = "{$wpdb->comments}.comment_ID";
832  }
833  }
834 
835  $date_query = $this->query_vars['date_query'];
836  if ( ! empty( $date_query ) && is_array( $date_query ) ) {
837  $date_query_object = new WP_Date_Query( $date_query, 'comment_date' );
838  $where[] = preg_replace( '/^\s*AND\s*/', '', $date_query_object->get_sql() );
839  }
840 
841  $where = implode( ' AND ', $where );
842 
843  $pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' );
852  $clauses = apply_filters_ref_array( 'comments_clauses', array( compact( $pieces ), &$this ) );
853 
854  $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : '';
855  $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : '';
856  $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : '';
857  $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : '';
858  $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : '';
859  $groupby = isset( $clauses[ 'groupby' ] ) ? $clauses[ 'groupby' ] : '';
860 
861  if ( $where ) {
862  $where = 'WHERE ' . $where;
863  }
864 
865  if ( $groupby ) {
866  $groupby = 'GROUP BY ' . $groupby;
867  }
868 
869  if ( $orderby ) {
870  $orderby = "ORDER BY $orderby";
871  }
872 
873  $this->request = "SELECT $fields FROM $wpdb->comments $join $where $groupby $orderby $limits";
874 
875  if ( $this->query_vars['count'] ) {
876  return $wpdb->get_var( $this->request );
877  }
878 
879  if ( 'ids' == $this->query_vars['fields'] ) {
880  $this->comments = $wpdb->get_col( $this->request );
881  return array_map( 'intval', $this->comments );
882  }
883 
884  $results = $wpdb->get_results( $this->request );
893  $comments = apply_filters_ref_array( 'the_comments', array( $results, &$this ) );
894 
895  wp_cache_add( $cache_key, $comments, 'comment' );
896 
897  $this->comments = $comments;
898  return $this->comments;
899  }
900 
911  protected function get_search_sql( $string, $cols ) {
912  global $wpdb;
913 
914  $like = '%' . $wpdb->esc_like( $string ) . '%';
915 
916  $searches = array();
917  foreach ( $cols as $col ) {
918  $searches[] = $wpdb->prepare( "$col LIKE %s", $like );
919  }
920 
921  return ' AND (' . implode(' OR ', $searches) . ')';
922  }
923 
935  protected function parse_orderby( $orderby ) {
936  global $wpdb;
937 
938  $allowed_keys = array(
939  'comment_agent',
940  'comment_approved',
941  'comment_author',
942  'comment_author_email',
943  'comment_author_IP',
944  'comment_author_url',
945  'comment_content',
946  'comment_date',
947  'comment_date_gmt',
948  'comment_ID',
949  'comment_karma',
950  'comment_parent',
951  'comment_post_ID',
952  'comment_type',
953  'user_id',
954  );
955 
956  if ( ! empty( $this->query_vars['meta_key'] ) ) {
957  $allowed_keys[] = $this->query_vars['meta_key'];
958  $allowed_keys[] = 'meta_value';
959  $allowed_keys[] = 'meta_value_num';
960  }
961 
962  $meta_query_clauses = $this->meta_query->get_clauses();
963  if ( $meta_query_clauses ) {
964  $allowed_keys = array_merge( $allowed_keys, array_keys( $meta_query_clauses ) );
965  }
966 
967  $parsed = false;
968  if ( $orderby == $this->query_vars['meta_key'] || $orderby == 'meta_value' ) {
969  $parsed = "$wpdb->commentmeta.meta_value";
970  } else if ( $orderby == 'meta_value_num' ) {
971  $parsed = "$wpdb->commentmeta.meta_value+0";
972  } else if ( in_array( $orderby, $allowed_keys ) ) {
973 
974  if ( isset( $meta_query_clauses[ $orderby ] ) ) {
975  $meta_clause = $meta_query_clauses[ $orderby ];
976  $parsed = sprintf( "CAST(%s.meta_value AS %s)", esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) );
977  } else {
978  $parsed = "$wpdb->comments.$orderby";
979  }
980  }
981 
982  return $parsed;
983  }
984 
994  protected function parse_order( $order ) {
995  if ( ! is_string( $order ) || empty( $order ) ) {
996  return 'DESC';
997  }
998 
999  if ( 'ASC' === strtoupper( $order ) ) {
1000  return 'ASC';
1001  } else {
1002  return 'DESC';
1003  }
1004  }
1005 }
1006 
1018  $status = array(
1019  'hold' => __('Unapproved'),
1020  /* translators: comment status */
1021  'approve' => _x('Approved', 'adjective'),
1022  /* translators: comment status */
1023  'spam' => _x('Spam', 'adjective'),
1024  );
1025 
1026  return $status;
1027 }
1028 
1040 function get_lastcommentmodified($timezone = 'server') {
1041  global $wpdb;
1042  static $cache_lastcommentmodified = array();
1043 
1044  if ( isset($cache_lastcommentmodified[$timezone]) )
1045  return $cache_lastcommentmodified[$timezone];
1046 
1047  $add_seconds_server = date('Z');
1048 
1049  switch ( strtolower($timezone)) {
1050  case 'gmt':
1051  $lastcommentmodified = $wpdb->get_var("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1");
1052  break;
1053  case 'blog':
1054  $lastcommentmodified = $wpdb->get_var("SELECT comment_date FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1");
1055  break;
1056  case 'server':
1057  $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));
1058  break;
1059  }
1060 
1061  $cache_lastcommentmodified[$timezone] = $lastcommentmodified;
1062 
1063  return $lastcommentmodified;
1064 }
1065 
1080 function get_comment_count( $post_id = 0 ) {
1081  global $wpdb;
1082 
1083  $post_id = (int) $post_id;
1084 
1085  $where = '';
1086  if ( $post_id > 0 ) {
1087  $where = $wpdb->prepare("WHERE comment_post_ID = %d", $post_id);
1088  }
1089 
1090  $totals = (array) $wpdb->get_results("
1091  SELECT comment_approved, COUNT( * ) AS total
1092  FROM {$wpdb->comments}
1093  {$where}
1094  GROUP BY comment_approved
1095  ", ARRAY_A);
1096 
1097  $comment_count = array(
1098  "approved" => 0,
1099  "awaiting_moderation" => 0,
1100  "spam" => 0,
1101  "total_comments" => 0
1102  );
1103 
1104  foreach ( $totals as $row ) {
1105  switch ( $row['comment_approved'] ) {
1106  case 'spam':
1107  $comment_count['spam'] = $row['total'];
1108  $comment_count["total_comments"] += $row['total'];
1109  break;
1110  case 1:
1111  $comment_count['approved'] = $row['total'];
1112  $comment_count['total_comments'] += $row['total'];
1113  break;
1114  case 0:
1115  $comment_count['awaiting_moderation'] = $row['total'];
1116  $comment_count['total_comments'] += $row['total'];
1117  break;
1118  default:
1119  break;
1120  }
1121  }
1122 
1123  return $comment_count;
1124 }
1125 
1126 //
1127 // Comment meta functions
1128 //
1129 
1142 function add_comment_meta($comment_id, $meta_key, $meta_value, $unique = false) {
1143  return add_metadata('comment', $comment_id, $meta_key, $meta_value, $unique);
1144 }
1145 
1161 function delete_comment_meta($comment_id, $meta_key, $meta_value = '') {
1162  return delete_metadata('comment', $comment_id, $meta_key, $meta_value);
1163 }
1164 
1177 function get_comment_meta($comment_id, $key = '', $single = false) {
1178  return get_metadata('comment', $comment_id, $key, $single);
1179 }
1180 
1198 function update_comment_meta($comment_id, $meta_key, $meta_value, $prev_value = '') {
1199  return update_metadata('comment', $comment_id, $meta_key, $meta_value, $prev_value);
1200 }
1201 
1212  if ( $user->exists() )
1213  return;
1214 
1222  $comment_cookie_lifetime = apply_filters( 'comment_cookie_lifetime', 30000000 );
1223  $secure = ( 'https' === parse_url( home_url(), PHP_URL_SCHEME ) );
1224  setcookie( 'comment_author_' . COOKIEHASH, $comment->comment_author, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
1225  setcookie( 'comment_author_email_' . COOKIEHASH, $comment->comment_author_email, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
1226  setcookie( 'comment_author_url_' . COOKIEHASH, esc_url($comment->comment_author_url), time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
1227 }
1228 
1238  if ( isset( $_COOKIE['comment_author_' . COOKIEHASH] ) ) {
1249  $comment_author = apply_filters( 'pre_comment_author_name', $_COOKIE['comment_author_' . COOKIEHASH] );
1252  $_COOKIE['comment_author_' . COOKIEHASH] = $comment_author;
1253  }
1254 
1255  if ( isset( $_COOKIE['comment_author_email_' . COOKIEHASH] ) ) {
1266  $comment_author_email = apply_filters( 'pre_comment_author_email', $_COOKIE['comment_author_email_' . COOKIEHASH] );
1269  $_COOKIE['comment_author_email_'.COOKIEHASH] = $comment_author_email;
1270  }
1271 
1272  if ( isset( $_COOKIE['comment_author_url_' . COOKIEHASH] ) ) {
1283  $comment_author_url = apply_filters( 'pre_comment_author_url', $_COOKIE['comment_author_url_' . COOKIEHASH] );
1285  $_COOKIE['comment_author_url_'.COOKIEHASH] = $comment_author_url;
1286  }
1287 }
1288 
1300  global $wpdb;
1301 
1302  // Simple duplicate check
1303  // expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content)
1304  $dupe = $wpdb->prepare(
1305  "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = %s AND comment_approved != 'trash' AND ( comment_author = %s ",
1306  wp_unslash( $commentdata['comment_post_ID'] ),
1307  wp_unslash( $commentdata['comment_parent'] ),
1308  wp_unslash( $commentdata['comment_author'] )
1309  );
1310  if ( $commentdata['comment_author_email'] ) {
1311  $dupe .= $wpdb->prepare(
1312  "OR comment_author_email = %s ",
1313  wp_unslash( $commentdata['comment_author_email'] )
1314  );
1315  }
1316  $dupe .= $wpdb->prepare(
1317  ") AND comment_content = %s LIMIT 1",
1318  wp_unslash( $commentdata['comment_content'] )
1319  );
1320  if ( $wpdb->get_var( $dupe ) ) {
1328  do_action( 'comment_duplicate_trigger', $commentdata );
1329  if ( defined( 'DOING_AJAX' ) ) {
1330  die( __('Duplicate comment detected; it looks as though you&#8217;ve already said that!') );
1331  }
1332  wp_die( __( 'Duplicate comment detected; it looks as though you&#8217;ve already said that!' ), 409 );
1333  }
1334 
1346  do_action(
1347  'check_comment_flood',
1348  $commentdata['comment_author_IP'],
1349  $commentdata['comment_author_email'],
1350  $commentdata['comment_date_gmt']
1351  );
1352 
1353  if ( ! empty( $commentdata['user_id'] ) ) {
1354  $user = get_userdata( $commentdata['user_id'] );
1355  $post_author = $wpdb->get_var( $wpdb->prepare(
1356  "SELECT post_author FROM $wpdb->posts WHERE ID = %d LIMIT 1",
1357  $commentdata['comment_post_ID']
1358  ) );
1359  }
1360 
1361  if ( isset( $user ) && ( $commentdata['user_id'] == $post_author || $user->has_cap( 'moderate_comments' ) ) ) {
1362  // The author and the admins get respect.
1363  $approved = 1;
1364  } else {
1365  // Everyone else's comments will be checked.
1366  if ( check_comment(
1367  $commentdata['comment_author'],
1368  $commentdata['comment_author_email'],
1369  $commentdata['comment_author_url'],
1370  $commentdata['comment_content'],
1371  $commentdata['comment_author_IP'],
1372  $commentdata['comment_agent'],
1373  $commentdata['comment_type']
1374  ) ) {
1375  $approved = 1;
1376  } else {
1377  $approved = 0;
1378  }
1379 
1380  if ( wp_blacklist_check(
1381  $commentdata['comment_author'],
1382  $commentdata['comment_author_email'],
1383  $commentdata['comment_author_url'],
1384  $commentdata['comment_content'],
1385  $commentdata['comment_author_IP'],
1386  $commentdata['comment_agent']
1387  ) ) {
1388  $approved = 'spam';
1389  }
1390  }
1391 
1400  $approved = apply_filters( 'pre_comment_approved', $approved, $commentdata );
1401  return $approved;
1402 }
1403 
1418 function check_comment_flood_db( $ip, $email, $date ) {
1419  global $wpdb;
1420  if ( current_user_can( 'manage_options' ) )
1421  return; // don't throttle admins
1422  $hour_ago = gmdate( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS );
1423  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 ) ) ) {
1424  $time_lastcomment = mysql2date('U', $lasttime, false);
1425  $time_newcomment = mysql2date('U', $date, false);
1435  $flood_die = apply_filters( 'comment_flood_filter', false, $time_lastcomment, $time_newcomment );
1436  if ( $flood_die ) {
1445  do_action( 'comment_flood_trigger', $time_lastcomment, $time_newcomment );
1446 
1447  if ( defined('DOING_AJAX') )
1448  die( __('You are posting comments too quickly. Slow down.') );
1449 
1450  wp_die( __( 'You are posting comments too quickly. Slow down.' ), 429 );
1451  }
1452  }
1453 }
1454 
1464  $comments_by_type = array('comment' => array(), 'trackback' => array(), 'pingback' => array(), 'pings' => array());
1465  $count = count($comments);
1466  for ( $i = 0; $i < $count; $i++ ) {
1467  $type = $comments[$i]->comment_type;
1468  if ( empty($type) )
1469  $type = 'comment';
1470  $comments_by_type[$type][] = &$comments[$i];
1471  if ( 'trackback' == $type || 'pingback' == $type )
1472  $comments_by_type['pings'][] = &$comments[$i];
1473  }
1474 
1475  return $comments_by_type;
1476 }
1477 
1490 function get_comment_pages_count( $comments = null, $per_page = null, $threaded = null ) {
1491  global $wp_query;
1492 
1493  if ( null === $comments && null === $per_page && null === $threaded && !empty($wp_query->max_num_comment_pages) )
1494  return $wp_query->max_num_comment_pages;
1495 
1496  if ( ( ! $comments || ! is_array( $comments ) ) && ! empty( $wp_query->comments ) )
1497  $comments = $wp_query->comments;
1498 
1499  if ( empty($comments) )
1500  return 0;
1501 
1502  if ( ! get_option( 'page_comments' ) )
1503  return 1;
1504 
1505  if ( !isset($per_page) )
1506  $per_page = (int) get_query_var('comments_per_page');
1507  if ( 0 === $per_page )
1508  $per_page = (int) get_option('comments_per_page');
1509  if ( 0 === $per_page )
1510  return 1;
1511 
1512  if ( !isset($threaded) )
1513  $threaded = get_option('thread_comments');
1514 
1515  if ( $threaded ) {
1516  $walker = new Walker_Comment;
1517  $count = ceil( $walker->get_number_of_root_elements( $comments ) / $per_page );
1518  } else {
1519  $count = ceil( count( $comments ) / $per_page );
1520  }
1521 
1522  return $count;
1523 }
1524 
1534 function get_page_of_comment( $comment_ID, $args = array() ) {
1535  global $wpdb;
1536 
1537  if ( !$comment = get_comment( $comment_ID ) )
1538  return;
1539 
1540  $defaults = array( 'type' => 'all', 'page' => '', 'per_page' => '', 'max_depth' => '' );
1541  $args = wp_parse_args( $args, $defaults );
1542 
1543  if ( '' === $args['per_page'] && get_option('page_comments') )
1544  $args['per_page'] = get_query_var('comments_per_page');
1545  if ( empty($args['per_page']) ) {
1546  $args['per_page'] = 0;
1547  $args['page'] = 0;
1548  }
1549  if ( $args['per_page'] < 1 )
1550  return 1;
1551 
1552  if ( '' === $args['max_depth'] ) {
1553  if ( get_option('thread_comments') )
1554  $args['max_depth'] = get_option('thread_comments_depth');
1555  else
1556  $args['max_depth'] = -1;
1557  }
1558 
1559  // Find this comment's top level parent if threading is enabled
1560  if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent )
1561  return get_page_of_comment( $comment->comment_parent, $args );
1562 
1563  $allowedtypes = array(
1564  'comment' => '',
1565  'pingback' => 'pingback',
1566  'trackback' => 'trackback',
1567  );
1568 
1569  $comtypewhere = ( 'all' != $args['type'] && isset($allowedtypes[$args['type']]) ) ? " AND comment_type = '" . $allowedtypes[$args['type']] . "'" : '';
1570 
1571  // Count comments older than this one
1572  $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 ) );
1573 
1574  // No older comments? Then it's page #1.
1575  if ( 0 == $oldercoms )
1576  return 1;
1577 
1578  // Divide comments older than this one by comments per page to get this comment's page number
1579  return ceil( ( $oldercoms + 1 ) / $args['per_page'] );
1580 }
1581 
1595 function wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent) {
1608  do_action( 'wp_blacklist_check', $author, $email, $url, $comment, $user_ip, $user_agent );
1609 
1610  $mod_keys = trim( get_option('blacklist_keys') );
1611  if ( '' == $mod_keys )
1612  return false; // If moderation keys are empty
1613  $words = explode("\n", $mod_keys );
1614 
1615  foreach ( (array) $words as $word ) {
1616  $word = trim($word);
1617 
1618  // Skip empty lines
1619  if ( empty($word) ) { continue; }
1620 
1621  // Do some escaping magic so that '#' chars in the
1622  // spam words don't break things:
1623  $word = preg_quote($word, '#');
1624 
1625  $pattern = "#$word#i";
1626  if (
1627  preg_match($pattern, $author)
1628  || preg_match($pattern, $email)
1629  || preg_match($pattern, $url)
1630  || preg_match($pattern, $comment)
1631  || preg_match($pattern, $user_ip)
1632  || preg_match($pattern, $user_agent)
1633  )
1634  return true;
1635  }
1636  return false;
1637 }
1638 
1655 function wp_count_comments( $post_id = 0 ) {
1656  global $wpdb;
1657 
1658  $post_id = (int) $post_id;
1659 
1668  $stats = apply_filters( 'wp_count_comments', array(), $post_id );
1669  if ( !empty($stats) )
1670  return $stats;
1671 
1672  $count = wp_cache_get("comments-{$post_id}", 'counts');
1673 
1674  if ( false !== $count )
1675  return $count;
1676 
1677  $where = '';
1678  if ( $post_id > 0 )
1679  $where = $wpdb->prepare( "WHERE comment_post_ID = %d", $post_id );
1680 
1681  $count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} {$where} GROUP BY comment_approved", ARRAY_A );
1682 
1683  $total = 0;
1684  $approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed');
1685  foreach ( (array) $count as $row ) {
1686  // Don't count post-trashed toward totals
1687  if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] )
1688  $total += $row['num_comments'];
1689  if ( isset( $approved[$row['comment_approved']] ) )
1690  $stats[$approved[$row['comment_approved']]] = $row['num_comments'];
1691  }
1692 
1693  $stats['total_comments'] = $total;
1694  foreach ( $approved as $key ) {
1695  if ( empty($stats[$key]) )
1696  $stats[$key] = 0;
1697  }
1698 
1699  $stats = (object) $stats;
1700  wp_cache_set("comments-{$post_id}", $stats, 'counts');
1701 
1702  return $stats;
1703 }
1704 
1722 function wp_delete_comment($comment_id, $force_delete = false) {
1723  global $wpdb;
1725  return false;
1726 
1727  if ( !$force_delete && EMPTY_TRASH_DAYS && !in_array( wp_get_comment_status($comment_id), array( 'trash', 'spam' ) ) )
1728  return wp_trash_comment($comment_id);
1729 
1737  do_action( 'delete_comment', $comment_id );
1738 
1739  // Move children up a level.
1740  $children = $wpdb->get_col( $wpdb->prepare("SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment_id) );
1741  if ( !empty($children) ) {
1742  $wpdb->update($wpdb->comments, array('comment_parent' => $comment->comment_parent), array('comment_parent' => $comment_id));
1743  clean_comment_cache($children);
1744  }
1745 
1746  // Delete metadata
1747  $meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->commentmeta WHERE comment_id = %d", $comment_id ) );
1748  foreach ( $meta_ids as $mid )
1749  delete_metadata_by_mid( 'comment', $mid );
1750 
1751  if ( ! $wpdb->delete( $wpdb->comments, array( 'comment_ID' => $comment_id ) ) )
1752  return false;
1753 
1761  do_action( 'deleted_comment', $comment_id );
1762 
1763  $post_id = $comment->comment_post_ID;
1764  if ( $post_id && $comment->comment_approved == 1 )
1766 
1768 
1770  do_action( 'wp_set_comment_status', $comment_id, 'delete' );
1771 
1772  wp_transition_comment_status('delete', $comment->comment_approved, $comment);
1773  return true;
1774 }
1775 
1787  if ( !EMPTY_TRASH_DAYS )
1788  return wp_delete_comment($comment_id, true);
1789 
1790  if ( !$comment = get_comment($comment_id) )
1791  return false;
1792 
1800  do_action( 'trash_comment', $comment_id );
1801 
1802  if ( wp_set_comment_status($comment_id, 'trash') ) {
1803  add_comment_meta($comment_id, '_wp_trash_meta_status', $comment->comment_approved);
1804  add_comment_meta($comment_id, '_wp_trash_meta_time', time() );
1805 
1813  do_action( 'trashed_comment', $comment_id );
1814  return true;
1815  }
1816 
1817  return false;
1818 }
1819 
1829  if ( ! (int)$comment_id )
1830  return false;
1831 
1839  do_action( 'untrash_comment', $comment_id );
1840 
1841  $status = (string) get_comment_meta($comment_id, '_wp_trash_meta_status', true);
1842  if ( empty($status) )
1843  $status = '0';
1844 
1845  if ( wp_set_comment_status($comment_id, $status) ) {
1846  delete_comment_meta($comment_id, '_wp_trash_meta_time');
1847  delete_comment_meta($comment_id, '_wp_trash_meta_status');
1855  do_action( 'untrashed_comment', $comment_id );
1856  return true;
1857  }
1858 
1859  return false;
1860 }
1861 
1871  if ( !$comment = get_comment($comment_id) )
1872  return false;
1873 
1881  do_action( 'spam_comment', $comment_id );
1882 
1883  if ( wp_set_comment_status($comment_id, 'spam') ) {
1884  add_comment_meta($comment_id, '_wp_trash_meta_status', $comment->comment_approved);
1892  do_action( 'spammed_comment', $comment_id );
1893  return true;
1894  }
1895 
1896  return false;
1897 }
1898 
1908  if ( ! (int)$comment_id )
1909  return false;
1910 
1918  do_action( 'unspam_comment', $comment_id );
1919 
1920  $status = (string) get_comment_meta($comment_id, '_wp_trash_meta_status', true);
1921  if ( empty($status) )
1922  $status = '0';
1923 
1924  if ( wp_set_comment_status($comment_id, $status) ) {
1925  delete_comment_meta($comment_id, '_wp_trash_meta_status');
1933  do_action( 'unspammed_comment', $comment_id );
1934  return true;
1935  }
1936 
1937  return false;
1938 }
1939 
1950  if ( !$comment )
1951  return false;
1952 
1953  $approved = $comment->comment_approved;
1954 
1955  if ( $approved == null )
1956  return false;
1957  elseif ( $approved == '1' )
1958  return 'approved';
1959  elseif ( $approved == '0' )
1960  return 'unapproved';
1961  elseif ( $approved == 'spam' )
1962  return 'spam';
1963  elseif ( $approved == 'trash' )
1964  return 'trash';
1965  else
1966  return false;
1967 }
1968 
1989 function wp_transition_comment_status($new_status, $old_status, $comment) {
1990  /*
1991  * Translate raw statuses to human readable formats for the hooks.
1992  * This is not a complete list of comment status, it's only the ones
1993  * that need to be renamed
1994  */
1995  $comment_statuses = array(
1996  0 => 'unapproved',
1997  'hold' => 'unapproved', // wp_set_comment_status() uses "hold"
1998  1 => 'approved',
1999  'approve' => 'approved', // wp_set_comment_status() uses "approve"
2000  );
2001  if ( isset($comment_statuses[$new_status]) ) $new_status = $comment_statuses[$new_status];
2002  if ( isset($comment_statuses[$old_status]) ) $old_status = $comment_statuses[$old_status];
2003 
2004  // Call the hooks
2005  if ( $new_status != $old_status ) {
2015  do_action( 'transition_comment_status', $new_status, $old_status, $comment );
2026  do_action( "comment_{$old_status}_to_{$new_status}", $comment );
2027  }
2042  do_action( "comment_{$new_status}_{$comment->comment_type}", $comment->comment_ID, $comment );
2043 }
2044 
2058  // Cookies should already be sanitized.
2059 
2060  $comment_author = '';
2061  if ( isset($_COOKIE['comment_author_'.COOKIEHASH]) )
2062  $comment_author = $_COOKIE['comment_author_'.COOKIEHASH];
2063 
2064  $comment_author_email = '';
2065  if ( isset($_COOKIE['comment_author_email_'.COOKIEHASH]) )
2066  $comment_author_email = $_COOKIE['comment_author_email_'.COOKIEHASH];
2067 
2068  $comment_author_url = '';
2069  if ( isset($_COOKIE['comment_author_url_'.COOKIEHASH]) )
2070  $comment_author_url = $_COOKIE['comment_author_url_'.COOKIEHASH];
2071 
2081  return apply_filters( 'wp_get_current_commenter', compact('comment_author', 'comment_author_email', 'comment_author_url') );
2082 }
2083 
2098  global $wpdb;
2099  $data = wp_unslash( $commentdata );
2100 
2101  $comment_author = ! isset( $data['comment_author'] ) ? '' : $data['comment_author'];
2102  $comment_author_email = ! isset( $data['comment_author_email'] ) ? '' : $data['comment_author_email'];
2103  $comment_author_url = ! isset( $data['comment_author_url'] ) ? '' : $data['comment_author_url'];
2104  $comment_author_IP = ! isset( $data['comment_author_IP'] ) ? '' : $data['comment_author_IP'];
2105 
2106  $comment_date = ! isset( $data['comment_date'] ) ? current_time( 'mysql' ) : $data['comment_date'];
2107  $comment_date_gmt = ! isset( $data['comment_date_gmt'] ) ? get_gmt_from_date( $comment_date ) : $data['comment_date_gmt'];
2108 
2109  $comment_post_ID = ! isset( $data['comment_post_ID'] ) ? '' : $data['comment_post_ID'];
2110  $comment_content = ! isset( $data['comment_content'] ) ? '' : $data['comment_content'];
2111  $comment_karma = ! isset( $data['comment_karma'] ) ? 0 : $data['comment_karma'];
2112  $comment_approved = ! isset( $data['comment_approved'] ) ? 1 : $data['comment_approved'];
2113  $comment_agent = ! isset( $data['comment_agent'] ) ? '' : $data['comment_agent'];
2114  $comment_type = ! isset( $data['comment_type'] ) ? '' : $data['comment_type'];
2115  $comment_parent = ! isset( $data['comment_parent'] ) ? 0 : $data['comment_parent'];
2116 
2117  $user_id = ! isset( $data['user_id'] ) ? 0 : $data['user_id'];
2118 
2119  $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' );
2120  if ( ! $wpdb->insert( $wpdb->comments, $compacted ) ) {
2121  return false;
2122  }
2123 
2124  $id = (int) $wpdb->insert_id;
2125 
2126  if ( $comment_approved == 1 ) {
2128  }
2129  $comment = get_comment( $id );
2130 
2139  do_action( 'wp_insert_comment', $id, $comment );
2140 
2141  wp_cache_set( 'last_changed', microtime(), 'comment' );
2142 
2143  return $id;
2144 }
2145 
2159  if ( isset( $commentdata['user_ID'] ) ) {
2170  $commentdata['user_id'] = apply_filters( 'pre_user_id', $commentdata['user_ID'] );
2171  } elseif ( isset( $commentdata['user_id'] ) ) {
2173  $commentdata['user_id'] = apply_filters( 'pre_user_id', $commentdata['user_id'] );
2174  }
2175 
2183  $commentdata['comment_agent'] = apply_filters( 'pre_comment_user_agent', ( isset( $commentdata['comment_agent'] ) ? $commentdata['comment_agent'] : '' ) );
2185  $commentdata['comment_author'] = apply_filters( 'pre_comment_author_name', $commentdata['comment_author'] );
2193  $commentdata['comment_content'] = apply_filters( 'pre_comment_content', $commentdata['comment_content'] );
2201  $commentdata['comment_author_IP'] = apply_filters( 'pre_comment_user_ip', $commentdata['comment_author_IP'] );
2203  $commentdata['comment_author_url'] = apply_filters( 'pre_comment_author_url', $commentdata['comment_author_url'] );
2205  $commentdata['comment_author_email'] = apply_filters( 'pre_comment_author_email', $commentdata['comment_author_email'] );
2206  $commentdata['filtered'] = true;
2207  return $commentdata;
2208 }
2209 
2220 function wp_throttle_comment_flood($block, $time_lastcomment, $time_newcomment) {
2221  if ( $block ) // a plugin has already blocked... we'll let that decision stand
2222  return $block;
2223  if ( ($time_newcomment - $time_lastcomment) < 15 )
2224  return true;
2225  return false;
2226 }
2227 
2245  global $wpdb;
2246 
2247  if ( isset( $commentdata['user_ID'] ) ) {
2248  $commentdata['user_id'] = $commentdata['user_ID'] = (int) $commentdata['user_ID'];
2249  }
2250 
2251  $prefiltered_user_id = ( isset( $commentdata['user_id'] ) ) ? (int) $commentdata['user_id'] : 0;
2252 
2260  $commentdata = apply_filters( 'preprocess_comment', $commentdata );
2261 
2262  $commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID'];
2263  if ( isset( $commentdata['user_ID'] ) && $prefiltered_user_id !== (int) $commentdata['user_ID'] ) {
2264  $commentdata['user_id'] = $commentdata['user_ID'] = (int) $commentdata['user_ID'];
2265  } elseif ( isset( $commentdata['user_id'] ) ) {
2266  $commentdata['user_id'] = (int) $commentdata['user_id'];
2267  }
2268 
2269  $commentdata['comment_parent'] = isset($commentdata['comment_parent']) ? absint($commentdata['comment_parent']) : 0;
2270  $parent_status = ( 0 < $commentdata['comment_parent'] ) ? wp_get_comment_status($commentdata['comment_parent']) : '';
2271  $commentdata['comment_parent'] = ( 'approved' == $parent_status || 'unapproved' == $parent_status ) ? $commentdata['comment_parent'] : 0;
2272 
2273  $commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR'] );
2274  $commentdata['comment_agent'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? substr( $_SERVER['HTTP_USER_AGENT'], 0, 254 ) : '';
2275 
2276  if ( empty( $commentdata['comment_date'] ) ) {
2277  $commentdata['comment_date'] = current_time('mysql');
2278  }
2279 
2280  if ( empty( $commentdata['comment_date_gmt'] ) ) {
2281  $commentdata['comment_date_gmt'] = current_time( 'mysql', 1 );
2282  }
2283 
2285 
2286  $commentdata['comment_approved'] = wp_allow_comment($commentdata);
2287 
2288  $comment_ID = wp_insert_comment($commentdata);
2289  if ( ! $comment_ID ) {
2290  $fields = array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content' );
2291 
2292  foreach( $fields as $field ) {
2293  if ( isset( $commentdata[ $field ] ) ) {
2294  $commentdata[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->comments, $field, $commentdata[ $field ] );
2295  }
2296  }
2297 
2299 
2300  $commentdata['comment_approved'] = wp_allow_comment( $commentdata );
2301 
2302  $comment_ID = wp_insert_comment( $commentdata );
2303  if ( ! $comment_ID ) {
2304  return false;
2305  }
2306  }
2307 
2316  do_action( 'comment_post', $comment_ID, $commentdata['comment_approved'] );
2317 
2318  if ( 'spam' !== $commentdata['comment_approved'] ) { // If it's spam save it silently for later crunching
2319  if ( '0' == $commentdata['comment_approved'] ) {
2320  wp_notify_moderator( $comment_ID );
2321  }
2322 
2323  // wp_notify_postauthor() checks if notifying the author of their own comment.
2324  // By default, it won't, but filters can override this.
2325  if ( get_option( 'comments_notify' ) && $commentdata['comment_approved'] ) {
2326  wp_notify_postauthor( $comment_ID );
2327  }
2328  }
2329 
2330  return $comment_ID;
2331 }
2332 
2346 function wp_set_comment_status($comment_id, $comment_status, $wp_error = false) {
2347  global $wpdb;
2348 
2349  switch ( $comment_status ) {
2350  case 'hold':
2351  case '0':
2352  $status = '0';
2353  break;
2354  case 'approve':
2355  case '1':
2356  $status = '1';
2357  if ( get_option('comments_notify') ) {
2358  wp_notify_postauthor( $comment_id );
2359  }
2360  break;
2361  case 'spam':
2362  $status = 'spam';
2363  break;
2364  case 'trash':
2365  $status = 'trash';
2366  break;
2367  default:
2368  return false;
2369  }
2370 
2371  $comment_old = clone get_comment($comment_id);
2372 
2373  if ( !$wpdb->update( $wpdb->comments, array('comment_approved' => $status), array('comment_ID' => $comment_id) ) ) {
2374  if ( $wp_error )
2375  return new WP_Error('db_update_error', __('Could not update comment status'), $wpdb->last_error);
2376  else
2377  return false;
2378  }
2379 
2381 
2383 
2394  do_action( 'wp_set_comment_status', $comment_id, $comment_status );
2395 
2396  wp_transition_comment_status($comment_status, $comment_old->comment_approved, $comment);
2397 
2398  wp_update_comment_count($comment->comment_post_ID);
2399 
2400  return true;
2401 }
2402 
2415 function wp_update_comment($commentarr) {
2416  global $wpdb;
2417 
2418  // First, get all of the original fields
2419  $comment = get_comment($commentarr['comment_ID'], ARRAY_A);
2420  if ( empty( $comment ) ) {
2421  return 0;
2422  }
2423 
2424  // Make sure that the comment post ID is valid (if specified).
2425  if ( isset( $commentarr['comment_post_ID'] ) && ! get_post( $commentarr['comment_post_ID'] ) ) {
2426  return 0;
2427  }
2428 
2429  // Escape data pulled from DB.
2431 
2432  $old_status = $comment['comment_approved'];
2433 
2434  // Merge old and new fields with new fields overwriting old ones.
2435  $commentarr = array_merge($comment, $commentarr);
2436 
2437  $commentarr = wp_filter_comment( $commentarr );
2438 
2439  // Now extract the merged array.
2440  $data = wp_unslash( $commentarr );
2441 
2449  $data['comment_content'] = apply_filters( 'comment_save_pre', $data['comment_content'] );
2450 
2451  $data['comment_date_gmt'] = get_gmt_from_date( $data['comment_date'] );
2452 
2453  if ( ! isset( $data['comment_approved'] ) ) {
2454  $data['comment_approved'] = 1;
2455  } elseif ( 'hold' == $data['comment_approved'] ) {
2456  $data['comment_approved'] = 0;
2457  } elseif ( 'approve' == $data['comment_approved'] ) {
2458  $data['comment_approved'] = 1;
2459  }
2460 
2461  $comment_ID = $data['comment_ID'];
2462  $comment_post_ID = $data['comment_post_ID'];
2463  $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' );
2464  $data = wp_array_slice_assoc( $data, $keys );
2465  $rval = $wpdb->update( $wpdb->comments, $data, compact( 'comment_ID' ) );
2466 
2467  clean_comment_cache( $comment_ID );
2478  do_action( 'edit_comment', $comment_ID );
2479  $comment = get_comment($comment_ID);
2480  wp_transition_comment_status($comment->comment_approved, $old_status, $comment);
2481  return $rval;
2482 }
2483 
2499  static $_defer = false;
2500 
2501  if ( is_bool($defer) ) {
2502  $_defer = $defer;
2503  // flush any deferred counts
2504  if ( !$defer )
2505  wp_update_comment_count( null, true );
2506  }
2507 
2508  return $_defer;
2509 }
2510 
2529 function wp_update_comment_count($post_id, $do_deferred=false) {
2530  static $_deferred = array();
2531 
2532  if ( $do_deferred ) {
2533  $_deferred = array_unique($_deferred);
2534  foreach ( $_deferred as $i => $_post_id ) {
2535  wp_update_comment_count_now($_post_id);
2536  unset( $_deferred[$i] );
2537  }
2538  }
2539 
2540  if ( wp_defer_comment_counting() ) {
2541  $_deferred[] = $post_id;
2542  return true;
2543  }
2544  elseif ( $post_id ) {
2546  }
2547 
2548 }
2549 
2561  global $wpdb;
2562  $post_id = (int) $post_id;
2563  if ( !$post_id )
2564  return false;
2565  if ( !$post = get_post($post_id) )
2566  return false;
2567 
2568  $old = (int) $post->comment_count;
2569  $new = (int) $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id) );
2570  $wpdb->update( $wpdb->posts, array('comment_count' => $new), array('ID' => $post_id) );
2571 
2573 
2583  do_action( 'wp_update_comment_count', $post_id, $new, $old );
2585  do_action( 'edit_post', $post_id, $post );
2586 
2587  return true;
2588 }
2589 
2590 //
2591 // Ping and trackback functions.
2592 //
2593 
2607 function discover_pingback_server_uri( $url, $deprecated = '' ) {
2608  if ( !empty( $deprecated ) )
2609  _deprecated_argument( __FUNCTION__, '2.7' );
2610 
2611  $pingback_str_dquote = 'rel="pingback"';
2612  $pingback_str_squote = 'rel=\'pingback\'';
2613 
2615  $parsed_url = parse_url($url);
2616 
2617  if ( ! isset( $parsed_url['host'] ) ) // Not an URL. This should never happen.
2618  return false;
2619 
2620  //Do not search for a pingback server on our own uploads
2621  $uploads_dir = wp_upload_dir();
2622  if ( 0 === strpos($url, $uploads_dir['baseurl']) )
2623  return false;
2624 
2625  $response = wp_safe_remote_head( $url, array( 'timeout' => 2, 'httpversion' => '1.0' ) );
2626 
2627  if ( is_wp_error( $response ) )
2628  return false;
2629 
2630  if ( wp_remote_retrieve_header( $response, 'x-pingback' ) )
2631  return wp_remote_retrieve_header( $response, 'x-pingback' );
2632 
2633  // Not an (x)html, sgml, or xml page, no use going further.
2634  if ( preg_match('#(image|audio|video|model)/#is', wp_remote_retrieve_header( $response, 'content-type' )) )
2635  return false;
2636 
2637  // Now do a GET since we're going to look in the html headers (and we're sure it's not a binary file)
2638  $response = wp_safe_remote_get( $url, array( 'timeout' => 2, 'httpversion' => '1.0' ) );
2639 
2640  if ( is_wp_error( $response ) )
2641  return false;
2642 
2643  $contents = wp_remote_retrieve_body( $response );
2644 
2645  $pingback_link_offset_dquote = strpos($contents, $pingback_str_dquote);
2646  $pingback_link_offset_squote = strpos($contents, $pingback_str_squote);
2647  if ( $pingback_link_offset_dquote || $pingback_link_offset_squote ) {
2648  $quote = ($pingback_link_offset_dquote) ? '"' : '\'';
2649  $pingback_link_offset = ($quote=='"') ? $pingback_link_offset_dquote : $pingback_link_offset_squote;
2650  $pingback_href_pos = @strpos($contents, 'href=', $pingback_link_offset);
2651  $pingback_href_start = $pingback_href_pos+6;
2652  $pingback_href_end = @strpos($contents, $quote, $pingback_href_start);
2653  $pingback_server_url_len = $pingback_href_end - $pingback_href_start;
2654  $pingback_server_url = substr($contents, $pingback_href_start, $pingback_server_url_len);
2655 
2656  // We may find rel="pingback" but an incomplete pingback URL
2657  if ( $pingback_server_url_len > 0 ) { // We got it!
2658  return $pingback_server_url;
2659  }
2660  }
2661 
2662  return false;
2663 }
2664 
2672 function do_all_pings() {
2673  global $wpdb;
2674 
2675  // Do pingbacks
2676  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")) {
2677  delete_metadata_by_mid( 'post', $ping->meta_id );
2678  pingback( $ping->post_content, $ping->ID );
2679  }
2680 
2681  // Do Enclosures
2682  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")) {
2683  delete_metadata_by_mid( 'post', $enclosure->meta_id );
2684  do_enclose( $enclosure->post_content, $enclosure->ID );
2685  }
2686 
2687  // Do Trackbacks
2688  $trackbacks = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE to_ping <> '' AND post_status = 'publish'");
2689  if ( is_array($trackbacks) )
2690  foreach ( $trackbacks as $trackback )
2691  do_trackbacks($trackback);
2692 
2693  //Do Update Services/Generic Pings
2694  generic_ping();
2695 }
2696 
2707  global $wpdb;
2708 
2709  $post = get_post( $post_id );
2710  $to_ping = get_to_ping($post_id);
2711  $pinged = get_pung($post_id);
2712  if ( empty($to_ping) ) {
2713  $wpdb->update($wpdb->posts, array('to_ping' => ''), array('ID' => $post_id) );
2714  return;
2715  }
2716 
2717  if ( empty($post->post_excerpt) ) {
2719  $excerpt = apply_filters( 'the_content', $post->post_content, $post->ID );
2720  } else {
2722  $excerpt = apply_filters( 'the_excerpt', $post->post_excerpt );
2723  }
2724 
2725  $excerpt = str_replace(']]>', ']]&gt;', $excerpt);
2726  $excerpt = wp_html_excerpt($excerpt, 252, '&#8230;');
2727 
2729  $post_title = apply_filters( 'the_title', $post->post_title, $post->ID );
2730  $post_title = strip_tags($post_title);
2731 
2732  if ( $to_ping ) {
2733  foreach ( (array) $to_ping as $tb_ping ) {
2734  $tb_ping = trim($tb_ping);
2735  if ( !in_array($tb_ping, $pinged) ) {
2736  trackback($tb_ping, $post_title, $excerpt, $post_id);
2737  $pinged[] = $tb_ping;
2738  } else {
2739  $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, '')) WHERE ID = %d", $tb_ping, $post_id) );
2740  }
2741  }
2742  }
2743 }
2744 
2753 function generic_ping( $post_id = 0 ) {
2754  $services = get_option('ping_sites');
2755 
2756  $services = explode("\n", $services);
2757  foreach ( (array) $services as $service ) {
2758  $service = trim($service);
2759  if ( '' != $service )
2760  weblog_ping($service);
2761  }
2762 
2763  return $post_id;
2764 }
2765 
2776  global $wp_version;
2777  include_once(ABSPATH . WPINC . '/class-IXR.php');
2778  include_once(ABSPATH . WPINC . '/class-wp-http-ixr-client.php');
2779 
2780  // original code by Mort (http://mort.mine.nu:8080)
2781  $post_links = array();
2782 
2783  $pung = get_pung($post_ID);
2784 
2785  // Step 1
2786  // Parsing the post, external links (if any) are stored in the $post_links array
2787  $post_links_temp = wp_extract_urls( $content );
2788 
2789  // Step 2.
2790  // Walking thru the links array
2791  // first we get rid of links pointing to sites, not to specific files
2792  // Example:
2793  // http://dummy-weblog.org
2794  // http://dummy-weblog.org/
2795  // http://dummy-weblog.org/post.php
2796  // We don't wanna ping first and second types, even if they have a valid <link/>
2797 
2798  foreach ( (array) $post_links_temp as $link_test ) :
2799  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
2800  && !is_local_attachment($link_test) ) : // Also, let's never ping local attachments.
2801  if ( $test = @parse_url($link_test) ) {
2802  if ( isset($test['query']) )
2803  $post_links[] = $link_test;
2804  elseif ( isset( $test['path'] ) && ( $test['path'] != '/' ) && ( $test['path'] != '' ) )
2805  $post_links[] = $link_test;
2806  }
2807  endif;
2808  endforeach;
2809 
2810  $post_links = array_unique( $post_links );
2820  do_action_ref_array( 'pre_ping', array( &$post_links, &$pung, $post_ID ) );
2821 
2822  foreach ( (array) $post_links as $pagelinkedto ) {
2823  $pingback_server_url = discover_pingback_server_uri( $pagelinkedto );
2824 
2825  if ( $pingback_server_url ) {
2826  @ set_time_limit( 60 );
2827  // Now, the RPC call
2828  $pagelinkedfrom = get_permalink($post_ID);
2829 
2830  // using a timeout of 3 seconds should be enough to cover slow servers
2831  $client = new WP_HTTP_IXR_Client($pingback_server_url);
2832  $client->timeout = 3;
2845  $client->useragent = apply_filters( 'pingback_useragent', $client->useragent . ' -- WordPress/' . $wp_version, $client->useragent, $pingback_server_url, $pagelinkedto, $pagelinkedfrom );
2846  // when set to true, this outputs debug messages by itself
2847  $client->debug = false;
2848 
2849  if ( $client->query('pingback.ping', $pagelinkedfrom, $pagelinkedto) || ( isset($client->error->code) && 48 == $client->error->code ) ) // Already registered
2850  add_ping( $post_ID, $pagelinkedto );
2851  }
2852  }
2853 }
2854 
2863 function privacy_ping_filter($sites) {
2864  if ( '0' != get_option('blog_public') )
2865  return $sites;
2866  else
2867  return '';
2868 }
2869 
2885 function trackback($trackback_url, $title, $excerpt, $ID) {
2886  global $wpdb;
2887 
2888  if ( empty($trackback_url) )
2889  return;
2890 
2891  $options = array();
2892  $options['timeout'] = 4;
2893  $options['body'] = array(
2894  'title' => $title,
2895  'url' => get_permalink($ID),
2896  'blog_name' => get_option('blogname'),
2897  'excerpt' => $excerpt
2898  );
2899 
2900  $response = wp_safe_remote_post( $trackback_url, $options );
2901 
2902  if ( is_wp_error( $response ) )
2903  return;
2904 
2905  $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', %s) WHERE ID = %d", $trackback_url, $ID) );
2906  return $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, '')) WHERE ID = %d", $trackback_url, $ID) );
2907 }
2908 
2918 function weblog_ping($server = '', $path = '') {
2919  global $wp_version;
2920  include_once(ABSPATH . WPINC . '/class-IXR.php');
2921  include_once(ABSPATH . WPINC . '/class-wp-http-ixr-client.php');
2922 
2923  // using a timeout of 3 seconds should be enough to cover slow servers
2924  $client = new WP_HTTP_IXR_Client($server, ((!strlen(trim($path)) || ('/' == $path)) ? false : $path));
2925  $client->timeout = 3;
2926  $client->useragent .= ' -- WordPress/'.$wp_version;
2927 
2928  // when set to true, this outputs debug messages by itself
2929  $client->debug = false;
2930  $home = trailingslashit( home_url() );
2931  if ( !$client->query('weblogUpdates.extendedPing', get_option('blogname'), $home, get_bloginfo('rss2_url') ) ) // then try a normal ping
2932  $client->query('weblogUpdates.ping', get_option('blogname'), $home);
2933 }
2934 
2944 function pingback_ping_source_uri( $source_uri ) {
2945  return (string) wp_http_validate_url( $source_uri );
2946 }
2947 
2960 function xmlrpc_pingback_error( $ixr_error ) {
2961  if ( $ixr_error->code === 48 )
2962  return $ixr_error;
2963  return new IXR_Error( 0, '' );
2964 }
2965 
2966 //
2967 // Cache
2968 //
2969 
2977 function clean_comment_cache($ids) {
2978  foreach ( (array) $ids as $id )
2979  wp_cache_delete($id, 'comment');
2980 
2981  wp_cache_set( 'last_changed', microtime(), 'comment' );
2982 }
2983 
2996  foreach ( (array) $comments as $comment )
2997  wp_cache_add($comment->comment_ID, $comment, 'comment');
2998 }
2999 
3000 //
3001 // Internal
3002 //
3003 
3014 function _close_comments_for_old_posts( $posts, $query ) {
3015  if ( empty( $posts ) || ! $query->is_singular() || ! get_option( 'close_comments_for_old_posts' ) )
3016  return $posts;
3017 
3025  $post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) );
3026  if ( ! in_array( $posts[0]->post_type, $post_types ) )
3027  return $posts;
3028 
3029  $days_old = (int) get_option( 'close_comments_days_old' );
3030  if ( ! $days_old )
3031  return $posts;
3032 
3033  if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( $days_old * DAY_IN_SECONDS ) ) {
3034  $posts[0]->comment_status = 'closed';
3035  $posts[0]->ping_status = 'closed';
3036  }
3037 
3038  return $posts;
3039 }
3040 
3052  if ( ! $open )
3053  return $open;
3054 
3055  if ( !get_option('close_comments_for_old_posts') )
3056  return $open;
3057 
3058  $days_old = (int) get_option('close_comments_days_old');
3059  if ( !$days_old )
3060  return $open;
3061 
3062  $post = get_post($post_id);
3063 
3065  $post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) );
3066  if ( ! in_array( $post->post_type, $post_types ) )
3067  return $open;
3068 
3069  if ( time() - strtotime( $post->post_date_gmt ) > ( $days_old * DAY_IN_SECONDS ) )
3070  return false;
3071 
3072  return $open;
3073 }
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:2944
apply_filters_ref_array($tag, $args)
Definition: plugin.php:239
endif
Definition: about.php:144
delete_comment_meta($comment_id, $meta_key, $meta_value= '')
Definition: comment.php:1161
do_action($tag, $arg= '')
Definition: plugin.php:453
wp_trash_comment($comment_id)
Definition: comment.php:1786
$user_id
Definition: user-edit.php:14
$_COOKIE
Definition: xmlrpc.php:16
wp_get_current_commenter()
Definition: comment.php:2057
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:4023
wp_die($message= '', $title= '', $args=array())
Definition: functions.php:2367
_close_comments_for_old_posts($posts, $query)
Definition: comment.php:3014
$comment_id
$parsed
Definition: site-info.php:45
$post_id
wp_delete_comment($comment_id, $force_delete=false)
Definition: comment.php:1722
privacy_ping_filter($sites)
Definition: comment.php:2863
const ABSPATH
get_option()
trackback($trackback_url, $title, $excerpt, $ID)
Definition: comment.php:2885
__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:3091
get_pung($post_id)
Definition: post.php:4090
get_approved_comments($post_id, $args=array())
Definition: comment.php:139
sanitize_comment_cookies()
Definition: comment.php:1237
update_comment_meta($comment_id, $meta_key, $meta_value, $prev_value= '')
Definition: comment.php:1198
wp_slash($value)
wp_extract_urls($content)
Definition: functions.php:461
COOKIE_DOMAIN
Definition: wp-login.php:439
parse_orderby($orderby)
Definition: comment.php:935
check_comment_flood_db($ip, $email, $date)
Definition: comment.php:1418
delete_metadata_by_mid($meta_type, $meta_id)
Definition: meta.php:689
wp_remote_retrieve_header($response, $header)
Definition: http.php:231
check_comment($author, $email, $url, $comment, $user_ip, $user_agent, $comment_type)
Definition: comment.php:39
if(isset($_REQUEST['post_id'])) $id
get_comment_statuses()
Definition: comment.php:1017
do_action_ref_array()
$path
Definition: wp-load.php:56
is_wp_error($thing)
esc_attr()
wp_unspam_comment($comment_id)
Definition: comment.php:1907
generic_ping($post_id=0)
Definition: comment.php:2753
if(current_theme_supports( 'post-formats')&&post_type_supports($post_type, 'post-formats')) add_meta_box( 'formatdiv'foreach(get_object_taxonomies($post) as $tax_name) if($thumbnail_support &&current_user_can( 'upload_files')) add_meta_box('postimagediv' __('Featured Image')
$out
wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent)
Definition: comment.php:1595
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:1040
get_comment_count($post_id=0)
Definition: comment.php:1080
wp_set_comment_status($comment_id, $comment_status, $wp_error=false)
Definition: comment.php:2346
const ARRAY_A
Definition: wp-db.php:31
get_metadata($meta_type, $object_id, $meta_key= '', $single=false)
Definition: meta.php:454
if(empty($post->comment_status)) $status
get_post($post=null, $output=OBJECT, $filter= 'raw')
Definition: post.php:421
$title
Definition: about.php:19
separate_comments(&$comments)
Definition: comment.php:1463
add_metadata($meta_type, $object_id, $meta_key, $meta_value, $unique=false)
Definition: meta.php:30
if(get_option('db_upgraded')) elseif(get_option('db_version')!=$wp_db_version &&empty($_POST))
Definition: admin.php:45
$url
Definition: customize.php:20
wp_safe_remote_get($url, $args=array())
Definition: http.php:67
wp_filter_comment($commentdata)
Definition: comment.php:2158
comment_ID()
get_comment_meta($comment_id, $key= '', $single=false)
Definition: comment.php:1177
if(defined( 'RELOCATE')&&RELOCATE) $secure
Definition: wp-login.php:437
weblog_ping($server= '', $path= '')
Definition: comment.php:2918
$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:70
get_to_ping($post_id)
Definition: post.php:4117
$comments
__call($name, $arguments)
Definition: comment.php:302
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:498
delete_metadata($meta_type, $object_id, $meta_key, $meta_value= '', $delete_all=false)
Definition: meta.php:307
$excerpt
const WPINC
wp_count_comments($post_id=0)
Definition: comment.php:1655
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
global $post
wp_defer_comment_counting($defer=null)
Definition: comment.php:2498
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:2220
clean_comment_cache($ids)
Definition: comment.php:2977
clean_post_cache($post)
Definition: post.php:5531
$post_title
Definition: image.php:23
get_comment_pages_count($comments=null, $per_page=null, $threaded=null)
Definition: comment.php:1490
parse_order($order)
Definition: comment.php:994
get_comment(&$comment, $output=OBJECT)
Definition: comment.php:170
wp_parse_id_list($list)
Definition: functions.php:3075
wp_get_comment_status($comment_id)
Definition: comment.php:1948
wp_untrash_comment($comment_id)
Definition: comment.php:1828
$ID
get_page_of_comment($comment_ID, $args=array())
Definition: comment.php:1534
_close_comments_for_old_post($open, $post_id)
Definition: comment.php:3051
wp_parse_args($args, $defaults= '')
Definition: functions.php:3054
wp_html_excerpt($str, $count, $more=null)
get_comments($args= '')
Definition: comment.php:225
_deprecated_argument($function, $version, $message=null)
Definition: functions.php:3485
is_local_attachment($url)
Definition: post.php:4730
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:911
wp_set_comment_cookies($comment, $user)
Definition: comment.php:1211
discover_pingback_server_uri($url, $deprecated= '')
Definition: comment.php:2607
update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_value= '')
Definition: meta.php:143
$_SERVER['REQUEST_URI']
Definition: edit-tags.php:311
wp_unslash($value)
$comment_author_url
url_to_postid($url)
Definition: rewrite.php:287
$comment_post_ID
$cols
Definition: my-sites.php:100
wp_new_comment($commentdata)
Definition: comment.php:2244
$commentdata
global $wp_version
wp_spam_comment($comment_id)
Definition: comment.php:1870
current_time($type, $gmt=0)
Definition: functions.php:61
wp_update_comment($commentarr)
Definition: comment.php:2415
esc_sql($data)
do_all_pings()
Definition: comment.php:2672
get_query_var($var, $default= '')
Definition: query.php:25
do_trackbacks($post_id)
Definition: comment.php:2706
$content
Definition: feed-rss2.php:96
$comment_author
wp_remote_retrieve_body($response)
Definition: http.php:283
add_comment_meta($comment_id, $meta_key, $meta_value, $unique=false)
Definition: comment.php:1142
xmlrpc_pingback_error($ixr_error)
Definition: comment.php:2960
wp_http_validate_url($url)
Definition: http.php:447
wp_insert_comment($commentdata)
Definition: comment.php:2097
wp_allow_comment($commentdata)
Definition: comment.php:1299
pingback($content, $post_ID)
Definition: comment.php:2775
if(!defined( 'WP_ADMIN'))
Definition: admin-ajax.php:17
wp_upload_dir($time=null)
Definition: functions.php:1722
$comment_content
get_gmt_from_date($string, $format= 'Y-m-d H:i:s')
wp_update_comment_count_now($post_id)
Definition: comment.php:2560
if(current_theme_supports( 'post-formats')&&post_type_supports($post_type, 'post-formats')) add_meta_box( 'formatdiv' null
global $wpdb
Definition: schema.php:12
parse_query($query= '')
Definition: comment.php:436
wp_transition_comment_status($new_status, $old_status, $comment)
Definition: comment.php:1989
update_comment_cache($comments)
Definition: comment.php:2995
wp_update_comment_count($post_id, $do_deferred=false)
Definition: comment.php:2529
current_user_can($capability)
endforeach
Definition: export.php:188