Completed
Push — update/video-block-json-endpoi... ( 66777e...cabde8 )
by
unknown
143:55 queued 135:01
created

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * REST API endpoint for managing VideoPress metadata.
4
 *
5
 * @package Jetpack
6
 * @since 9.2.0
7
 */
8
9
use Automattic\Jetpack\Connection\Client;
10
11
/**
12
 * VideoPress wpcom api v2 endpoint
13
 */
14
class WPCOM_REST_API_V2_Endpoint_VideoPress extends WP_REST_Controller {
15
	/**
16
	 * Constructor.
17
	 */
18
	public function __construct() {
19
		$this->namespace = 'wpcom/v2';
20
		$this->rest_base = 'videopress';
21
22
		add_action( 'rest_api_init', array( $this, 'register_routes' ) );
23
	}
24
25
	/**
26
	 * Register the route.
27
	 */
28
	public function register_routes() {
29
		register_rest_route(
30
			$this->namespace,
31
			$this->rest_base . '/meta',
32
			array(
33
				'args'                => array(
34
					'guid'          => array(
35
						'description' => __( 'The VideoPress video guid.', 'jetpack' ),
36
						'type'        => 'string',
37
						'required'    => true,
38
					),
39
					'title'         => array(
40
						'description' => __( 'The title of the video.', 'jetpack' ),
41
						'type'        => 'string',
42
						'required'    => false,
43
					),
44
					'description'   => array(
45
						'description' => __( 'The description of the video.', 'jetpack' ),
46
						'type'        => 'string',
47
						'required'    => false,
48
					),
49
					'rating'        => array(
50
						'description' => __( 'The video content rating. One of G, PG, PG-13, R-17 or X-18', 'jetpack' ),
51
						'type'        => 'string',
52
						'required'    => false,
53
					),
54
					'display_embed' => array(
55
						'description' => __( 'Display the share menu in the player.', 'jetpack' ),
56
						'type'        => 'boolean',
57
						'required'    => false,
58
					),
59
				),
60
				'methods'             => WP_REST_Server::EDITABLE,
61
				'callback'            => array( $this, 'videopress_block_update_meta' ),
62
				'permission_callback' => function () {
63
					return current_user_can( 'edit_posts' );
64
				},
65
			)
66
		);
67
	}
68
69
	/**
70
	 * Updates video metadata via the WPCOM REST API.
71
	 *
72
	 * @param WP_REST_Request $request the request object.
73
	 * @return bool If the request was successful
74
	 */
75
	public function videopress_block_update_meta( $request ) {
76
		$json_params = $request->get_json_params();
77
		if ( ! isset( $json_params ) || ! isset( $json_params['guid'] ) ) {
78
			return false;
79
		}
80
81
		$endpoint = "videos/{$json_params['guid']}";
82
		$args     = array(
83
			'method'  => 'POST',
84
			'headers' => array( 'Content-Type' => 'application/json' ),
85
		);
86
87
		$result = Client::wpcom_json_api_request_as_blog(
88
			$endpoint,
89
			Client::WPCOM_JSON_API_VERSION,
90
			$args,
91
			wp_json_encode( $json_params )
92
		);
93
94
		if ( is_wp_error( $result ) ) {
95
			return false;
96
		}
97
98
		return true;
99
	}
100
}
101
102
wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_VideoPress' );
103