-
Notifications
You must be signed in to change notification settings - Fork 3
/
init.php
322 lines (292 loc) · 10 KB
/
init.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
/**
* Blocks Initializer
*
* Enqueue CSS/JS of all the blocks.
*
* @since 1.0.0
* @package cloudflare-stream
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Enqueue Cloudflare Stream block assets for both frontend + backend.
*
* `wp-blocks`: includes block type registration and related functions.
*
* @since 1.0.0
*/
function cloudflare_stream_block_assets() {
// Styles.
wp_enqueue_style(
'cloudflare-stream-block-style-css',
// Handle.
plugins_url( 'dist/style-blocks.css', dirname( __FILE__ ) ),
// Block style CSS.
array( 'wp-block-library' ),
// Dependency to include the CSS after it.
filemtime( plugin_dir_path( __DIR__ ) . 'dist/style-blocks.css' )
// Version: filemtime — Gets file modification time.
);
} // End function cloudflare_stream_block_assets().
// Hook: Frontend assets.
add_action( 'enqueue_block_assets', 'cloudflare_stream_block_assets' );
/**
* Enqueue Cloudflare Stream block assets for backend editor.
*
* `wp-blocks`: includes block type registration and related functions.
* `wp-element`: includes the WordPress Element abstraction for describing the structure of your blocks.
* `wp-i18n`: To internationalize the block's text.
*
* @since 1.0.0
*/
function cloudflare_stream_block_editor_assets() {
// Don't load the block assets if the API credentials are not configured.
if ( ! Cloudflare_Stream_Settings::is_configured() ) {
return;
}
$current_user = wp_get_current_user();
// Scripts.
wp_enqueue_script(
'cloudflare-stream-block-js',
// Handle.
plugins_url( '/dist/blocks.build.js', dirname( __FILE__ ) ),
// Block.build.js: We register the block here. Built with Webpack.
array( 'wp-blocks', 'wp-i18n', 'wp-element' ),
// Dependencies, defined above.
filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.build.js' ),
// Version: filemtime — Gets file modification time.
true
// Enqueue the script in the footer.
);
// Don't load the API Credentials if the user cannot edit the post.
$api_token = current_user_can( 'administrator' ) ? get_option( Cloudflare_Stream_Settings::OPTION_API_TOKEN ) : '';
$api = Cloudflare_Stream_API::instance();
wp_localize_script(
'cloudflare-stream-block-js',
'cloudflareStream',
array(
'nonce' => wp_create_nonce( Cloudflare_Stream_Settings::NONCE ),
'api' => array(
'token' => $api_token,
'account' => get_option( Cloudflare_Stream_Settings::OPTION_API_ACCOUNT ),
'posts_per_page' => $api->api_limit,
'uid' => md5( $current_user->user_login ),
),
'media' => array(
'view' => array(),
'model' => array(),
),
)
);
// The jQuery UI progress bar.
wp_enqueue_script( 'jquery-ui-progressbar' );
// Styles.
wp_enqueue_style(
'cloudflare-stream-block-editor-css',
// Handle.
plugins_url( 'dist/blocks.css', dirname( __FILE__ ) ),
// Block editor CSS.
array( 'wp-edit-blocks' ),
// Dependency to include the CSS after it.
filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.css' )
// Version: filemtime — Gets file modification time.
);
wp_enqueue_style(
'progressbar',
'https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css',
array(),
'1.12.1'
);
} // End function cloudflare_stream_editor_assets().
// Hook: Editor assets.
add_action( 'enqueue_block_editor_assets', 'cloudflare_stream_block_editor_assets' );
/**
* Load the JavaScript on admin
*
* @since 1.0.0
*/
function cloudflare_stream_admin_enqueue_scripts() {
// Registering the block.
register_block_type(
'cloudflare-stream/block-video',
array(
'render_callback' => 'cloudflare_stream_render_block',
)
);
}
add_action( 'init', 'cloudflare_stream_admin_enqueue_scripts' );
/**
* Render the video block.
*
* @param array $block_attributes The attributes stored in the block.
* @param string $content The static markup of the block.
* @since 1.0.9
*/
function cloudflare_stream_render_block( $block_attributes, $content ) {
// Only proceed if we have a UID.
if ( ! isset( $block_attributes['uid'] ) || empty( $block_attributes['uid'] ) ) {
return $content;
}
// Apply default attributes.
$defaults = array(
'controls' => true,
'autoplay' => false,
'loop' => false,
'preload' => false,
'muted' => false,
);
$attributes = wp_parse_args( $block_attributes, $defaults );
$api = Cloudflare_Stream_API::instance();
$embed = $api->get_video_embed( $attributes['uid'], $attributes );
return '<figure class="wp-block-cloudflare-stream-block-video">' . $embed . '</figure>';
}
/**
* Adds 'upload-php' class to the <body> tag.
*
* @param array $classes Array of CSS classes.
* @since 1.0.0
*/
function cloudflare_stream_admin_body_class( $classes ) {
return "$classes upload-php cloudflare-stream";
}
add_filter( 'admin_body_class', 'cloudflare_stream_admin_body_class' );
/**
* AJAX method for retrieving a collection of Stream videos.
*
* @since 1.0.0
*/
function action_wp_ajax_query_cloudflare_stream_attachments() {
check_ajax_referer( Cloudflare_Stream_Settings::NONCE, 'nonce' );
$api = Cloudflare_Stream_API::instance();
$args['query'] = isset( $_REQUEST['query'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['query'] ) ) : '';
$args['query'] .= '&limit=' . $api->api_limit;
$data = array();
$videos = $api->get_videos( $args );
foreach ( $videos->result as $video ) {
$datetime = new DateTime( $video->created );
// phpcs:ignore
$embedcode = '<stream src="' . $video->uid . '" controls></stream><script data-cfasync="false" defer type="text/javascript" src="https://embed.videodelivery.net/embed/r4xu.fla9.latest.js?video=' . $video->uid . '"></script>';
$shortcode = '[cloudflare_stream uid="' . $video->uid . '"]';
$data[] = array(
'uid' => $video->uid,
'id' => $video->uid,
'title' => $video->meta->name,
'filename' => $video->meta->name,
'url' => 'https://watch.cloudflarestream.com/' . $video->uid,
'link' => 'https://watch.cloudflarestream.com/' . $video->uid,
'description' => $embedcode,
'caption' => $shortcode,
'status' => 'inherit',
'uploadedTo' => 0,
'date' => $video->created,
'modified' => $video->created,
'menuOrder' => 0,
'mime' => 'video/mp4',
'type' => 'video',
'subtype' => 'mp4',
'icon' => $video->thumbnail,
'dateFormatted' => $datetime->format( 'F j, Y' ),
'nonces' =>
array(
'delete' => Cloudflare_Stream_Settings::NONCE,
),
'filesizeInBytes' => $video->size,
'filesizeHumanReadable' => size_format( $video->size ),
'image' => array(
'src' => $video->thumbnail,
'width' => 64,
'height' => 48,
),
'fileLength' => gmdate( 'H:i:s', round( $video->duration ) ),
'fileLengthHumanReadable' => human_readable_duration( gmdate( 'H:i:s', round( $video->duration ) ) ),
'thumb' => array(
'src' => $video->thumbnail,
'width' => 64,
'height' => 48,
),
'compat' => array(
'item' => '',
'meta' => '',
),
'cloudflare' => $video,
);
}//end foreach
$response = array( 'success' => true );
if ( isset( $data ) ) {
$response['args'] = $args;
$response['data'] = $data;
$response['success'] = true;
};
wp_send_json( $response, 200 );
}
add_action( 'wp_ajax_query-cloudflare-stream-attachments', 'action_wp_ajax_query_cloudflare_stream_attachments' );
/**
* AJAX method for checking the status of a video upload.
*
* @since 1.0.0
*/
function action_wp_ajax_refresh_check_upload() {
check_ajax_referer( Cloudflare_Stream_Settings::NONCE, 'nonce' );
$uid = isset( $_REQUEST['uid'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['uid'] ) ) : '';
$data = array();
$api = Cloudflare_Stream_API::instance();
$data = $api->get_video_details( $uid );
if ( isset( $data->success ) && $data->success ) {
wp_send_json_success( $data->result );
} else {
wp_send_json_error( $data->errors[0]->code . ' - ' . $data->errors[0]->message );
}
}
add_action( 'wp_ajax_cloudflare-stream-check-upload', 'action_wp_ajax_refresh_check_upload' );
/**
* AJAX method for initializing a video upload.
*
* @since 1.0.0
*/
function action_wp_ajax_query_cloudflare_stream_upload() {
check_ajax_referer( Cloudflare_Stream_Settings::NONCE, 'nonce' );
$api = Cloudflare_Stream_API::instance();
$data = $api->init_video();
wp_send_json_success( $data );
}
add_action( 'wp_ajax_query-cloudflare-stream-upload', 'action_wp_ajax_query_cloudflare_stream_upload' );
/**
* AJAX method for deleting a video.
*
* @since 1.0.0
*/
function action_wp_ajax_cloudflare_stream_delete() {
check_ajax_referer( Cloudflare_Stream_Settings::NONCE, 'nonce' );
$uid = isset( $_REQUEST['uid'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['uid'] ) ) : '';
$data = array();
$api = Cloudflare_Stream_API::instance();
$data = $api->delete_video( $uid );
wp_send_json_success( $data );
}
add_action( 'wp_ajax_cloudflare-stream-delete', 'action_wp_ajax_cloudflare_stream_delete' );
/**
* AJAX method for updating a video.
*
* @since 1.0.0
*/
function action_wp_ajax_cloudflare_stream_update() {
check_ajax_referer( Cloudflare_Stream_Settings::NONCE, 'nonce' );
$uid = isset( $_REQUEST['uid'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['uid'] ) ) : '';
$title = isset( $_REQUEST['title'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['title'] ) ) : '';
$upload = isset( $_REQUEST['upload'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['upload'] ) ) : '';
$data = array();
$args = array(
'uid' => $uid,
'meta' => array(
'name' => $title,
'upload' => $upload,
),
);
$api = Cloudflare_Stream_API::instance();
$data = $api->update_video_details( 'b9105cd434ea071e32690336969991cd', $args );
wp_send_json_success( $data );
}
add_action( 'wp_ajax_cloudflare-stream-update', 'action_wp_ajax_cloudflare_stream_update' );