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