Jetpack_Sync_Module_Comments

The WordPress Core Jetpack Sync Module Comments class.

Defined (1)

The class is defined in the following location(s).

/sync/class.jetpack-sync-module-comments.php  
  1. class Jetpack_Sync_Module_Comments extends Jetpack_Sync_Module { 
  2.  
  3. public function name() { 
  4. return 'comments'; 
  5.  
  6. public function get_object_by_id( $object_type, $id ) { 
  7. $comment_id = intval( $id ); 
  8. if ( $object_type === 'comment' && $comment = get_comment( $comment_id ) ) { 
  9. return $this->filter_comment( $comment ); 
  10.  
  11. return false; 
  12.  
  13. public function init_listeners( $callable ) { 
  14. add_action( 'wp_insert_comment', $callable, 10, 2 ); 
  15. add_action( 'deleted_comment', $callable ); 
  16. add_action( 'trashed_comment', $callable ); 
  17. add_action( 'spammed_comment', $callable ); 
  18. add_action( 'trashed_post_comments', $callable, 10, 2 ); 
  19. add_action( 'untrash_post_comments', $callable ); 
  20. add_action( 'comment_approved_to_unapproved', $callable ); 
  21. add_action( 'comment_unapproved_to_approved', $callable ); 
  22. add_action( 'jetpack_modified_comment_contents', $callable, 10, 2 ); 
  23. add_filter( 'wp_update_comment_data', array( $this, 'handle_comment_contents_modification' ), 10, 3 ); 
  24.  
  25. // even though it's messy, we implement these hooks because 
  26. // the edit_comment hook doesn't include the data 
  27. // so this saves us a DB read for every comment event 
  28. foreach ( array( '', 'trackback', 'pingback' ) as $comment_type ) { 
  29. foreach ( array( 'unapproved', 'approved' ) as $comment_status ) { 
  30. $comment_action_name = "comment_{$comment_status}_{$comment_type}"; 
  31. add_action( $comment_action_name, $callable, 10, 2 ); 
  32.  
  33. // listen for meta changes 
  34. $this->init_listeners_for_meta_type( 'comment', $callable ); 
  35. $this->init_meta_whitelist_handler( 'comment', array( $this, 'filter_meta' ) ); 
  36.  
  37. public function handle_comment_contents_modification( $new_comment, $old_comment, $new_comment_with_slashes ) { 
  38. $content_fields = array( 
  39. 'comment_author',  
  40. 'comment_author_email',  
  41. 'comment_author_url',  
  42. 'comment_content',  
  43. ); 
  44. $changes = array(); 
  45. foreach ( $content_fields as $field ) { 
  46. if ( $new_comment_with_slashes[$field] != $old_comment[$field] ) { 
  47. $changes[$field] = array( $new_comment[$field], $old_comment[$field] ); 
  48.  
  49. if ( ! empty( $changes ) ) { 
  50. /** 
  51. * Signals to the sync listener that this comment's contents were modified and a sync action 
  52. * reflecting the change(s) to the content should be sent 
  53. * @since 4.9.0 
  54. * @param int $new_comment['comment_ID'] ID of comment whose content was modified 
  55. * @param mixed $changes Array of changed comment fields with before and after values 
  56. */ 
  57. do_action( 'jetpack_modified_comment_contents', $new_comment['comment_ID'], $changes ); 
  58. return $new_comment; 
  59.  
  60. public function init_full_sync_listeners( $callable ) { 
  61. add_action( 'jetpack_full_sync_comments', $callable ); // also send comments meta 
  62.  
  63. public function init_before_send() { 
  64. add_filter( 'jetpack_sync_before_send_wp_insert_comment', array( $this, 'expand_wp_insert_comment' ) ); 
  65.  
  66. foreach ( array( '', 'trackback', 'pingback' ) as $comment_type ) { 
  67. foreach ( array( 'unapproved', 'approved' ) as $comment_status ) { 
  68. $comment_action_name = "comment_{$comment_status}_{$comment_type}"; 
  69. add_filter( 'jetpack_sync_before_send_' . $comment_action_name, array( 
  70. $this,  
  71. 'expand_wp_insert_comment',  
  72. ) ); 
  73.  
  74. // full sync 
  75. add_filter( 'jetpack_sync_before_send_jetpack_full_sync_comments', array( $this, 'expand_comment_ids' ) ); 
  76.  
  77. public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { 
  78. global $wpdb; 
  79. return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_comments', $wpdb->comments, 'comment_ID', $this->get_where_sql( $config ), $max_items_to_enqueue, $state ); 
  80.  
  81. public function estimate_full_sync_actions( $config ) { 
  82. global $wpdb; 
  83.  
  84. $query = "SELECT count(*) FROM $wpdb->comments"; 
  85.  
  86. if ( $where_sql = $this->get_where_sql( $config ) ) { 
  87. $query .= ' WHERE ' . $where_sql; 
  88.  
  89. $count = $wpdb->get_var( $query ); 
  90.  
  91. return (int) ceil( $count / self::ARRAY_CHUNK_SIZE ); 
  92.  
  93. private function get_where_sql( $config ) { 
  94. if ( is_array( $config ) ) { 
  95. return 'comment_ID IN (' . implode( ', ', array_map( 'intval', $config ) ) . ')'; 
  96.  
  97. return null; 
  98.  
  99. public function get_full_sync_actions() { 
  100. return array( 'jetpack_full_sync_comments' ); 
  101.  
  102. public function count_full_sync_actions( $action_names ) { 
  103. return $this->count_actions( $action_names, array( 'jetpack_full_sync_comments' ) ); 
  104.  
  105. function expand_wp_comment_status_change( $args ) { 
  106. return array( $args[0], $this->filter_comment( $args[1] ) ); 
  107.  
  108. function expand_wp_insert_comment( $args ) { 
  109. return array( $args[0], $this->filter_comment( $args[1] ) ); 
  110.  
  111. function filter_comment( $comment ) { 
  112. /** 
  113. * Filters whether to prevent sending comment data to .com 
  114. * Passing true to the filter will prevent the comment data from being sent 
  115. * to the WordPress.com. 
  116. * Instead we pass data that will still enable us to do a checksum against the 
  117. * Jetpacks data but will prevent us from displaying the data on in the API as well as 
  118. * other services. 
  119. * @since 4.2.0 
  120. * @param boolean false prevent post data from bing synced to WordPress.com 
  121. * @param mixed $comment WP_COMMENT object 
  122. */ 
  123. if ( apply_filters( 'jetpack_sync_prevent_sending_comment_data', false, $comment ) ) { 
  124. $blocked_comment = new stdClass(); 
  125. $blocked_comment->comment_ID = $comment->comment_ID; 
  126. $blocked_comment->comment_date = $comment->comment_date; 
  127. $blocked_comment->comment_date_gmt = $comment->comment_date_gmt; 
  128. $blocked_comment->comment_approved = 'jetpack_sync_blocked'; 
  129.  
  130. return $blocked_comment; 
  131.  
  132. return $comment; 
  133.  
  134. // Comment Meta 
  135. function is_whitelisted_comment_meta( $meta_key ) { 
  136. return in_array( $meta_key, Jetpack_Sync_Settings::get_setting( 'comment_meta_whitelist' ) ); 
  137.  
  138. function filter_meta( $args ) { 
  139. return ( $this->is_whitelisted_comment_meta( $args[2] ) ? $args : false ); 
  140.  
  141. public function expand_comment_ids( $args ) { 
  142. $comment_ids = $args[0]; 
  143. $comments = get_comments( array( 
  144. 'include_unapproved' => true,  
  145. 'comment__in' => $comment_ids,  
  146. ) ); 
  147.  
  148. return array( 
  149. $comments,  
  150. $this->get_metadata( $comment_ids, 'comment', Jetpack_Sync_Settings::get_setting( 'comment_meta_whitelist' ) ),  
  151. );