Completed
Push — psr4-jetpack-sync-module ( 862b11...910f64 )
by
unknown
277:16 queued 269:36
created

Comments   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 194
Duplicated Lines 16.49 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 32
loc 194
c 0
b 0
f 0
rs 10
wmc 30
lcom 1
cbo 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Automattic\Jetpack\Sync\Modules;
4
5
use Automattic\Jetpack\Sync\Settings;
6
^
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

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