Completed
Push — add/comments-bulk-endpoint ( cfed03 )
by
unknown
11:38
created

callback()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 38

Duplication

Lines 7
Ratio 18.42 %

Importance

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