Completed
Push — sync/jonathansadowski/r163313-... ( b2f1a4 )
by
unknown
07:34
created

callback()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 18
nc 10
nop 2
dl 0
loc 30
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
new WPCOM_JSON_API_Bulk_Delete_Post_Endpoint( array(
4
	'description' => 'Delete multiple posts. Note: If the trash is enabled, this request will send non-trashed posts to the trash. Trashed posts will be permanently deleted.',
5
	'group'       => 'posts',
6
	'stat'        => 'posts:1:bulk-delete',
7
	'min_version' => '1.1',
8
	'max_version' => '1.1',
9
	'method'      => 'POST',
10
	'path'        => '/sites/%s/posts/delete',
11
	'path_labels' => array(
12
		'$site'    => '(int|string) Site ID or domain',
13
	),
14
	'request_format' => array(
15
		'post_ids' => '(array|string) An array, or comma-separated list, of Post IDs to delete or trash.',
16
	),
17
18
	'response_format' => array(
19
		'results' => '(object) An object containing results, '
20
	),
21
22
	'example_request'      => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/posts/delete',
23
24
	'example_request_data' => array(
25
		'headers' => array(
26
			'authorization' => 'Bearer YOUR_API_TOKEN'
27
		),
28
29
		'body' => array(
30
			'post_ids' => array( 881, 882 ),
31
		),
32
33
	)
34
) );
35
36
class WPCOM_JSON_API_Bulk_Delete_Post_Endpoint extends WPCOM_JSON_API_Update_Post_v1_1_Endpoint {
37
	// /sites/%s/posts/delete
38
	function callback( $path = '', $blog_id = 0 ) {
39
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
40
		if ( is_wp_error( $blog_id ) ) {
41
			return $blog_id;
42
		}
43
44
		$input = $this->input();
45
46
		if ( is_array( $input['post_ids'] ) ) {
47
			$post_ids = (array) $input['post_ids'];
48
		} else if ( ! empty( $input['post_ids'] ) ) {
49
			$post_ids = explode( ',', $input['post_ids'] );
50
		} else {
51
			$post_ids = array();
52
		}
53
54
		if ( count( $post_ids ) < 1 ) {
55
			return new WP_Error( 'empty_post_ids', 'The request must include post_ids' );
56
		}
57
58
		$result = array(
59
			'results' => array(),
60
		);
61
62
		foreach( $post_ids as $post_id ) {
63
			$result['results'][ $post_id ] = $this->delete_post( $path, $blog_id, $post_id );
64
		}
65
66
		return $result;
67
	}
68
}
69