Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
98.99% |
197 / 199 |
|
100.00% |
11 / 11 |
CRAP | |
100.00% |
1 / 1 |
| WPCOM_REST_API_V2_Endpoint_VideoPress_Edits | |
100.00% |
195 / 195 |
|
100.00% |
11 / 11 |
31 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| register_routes | |
100.00% |
132 / 132 |
|
100.00% |
1 / 1 |
4 | |||
| permissions_check | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| get_edits | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| save_edits | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| retry_edits | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| copy_edits | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
| get_copy | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| restore_original | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_storyboard | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| proxy_request | |
100.00% |
34 / 34 |
|
100.00% |
1 / 1 |
15 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * REST endpoints for trimming and cutting VideoPress videos. |
| 4 | * |
| 5 | * @package automattic/jetpack-videopress |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\VideoPress; |
| 9 | |
| 10 | use Automattic\Jetpack\Connection\Client; |
| 11 | use Automattic\Jetpack\Constants; |
| 12 | use WP_Error; |
| 13 | use WP_REST_Request; |
| 14 | use WP_REST_Response; |
| 15 | use WP_REST_Server; |
| 16 | |
| 17 | if ( ! defined( 'ABSPATH' ) ) { |
| 18 | exit( 0 ); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Proxies edit operations and original-timeline storyboards to WordPress.com. |
| 23 | * |
| 24 | * @phan-constructor-used-for-side-effects |
| 25 | */ |
| 26 | class WPCOM_REST_API_V2_Endpoint_VideoPress_Edits { |
| 27 | |
| 28 | /** |
| 29 | * Constructor. |
| 30 | */ |
| 31 | public function __construct() { |
| 32 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Register the feature-gated editor routes. |
| 37 | */ |
| 38 | public function register_routes() { |
| 39 | // WordPress.com checks the rollout on the target site in the upstream endpoint. |
| 40 | if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) && ! Admin_UI::is_trim_cut_enabled() ) { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | $guid_arg = array( |
| 45 | 'guid' => array( |
| 46 | 'description' => __( 'The VideoPress GUID.', 'jetpack-videopress-pkg' ), |
| 47 | 'type' => 'string', |
| 48 | 'required' => true, |
| 49 | ), |
| 50 | ); |
| 51 | |
| 52 | $edit_args = array( |
| 53 | 'base_revision' => array( |
| 54 | 'description' => __( 'The revision the edits are based on.', 'jetpack-videopress-pkg' ), |
| 55 | 'type' => 'integer', |
| 56 | 'minimum' => 0, |
| 57 | 'required' => true, |
| 58 | ), |
| 59 | 'operations' => array( |
| 60 | 'description' => __( 'Trim and cut operations on the original video timeline.', 'jetpack-videopress-pkg' ), |
| 61 | 'type' => 'array', |
| 62 | 'required' => true, |
| 63 | 'items' => array( |
| 64 | 'type' => 'object', |
| 65 | 'additionalProperties' => false, |
| 66 | 'required' => array( 'type', 'start_ms', 'end_ms' ), |
| 67 | 'properties' => array( |
| 68 | 'type' => array( |
| 69 | 'type' => 'string', |
| 70 | 'enum' => array( 'trim', 'cut' ), |
| 71 | ), |
| 72 | 'start_ms' => array( |
| 73 | 'type' => 'integer', |
| 74 | 'minimum' => 0, |
| 75 | ), |
| 76 | 'end_ms' => array( |
| 77 | 'type' => 'integer', |
| 78 | 'minimum' => 1, |
| 79 | ), |
| 80 | ), |
| 81 | ), |
| 82 | ), |
| 83 | ); |
| 84 | |
| 85 | register_rest_route( |
| 86 | 'wpcom/v2', |
| 87 | 'videopress/(?P<guid>[A-Za-z0-9]{8})/edits', |
| 88 | array( |
| 89 | 'args' => $guid_arg, |
| 90 | 0 => array( |
| 91 | 'methods' => WP_REST_Server::READABLE, |
| 92 | 'callback' => array( $this, 'get_edits' ), |
| 93 | 'permission_callback' => array( $this, 'permissions_check' ), |
| 94 | ), |
| 95 | 1 => array( |
| 96 | 'methods' => WP_REST_Server::CREATABLE, |
| 97 | 'callback' => array( $this, 'save_edits' ), |
| 98 | 'permission_callback' => array( $this, 'permissions_check' ), |
| 99 | 'args' => $edit_args, |
| 100 | ), |
| 101 | 2 => array( |
| 102 | 'methods' => WP_REST_Server::DELETABLE, |
| 103 | 'callback' => array( $this, 'restore_original' ), |
| 104 | 'permission_callback' => array( $this, 'permissions_check' ), |
| 105 | ), |
| 106 | ) |
| 107 | ); |
| 108 | |
| 109 | register_rest_route( |
| 110 | 'wpcom/v2', |
| 111 | 'videopress/(?P<guid>[A-Za-z0-9]{8})/storyboard', |
| 112 | array( |
| 113 | 'args' => $guid_arg, |
| 114 | 'methods' => WP_REST_Server::READABLE, |
| 115 | 'callback' => array( $this, 'get_storyboard' ), |
| 116 | 'permission_callback' => array( $this, 'permissions_check' ), |
| 117 | ) |
| 118 | ); |
| 119 | register_rest_route( |
| 120 | 'wpcom/v2', |
| 121 | 'videopress/(?P<guid>[A-Za-z0-9]{8})/edits/retry', |
| 122 | array( |
| 123 | 'args' => array_merge( |
| 124 | $guid_arg, |
| 125 | array( |
| 126 | 'job_id' => array( |
| 127 | 'type' => 'string', |
| 128 | 'pattern' => '^[0-9]+$', |
| 129 | 'required' => true, |
| 130 | ), |
| 131 | ) |
| 132 | ), |
| 133 | 'methods' => WP_REST_Server::CREATABLE, |
| 134 | 'callback' => array( $this, 'retry_edits' ), |
| 135 | 'permission_callback' => array( $this, 'permissions_check' ), |
| 136 | ) |
| 137 | ); |
| 138 | |
| 139 | $copy_args = array_merge( |
| 140 | $guid_arg, |
| 141 | array( |
| 142 | 'request_id' => array( |
| 143 | 'type' => 'string', |
| 144 | 'pattern' => '^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$', |
| 145 | 'required' => true, |
| 146 | ), |
| 147 | ) |
| 148 | ); |
| 149 | register_rest_route( |
| 150 | 'wpcom/v2', |
| 151 | 'videopress/(?P<guid>[A-Za-z0-9]{8})/edits/copy', |
| 152 | array( |
| 153 | 'args' => array_merge( |
| 154 | $copy_args, |
| 155 | $edit_args, |
| 156 | array( |
| 157 | 'title' => array( |
| 158 | 'type' => 'string', |
| 159 | 'maxLength' => 1000, |
| 160 | ), |
| 161 | ) |
| 162 | ), |
| 163 | 'methods' => WP_REST_Server::CREATABLE, |
| 164 | 'callback' => array( $this, 'copy_edits' ), |
| 165 | 'permission_callback' => array( $this, 'permissions_check' ), |
| 166 | ) |
| 167 | ); |
| 168 | register_rest_route( |
| 169 | 'wpcom/v2', |
| 170 | 'videopress/(?P<guid>[A-Za-z0-9]{8})/edits/copy/(?P<request_id>[a-f0-9-]{36})', |
| 171 | array( |
| 172 | 'args' => $copy_args, |
| 173 | 'methods' => WP_REST_Server::READABLE, |
| 174 | 'callback' => array( $this, 'get_copy' ), |
| 175 | 'permission_callback' => array( $this, 'permissions_check' ), |
| 176 | ) |
| 177 | ); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Authorize access to the original video and editing controls. |
| 182 | * |
| 183 | * @param WP_REST_Request $request The request object. |
| 184 | * @return bool |
| 185 | */ |
| 186 | public function permissions_check( $request ) { |
| 187 | return Data::can_perform_action() |
| 188 | && current_user_can( 'upload_files' ) |
| 189 | && current_user_can( 'edit_post', WPCOM_REST_API_V2_Endpoint_VideoPress::get_video_attachment_id( $request['guid'] ) ); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Fetch the current revision and processing job. |
| 194 | * |
| 195 | * @param WP_REST_Request $request The request object. |
| 196 | * @return WP_REST_Response|WP_Error |
| 197 | */ |
| 198 | public function get_edits( $request ) { |
| 199 | return $this->proxy_request( sprintf( 'videos/%s/edits', $request['guid'] ) ); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Submit operations against a specific revision. |
| 204 | * |
| 205 | * @param WP_REST_Request $request The request object. |
| 206 | * @return WP_REST_Response|WP_Error |
| 207 | */ |
| 208 | public function save_edits( $request ) { |
| 209 | return $this->proxy_request( |
| 210 | sprintf( 'videos/%s/edits', $request['guid'] ), |
| 211 | 'POST', |
| 212 | array( |
| 213 | 'base_revision' => $request['base_revision'], |
| 214 | 'operations' => $request['operations'], |
| 215 | ) |
| 216 | ); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Retry the exact failed job without changing the video's identity. |
| 221 | * |
| 222 | * @param WP_REST_Request $request The request object. |
| 223 | * @return WP_REST_Response|WP_Error |
| 224 | */ |
| 225 | public function retry_edits( $request ) { |
| 226 | return $this->proxy_request( |
| 227 | sprintf( 'videos/%s/edits/retry', $request['guid'] ), |
| 228 | 'POST', |
| 229 | array( 'job_id' => $request['job_id'] ) |
| 230 | ); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Create an independent video from original-timeline edits. |
| 235 | * |
| 236 | * @param WP_REST_Request $request The request object. |
| 237 | * @return WP_REST_Response|WP_Error |
| 238 | */ |
| 239 | public function copy_edits( $request ) { |
| 240 | $body = array( |
| 241 | 'base_revision' => $request['base_revision'], |
| 242 | 'operations' => $request['operations'], |
| 243 | 'request_id' => $request['request_id'], |
| 244 | ); |
| 245 | if ( isset( $request['title'] ) ) { |
| 246 | $body['title'] = $request['title']; |
| 247 | } |
| 248 | return $this->proxy_request( sprintf( 'videos/%s/edits/copy', $request['guid'] ), 'POST', $body, true ); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Fetch progress for the same idempotent copy request. |
| 253 | * |
| 254 | * @param WP_REST_Request $request The request object. |
| 255 | * @return WP_REST_Response|WP_Error |
| 256 | */ |
| 257 | public function get_copy( $request ) { |
| 258 | return $this->proxy_request( sprintf( 'videos/%s/edits/copy/%s', $request['guid'], $request['request_id'] ) ); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Restore the original using the upstream API's POST deletion convention. |
| 263 | * |
| 264 | * @param WP_REST_Request $request The request object. |
| 265 | * @return WP_REST_Response|WP_Error |
| 266 | */ |
| 267 | public function restore_original( $request ) { |
| 268 | return $this->proxy_request( sprintf( 'videos/%s/edits/delete', $request['guid'] ), 'POST' ); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Fetch the original video's storyboard tiles. |
| 273 | * |
| 274 | * @param WP_REST_Request $request The request object. |
| 275 | * @return WP_REST_Response|WP_Error |
| 276 | */ |
| 277 | public function get_storyboard( $request ) { |
| 278 | return $this->proxy_request( sprintf( 'videos/%s/storyboard', $request['guid'] ) ); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Preserve upstream statuses and normalize v1.1 errors for REST clients. |
| 283 | * |
| 284 | * @param string $path WordPress.com REST v1.1 path. |
| 285 | * @param string $method HTTP method. |
| 286 | * @param array|null $body JSON request data. |
| 287 | * @param bool $as_user Preserve the connected actor when creating a copy. |
| 288 | * @return WP_REST_Response|WP_Error |
| 289 | */ |
| 290 | private function proxy_request( $path, $method = 'GET', $body = null, $as_user = false ) { |
| 291 | $args = array( 'method' => $method ); |
| 292 | if ( null !== $body ) { |
| 293 | $args['headers'] = array( 'content-type' => 'application/json' ); |
| 294 | $body = wp_json_encode( $body, JSON_UNESCAPED_SLASHES ); |
| 295 | } |
| 296 | |
| 297 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 298 | try { |
| 299 | $token = VideoPressToken::videopress_onetime_upload_token(); |
| 300 | } catch ( Upload_Exception $e ) { |
| 301 | return new WP_Error( 'videopress_edits_request_failed', $e->getMessage(), array( 'status' => 502 ) ); |
| 302 | } |
| 303 | $args['headers']['Authorization'] = sprintf( 'X_UPLOAD_TOKEN token="%s" blog_id="%s"', $token, VideoPressToken::blog_id() ); |
| 304 | $args['body'] = $body; |
| 305 | $args['timeout'] = 30; |
| 306 | $args['redirection'] = 0; |
| 307 | $url = Constants::get_constant( 'JETPACK__WPCOM_JSON_API_BASE' ) . '/rest/v1.1/' . $path; |
| 308 | // @phan-suppress-next-line PhanAccessMethodInternal -- Use the poster transport; the direct client only dispatches v2 routes. |
| 309 | $response = Client::_wp_remote_request( $url, $args ); |
| 310 | } elseif ( $as_user ) { |
| 311 | $response = Client::wpcom_json_api_request_as_user( $path, '1.1', $args, $body, 'rest' ); |
| 312 | } else { |
| 313 | $response = Client::wpcom_json_api_request_as_blog( $path, '1.1', $args, $body, 'rest' ); |
| 314 | } |
| 315 | if ( is_wp_error( $response ) ) { |
| 316 | return new WP_Error( 'videopress_edits_request_failed', $response->get_error_message(), array( 'status' => 502 ) ); |
| 317 | } |
| 318 | |
| 319 | $status = (int) wp_remote_retrieve_response_code( $response ); |
| 320 | $decoded = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 321 | if ( ! is_array( $decoded ) || $status < 100 ) { |
| 322 | return new WP_Error( |
| 323 | 'videopress_edits_invalid_response', |
| 324 | __( 'The video editing service returned an invalid response.', 'jetpack-videopress-pkg' ), |
| 325 | array( 'status' => 502 ) |
| 326 | ); |
| 327 | } |
| 328 | |
| 329 | if ( $status >= 400 && isset( $decoded['error'] ) && ! isset( $decoded['code'] ) ) { |
| 330 | $decoded['code'] = $decoded['error']; |
| 331 | unset( $decoded['error'] ); |
| 332 | } |
| 333 | if ( $status >= 400 ) { |
| 334 | $decoded['data'] = isset( $decoded['data'] ) && is_array( $decoded['data'] ) ? $decoded['data'] : array(); |
| 335 | $decoded['data']['status'] = $status; |
| 336 | } |
| 337 | |
| 338 | return new WP_REST_Response( $decoded, $status ); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 343 | wpcom_rest_api_v2_load_plugin( __NAMESPACE__ . '\\WPCOM_REST_API_V2_Endpoint_VideoPress_Edits' ); |
| 344 | } |