| 1 |
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 |
/** |
| 3 |
* Bulk delete posts on a site. |
| 4 |
* |
| 5 |
* Endpoint: /sites/%s/posts/delete |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit( 0 ); |
| 10 |
} |
| 11 |
|
| 12 |
new WPCOM_JSON_API_Bulk_Delete_Post_Endpoint( |
| 13 |
array( |
| 14 |
'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.', |
| 15 |
'group' => 'posts', |
| 16 |
'stat' => 'posts:1:bulk-delete', |
| 17 |
'min_version' => '1.1', |
| 18 |
'max_version' => '1.1', |
| 19 |
'method' => 'POST', |
| 20 |
'path' => '/sites/%s/posts/delete', |
| 21 |
'path_labels' => array( |
| 22 |
'$site' => '(int|string) Site ID or domain', |
| 23 |
), |
| 24 |
'request_format' => array( |
| 25 |
'post_ids' => '(array|string) An array, or comma-separated list, of Post IDs to delete or trash.', |
| 26 |
), |
| 27 |
|
| 28 |
'response_format' => array( |
| 29 |
'results' => '(object) An object containing results, ', |
| 30 |
), |
| 31 |
|
| 32 |
'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/posts/delete', |
| 33 |
|
| 34 |
'example_request_data' => array( |
| 35 |
'headers' => array( |
| 36 |
'authorization' => 'Bearer YOUR_API_TOKEN', |
| 37 |
), |
| 38 |
|
| 39 |
'body' => array( |
| 40 |
'post_ids' => array( 881, 882 ), |
| 41 |
), |
| 42 |
|
| 43 |
), |
| 44 |
) |
| 45 |
); |
| 46 |
|
| 47 |
/** |
| 48 |
* Bulk delete post endpoint class. |
| 49 |
* |
| 50 |
* @phan-constructor-used-for-side-effects |
| 51 |
*/ |
| 52 |
class WPCOM_JSON_API_Bulk_Delete_Post_Endpoint extends WPCOM_JSON_API_Update_Post_v1_1_Endpoint { |
| 53 |
/** |
| 54 |
* |
| 55 |
* API callback. |
| 56 |
* |
| 57 |
* @param string $path - the path. |
| 58 |
* @param int $blog_id - the blog ID. |
| 59 |
* @param int $post_id - the post ID. |
| 60 |
*/ |
| 61 |
public function callback( $path = '', $blog_id = 0, $post_id = 0 ) { |
| 62 |
$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); |
| 63 |
if ( is_wp_error( $blog_id ) ) { |
| 64 |
return $blog_id; |
| 65 |
} |
| 66 |
|
| 67 |
$input = $this->input(); |
| 68 |
|
| 69 |
if ( is_array( $input['post_ids'] ) ) { |
| 70 |
$post_ids = (array) $input['post_ids']; |
| 71 |
} elseif ( ! empty( $input['post_ids'] ) ) { |
| 72 |
$post_ids = explode( ',', $input['post_ids'] ); |
| 73 |
} else { |
| 74 |
$post_ids = array(); |
| 75 |
} |
| 76 |
|
| 77 |
if ( count( $post_ids ) < 1 ) { |
| 78 |
return new WP_Error( 'empty_post_ids', 'The request must include post_ids' ); |
| 79 |
} |
| 80 |
|
| 81 |
$result = array( |
| 82 |
'results' => array(), |
| 83 |
); |
| 84 |
|
| 85 |
foreach ( $post_ids as $post_id ) { |
| 86 |
$result['results'][ $post_id ] = $this->delete_post( $path, $blog_id, $post_id ); |
| 87 |
} |
| 88 |
|
| 89 |
return $result; |
| 90 |
} |
| 91 |
} |
| 92 |
|