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
|
|
|
|
26
|
|
|
// even though it's messy, we implement these hooks because |
27
|
|
|
// the edit_comment hook doesn't include the data |
28
|
|
|
// so this saves us a DB read for every comment event |
29
|
|
View Code Duplication |
foreach ( array( '', 'trackback', 'pingback' ) as $comment_type ) { |
30
|
|
|
foreach ( array( 'unapproved', 'approved' ) as $comment_status ) { |
31
|
|
|
$comment_action_name = "comment_{$comment_status}_{$comment_type}"; |
32
|
|
|
add_action( $comment_action_name, $callable, 10, 2 ); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function init_full_sync_listeners( $callable ) { |
38
|
|
|
add_action( 'jetpack_full_sync_comments', $callable ); // also send comments meta |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function init_before_send() { |
42
|
|
|
add_filter( 'jetpack_sync_before_send_wp_insert_comment', array( $this, 'expand_wp_insert_comment' ) ); |
43
|
|
|
|
44
|
|
View Code Duplication |
foreach ( array( '', 'trackback', 'pingback' ) as $comment_type ) { |
45
|
|
|
foreach ( array( 'unapproved', 'approved' ) as $comment_status ) { |
46
|
|
|
$comment_action_name = "comment_{$comment_status}_{$comment_type}"; |
47
|
|
|
add_filter( 'jetpack_sync_before_send_' . $comment_action_name, array( |
48
|
|
|
$this, |
49
|
|
|
'expand_wp_insert_comment', |
50
|
|
|
) ); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// full sync |
55
|
|
|
add_filter( 'jetpack_sync_before_send_jetpack_full_sync_comments', array( $this, 'expand_comment_ids' ) ); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function enqueue_full_sync_actions( $config ) { |
59
|
|
|
global $wpdb; |
60
|
|
|
return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_comments', $wpdb->comments, 'comment_ID', $this->get_where_sql( $config ) ); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
View Code Duplication |
public function estimate_full_sync_actions( $config ) { |
64
|
|
|
global $wpdb; |
65
|
|
|
|
66
|
|
|
$query = "SELECT count(*) FROM $wpdb->comments"; |
67
|
|
|
|
68
|
|
|
if ( $where_sql = $this->get_where_sql( $config ) ) { |
69
|
|
|
$query .= ' WHERE ' . $where_sql; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$count = $wpdb->get_var( $query ); |
73
|
|
|
|
74
|
|
|
return (int) ceil( $count / self::ARRAY_CHUNK_SIZE ); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function get_where_sql( $config ) { |
78
|
|
|
if ( is_array( $config ) ) { |
79
|
|
|
return 'comment_ID IN (' . implode( ',', array_map( 'intval', $config ) ) . ')'; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return null; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function get_full_sync_actions() { |
86
|
|
|
return array( 'jetpack_full_sync_comments' ); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function count_full_sync_actions( $action_names ) { |
90
|
|
|
return $this->count_actions( $action_names, array( 'jetpack_full_sync_comments' ) ); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
function expand_wp_comment_status_change( $args ) { |
94
|
|
|
return array( $args[0], $this->filter_comment( $args[1] ) ); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
function expand_wp_insert_comment( $args ) { |
98
|
|
|
return array( $args[0], $this->filter_comment( $args[1] ) ); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
function filter_comment( $comment ) { |
102
|
|
|
/** |
103
|
|
|
* Filters whether to prevent sending comment data to .com |
104
|
|
|
* |
105
|
|
|
* Passing true to the filter will prevent the comment data from being sent |
106
|
|
|
* to the WordPress.com. |
107
|
|
|
* Instead we pass data that will still enable us to do a checksum against the |
108
|
|
|
* Jetpacks data but will prevent us from displaying the data on in the API as well as |
109
|
|
|
* other services. |
110
|
|
|
* @since 4.2.0 |
111
|
|
|
* |
112
|
|
|
* @param boolean false prevent post data from bing synced to WordPress.com |
113
|
|
|
* @param mixed $comment WP_COMMENT object |
114
|
|
|
*/ |
115
|
|
|
if ( apply_filters( 'jetpack_sync_prevent_sending_comment_data', false, $comment ) ) { |
116
|
|
|
$blocked_comment = new stdClass(); |
117
|
|
|
$blocked_comment->comment_ID = $comment->comment_ID; |
118
|
|
|
$blocked_comment->comment_date = $comment->comment_date; |
119
|
|
|
$blocked_comment->comment_date_gmt = $comment->comment_date_gmt; |
120
|
|
|
$blocked_comment->comment_approved = 'jetpack_sync_blocked'; |
121
|
|
|
|
122
|
|
|
return $blocked_comment; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $comment; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function expand_comment_ids( $args ) { |
129
|
|
|
$comment_ids = $args[0]; |
130
|
|
|
$comments = get_comments( array( |
131
|
|
|
'include_unapproved' => true, |
132
|
|
|
'comment__in' => $comment_ids, |
133
|
|
|
) ); |
134
|
|
|
|
135
|
|
|
return array( |
136
|
|
|
$comments, |
137
|
|
|
$this->get_metadata( $comment_ids, 'comment' ), |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|