Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 51
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Uploader_Rest_Endpoints
0.00% covered (danger)
0.00%
0 / 51
0.00% covered (danger)
0.00%
0 / 6
72
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 register_rest_endpoints
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
2
 permissions_callback
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 validate_attachment_id
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 check_status
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 do_upload
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * VideoPress Uploader
4 *
5 * @package automattic/jetpack-videopress
6 */
7
8namespace Automattic\Jetpack\VideoPress;
9
10use WP_Error;
11
12/**
13 * VideoPress Uploader class
14 *
15 * Handles the upload from the Media Library to VideoPress servers
16 */
17class Uploader_Rest_Endpoints {
18
19    /**
20     * Initializes the endpoints
21     *
22     * @return void
23     */
24    public static function init() {
25        add_action( 'rest_api_init', array( __CLASS__, 'register_rest_endpoints' ) );
26    }
27
28    /**
29     * Register the REST API routes.
30     *
31     * @return void
32     */
33    public static function register_rest_endpoints() {
34        $id_arg = array(
35            'description'       => __( 'The ID of the attachment you want to upload to VideoPress', 'jetpack-videopress-pkg' ),
36            'type'              => 'integer',
37            'required'          => true,
38            'validate_callback' => __CLASS__ . '::validate_attachment_id',
39        );
40        register_rest_route(
41            'videopress/v1',
42            'upload/(?P<attachment_id>\d+)',
43            array(
44                array(
45                    'methods'             => \WP_REST_Server::READABLE,
46                    'callback'            => __CLASS__ . '::check_status',
47                    'permission_callback' => __CLASS__ . '::permissions_callback',
48                    'args'                => array(
49                        'attachment_id' => $id_arg,
50                    ),
51                ),
52                array(
53                    'methods'             => \WP_REST_Server::EDITABLE,
54                    'callback'            => __CLASS__ . '::do_upload',
55                    'permission_callback' => __CLASS__ . '::permissions_callback',
56                    'args'                => array(
57                        'attachment_id' => $id_arg,
58                    ),
59                ),
60            )
61        );
62    }
63
64    /**
65     * Checks wether the user have permission to perform the upload
66     *
67     * @return boolean
68     */
69    public static function permissions_callback() {
70        return current_user_can( 'upload_files' );
71    }
72
73    /**
74     * Validates the attachment ID argument
75     *
76     * @param integer|string $value The attachment ID passed as an argument to the endpoint.
77     * @return boolean|WP_Error
78     */
79    public static function validate_attachment_id( $value ) {
80        return Uploader::is_valid_attachment_id( $value );
81    }
82
83    /**
84     * Endpoint callback for the GET method. Checks the upload status
85     *
86     * @param \WP_REST_Request $request The request object.
87     * @return array|WP_Error
88     */
89    public static function check_status( $request ) {
90        $attachment_id = $request->get_param( 'attachment_id' );
91        try {
92            $uploader = new Uploader( $attachment_id );
93            $status   = $uploader->check_status();
94            return rest_ensure_response( $status );
95        } catch ( Upload_Exception $e ) {
96            return new WP_Error(
97                'rest_invalid_param',
98                $e->getMessage(),
99                array( 'status' => 400 )
100            );
101        }
102    }
103
104    /**
105     * Endpoint callback for the POST method. Uploads the video
106     *
107     * @param \WP_REST_Request $request The request object.
108     * @return array|WP_Error
109     */
110    public static function do_upload( $request ) {
111        $attachment_id = $request->get_param( 'attachment_id' );
112        try {
113            $uploader = new Uploader( $attachment_id );
114            $status   = $uploader->upload();
115            return rest_ensure_response( $status );
116        } catch ( Upload_Exception $e ) {
117            return new WP_Error(
118                'rest_invalid_param',
119                $e->getMessage(),
120                array( 'status' => 400 )
121            );
122        }
123    }
124}