PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 4.2.5
Jetpack – WP Security, Backup, Speed, & Growth v4.2.5
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / sync / class.jetpack-sync-module-comments.php
class.jetpack-sync-module-comments.php
132 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Jetpack_Sync_Module_Comments extends Jetpack_Sync_Module {
4
5 public function name() {
6 return 'comments';
7 }
8
9 public function init_listeners( $callable ) {
10 add_action( 'wp_insert_comment', $callable, 10, 2 );
11 add_action( 'deleted_comment', $callable );
12 add_action( 'trashed_comment', $callable );
13 add_action( 'spammed_comment', $callable );
14 add_action( 'trashed_post_comments', $callable, 10, 2 );
15 add_action( 'untrash_post_comments', $callable );
16
17 // even though it's messy, we implement these hooks because
18 // the edit_comment hook doesn't include the data
19 // so this saves us a DB read for every comment event
20 foreach ( array( '', 'trackback', 'pingback' ) as $comment_type ) {
21 foreach ( array( 'unapproved', 'approved' ) as $comment_status ) {
22 $comment_action_name = "comment_{$comment_status}_{$comment_type}";
23 add_action( $comment_action_name, $callable, 10, 2 );
24 }
25 }
26 }
27
28 public function init_full_sync_listeners( $callable ) {
29 add_action( 'jetpack_full_sync_comments', $callable ); // also send comments meta
30 }
31
32 public function init_before_send() {
33 add_filter( 'jetpack_sync_before_send_wp_insert_comment', array( $this, 'expand_wp_insert_comment' ) );
34
35 foreach ( array( '', 'trackback', 'pingback' ) as $comment_type ) {
36 foreach ( array( 'unapproved', 'approved' ) as $comment_status ) {
37 $comment_action_name = "comment_{$comment_status}_{$comment_type}";
38 add_filter( 'jetpack_sync_before_send_' . $comment_action_name, array(
39 $this,
40 'expand_wp_insert_comment',
41 ) );
42 }
43 }
44
45 // full sync
46 add_filter( 'jetpack_sync_before_send_jetpack_full_sync_comments', array( $this, 'expand_comment_ids' ) );
47 }
48
49 public function enqueue_full_sync_actions( $config ) {
50 global $wpdb;
51 return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_comments', $wpdb->comments, 'comment_ID', $this->get_where_sql( $config ) );
52 }
53
54 public function estimate_full_sync_actions( $config ) {
55 global $wpdb;
56
57 $query = "SELECT count(*) FROM $wpdb->comments";
58
59 if ( $where_sql = $this->get_where_sql( $config ) ) {
60 $query .= ' WHERE ' . $where_sql;
61 }
62
63 $count = $wpdb->get_var( $query );
64
65 return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
66 }
67
68 private function get_where_sql( $config ) {
69 if ( is_array( $config ) ) {
70 return 'comment_ID IN (' . implode( ',', array_map( 'intval', $config ) ) . ')';
71 }
72
73 return null;
74 }
75
76 public function get_full_sync_actions() {
77 return array( 'jetpack_full_sync_comments' );
78 }
79
80 public function count_full_sync_actions( $action_names ) {
81 return $this->count_actions( $action_names, array( 'jetpack_full_sync_comments' ) );
82 }
83
84 function expand_wp_comment_status_change( $args ) {
85 return array( $args[0], $this->filter_comment( $args[1] ) );
86 }
87
88 function expand_wp_insert_comment( $args ) {
89 return array( $args[0], $this->filter_comment( $args[1] ) );
90 }
91
92 function filter_comment( $comment ) {
93 /**
94 * Filters whether to prevent sending comment data to .com
95 *
96 * Passing true to the filter will prevent the comment data from being sent
97 * to the WordPress.com.
98 * Instead we pass data that will still enable us to do a checksum against the
99 * Jetpacks data but will prevent us from displaying the data on in the API as well as
100 * other services.
101 * @since 4.2.0
102 *
103 * @param boolean false prevent post data from bing synced to WordPress.com
104 * @param mixed $comment WP_COMMENT object
105 */
106 if ( apply_filters( 'jetpack_sync_prevent_sending_comment_data', false, $comment ) ) {
107 $blocked_comment = new stdClass();
108 $blocked_comment->comment_ID = $comment->comment_ID;
109 $blocked_comment->comment_date = $comment->comment_date;
110 $blocked_comment->comment_date_gmt = $comment->comment_date_gmt;
111 $blocked_comment->comment_approved = 'jetpack_sync_blocked';
112
113 return $blocked_comment;
114 }
115
116 return $comment;
117 }
118
119 public function expand_comment_ids( $args ) {
120 $comment_ids = $args[0];
121 $comments = get_comments( array(
122 'include_unapproved' => true,
123 'comment__in' => $comment_ids,
124 ) );
125
126 return array(
127 $comments,
128 $this->get_metadata( $comment_ids, 'comment' ),
129 );
130 }
131 }
132