Completed
Push — fix/small-cleanup-of-before-sy... ( 811ac4 )
by
unknown
206:01 queued 195:46
created

Jetpack_Sync_Module_Comments   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 71
Duplicated Lines 16.9 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 12
loc 71
rs 10
wmc 11
lcom 0
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A init_listeners() 6 19 3
A init_before_send() 6 10 3
A expand_wp_comment_status_change() 0 3 1
A expand_wp_insert_comment() 0 3 1
B filter_comment() 0 25 2

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 init_listeners( $callable ) {
10
		add_action( 'wp_insert_comment', $callable, 10, 2 );
11
		add_action( 'deleted_comment', $callable, 10 );
12
		add_action( 'trashed_comment', $callable, 10 );
13
		add_action( 'spammed_comment', $callable, 10 );
14
15
		// even though it's messy, we implement these hooks because
16
		// the edit_comment hook doesn't include the data
17
		// so this saves us a DB read for every comment event
18 View Code Duplication
		foreach ( array( '', 'trackback', 'pingback' ) as $comment_type ) {
19
			foreach ( array( 'unapproved', 'approved' ) as $comment_status ) {
20
				$comment_action_name = "comment_{$comment_status}_{$comment_type}";
21
				add_action( $comment_action_name, $callable, 10, 2 );
22
			}
23
		}
24
25
		// full sync
26
		add_action( 'jetpack_full_sync_comments', $callable ); // also send comments meta
27
	}
28
29
	public function init_before_send() {
30
		add_filter( 'jetpack_sync_before_send_wp_insert_comment', array( $this, 'expand_wp_insert_comment' ) );
31
32 View Code Duplication
		foreach ( array( '', 'trackback', 'pingback' ) as $comment_type ) {
33
			foreach ( array( 'unapproved', 'approved' ) as $comment_status ) {
34
				$comment_action_name = "comment_{$comment_status}_{$comment_type}";
35
				add_filter( 'jetpack_sync_before_send_' . $comment_action_name, array( $this, 'expand_wp_insert_comment' ) );
36
			}
37
		}
38
	}
39
40
	function expand_wp_comment_status_change( $args ) {
41
		return array( $args[0], $this->filter_comment( $args[1] ) );
42
	}
43
44
	function expand_wp_insert_comment( $args ) {
45
		return array( $args[0], $this->filter_comment( $args[1] ) );
46
	}
47
48
	function filter_comment( $comment ) {
49
		/**
50
		 * Filters whether to prevent sending comment data to .com
51
		 *
52
		 * Passing true to the filter will prevent the comment data from being sent
53
		 * to the WordPress.com.
54
		 * Instead we pass data that will still enable us to do a checksum against the
55
		 * Jetpacks data but will prevent us from displaying the data on in the API as well as
56
		 * other services.
57
		 * @since 4.2.0
58
		 *
59
		 * @param boolean false prevent post data from bing sycned to WordPress.com
60
		 * @param mixed $comment WP_COMMENT object
61
		 */
62
		if ( apply_filters( 'jetpack_sync_prevent_sending_comment_data', false, $comment ) ) {
63
			$blocked_comment = new stdClass();
64
			$blocked_comment->comment_ID = $comment->comment_ID;
65
			$blocked_comment->comment_date = $comment->comment_date;
66
			$blocked_comment->comment_date_gmt = $comment->comment_date_gmt;
67
			$blocked_comment->comment_approved = 'jetpack_sync_blocked';
68
			return $blocked_comment;
69
		}
70
71
		return $comment;
72
	}
73
}