1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
2
|
|
|
/** |
3
|
|
|
* Block Editor functionality for VideoPress users. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
use Automattic\Jetpack\Assets; |
9
|
|
|
use Automattic\Jetpack\Blocks; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Register a VideoPress extension to replace the default Core Video block. |
13
|
|
|
*/ |
14
|
|
|
class VideoPress_Gutenberg { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Singleton |
18
|
|
|
*/ |
19
|
|
|
public static function init() { |
20
|
|
|
static $instance = false; |
21
|
|
|
|
22
|
|
|
if ( ! $instance ) { |
23
|
|
|
$instance = new VideoPress_Gutenberg(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
return $instance; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* VideoPress_Gutenberg constructor. |
31
|
|
|
* |
32
|
|
|
* Initialize the VideoPress Gutenberg extension |
33
|
|
|
*/ |
34
|
|
|
private function __construct() { |
35
|
|
|
add_action( 'init', array( $this, 'register_video_block_with_videopress' ) ); |
36
|
|
|
add_action( 'jetpack_register_gutenberg_extensions', array( $this, 'set_extension_availability' ) ); |
37
|
|
|
add_action( 'enqueue_block_editor_assets', array( $this, 'override_video_upload' ) ); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get site's ID. |
42
|
|
|
* |
43
|
|
|
* @return int $blog_id Site ID. |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
private static function get_blog_id() { |
46
|
|
|
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
47
|
|
|
return get_current_blog_id(); |
48
|
|
|
} elseif ( method_exists( 'Jetpack', 'is_active' ) && Jetpack::is_active() ) { |
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* We're intentionally not using `get_current_blog_id` because it was returning unexpected values. |
51
|
|
|
* |
52
|
|
|
* @see https://github.com/Automattic/jetpack/pull/11193#issuecomment-457883886 |
53
|
|
|
* @see https://github.com/Automattic/jetpack/pull/11193/commits/215cf789f3d8bd03ff9eb1bbdb693acb8831d273 |
54
|
|
|
*/ |
55
|
|
|
return Jetpack_Options::get_option( 'id' ); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return null; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Used to check whether VideoPress is enabled for given site. |
63
|
|
|
* |
64
|
|
|
* @todo Create a global `jetpack_check_module_availability( $module )` helper so we can re-use it on other modules. |
65
|
|
|
* This global helper should be created in a file synced with WordPress.com so we can use it there too. |
66
|
|
|
* @see https://github.com/Automattic/jetpack/pull/11321#discussion_r255477815 |
67
|
|
|
* |
68
|
|
|
* @return array Associative array indicating if the module is available (key `available`) and the reason why it is |
69
|
|
|
* unavailable (key `unavailable_reason`) |
70
|
|
|
*/ |
71
|
|
|
public function check_videopress_availability() { |
72
|
|
|
if ( |
73
|
|
|
defined( 'IS_WPCOM' ) && IS_WPCOM && |
74
|
|
|
function_exists( 'require_lib' ) |
75
|
|
|
) { |
76
|
|
|
require_lib( 'wpforteams' ); |
77
|
|
|
|
78
|
|
|
if ( WPForTeams\Workspace\is_part_of_active_workspace( self::get_blog_id() ) ) { |
79
|
|
|
return array( 'available' => true ); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// It is available on Simple Sites having the appropriate a plan. |
84
|
|
|
if ( |
85
|
|
|
defined( 'IS_WPCOM' ) && IS_WPCOM |
86
|
|
|
&& method_exists( 'Store_Product_List', 'get_site_specific_features_data' ) |
87
|
|
|
) { |
88
|
|
|
$features = Store_Product_List::get_site_specific_features_data(); |
89
|
|
|
if ( in_array( 'videopress', $features['active'], true ) ) { |
90
|
|
|
return array( 'available' => true ); |
91
|
|
|
} else { |
92
|
|
|
return array( |
93
|
|
|
'available' => false, |
94
|
|
|
'unavailable_reason' => 'missing_plan', |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// It is available on Jetpack Sites having the module active. |
100
|
|
|
if ( |
101
|
|
|
method_exists( 'Jetpack', 'is_connection_ready' ) && Jetpack::is_connection_ready() |
102
|
|
|
&& method_exists( 'Jetpack', 'is_module_active' ) |
103
|
|
|
&& method_exists( 'Jetpack_Plan', 'supports' ) |
104
|
|
|
) { |
105
|
|
|
if ( Jetpack::is_module_active( 'videopress' ) ) { |
106
|
|
|
return array( 'available' => true ); |
107
|
|
|
} elseif ( ! Jetpack_Plan::supports( 'videopress' ) ) { |
108
|
|
|
return array( |
109
|
|
|
'available' => false, |
110
|
|
|
'unavailable_reason' => 'missing_plan', |
111
|
|
|
); |
112
|
|
|
} else { |
113
|
|
|
return array( |
114
|
|
|
'available' => false, |
115
|
|
|
'unavailable_reason' => 'missing_module', |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return array( |
121
|
|
|
'available' => false, |
122
|
|
|
'unavailable_reason' => 'unknown', |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Set the Jetpack Gutenberg extension availability. |
128
|
|
|
*/ |
129
|
|
|
public function set_extension_availability() { |
130
|
|
|
$availability = $this->check_videopress_availability(); |
131
|
|
|
if ( $availability['available'] ) { |
132
|
|
|
Jetpack_Gutenberg::set_extension_available( 'jetpack/videopress' ); |
133
|
|
|
} else { |
134
|
|
|
Jetpack_Gutenberg::set_extension_unavailable( 'jetpack/videopress', $availability['unavailable_reason'] ); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Register the core video block as a dynamic block. |
140
|
|
|
* |
141
|
|
|
* It defines a server-side rendering that adds VideoPress support to the core video block. |
142
|
|
|
*/ |
143
|
|
|
public function register_video_block_with_videopress() { |
144
|
|
|
Blocks::jetpack_register_block( |
145
|
|
|
'core/video', |
146
|
|
|
array( |
147
|
|
|
'render_callback' => array( $this, 'render_video_block_with_videopress' ), |
148
|
|
|
) |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Render the core video block replacing the src attribute with the VideoPress URL |
154
|
|
|
* |
155
|
|
|
* @param array $attributes Array containing the video block attributes. |
156
|
|
|
* @param string $content String containing the video block content. |
157
|
|
|
* |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
|
|
public function render_video_block_with_videopress( $attributes, $content ) { |
161
|
|
|
if ( ! isset( $attributes['id'] ) || isset( $attributes['guid'] ) ) { |
162
|
|
|
return $content; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$blog_id = self::get_blog_id(); |
166
|
|
|
|
167
|
|
|
if ( ! isset( $blog_id ) ) { |
168
|
|
|
return $content; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$post_id = absint( $attributes['id'] ); |
172
|
|
|
$videopress_id = video_get_info_by_blogpostid( $blog_id, $post_id )->guid; |
173
|
|
|
$videopress_data = videopress_get_video_details( $videopress_id ); |
174
|
|
|
|
175
|
|
|
if ( empty( $videopress_data->file_url_base->https ) || empty( $videopress_data->files->hd->mp4 ) ) { |
176
|
|
|
return $content; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$videopress_url = $videopress_data->file_url_base->https . $videopress_data->files->hd->mp4; |
180
|
|
|
|
181
|
|
|
$pattern = '/(\s)src=([\'"])(?:(?!\2).)+?\2/'; |
182
|
|
|
|
183
|
|
|
return preg_replace( |
184
|
|
|
$pattern, |
185
|
|
|
sprintf( |
186
|
|
|
'\1src="%1$s"', |
187
|
|
|
esc_url_raw( $videopress_url ) |
188
|
|
|
), |
189
|
|
|
$content, |
190
|
|
|
1 |
191
|
|
|
); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Replaces the video uploaded in the block editor. |
196
|
|
|
* |
197
|
|
|
* Enqueues a script that registers an API fetch middleware replacing the video uploads in Gutenberg so they are |
198
|
|
|
* uploaded against the WP.com API media endpoint and thus transcoded by VideoPress. |
199
|
|
|
*/ |
200
|
|
|
public function override_video_upload() { |
201
|
|
|
// Bail if Jetpack is not connected or VideoPress module is not active. |
202
|
|
|
if ( ! Jetpack::is_connection_ready() || ! Jetpack::is_module_active( 'videopress' ) ) { |
203
|
|
|
return; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
wp_enqueue_script( |
207
|
|
|
'jetpack-videopress-gutenberg-override-video-upload', |
208
|
|
|
Assets::get_file_url_for_environment( |
209
|
|
|
'_inc/build/videopress/js/gutenberg-video-upload.min.js', |
210
|
|
|
'modules/videopress/js/gutenberg-video-upload.js' |
211
|
|
|
), |
212
|
|
|
array( 'wp-api-fetch', 'wp-polyfill', 'lodash' ), |
213
|
|
|
JETPACK__VERSION, |
214
|
|
|
false |
215
|
|
|
); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
VideoPress_Gutenberg::init(); |
220
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.