Completed
Push — fix/refactor-full-sync-status-... ( baeb97...466398 )
by
unknown
63:36 queued 54:01
created

Jetpack_Sync_Module_Comments::name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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
		// full sync
40
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_comments', array( $this, 'expand_comment_ids' ) );
41
	}
42
43
	public function enqueue_full_sync_actions() {
44
		global $wpdb;
45
		return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_comments', $wpdb->comments, 'comment_ID', null );
46
	}
47
48
	public function get_full_sync_actions() {
49
		return array( 'jetpack_full_sync_comments' );
50
	}
51
52
	public function count_full_sync_actions( $action_names ) {
53
		return $this->count_actions( $action_names, array( 'jetpack_full_sync_comments' ) );
54
	}
55
56
	function expand_wp_comment_status_change( $args ) {
57
		return array( $args[0], $this->filter_comment( $args[1] ) );
58
	}
59
60
	function expand_wp_insert_comment( $args ) {
61
		return array( $args[0], $this->filter_comment( $args[1] ) );
62
	}
63
64
	function filter_comment( $comment ) {
65
		/**
66
		 * Filters whether to prevent sending comment data to .com
67
		 *
68
		 * Passing true to the filter will prevent the comment data from being sent
69
		 * to the WordPress.com.
70
		 * Instead we pass data that will still enable us to do a checksum against the
71
		 * Jetpacks data but will prevent us from displaying the data on in the API as well as
72
		 * other services.
73
		 * @since 4.2.0
74
		 *
75
		 * @param boolean false prevent post data from bing sycned to WordPress.com
76
		 * @param mixed $comment WP_COMMENT object
77
		 */
78
		if ( apply_filters( 'jetpack_sync_prevent_sending_comment_data', false, $comment ) ) {
79
			$blocked_comment = new stdClass();
80
			$blocked_comment->comment_ID = $comment->comment_ID;
81
			$blocked_comment->comment_date = $comment->comment_date;
82
			$blocked_comment->comment_date_gmt = $comment->comment_date_gmt;
83
			$blocked_comment->comment_approved = 'jetpack_sync_blocked';
84
			return $blocked_comment;
85
		}
86
87
		return $comment;
88
	}
89
90
	public function expand_comment_ids( $args ) {
91
		$comment_ids = $args[0];
92
		$comments    = get_comments( array(
93
			'include_unapproved' => true,
94
			'comment__in'        => $comment_ids,
95
		) );
96
97
		return array(
98
			$comments,
99
			$this->get_metadata( $comment_ids, 'comment' ),
100
		);
101
	}
102
}
103