Completed
Push — psr4-jetpack-sync-module ( 910f64...b1c668 )
by
unknown
284:58 queued 277:10
created

Comments   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 194
Duplicated Lines 19.59 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

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

17 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A get_object_by_id() 0 8 3
A init_listeners() 6 28 3
A handle_comment_contents_modification() 0 28 4
A init_full_sync_listeners() 0 3 1
A enqueue_full_sync_actions() 0 4 1
A estimate_full_sync_actions() 13 13 2
A get_where_sql() 7 7 2
A get_full_sync_actions() 0 3 1
A count_full_sync_actions() 0 3 1
A expand_wp_comment_status_change() 0 3 1
A expand_wp_insert_comment() 0 3 1
A filter_comment() 0 26 2
A is_whitelisted_comment_meta() 0 3 1
A filter_meta() 0 3 2
A expand_comment_ids() 0 17 1
A init_before_send() 12 19 3

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