Completed
Push — fix/7357 ( 13a0c4...764bff )
by
unknown
11:52
created

class.wpcom-json-api-bulk-delete-post-endpoint.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 View Code Duplication
class WPCOM_JSON_API_Bulk_Delete_Post_Endpoint extends WPCOM_JSON_API_Update_Post_v1_1_Endpoint {
0 ignored issues
show
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
	// /sites/%s/posts/delete
38
	// The unused $object parameter is for making the method signature compatible with its parent class method.
39
	function callback( $path = '', $blog_id = 0, $object = null ) {
40
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
41
		if ( is_wp_error( $blog_id ) ) {
42
			return $blog_id;
43
		}
44
45
		$input = $this->input();
46
47
		if ( is_array( $input['post_ids'] ) ) {
48
			$post_ids = (array) $input['post_ids'];
49
		} else if ( ! empty( $input['post_ids'] ) ) {
50
			$post_ids = explode( ',', $input['post_ids'] );
51
		} else {
52
			$post_ids = array();
53
		}
54
55
		if ( count( $post_ids ) < 1 ) {
56
			return new WP_Error( 'empty_post_ids', 'The request must include post_ids' );
57
		}
58
59
		$result = array(
60
			'results' => array(),
61
		);
62
63
		foreach( $post_ids as $post_id ) {
64
			$result['results'][ $post_id ] = $this->delete_post( $path, $blog_id, $post_id );
65
		}
66
67
		return $result;
68
	}
69
}
70