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