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