PluginProbe ʕ •ᴥ•ʔ
Jetpack – WP Security, Backup, Speed, & Growth / 16.1-beta.3
Jetpack – WP Security, Backup, Speed, & Growth v16.1-beta.3
16.2-a.3 16.1.2 16.2-a.1 16.1.1 16.1 16.1-beta 16.1-beta.2 16.1-beta.3 16.1-a.5 16.1-a.3 16.0.1 16.1-a.1 16.0 16.0-beta 16.0-a.7 16.0-a.5 15.9.1 16.0-a.3 16.0-a.1 15.9 15.9-beta 15.9-a.7 15.9-a.5 15.9-a.3 15.9-a.1 15.8 15.8-beta 15.8-a.7 15.8-a.5 5.2.5 5.3.4 5.4.4 5.5.5 5.6.5 5.7.5 5.8.4 5.9.4 6.0.4 6.1 6.1.1 6.1.2 6.1.3 6.1.4 6.1.5 6.2 6.2.1 6.2.2 6.2.3 6.2.4 6.2.5 6.3 6.3.1 6.3.2 6.3.3 6.3.4 6.3.5 6.3.6 6.3.7 6.4 6.4.1 6.4.2 6.4.3 6.4.4 6.4.5 6.4.6 6.5 6.5.1 6.5.2 6.5.3 6.5.4 6.6 6.6.1 6.6.2 6.6.3 6.6.4 6.6.5 6.7 6.7.1 6.7.2 6.7.3 6.7.4 6.8 6.8.1 6.8.2 6.8.3 6.8.4 6.8.5 6.9 6.9.1 6.9.2 6.9.3 6.9.4 7.0 7.0.1 7.0.2 7.0.3 7.0.4 7.0.5 7.1 7.1.1 7.1.2 7.1.3 7.1.4 7.1.5 7.2 7.2.1 7.2.1.1 7.2.2 7.2.3 7.2.4 7.2.5 7.3 7.3.0.1 7.3.1 7.3.1.1 7.3.2 7.3.3 7.3.4 7.3.5 7.4 7.4.1 7.4.2 7.4.3 7.4.4 7.4.5 7.5 7.5.0.1 7.5.1 7.5.2 7.5.3 7.5.4 7.5.5 7.5.6 7.5.7 7.6 7.6.1 7.6.2 7.6.3 7.6.4 7.7 7.7.1 7.7.2 7.7.3 7.7.4 7.7.5 7.7.6 7.8 7.8.1 7.8.2 7.8.3 7.8.4 7.9 7.9.1 7.9.2 7.9.3 7.9.4 8.0 8.0.1 8.0.2 8.0.3 8.1 8.1.1 8.1.2 8.1.3 8.1.4 8.2 8.2.0.1 8.2.1 8.2.2 8.2.3 8.2.4 8.2.5 8.2.6 8.3 8.3.1 8.3.2 8.3.3 8.4 8.4.1 8.4.2 8.4.3 8.4.4 8.4.5 8.5 8.5.1 8.5.2 8.5.3 8.6 8.6.1 8.6.2 8.6.3 8.6.4 8.7 8.7.0.1 8.7.1 8.7.2 8.7.3 8.7.4 8.8 8.8.1 8.8.2 8.8.3 8.8.4 8.8.5 8.9 8.9.1 8.9.2 8.9.3 8.9.4 9.0 9.0.1 9.0.2 9.0.3 9.0.4 9.0.5 9.1 9.1.1 9.1.2 9.1.3 9.2 9.2.1 9.2.2 9.2.3 9.2.4 9.3 9.3.1 9.3.2 9.3.3 9.3.4 9.3.5 9.4 9.4.1 9.4.2 9.4.3 9.4.4 9.5 9.5.1 9.5.2 9.5.3 9.5.4 9.5.5 9.6 9.6.1 9.6.2 9.6.3 9.6.4 9.7 9.7.1 9.7.2 15.7-beta.2 9.7.3 15.7.1 9.8 15.8-a.1 9.8.1 15.8-a.3 9.8.2 2.0.9 9.8.3 2.1.7 9.9 2.2.10 9.9.1 2.3.10 9.9.2 2.4.7 9.9.3 2.5.5 2.6.6 2.7.5 2.8.5 2.9.6 3.0.6 3.1.5 3.2.5 3.3.6 3.4.6 3.5.6 3.6.4 3.7.5 3.8.5 3.9.10 4.0.7 4.1.4 4.2.5 4.3.5 4.4.5 4.5.3 4.6.3 4.7.4 4.8.5 4.9.3 5.0.3 5.1.4 trunk 10.0 10.0.1 10.0.2 10.1 10.1.1 10.1.2 10.2 10.2.1 10.2.2 10.2.3 10.3 10.3.1 10.3.2 10.4 10.4.1 10.4.2 10.5 10.5.1 10.5.2 10.5.3 10.6 10.6.1 10.6.2 10.7 10.7.1 10.7.2 10.8 10.8.1 10.8.2 10.9 10.9.1 10.9.2 10.9.3 11.0 11.0.1 11.0.2 11.1 11.1.1 11.1.2 11.1.3 11.1.4 11.2 11.2.1 11.2.2 11.3 11.3.1 11.3.2 11.3.3 11.3.4 11.4 11.4.1 11.4.2 11.5 11.5.1 11.5.2 11.5.3 11.6 11.6.1 11.6.2 11.7 11.7.1 11.7.2 11.7.3 11.8 11.8.3 11.8.4 11.8.5 11.8.6 11.9 11.9.1 11.9.2 11.9.3 12.0 12.0.1 12.0.2 12.1 12.1.1 12.1.2 12.2 12.2.1 12.2.2 12.3 12.3.1 12.4 12.4.1 12.5 12.5.1 12.6 12.6.1 12.6.2 12.6.3 12.7 12.7.1 12.7.2 12.8 12.8.1 12.8.2 12.9 12.9.1 12.9.2 12.9.3 12.9.4 13.0 13.0.1 13.1 13.1.1 13.1.2 13.1.3 13.1.4 13.2 13.2.1 13.2.2 13.2.3 13.3 13.3.1 13.3.2 13.4 13.4.1 13.4.2 13.4.3 13.4.4 13.5 13.5.1 13.6 13.6.1 13.7 13.7.1 13.8 13.8.1 13.8.2 13.9 13.9.1 14.0 14.1 14.2 14.2.1 14.3 14.4 14.4.1 14.5 14.6 14.7 14.8 14.9 14.9.1 15.0 15.0.1 15.0.2 15.1 15.1.1 15.2 15.3 15.3.1 15.4 15.5 15.6 15.7 15.7-a.1 15.7-a.3 15.7-a.5 15.7-a.7 15.7-beta
jetpack / jetpack_vendor / automattic / jetpack-videopress / src / class-initializer.php
jetpack / jetpack_vendor / automattic / jetpack-videopress / src Last commit date
js 2 months ago tus 1 month ago videopress-divi 2 months ago videopress-divi-5 2 months ago class-access-control.php 3 months ago class-admin-ui.php 3 weeks ago class-ajax.php 1 month ago class-attachment-handler.php 1 month ago class-block-editor-content.php 1 month ago class-block-editor-extensions.php 1 month ago class-block-replacement.php 9 months ago class-caption-tracks.php 1 month ago class-data.php 1 month ago class-divi.php 2 months ago class-initial-state.php 1 month ago class-initializer.php 4 weeks ago class-jwt-token-bridge.php 2 years ago class-module-control.php 3 years ago class-options.php 2 years ago class-package-version.php 3 weeks ago class-plan.php 2 years ago class-rest-controller.php 1 month ago class-site.php 3 years ago class-stats.php 1 year ago class-status.php 9 months ago class-upload-exception.php 3 years ago class-uploader-rest-endpoints.php 9 months ago class-uploader.php 1 month ago class-utils.php 3 months ago class-video-block-email-renderer.php 2 months ago class-videopress-rest-api-v1-features.php 6 months ago class-videopress-rest-api-v1-settings.php 1 month ago class-videopress-rest-api-v1-site.php 2 years ago class-videopress-rest-api-v1-stats.php 3 years ago class-videopresstoken.php 1 month ago class-wpcom-rest-api-v2-attachment-field-videopress.php 9 months ago class-wpcom-rest-api-v2-attachment-videopress-data.php 1 month ago class-wpcom-rest-api-v2-endpoint-videopress-caption-tracks.php 1 month ago class-wpcom-rest-api-v2-endpoint-videopress.php 4 weeks ago class-xmlrpc.php 1 month ago utility-functions.php 4 weeks ago
class-initializer.php
521 lines
1 <?php
2 /**
3 * The initializer class for the videopress package
4 *
5 * @package automattic/jetpack-videopress
6 */
7
8 namespace Automattic\Jetpack\VideoPress;
9
10 /**
11 * Initialized the VideoPress package
12 */
13 class Initializer {
14
15 const JETPACK_VIDEOPRESS_IFRAME_API_HANDLER = 'jetpack-videopress-iframe-api';
16
17 /**
18 * Initialization optinos
19 *
20 * @var array
21 */
22 protected static $init_options = array();
23
24 /**
25 * Initializes the VideoPress package
26 *
27 * This method is called by Config::ensure.
28 *
29 * @return void
30 */
31 public static function init() {
32 if ( ! did_action( 'videopress_init' ) ) {
33
34 self::unconditional_initialization();
35
36 if ( Status::is_active() ) {
37 self::active_initialization();
38 } elseif ( self::should_initialize_admin_ui() ) {
39 // Keep "Jetpack > VideoPress" in the menu when the module is not
40 // active, linking to the My Jetpack interstitial to activate it.
41 Admin_UI::init_inactive_menu();
42 }
43 }
44
45 /**
46 * Fires after the VideoPress package is initialized
47 *
48 * @since 0.1.1
49 */
50 do_action( 'videopress_init' );
51 }
52
53 /**
54 * Update the initialization options
55 *
56 * This method is called by the Config class
57 *
58 * @param array $options The initialization options.
59 * @return void
60 */
61 public static function update_init_options( array $options ) {
62 if ( empty( $options['admin_ui'] ) || self::should_initialize_admin_ui() ) { // do not overwrite if already set to true.
63 return;
64 }
65
66 self::$init_options['admin_ui'] = $options['admin_ui'];
67 }
68
69 /**
70 * Checks the initialization options and returns whether the admin_ui should be initialized or not
71 *
72 * @return boolean
73 */
74 public static function should_initialize_admin_ui() {
75 return isset( self::$init_options['admin_ui'] ) && true === self::$init_options['admin_ui'];
76 }
77
78 /**
79 * Initialize VideoPress features that should be initialized whenever VideoPress is present, even if the module is not active
80 *
81 * @return void
82 */
83 private static function unconditional_initialization() {
84 if ( self::should_include_utilities() ) {
85 require_once __DIR__ . '/utility-functions.php';
86 }
87
88 // Set up package version hook.
89 add_filter( 'jetpack_package_versions', __NAMESPACE__ . '\Package_Version::send_package_version_to_tracker' );
90
91 Module_Control::init();
92
93 /*
94 * The WPCOM REST API v2 endpoints only register routes/fields on REST
95 * init, so defer constructing them (and autoloading their classes) until
96 * a REST request is actually served. Registered on both REST init hooks
97 * so the routes remain available in every context they were before, and
98 * guarded so the endpoints are instantiated only once per request.
99 */
100 $register_rest_api_v2_endpoints = static function () {
101 static $registered = false;
102 if ( $registered ) {
103 return;
104 }
105 $registered = true;
106 new WPCOM_REST_API_V2_Endpoint_VideoPress();
107 new WPCOM_REST_API_V2_Endpoint_VideoPress_Caption_Tracks();
108 new WPCOM_REST_API_V2_Attachment_VideoPress_Field();
109 new WPCOM_REST_API_V2_Attachment_VideoPress_Data();
110 };
111 add_action( 'rest_api_init', $register_rest_api_v2_endpoints, 0 );
112 add_action( 'restapi_theme_init', $register_rest_api_v2_endpoints, 0 );
113
114 if ( is_admin() ) {
115 AJAX::init();
116 } else {
117 require_once __DIR__ . '/class-block-replacement.php';
118 Block_Replacement::init();
119 }
120 }
121
122 /**
123 * This avoids conflicts when running VideoPress plugin with older versions of the Jetpack plugin
124 *
125 * On version 11.3-a.7 utility functions include were removed from the plugin and it is safe to include it from the package
126 *
127 * @return boolean
128 */
129 private static function should_include_utilities() {
130 if ( ! class_exists( 'Jetpack' ) || ! defined( 'JETPACK__VERSION' ) ) {
131 return true;
132 }
133
134 return version_compare( JETPACK__VERSION, '11.3-a.7', '>=' );
135 }
136
137 /**
138 * Initialize VideoPress features that should be initialized only when the module is active
139 *
140 * @return void
141 */
142 private static function active_initialization() {
143 Attachment_Handler::init();
144 Jwt_Token_Bridge::init();
145 Caption_Tracks::init();
146 Initial_State::init();
147 XMLRPC::init();
148 Block_Editor_Content::init();
149
150 /*
151 * These endpoints only add their routes on REST init, so defer calling
152 * init() (and autoloading the endpoint classes) until a REST request is
153 * served. Priority 0 ensures the routes still register before the
154 * default-priority rest_api_init handlers run. Class-name strings are
155 * used so the classes are not autoloaded on non-REST requests.
156 */
157 foreach (
158 array(
159 Uploader_Rest_Endpoints::class,
160 Rest_Controller::class,
161 VideoPress_Rest_Api_V1_Stats::class,
162 VideoPress_Rest_Api_V1_Site::class,
163 VideoPress_Rest_Api_V1_Settings::class,
164 VideoPress_Rest_Api_V1_Features::class,
165 ) as $rest_endpoint
166 ) {
167 add_action( 'rest_api_init', array( $rest_endpoint, 'init' ), 0 );
168 }
169 self::register_oembed_providers();
170
171 // Enqueuethe VideoPress Iframe API script in the front-end.
172 add_filter( 'embed_oembed_html', array( __CLASS__, 'enqueue_videopress_iframe_api_script' ), 10, 4 );
173
174 if ( self::should_initialize_admin_ui() ) {
175 Admin_UI::init();
176 }
177
178 Divi::init();
179 }
180
181 /**
182 * Explicitly register VideoPress oembed provider for patterns not supported by core
183 *
184 * @return void
185 */
186 public static function register_oembed_providers() {
187 $host = rawurlencode( home_url() );
188 // videopress.com/v is already registered in core.
189 // By explicitly declaring the provider here, we can speed things up by not relying on oEmbed discovery.
190 wp_oembed_add_provider( '#^https?://video.wordpress.com/v/.*#', 'https://public-api.wordpress.com/oembed/?for=' . $host, true );
191 // This is needed as it's not supported in oEmbed discovery
192 wp_oembed_add_provider( '|^https?://v\.wordpress\.com/([a-zA-Z\d]{8})(.+)?$|i', 'https://public-api.wordpress.com/oembed/?for=' . $host, true ); // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledInText
193
194 add_filter( 'embed_oembed_html', array( __CLASS__, 'video_enqueue_bridge_when_oembed_present' ), 10, 4 );
195 }
196
197 /**
198 * Enqueues VideoPress token bridge when a VideoPress oembed is present on the current page.
199 *
200 * @param string|false $cache The cached HTML result, stored in post meta.
201 * @param string $url The attempted embed URL.
202 * @param array $attr An array of shortcode attributes.
203 * @param int $post_ID Post ID.
204 *
205 * @return string|false
206 */
207 public static function video_enqueue_bridge_when_oembed_present( $cache, $url, $attr, $post_ID = null ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
208 if ( Utils::is_videopress_url( $url ) ) {
209 Jwt_Token_Bridge::enqueue_jwt_token_bridge();
210 }
211
212 return $cache;
213 }
214
215 /**
216 * Register all VideoPress blocks
217 *
218 * @return void
219 */
220 public static function register_videopress_blocks() {
221 // Register VideoPress Video block.
222 self::register_videopress_video_block();
223 }
224
225 /**
226 * VideoPress video block render method
227 *
228 * @global \WP_Embed $wp_embed WordPress embed handler.
229 *
230 * @param array $block_attributes Block attributes.
231 * @param string $content Current block markup.
232 * @param \WP_Block $block Current block.
233 *
234 * @return string Block markup.
235 */
236 public static function render_videopress_video_block( $block_attributes, $content, $block ) {
237 global $wp_embed;
238
239 // CSS classes.
240 $align = $block_attributes['align'] ?? null;
241 $align_class = $align ? ' align' . $align : '';
242 $custom_class = isset( $block_attributes['className'] ) ? ' ' . $block_attributes['className'] : '';
243 $classes = 'wp-block-jetpack-videopress jetpack-videopress-player' . $custom_class . $align_class;
244
245 // Inline style.
246 $style = '';
247 $max_width = $block_attributes['maxWidth'] ?? null;
248
249 if ( $max_width && $max_width !== '100%' ) {
250 $style = sprintf( 'max-width: %s;', $max_width );
251 $classes .= ' wp-block-jetpack-videopress--has-max-width';
252 }
253
254 /*
255 * <figcaption /> element
256 * Caption is stored into the block attributes,
257 * but also it was stored into the <figcaption /> element,
258 * meaning that it could be stored in two different places.
259 */
260 $figcaption = '';
261
262 // Caption from block attributes.
263 $caption = $block_attributes['caption'] ?? null;
264
265 /*
266 * If the caption is not stored into the block attributes,
267 * try to get it from the <figcaption /> element.
268 */
269 if ( $caption === null ) {
270 preg_match( '/<figcaption>(.*?)<\/figcaption>/', $content, $matches );
271 $caption = $matches[1] ?? null;
272 }
273
274 // If we have a caption, create the <figcaption /> element.
275 if ( $caption !== null ) {
276 $figcaption = sprintf( '<figcaption>%s</figcaption>', wp_kses_post( $caption ) );
277 }
278
279 // Custom anchor from block content.
280 $id_attribute = '';
281
282 // Try to get the custom anchor from the block attributes.
283 if ( isset( $block_attributes['anchor'] ) && $block_attributes['anchor'] ) {
284 $id_attribute = sprintf( 'id="%s"', esc_attr( $block_attributes['anchor'] ) );
285 } elseif ( preg_match( '/<figure[^>]*id="([^"]+)"/', $content, $matches ) ) {
286 // Otherwise, try to get the custom anchor from the <figure /> element.
287 $id_attribute = sprintf( 'id="%s"', esc_attr( $matches[1] ) );
288 }
289
290 // Preview On Hover data.
291 $is_poh_enabled =
292 isset( $block_attributes['posterData']['previewOnHover'] ) &&
293 $block_attributes['posterData']['previewOnHover'];
294
295 $autoplay = $block_attributes['autoplay'] ?? false;
296 $controls = $block_attributes['controls'] ?? false;
297 $poster = $block_attributes['posterData']['url'] ?? null;
298
299 $preview_on_hover = '';
300
301 if ( $is_poh_enabled ) {
302 $preview_on_hover = array(
303 'previewAtTime' => $block_attributes['posterData']['previewAtTime'],
304 'previewLoopDuration' => $block_attributes['posterData']['previewLoopDuration'],
305 'autoplay' => $autoplay,
306 'showControls' => $controls,
307 );
308
309 // Create inline style in case video has a custom poster.
310 $inline_style = '';
311 if ( $poster ) {
312 $inline_style = sprintf(
313 'style="background-image: url(%s); background-size: cover; background-position: center center;"',
314 esc_attr( $poster )
315 );
316 }
317
318 // Expose the preview on hover data to the client.
319 $preview_on_hover = sprintf(
320 '<div class="jetpack-videopress-player__overlay" %s></div><script type="application/json">%s</script>',
321 $inline_style,
322 wp_json_encode( $preview_on_hover, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP )
323 );
324
325 // Set `autoplay` and `muted` attributes to the video element.
326 $block_attributes['autoplay'] = true;
327 $block_attributes['muted'] = true;
328 }
329
330 $figure_template = '
331 <figure class="%1$s" style="%2$s" %3$s>
332 %4$s
333 %5$s
334 %6$s
335 </figure>
336 ';
337
338 // VideoPress URL.
339 $guid = $block_attributes['guid'] ?? null;
340 $videopress_url = Utils::get_video_press_url( $guid, $block_attributes );
341
342 $video_wrapper = '';
343 $video_wrapper_classes = 'jetpack-videopress-player__wrapper';
344
345 if ( $videopress_url ) {
346 $videopress_url = wp_kses_post( $videopress_url );
347
348 /*
349 * Provide a fallback iframe for when the oEmbed endpoint fails, e.g.
350 * when the VideoPress backend isn't ready for a freshly uploaded video.
351 * This prevents the published page from showing a bare link.
352 */
353 $fallback = function ( $output, $url ) use ( $videopress_url ) {
354 if ( $url !== html_entity_decode( $videopress_url, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ) ) {
355 return $output;
356 }
357
358 return sprintf(
359 '<iframe title="%1$s" aria-label="%1$s" src="%2$s" width="640" height="360" allowfullscreen data-resize-to-parent="true" allow="clipboard-write; presentation"></iframe>',
360 esc_attr__( 'VideoPress Video Player', 'jetpack-videopress-pkg' ),
361 esc_url( preg_replace( '#/v/#', '/embed/', $url, 1 ) )
362 );
363 };
364
365 add_filter( 'embed_maybe_make_link', $fallback, 10, 2 );
366 $oembed_html = apply_filters( 'video_embed_html', $wp_embed->shortcode( array(), $videopress_url ) );
367 remove_filter( 'embed_maybe_make_link', $fallback );
368
369 $video_wrapper = sprintf(
370 '<div class="%s">%s %s</div>',
371 $video_wrapper_classes,
372 $preview_on_hover,
373 $oembed_html
374 );
375
376 /*
377 * Self-heal failed oEmbed cache for VideoPress URLs.
378 *
379 * When the VideoPress backend isn't ready for a freshly uploaded video,
380 * WordPress caches '{{unknown}}' in post meta with a TTL that is too long
381 * for this use case. Clear recent failures so the next page render retries
382 * oEmbed discovery, keeping the fallback iframe above temporary.
383 */
384 $post_id = $block->context['postId'] ?? get_the_ID();
385
386 if ( $post_id ) {
387 $key_suffix = md5( $videopress_url . serialize( wp_embed_defaults( $videopress_url ) ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize -- Matching WP_Embed cache key format.
388 $oembed_value = get_post_meta( $post_id, '_oembed_' . $key_suffix, true );
389 $oembed_time = (int) get_post_meta( $post_id, '_oembed_time_' . $key_suffix, true );
390
391 /*
392 * Only clear the '{{unknown}}' cache entry when it is recent, to avoid
393 * disabling WordPress's oEmbed backoff for persistent provider failures.
394 */
395 if (
396 '{{unknown}}' === $oembed_value
397 && ( ! $oembed_time || ( time() - $oembed_time ) < MINUTE_IN_SECONDS )
398 ) {
399 delete_post_meta( $post_id, '_oembed_' . $key_suffix );
400 delete_post_meta( $post_id, '_oembed_time_' . $key_suffix );
401 }
402 }
403 }
404
405 // Get premium content from block context.
406 $premium_block_plan_id = isset( $block->context['premium-content/planId'] ) ? intval( $block->context['premium-content/planId'] ) : 0;
407 $is_premium_content_child = isset( $block->context['isPremiumContentChild'] ) ? (bool) $block->context['isPremiumContentChild'] : false;
408 $maybe_premium_script = '';
409 if ( $is_premium_content_child ) {
410 Access_Control::instance()->set_guid_subscription( $guid, $premium_block_plan_id );
411 $escaped_guid = wp_json_encode( $guid, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP );
412 $script_content = "if ( ! window.__guidsToPlanIds ) { window.__guidsToPlanIds = {}; }; window.__guidsToPlanIds[$escaped_guid] = $premium_block_plan_id;";
413 $maybe_premium_script = '<script>' . $script_content . '</script>';
414 }
415
416 // $id_attribute, $video_wrapper, $figcaption properly escaped earlier in the code.
417 return sprintf(
418 $figure_template,
419 esc_attr( $classes ),
420 esc_attr( $style ),
421 $id_attribute,
422 $video_wrapper,
423 $figcaption,
424 $maybe_premium_script
425 );
426 }
427
428 /**
429 * Register the VideoPress block editor block,
430 * AKA "VideoPress Block v6".
431 *
432 * @return void
433 */
434 public static function register_videopress_video_block() {
435 /*
436 * If only Jetpack is active, and if the VideoPress module is not active,
437 * we can register the block just to display a placeholder to turn on the module.
438 * That invitation is only useful for admins though.
439 */
440 if (
441 Status::is_jetpack_plugin_without_videopress_module_active()
442 && ! Status::is_standalone_plugin_active()
443 && ! current_user_can( 'jetpack_activate_modules' )
444 ) {
445 return;
446 }
447
448 $videopress_video_metadata_file = __DIR__ . '/../build/block-editor/blocks/video/block.json';
449 $videopress_video_metadata_file_exists = file_exists( $videopress_video_metadata_file );
450 if ( ! $videopress_video_metadata_file_exists ) {
451 return;
452 }
453
454 $videopress_video_metadata = json_decode(
455 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
456 file_get_contents( $videopress_video_metadata_file )
457 );
458
459 // Pick the block name straight from the block metadata .json file.
460 $videopress_video_block_name = $videopress_video_metadata->name;
461
462 // Is the block already registered?
463 $is_block_registered = \WP_Block_Type_Registry::get_instance()->is_registered( $videopress_video_block_name );
464
465 // Do not register if the block is already registered.
466 if ( $is_block_registered ) {
467 return;
468 }
469
470 $registration = register_block_type(
471 $videopress_video_metadata_file,
472 array(
473 'render_callback' => array( __CLASS__, 'render_videopress_video_block' ),
474 'render_email_callback' => array( Video_Block_Email_Renderer::class, 'render' ),
475 'uses_context' => array( 'premium-content/planId', 'isPremiumContentChild', 'selectedPlanId' ),
476 )
477 );
478
479 // Do not enqueue scripts if the block could not be registered.
480 if ( empty( $registration ) || empty( $registration->editor_script_handles ) ) {
481 return;
482 }
483
484 // Extensions use Connection_Initial_State::render_script with script handle as parameter.
485 if ( is_array( $registration->editor_script_handles ) ) {
486 $script_handle = $registration->editor_script_handles[0];
487 } else {
488 $script_handle = $registration->editor_script_handles;
489 }
490
491 // Register and enqueue scripts used by the VideoPress video block.
492 Block_Editor_Extensions::init( $script_handle );
493 }
494
495 /**
496 * Enqueue the VideoPress Iframe API script
497 * when the URL of oEmbed HTML is a VideoPress URL.
498 *
499 * @param string|false $cache The cached HTML result, stored in post meta.
500 * @param string $url The attempted embed URL.
501 * @param array $attr An array of shortcode attributes.
502 * @param int $post_ID Post ID.
503 *
504 * @return string|false
505 */
506 public static function enqueue_videopress_iframe_api_script( $cache, $url, $attr, $post_ID ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
507 if ( Utils::is_videopress_url( $url ) ) {
508 // Enqueue the VideoPress IFrame API in the front-end.
509 wp_enqueue_script(
510 self::JETPACK_VIDEOPRESS_IFRAME_API_HANDLER,
511 'https://s0.wp.com/wp-content/plugins/video/assets/js/videojs/videopress-iframe-api.js',
512 array(),
513 gmdate( 'YW' ),
514 false
515 );
516 }
517
518 return $cache;
519 }
520 }
521