Completed
Push — master-stable ( 2e27ce...519850 )
by Jeremy
12:36
created

Jetpack_Sync_Module_Comments::get_where_sql()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 7
loc 7
rs 9.4285
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
26
	public function init_full_sync_listeners( $callable ) {
27
		add_action( 'jetpack_full_sync_comments', $callable ); // also send comments meta
28
	}
29
30
	public function init_before_send() {
31
		add_filter( 'jetpack_sync_before_send_wp_insert_comment', array( $this, 'expand_wp_insert_comment' ) );
32
33 View Code Duplication
		foreach ( array( '', 'trackback', 'pingback' ) as $comment_type ) {
34
			foreach ( array( 'unapproved', 'approved' ) as $comment_status ) {
35
				$comment_action_name = "comment_{$comment_status}_{$comment_type}";
36
				add_filter( 'jetpack_sync_before_send_' . $comment_action_name, array(
37
					$this,
38
					'expand_wp_insert_comment',
39
				) );
40
			}
41
		}
42
43
		// full sync
44
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_comments', array( $this, 'expand_comment_ids' ) );
45
	}
46
47
	public function enqueue_full_sync_actions( $config ) {
48
		global $wpdb;
49
		return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_comments', $wpdb->comments, 'comment_ID', $this->get_where_sql( $config ) );
50
	}
51
52 View Code Duplication
	public function estimate_full_sync_actions( $config ) {
53
		global $wpdb;
54
55
		$query = "SELECT count(*) FROM $wpdb->comments";
56
		
57
		if ( $where_sql = $this->get_where_sql( $config ) ) {
58
			$query .= ' WHERE ' . $where_sql;
59
		}
60
61
		$count = $wpdb->get_var( $query );
62
63
		return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
64
	}
65
66 View Code Duplication
	private function get_where_sql( $config ) {
67
		if ( is_array( $config ) ) {
68
			return 'comment_ID IN (' . implode( ',', array_map( 'intval', $config ) ) . ')';
69
		} 
70
71
		return null;
72
	}
73
74
	public function get_full_sync_actions() {
75
		return array( 'jetpack_full_sync_comments' );
76
	}
77
78
	public function count_full_sync_actions( $action_names ) {
79
		return $this->count_actions( $action_names, array( 'jetpack_full_sync_comments' ) );
80
	}
81
82
	function expand_wp_comment_status_change( $args ) {
83
		return array( $args[0], $this->filter_comment( $args[1] ) );
84
	}
85
86
	function expand_wp_insert_comment( $args ) {
87
		return array( $args[0], $this->filter_comment( $args[1] ) );
88
	}
89
90
	function filter_comment( $comment ) {
91
		/**
92
		 * Filters whether to prevent sending comment data to .com
93
		 *
94
		 * Passing true to the filter will prevent the comment data from being sent
95
		 * to the WordPress.com.
96
		 * Instead we pass data that will still enable us to do a checksum against the
97
		 * Jetpacks data but will prevent us from displaying the data on in the API as well as
98
		 * other services.
99
		 * @since 4.2.0
100
		 *
101
		 * @param boolean false prevent post data from bing synced to WordPress.com
102
		 * @param mixed $comment WP_COMMENT object
103
		 */
104
		if ( apply_filters( 'jetpack_sync_prevent_sending_comment_data', false, $comment ) ) {
105
			$blocked_comment                   = new stdClass();
106
			$blocked_comment->comment_ID       = $comment->comment_ID;
107
			$blocked_comment->comment_date     = $comment->comment_date;
108
			$blocked_comment->comment_date_gmt = $comment->comment_date_gmt;
109
			$blocked_comment->comment_approved = 'jetpack_sync_blocked';
110
111
			return $blocked_comment;
112
		}
113
114
		return $comment;
115
	}
116
117
	public function expand_comment_ids( $args ) {
118
		$comment_ids = $args[0];
119
		$comments    = get_comments( array(
120
			'include_unapproved' => true,
121
			'comment__in'        => $comment_ids,
122
		) );
123
124
		return array(
125
			$comments,
126
			$this->get_metadata( $comment_ids, 'comment' ),
127
		);
128
	}
129
}
130