Completed
Push — fix/external-stats-link ( eaed7e )
by
unknown
234:09 queued 224:03
created

Jetpack_Sync_Module_Comments   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 138
Duplicated Lines 18.12 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 25
loc 138
rs 10
wmc 23
lcom 2
cbo 1

14 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A get_object_by_id() 0 8 3
A init_listeners() 6 18 3
A init_full_sync_listeners() 0 3 1
A init_before_send() 6 16 3
A enqueue_full_sync_actions() 0 4 1
A estimate_full_sync_actions() 13 13 2
A get_where_sql() 0 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
B filter_comment() 0 26 2
A expand_comment_ids() 0 12 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
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