Completed
Push — add/crowdsignal-shortcode ( 65c42e...1b4a63 )
by Kuba
14:46 queued 06:22
created

callback()   B

Complexity

Conditions 10
Paths 13

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 13
nop 2
dl 0
loc 37
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
new WPCOM_JSON_API_Bulk_Update_Comments_Endpoint( array(
4
	'description'          => 'Update multiple comment\'s status.',
5
	'group'                => 'comments',
6
	'stat'                 => 'comments:1:bulk-update-status',
7
	'min_version'          => '1',
8
	'max_version'          => '1',
9
	'method'               => 'POST',
10
	'path'                 => '/sites/%s/comments/status',
11
	'path_labels'          => array(
12
		'$site' => '(int|string) Site ID or domain',
13
	),
14
	'request_format'       => array(
15
		'comment_ids' => '(array|string) An array, or comma-separated list, of Comment IDs to update.',
16
		'status'      => '(string) The new status value. Allowed values: approved, unapproved, spam, trash',
17
	),
18
	'response_format'      => array(
19
		'results' => '(array) An array of updated Comment IDs.'
20
	),
21
	'example_request'      => 'https://public-api.wordpress.com/rest/v1/sites/82974409/comments/status',
22
	'example_request_data' => array(
23
		'headers' => array(
24
			'authorization' => 'Bearer YOUR_API_TOKEN'
25
		),
26
		'body' => array(
27
			'comment_ids' => array( 881, 882 ),
28
			'status'      => 'approved',
29
		),
30
	)
31
) );
32
33
new WPCOM_JSON_API_Bulk_Update_Comments_Endpoint( array(
34
	'description'          => 'Permanently delete multiple comments. Note: this request will send non-trashed comments to the trash. Trashed comments will be permanently deleted.',
35
	'group'                => 'comments',
36
	'stat'                 => 'comments:1:bulk-delete',
37
	'min_version'          => '1',
38
	'max_version'          => '1',
39
	'method'               => 'POST',
40
	'path'                 => '/sites/%s/comments/delete',
41
	'path_labels'          => array(
42
		'$site' => '(int|string) Site ID or domain',
43
	),
44
	'request_format'       => array(
45
		'comment_ids'  => '(array|string) An array, or comma-separated list, of Comment IDs to delete or trash. (optional)',
46
		'empty_status' => '(string) Force to permanently delete all spam or trash comments. (optional). Allowed values: spam, trash',
47
	),
48
	'response_format'      => array(
49
		'results' => '(array) An array of deleted or trashed Comment IDs.'
50
	),
51
	'example_request'      => 'https://public-api.wordpress.com/rest/v1/sites/82974409/comments/delete',
52
	'example_request_data' => array(
53
		'headers' => array(
54
			'authorization' => 'Bearer YOUR_API_TOKEN'
55
		),
56
		'body' => array(
57
			'comment_ids' => array( 881, 882 ),
58
		),
59
	)
60
) );
61
62
class WPCOM_JSON_API_Bulk_Update_Comments_Endpoint extends WPCOM_JSON_API_Endpoint {
63
	// /sites/%s/comments/status
64
	// /sites/%s/comments/delete
65
	function callback( $path = '', $blog_id = 0 ) {
66
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
67
		if ( is_wp_error( $blog_id ) ) {
68
			return $blog_id;
69
		}
70
71
		$input = $this->input();
72
73
		if ( isset( $input['comment_ids'] ) && is_array( $input['comment_ids'] ) ) {
74
			$comment_ids = $input['comment_ids'];
75
		} else if ( isset( $input['comment_ids'] ) && ! empty( $input['comment_ids'] ) ) {
76
			$comment_ids = explode( ',', $input['comment_ids'] );
77
		} else {
78
			$comment_ids = array();
79
		}
80
81
		$result = array(
82
			'results' => array(),
83
		);
84
85
		wp_defer_comment_counting( true );
86
87
		if ( $this->api->ends_with( $path, '/delete' ) ) {
88
			if ( isset( $input['empty_status'] ) && $this->validate_empty_status_param( $input['empty_status'] ) ) {
89
				$result['results'] = $this->delete_all( $input['empty_status'] );
90
			} else {
91
				$result['results'] = $this->bulk_delete_comments( $comment_ids );
92
			}
93
		} else {
94
			$status = isset( $input['status'] ) ? $input['status'] : '';
95
			$result['results'] = $this->bulk_update_comments_status( $comment_ids, $status );
96
		}
97
98
		wp_defer_comment_counting( false );
99
100
		return $result;
101
	}
102
103
	/**
104
	 * Determine if the passed comment status is valid or not.
105
	 *
106
	 * @param string $status
107
	 *
108
	 * @return boolean
109
	 */
110
	function validate_status_param( $status ) {
111
		return in_array( $status, array( 'approved', 'unapproved', 'pending', 'spam', 'trash' ), true );
112
	}
113
114
	/**
115
	 * Determine if the passed empty status is valid or not.
116
	 *
117
	 * @param string $empty_status
118
	 *
119
	 * @return boolean
120
	 */
121
	function validate_empty_status_param( $empty_status ) {
122
		return in_array( $empty_status, array( 'spam', 'trash' ), true );
123
	}
124
125
	/**
126
	 * Update the status of multiple comments.
127
	 *
128
	 * @param array $comment_ids Comments to update.
129
	 * @param string $status New status value.
130
	 *
131
	 * @return array Updated comments IDs.
132
	 */
133
	function bulk_update_comments_status( $comment_ids, $status ) {
134
		if ( count( $comment_ids ) < 1 ) {
135
			return new WP_Error( 'empty_comment_ids', 'The request must include comment_ids', 400 );
136
		}
137
		if ( ! $this->validate_status_param( $status ) ) {
138
			return new WP_Error( 'invalid_status', "Invalid comment status value provided: '$status'.", 400 );
139
		}
140
		$results = array();
141
		foreach( $comment_ids as $comment_id ) {
142
			if ( ! current_user_can( 'edit_comment', $comment_id ) ) {
143
				continue;
144
			}
145
			$result = false;
146
			switch( $status ) {
147
				case 'approved':
148
					$result = wp_set_comment_status( $comment_id, 'approve' );
149
					break;
150
				case 'unapproved':
151
				case 'pending':
152
					$result = wp_set_comment_status( $comment_id, 'hold' );
153
					break;
154
				case 'spam':
155
					$result = wp_spam_comment( $comment_id );
156
					break;
157
				case 'trash':
158
					$result = wp_trash_comment( $comment_id );
159
					break;
160
			}
161
			if ( $result ) {
162
				$results[] = $comment_id;
163
			}
164
		}
165
		return $results;
166
	}
167
168
	/**
169
	 * Permanenty delete multiple comments.
170
	 *
171
	 * Comments are only permanently deleted if trash is disabled or their status is `trash` or `spam`.
172
	 * Otherwise they are moved to trash.
173
	 *
174
	 * @param array $comment_ids Comments to trash or delete.
175
	 *
176
	 * @return array Deleted comments IDs.
177
	 */
178
	function bulk_delete_comments( $comment_ids ) {
179
		if ( count( $comment_ids ) < 1 ) {
180
			return new WP_Error( 'empty_comment_ids', 'The request must include comment_ids', 400 );
181
		}
182
		$results = array();
183
		foreach( $comment_ids as $comment_id ) {
184
			if ( ! current_user_can( 'edit_comment', $comment_id ) ) {
185
				continue;
186
			}
187
			if ( wp_delete_comment( $comment_id ) ) {
188
				$results[] = $comment_id;
189
			}
190
		}
191
		return $results;
192
	}
193
194
	/**
195
	 * Delete all spam or trash comments.
196
	 *
197
	 * Comments are only permanently deleted if trash is disabled or their status is `trash` or `spam`.
198
	 * Otherwise they are moved to trash.
199
	 *
200
	 * @param string $status Can be `spam` or `trash`.
201
	 *
202
	 * @return array Deleted comments IDs.
203
	 */
204
	function delete_all( $status ) {
205
		global $wpdb;
206
		// This could potentially take a long time, so we only want to delete comments created
207
		// before this operation.
208
		// Comments marked `spam` or `trash` after this moment won't be touched.
209
		// Core uses the `pagegen_timestamp` hidden field for this same reason.
210
		$delete_time = gmdate('Y-m-d H:i:s');
211
		$comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_approved = %s AND %s > comment_date_gmt", $status, $delete_time ) );
212
213
		if ( count( $comment_ids ) < 1 ) {
214
			return array();
215
		}
216
217
		return $this->bulk_delete_comments( $comment_ids );
218
	}
219
}
220