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