-
Notifications
You must be signed in to change notification settings - Fork 3
/
class-cloudflare-stream-api.php
301 lines (271 loc) · 8.33 KB
/
class-cloudflare-stream-api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
/**
* Cloudflare Stream API class
*
* Methods for interacting with the Cloudflare Stream API.
*
* @package cloudflare-stream
* @since 1.0.0
*/
/**
* Cloudflare_Stream_API
*/
class Cloudflare_Stream_API {
/**
* API Token
*
* @var string $api_token Cloudflare API token.
*/
private $api_token = '';
/**
* API Zone ID
*
* @var string $api_zone_id Cloudflare API zone ID.
*/
private $api_zone_id = '';
/**
* Use signed URLs
*
* @var bool $signed_urls Use signed URLs.
*/
private $signed_urls = '';
/**
* Duration of signed URLs
*
* @var int $signed_urls_duration Duration of signed URLs.
*/
private $signed_urls_duration = '';
/**
* Last video seen when retrieving paginated results.
*
* @var string $last_seen Timestamp of the last returned result.
*/
public $last_seen = false;
/**
* REST API limit
*
* @var string $api_limit Number of results to return from the API by default.
*/
public $api_limit = 40;
/**
* Define and register singleton
*
* @var $instance The singleton instance of the class.
*/
private static $instance = false;
/**
* Singleton
*
* @since 1.0.0
*/
public static function instance() {
if ( ! self::$instance ) {
self::$instance = new self();
self::$instance->setup();
}
return self::$instance;
}
/**
* Constructor
*
* @since 1.0.0
*/
private function __construct() { }
/**
* Clone
*
* @since 1.0.0
*/
private function __clone() { }
/**
* Add actions and filters
*
* @uses add_action, add_filter
* @since 1.0.0
*/
private function setup() {
return false;
}
/**
* Make the request to the API
*
* @param string $endpoint API Endpoint.
* @param array $args Additional API arguments.
* @param bool $return_headers Return the response headers intead of the response body.
* @since 1.0.0
*/
public function request( $endpoint, $args = array(), $return_headers = false ) {
$this->api_token = get_option( Cloudflare_Stream_Settings::OPTION_API_TOKEN );
$this->api_zone_id = get_option( Cloudflare_Stream_Settings::OPTION_API_ZONE_ID );
if ( isset( $args['method'] ) ) {
$method = $args['method'];
} else {
$method = 'GET';
}
$base_url = 'https://api.cloudflare.com/client/v4/zones/' . $this->api_zone_id . '/';
$args['headers'] = array(
'Authorization' => 'Bearer ' . $this->api_token,
'Content-Type' => 'application/json',
);
$query_string = isset( $args['query'] ) ? '?' . $args['query'] : '';
$endpoint .= $query_string;
$route = $base_url . $endpoint;
// Get remote HTML file.
$response = wp_remote_request( $route, $args );
// Check for error.
if ( is_wp_error( $response ) ) {
return $response->get_error_message();
} elseif ( 'headers' === $return_headers ) {
return wp_remote_retrieve_headers( $response );
}
return wp_remote_retrieve_body( $response );
}
/**
* Make a POST request
*
* @param string $endpoint API Endpoint.
* @param array $args Additional API arguments.
* @param bool $return_headers Return the response headers intead of the response body.
* @since 1.0.0
*/
public function post( $endpoint, $args = array(), $return_headers = false ) {
$args['method'] = 'POST';
$response_text = $this->request( $endpoint, $args, $return_headers );
return ( $response_text );
}
/**
* Make a DELETE request
*
* @param string $endpoint API Endpoint.
* @param array $args Additional API arguments.
* @param bool $return_headers Return the response headers intead of the response body.
* @since 1.0.0
*
* @returns object $response HTTP response object.
*/
public function delete( $endpoint, $args = array(), $return_headers = false ) {
$args['method'] = 'DELETE';
$response_text = $this->request( $endpoint, $args, $return_headers );
return ( $response_text );
}
/**
* Wrapper for listing videos
*
* @param array $args Additional API arguments.
* @param bool $return_headers Return the response headers intead of the response body.
* @since 1.0.0
*/
public function get_videos( $args = array(), $return_headers = 'false' ) {
$response_text = $this->request( 'media', $args, $return_headers );
return json_decode( $response_text );
}
/**
* API wrapper for requesting a specific video's details
*
* @param string $uid Unique Video ID.
* @param array $args Additional API arguments.
* @param bool $return_headers Return the response headers intead of the response body.
* @since 1.0.0
*/
public function get_video_details( $uid, $args = array(), $return_headers = false ) {
$response_text = $this->request( 'media/' . $uid, $args, $return_headers );
return json_decode( $response_text );
}
/**
* API wrapper for updating a specific video's details
*
* @param string $uid Unique Video ID.
* @param array $args Additional API arguments.
* @param bool $return_headers Return the response headers intead of the response body.
* @since 1.0.0
*/
public function update_video_details( $uid, $args = array(), $return_headers = false ) {
$response_text = $this->post( 'media/' . $uid, $args, $return_headers );
return json_decode( $response_text );
}
/**
* Get the embed code
*
* @param string $uid Unique Video ID.
* @param array $args Additional API arguments.
* @param bool $return_headers Return the response headers intead of the response body.
* @since 1.0.0
*/
public function get_video_embed( $uid, $args = array(), $return_headers = false ) {
$signed_urls = get_option( Cloudflare_Stream_Settings::OPTION_SIGNED_URLS );
if ( $signed_urls ) {
$uid = $this->get_signed_video_token($uid)->result->token;
}
$video_embed = '<div style="position: relative; padding-top: 56.25%"><iframe'
. ' src="https://iframe.videodelivery.net/' . $uid . '?'
. 'muted=' . $args['muted'] . '&'
. 'preload=' . $args['preload'] . '&'
. 'loop=' . $args['loop'] . '&'
. 'autoplay=' . $args['autoplay'] . '&'
. 'controls=' . $args['controls'] . '" '
. 'style="border: none; position: absolute; top: 0; height: 100%; width: 100%" '
. 'allow="accelerometer; gyroscope; autoplay; encrypted-media; picture-in-picture;" '
. 'allowfullscreen="true" '
. 'id="stream-player"'
. '></iframe></div>';
return $video_embed;
}
/**
* Get a specific video's signed id.
*
* @param string $uid Unique Video ID.
* @param array $args Additional API arguments.
* @param bool $return_headers Return the response headers intead of the response body.
* @since 1.0.5
*/
public function get_signed_video_token( $uid, $args = array(), $return_headers = false ) {
$signed_urls_duration = get_option( Cloudflare_Stream_Settings::OPTION_SIGNED_URLS_DURATION );
// Determine token expiration time if custom signed urls duration is set.
if ( $signed_urls_duration != false ) {
$body = [
'exp' => ( time() + ( intval($signed_urls_duration) * 60 ) ),
];
$body = wp_json_encode( $body );
$args['body'] = $body;
}
$args['method'] = 'POST';
$response_text = $this->request( 'media/' . $uid . '/token', $args, $return_headers );
return json_decode( $response_text );
}
/**
* Get a specific video's hotlink.
*
* @param string $uid Unique Video ID.
* @param array $args Additional API arguments.
* @param bool $return_headers Return the response headers intead of the response body.
* @since 1.0.0
*/
public function get_video_link( $uid, $args = array(), $return_headers = false ) {
$response_text = $this->request( 'media/' . $uid . '/preview', $args, $return_headers );
return $response_text;
}
/**
* Setup video.
*
* @param array $args Additional API arguments.
* @param bool $return_headers Return the response headers intead of the response body.
* @since 1.0.0
*/
public function init_video( $args = array(), $return_headers = true ) {
$response_text = $this->post( 'media', $args, $return_headers );
return $response_text;
}
/**
* Delete video.
*
* @param array $uid Unique Video ID.
* @param array $args Additional API arguments.
* @param bool $return_headers Return the response headers intead of the response body.
* @since 1.0.0
*/
public function delete_video( $uid, $args = array(), $return_headers = false ) {
$response_text = $this->delete( 'media/' . $uid, $args, $return_headers );
return json_decode( $response_text );
}
}
Cloudflare_Stream_API::instance();