Completed
Push — add/changelog-66 ( 3dd88f...f645f6 )
by Jeremy
27:12 queued 17:45
created

callback()   B

Complexity

Conditions 10
Paths 13

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 13
nop 2
dl 0
loc 36
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 if ( isset( $input['status'] ) ) {
94
			$result['results'] = $this->bulk_update_comments_status( $comment_ids, $input['status'] );
95
		}
96
97
		wp_defer_comment_counting( false );
98
99
		return $result;
100
	}
101
102
	/**
103
	 * Determine if the passed comment status is valid or not.
104
	 *
105
	 * @param string $status
106
	 *
107
	 * @return boolean
108
	 */
109
	function validate_status_param( $status ) {
110
		return in_array( $status, array( 'approved', 'unapproved', 'pending', 'spam', 'trash' ), true );
111
	}
112
113
	/**
114
	 * Determine if the passed empty status is valid or not.
115
	 *
116
	 * @param string $empty_status
117
	 *
118
	 * @return boolean
119
	 */
120
	function validate_empty_status_param( $empty_status ) {
121
		return in_array( $empty_status, array( 'spam', 'trash' ), true );
122
	}
123
124
	/**
125
	 * Update the status of multiple comments.
126
	 *
127
	 * @param array $comment_ids Comments to update.
128
	 * @param string $status New status value.
129
	 *
130
	 * @return array Updated comments IDs.
131
	 */
132
	function bulk_update_comments_status( $comment_ids, $status ) {
133
		if ( count( $comment_ids ) < 1 ) {
134
			return new WP_Error( 'empty_comment_ids', 'The request must include comment_ids', 400 );
135
		}
136
		if ( ! $this->validate_status_param( $status ) ) {
137
			return new WP_Error( 'invalid_status', "Invalid comment status value provided: '$status'.", 400 );
138
		}
139
		$results = array();
140
		foreach( $comment_ids as $comment_id ) {
141
			if ( ! current_user_can( 'edit_comment', $comment_id ) ) {
142
				continue;
143
			}
144
			$result = false;
145
			switch( $status ) {
146
				case 'approved':
147
					$result = wp_set_comment_status( $comment_id, 'approve' );
148
					break;
149
				case 'unapproved':
150
				case 'pending':
151
					$result = wp_set_comment_status( $comment_id, 'hold' );
152
					break;
153
				case 'spam':
154
					$result = wp_spam_comment( $comment_id );
155
					break;
156
				case 'trash':
157
					$result = wp_trash_comment( $comment_id );
158
					break;
159
			}
160
			if ( $result ) {
161
				$results[] = $comment_id;
162
			}
163
		}
164
		return $results;
165
	}
166
167
	/**
168
	 * Permanenty delete multiple comments.
169
	 *
170
	 * Comments are only permanently deleted if trash is disabled or their status is `trash` or `spam`.
171
	 * Otherwise they are moved to trash.
172
	 *
173
	 * @param array $comment_ids Comments to trash or delete.
174
	 *
175
	 * @return array Deleted comments IDs.
176
	 */
177
	function bulk_delete_comments( $comment_ids ) {
178
		if ( count( $comment_ids ) < 1 ) {
179
			return new WP_Error( 'empty_comment_ids', 'The request must include comment_ids', 400 );
180
		}
181
		$results = array();
182
		foreach( $comment_ids as $comment_id ) {
183
			if ( ! current_user_can( 'edit_comment', $comment_id ) ) {
184
				continue;
185
			}
186
			if ( wp_delete_comment( $comment_id ) ) {
187
				$results[] = $comment_id;
188
			}
189
		}
190
		return $results;
191
	}
192
193
	/**
194
	 * Delete all spam or trash comments.
195
	 *
196
	 * Comments are only permanently deleted if trash is disabled or their status is `trash` or `spam`.
197
	 * Otherwise they are moved to trash.
198
	 *
199
	 * @param string $status Can be `spam` or `trash`.
200
	 *
201
	 * @return array Deleted comments IDs.
202
	 */
203
	function delete_all( $status ) {
204
		global $wpdb;
205
		// This could potentially take a long time, so we only want to delete comments created
206
		// before this operation.
207
		// Comments marked `spam` or `trash` after this moment won't be touched.
208
		// Core uses the `pagegen_timestamp` hidden field for this same reason.
209
		$delete_time = gmdate('Y-m-d H:i:s');
210
		$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 ) );
211
212
		if ( count( $comment_ids ) < 1 ) {
213
			return array();
214
		}
215
216
		return $this->bulk_delete_comments( $comment_ids );
217
	}
218
}
219