Completed
Push — add/edit_comment ( 4c0554...1379bf )
by
unknown
57:36 queued 48:58
created

handle_comment_contents_modification()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
		add_action( 'comment_approved_to_unapproved', $callable );
26
		add_action( 'comment_unapproved_to_approved', $callable );
27
		add_filter( 'wp_update_comment_data', array( $this, 'handle_comment_contents_modification' ), 10, 3 );
28
29
		// even though it's messy, we implement these hooks because
30
		// the edit_comment hook doesn't include the data
31
		// so this saves us a DB read for every comment event
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_action( $comment_action_name, $callable, 10, 2 );
36
			}
37
		}
38
39
		// listen for meta changes
40
		$this->init_listeners_for_meta_type( 'comment', $callable );
41
		$this->init_meta_whitelist_handler( 'comment', array( $this, 'filter_meta' ) );
42
	}
43
44
	/**
45
	 * Filters the comment data immediately before it is updated in the database.
46
	 *
47
	 * Note: data being passed to the filter is already unslashed.
48
	 *
49
	 * @since 4.7.0
50
	 *
51
	 * @param array $data       The new, processed comment data.
0 ignored issues
show
Bug introduced by
There is no parameter named $data. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
52
	 * @param array $comment    The old, unslashed comment data.
0 ignored issues
show
Documentation introduced by
There is no parameter named $comment. Did you maybe mean $new_comment_data?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
53
	 * @param array $commentarr The new, raw comment data.
0 ignored issues
show
Bug introduced by
There is no parameter named $commentarr. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
54
	 */
55
	public function handle_comment_contents_modification( $new_comment_data, $old_comment_data, $new_raw ) {
0 ignored issues
show
Unused Code introduced by
The parameter $new_comment_data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $old_comment_data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $new_raw is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
56
		error_log ("Hello!" );
57
	}
58
59
	public function init_full_sync_listeners( $callable ) {
60
		add_action( 'jetpack_full_sync_comments', $callable ); // also send comments meta
61
	}
62
63
	public function init_before_send() {
64
		add_filter( 'jetpack_sync_before_send_wp_insert_comment', array( $this, 'expand_wp_insert_comment' ) );
65
66 View Code Duplication
		foreach ( array( '', 'trackback', 'pingback' ) as $comment_type ) {
67
			foreach ( array( 'unapproved', 'approved' ) as $comment_status ) {
68
				$comment_action_name = "comment_{$comment_status}_{$comment_type}";
69
				add_filter( 'jetpack_sync_before_send_' . $comment_action_name, array(
70
					$this,
71
					'expand_wp_insert_comment',
72
				) );
73
			}
74
		}
75
76
		// full sync
77
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_comments', array( $this, 'expand_comment_ids' ) );
78
	}
79
80
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
81
		global $wpdb;
82
		return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_comments', $wpdb->comments, 'comment_ID', $this->get_where_sql( $config ), $max_items_to_enqueue, $state );
83
	}
84
85 View Code Duplication
	public function estimate_full_sync_actions( $config ) {
86
		global $wpdb;
87
88
		$query = "SELECT count(*) FROM $wpdb->comments";
89
		
90
		if ( $where_sql = $this->get_where_sql( $config ) ) {
91
			$query .= ' WHERE ' . $where_sql;
92
		}
93
94
		$count = $wpdb->get_var( $query );
95
96
		return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
97
	}
98
99
	private function get_where_sql( $config ) {
100
		if ( is_array( $config ) ) {
101
			return 'comment_ID IN (' . implode( ',', array_map( 'intval', $config ) ) . ')';
102
		} 
103
104
		return null;
105
	}
106
107
	public function get_full_sync_actions() {
108
		return array( 'jetpack_full_sync_comments' );
109
	}
110
111
	public function count_full_sync_actions( $action_names ) {
112
		return $this->count_actions( $action_names, array( 'jetpack_full_sync_comments' ) );
113
	}
114
115
	function expand_wp_comment_status_change( $args ) {
116
		return array( $args[0], $this->filter_comment( $args[1] ) );
117
	}
118
119
	function expand_wp_insert_comment( $args ) {
120
		return array( $args[0], $this->filter_comment( $args[1] ) );
121
	}
122
123
	function filter_comment( $comment ) {
124
		/**
125
		 * Filters whether to prevent sending comment data to .com
126
		 *
127
		 * Passing true to the filter will prevent the comment data from being sent
128
		 * to the WordPress.com.
129
		 * Instead we pass data that will still enable us to do a checksum against the
130
		 * Jetpacks data but will prevent us from displaying the data on in the API as well as
131
		 * other services.
132
		 * @since 4.2.0
133
		 *
134
		 * @param boolean false prevent post data from bing synced to WordPress.com
135
		 * @param mixed $comment WP_COMMENT object
136
		 */
137
		if ( apply_filters( 'jetpack_sync_prevent_sending_comment_data', false, $comment ) ) {
138
			$blocked_comment                   = new stdClass();
139
			$blocked_comment->comment_ID       = $comment->comment_ID;
140
			$blocked_comment->comment_date     = $comment->comment_date;
141
			$blocked_comment->comment_date_gmt = $comment->comment_date_gmt;
142
			$blocked_comment->comment_approved = 'jetpack_sync_blocked';
143
144
			return $blocked_comment;
145
		}
146
147
		return $comment;
148
	}
149
150
	// Comment Meta
151
	function is_whitelisted_comment_meta( $meta_key ) {
152
		return in_array( $meta_key, Jetpack_Sync_Settings::get_setting( 'comment_meta_whitelist' ) );
153
	}
154
155
	function filter_meta( $args ) {
156
		return ( $this->is_whitelisted_comment_meta( $args[2] ) ? $args : false );
157
	}
158
159
	public function expand_comment_ids( $args ) {
160
		$comment_ids = $args[0];
161
		$comments    = get_comments( array(
162
			'include_unapproved' => true,
163
			'comment__in'        => $comment_ids,
164
		) );
165
166
		return array(
167
			$comments,
168
			$this->get_metadata( $comment_ids, 'comment', Jetpack_Sync_Settings::get_setting( 'comment_meta_whitelist' ) ),
169
		);
170
	}
171
}
172