1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class VideoPress_Gutenberg { |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Initialize the VideoPress Gutenberg extension |
7
|
|
|
*/ |
8
|
|
|
public static function init() { |
9
|
|
|
// Should not initialize if Gutenberg is not available |
10
|
|
|
if ( function_exists( 'register_block_type' ) ) { |
11
|
|
|
add_action( 'init', array( __CLASS__, 'register_video_block_with_videopress' ) ); |
12
|
|
|
} |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Register the Jetpack Gutenberg extension that adds VideoPress support to the core video block. |
17
|
|
|
*/ |
18
|
|
|
public static function register_video_block_with_videopress() { |
19
|
|
|
// We intentionally don't use `jetpack_register_block` because the current Jetpack extensions |
20
|
|
|
// registration doesn't fit our needs. Right now, any extension registered with `jetpack_register_block` |
21
|
|
|
// needs to have a name that is equal to the name of the registered block (our extension name would be |
22
|
|
|
// "videopress" and the name of the block we need to register "core/video"). |
23
|
|
|
register_block_type( 'core/video', array( |
24
|
|
|
'render_callback' => array( __CLASS__, 'render_video_block_with_videopress' ), |
25
|
|
|
) ); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Render the core video block replacing the src attribute with the VideoPress URL |
30
|
|
|
* |
31
|
|
|
* @param array $attributes Array containing the video block attributes. |
32
|
|
|
* @param string $content String containing the video block content. |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
public static function render_video_block_with_videopress( $attributes, $content ) { |
37
|
|
|
if ( ! isset( $attributes['id'] ) ) { |
38
|
|
|
return $content; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
View Code Duplication |
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
42
|
|
|
$blog_id = get_current_blog_id(); |
43
|
|
|
} else { |
44
|
|
|
$blog_id = Jetpack_Options::get_option( 'id' ); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$post_id = absint( $attributes['id'] ); |
48
|
|
|
$videopress_id = video_get_info_by_blogpostid( $blog_id, $post_id )->guid; |
49
|
|
|
$videopress_data = videopress_get_video_details( $videopress_id ); |
50
|
|
|
|
51
|
|
|
if ( empty( $videopress_data->file_url_base->https ) || empty( $videopress_data->files->hd->mp4 ) ) { |
52
|
|
|
return $content; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$videopress_url = $videopress_data->file_url_base->https . $videopress_data->files->hd->mp4; |
56
|
|
|
|
57
|
|
|
$pattern = '/(\s)src=([\'"])(?:(?!\2).)+?\2/'; |
58
|
|
|
|
59
|
|
|
return preg_replace( |
60
|
|
|
$pattern, |
61
|
|
|
sprintf( |
62
|
|
|
'\1src="%1$s"', |
63
|
|
|
esc_url_raw( $videopress_url ) |
64
|
|
|
), |
65
|
|
|
$content, |
66
|
|
|
1 |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
VideoPress_Gutenberg::init(); |
72
|
|
|
|