1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* REST API endpoint for managing VideoPress metadata. |
4
|
|
|
* |
5
|
|
|
* @package Jetpack |
6
|
|
|
* @since 9.3.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
|
|
|
'sanitize_callback' => 'sanitize_text_field', |
39
|
|
|
), |
40
|
|
|
'title' => array( |
41
|
|
|
'description' => __( 'The title of the video.', 'jetpack' ), |
42
|
|
|
'type' => 'string', |
43
|
|
|
'required' => false, |
44
|
|
|
'sanitize_callback' => 'sanitize_text_field', |
45
|
|
|
), |
46
|
|
|
'description' => array( |
47
|
|
|
'description' => __( 'The description of the video.', 'jetpack' ), |
48
|
|
|
'type' => 'string', |
49
|
|
|
'required' => false, |
50
|
|
|
'sanitize_callback' => 'sanitize_text_field', |
51
|
|
|
), |
52
|
|
|
'rating' => array( |
53
|
|
|
'description' => __( 'The video content rating. One of G, PG-13, R-17 or X-18', 'jetpack' ), |
54
|
|
|
'type' => 'string', |
55
|
|
|
'required' => false, |
56
|
|
|
'sanitize_callback' => 'sanitize_text_field', |
57
|
|
|
), |
58
|
|
|
'display_embed' => array( |
59
|
|
|
'description' => __( 'Display the share menu in the player.', 'jetpack' ), |
60
|
|
|
'type' => 'boolean', |
61
|
|
|
'required' => false, |
62
|
|
|
'sanitize_callback' => 'rest_sanitize_boolean', |
63
|
|
|
), |
64
|
|
|
), |
65
|
|
|
'methods' => WP_REST_Server::EDITABLE, |
66
|
|
|
'callback' => array( $this, 'videopress_block_update_meta' ), |
67
|
|
|
'permission_callback' => function () { |
68
|
|
|
return current_user_can( 'edit_posts' ); |
69
|
|
|
}, |
70
|
|
|
) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Updates video metadata via the WPCOM REST API. |
76
|
|
|
* |
77
|
|
|
* @param WP_REST_Request $request the request object. |
78
|
|
|
* @return object|WP_Error Success object or WP_Error with error details. |
79
|
|
|
*/ |
80
|
|
|
public function videopress_block_update_meta( $request ) { |
81
|
|
|
$json_params = $request->get_json_params(); |
82
|
|
|
|
83
|
|
|
$endpoint = 'videos'; |
84
|
|
|
$args = array( |
85
|
|
|
'method' => 'POST', |
86
|
|
|
'headers' => array( 'content-type' => 'application/json' ), |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$result = Client::wpcom_json_api_request_as_blog( |
90
|
|
|
$endpoint, |
91
|
|
|
'2', |
92
|
|
|
$args, |
93
|
|
|
wp_json_encode( $json_params ), |
|
|
|
|
94
|
|
|
'wpcom' |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
if ( is_wp_error( $result ) ) { |
98
|
|
|
return rest_ensure_response( $result ); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$response_body = json_decode( wp_remote_retrieve_body( $result ) ); |
102
|
|
View Code Duplication |
if ( is_bool( $response_body ) && $response_body ) { |
103
|
|
|
return rest_ensure_response( |
104
|
|
|
array( |
105
|
|
|
'code' => 'success', |
106
|
|
|
'message' => __( 'Video meta updated successfully.', 'jetpack' ), |
107
|
|
|
'data' => 200, |
108
|
|
|
) |
109
|
|
|
); |
110
|
|
|
} else { |
111
|
|
|
return rest_ensure_response( |
112
|
|
|
new WP_Error( |
113
|
|
|
$response_body->code, |
|
|
|
|
114
|
|
|
$response_body->message, |
115
|
|
|
$response_body->data |
116
|
|
|
) |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_VideoPress' ); |
123
|
|
|
|