Completed
Push — dna-full-sync-module ( 762324...644e0a )
by
unknown
113:10 queued 100:39
created

Comments::handle_comment_contents_modification()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

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