Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
62.76% covered (warning)
62.76%
123 / 196
33.33% covered (danger)
33.33%
4 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
Initializer
62.76% covered (warning)
62.76%
123 / 196
33.33% covered (danger)
33.33%
4 / 12
211.29
0.00% covered (danger)
0.00%
0 / 1
 init
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 update_init_options
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 should_initialize_admin_ui
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 unconditional_initialization
89.47% covered (warning)
89.47%
17 / 19
0.00% covered (danger)
0.00%
0 / 1
4.02
 should_include_utilities
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
3.33
 active_initialization
94.74% covered (success)
94.74%
18 / 19
0.00% covered (danger)
0.00%
0 / 1
3.00
 register_oembed_providers
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 video_enqueue_bridge_when_oembed_present
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 register_videopress_blocks
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 render_videopress_video_block
73.00% covered (warning)
73.00%
73 / 100
0.00% covered (danger)
0.00%
0 / 1
31.53
 register_videopress_video_block
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
90
 enqueue_videopress_iframe_api_script
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * The initializer class for the videopress package
4 *
5 * @package automattic/jetpack-videopress
6 */
7
8namespace Automattic\Jetpack\VideoPress;
9
10/**
11 * Initialized the VideoPress package
12 */
13class 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            }
39        }
40
41        /**
42         * Fires after the VideoPress package is initialized
43         *
44         * @since 0.1.1
45         */
46        do_action( 'videopress_init' );
47    }
48
49    /**
50     * Update the initialization options
51     *
52     * This method is called by the Config class
53     *
54     * @param array $options The initialization options.
55     * @return void
56     */
57    public static function update_init_options( array $options ) {
58        if ( empty( $options['admin_ui'] ) || self::should_initialize_admin_ui() ) { // do not overwrite if already set to true.
59            return;
60        }
61
62        self::$init_options['admin_ui'] = $options['admin_ui'];
63    }
64
65    /**
66     * Checks the initialization options and returns whether the admin_ui should be initialized or not
67     *
68     * @return boolean
69     */
70    public static function should_initialize_admin_ui() {
71        return isset( self::$init_options['admin_ui'] ) && true === self::$init_options['admin_ui'];
72    }
73
74    /**
75     * Initialize VideoPress features that should be initialized whenever VideoPress is present, even if the module is not active
76     *
77     * @return void
78     */
79    private static function unconditional_initialization() {
80        if ( self::should_include_utilities() ) {
81            require_once __DIR__ . '/utility-functions.php';
82        }
83
84        // Set up package version hook.
85        add_filter( 'jetpack_package_versions', __NAMESPACE__ . '\Package_Version::send_package_version_to_tracker' );
86
87        Module_Control::init();
88
89        /*
90         * The WPCOM REST API v2 endpoints only register routes/fields on REST
91         * init, so defer constructing them (and autoloading their classes) until
92         * a REST request is actually served. Registered on both REST init hooks
93         * so the routes remain available in every context they were before, and
94         * guarded so the endpoints are instantiated only once per request.
95         */
96        $register_rest_api_v2_endpoints = static function () {
97            static $registered = false;
98            if ( $registered ) {
99                return;
100            }
101            $registered = true;
102            new WPCOM_REST_API_V2_Endpoint_VideoPress();
103            new WPCOM_REST_API_V2_Attachment_VideoPress_Field();
104            new WPCOM_REST_API_V2_Attachment_VideoPress_Data();
105        };
106        add_action( 'rest_api_init', $register_rest_api_v2_endpoints, 0 );
107        add_action( 'restapi_theme_init', $register_rest_api_v2_endpoints, 0 );
108
109        if ( is_admin() ) {
110            AJAX::init();
111        } else {
112            require_once __DIR__ . '/class-block-replacement.php';
113            Block_Replacement::init();
114        }
115    }
116
117    /**
118     * This avoids conflicts when running VideoPress plugin with older versions of the Jetpack plugin
119     *
120     * On version 11.3-a.7 utility functions include were removed from the plugin and it is safe to include it from the package
121     *
122     * @return boolean
123     */
124    private static function should_include_utilities() {
125        if ( ! class_exists( 'Jetpack' ) || ! defined( 'JETPACK__VERSION' ) ) {
126            return true;
127        }
128
129        return version_compare( JETPACK__VERSION, '11.3-a.7', '>=' );
130    }
131
132    /**
133     * Initialize VideoPress features that should be initialized only when the module is active
134     *
135     * @return void
136     */
137    private static function active_initialization() {
138        Attachment_Handler::init();
139        Jwt_Token_Bridge::init();
140        Initial_State::init();
141        XMLRPC::init();
142        Block_Editor_Content::init();
143
144        /*
145         * These endpoints only add their routes on REST init, so defer calling
146         * init() (and autoloading the endpoint classes) until a REST request is
147         * served. Priority 0 ensures the routes still register before the
148         * default-priority rest_api_init handlers run. Class-name strings are
149         * used so the classes are not autoloaded on non-REST requests.
150         */
151        foreach (
152            array(
153                Uploader_Rest_Endpoints::class,
154                Rest_Controller::class,
155                VideoPress_Rest_Api_V1_Stats::class,
156                VideoPress_Rest_Api_V1_Site::class,
157                VideoPress_Rest_Api_V1_Settings::class,
158                VideoPress_Rest_Api_V1_Features::class,
159            ) as $rest_endpoint
160        ) {
161            add_action( 'rest_api_init', array( $rest_endpoint, 'init' ), 0 );
162        }
163        self::register_oembed_providers();
164
165        // Enqueuethe VideoPress Iframe API script in the front-end.
166        add_filter( 'embed_oembed_html', array( __CLASS__, 'enqueue_videopress_iframe_api_script' ), 10, 4 );
167
168        if ( self::should_initialize_admin_ui() ) {
169            Admin_UI::init();
170        }
171
172        Divi::init();
173    }
174
175    /**
176     * Explicitly register VideoPress oembed provider for patterns not supported by core
177     *
178     * @return void
179     */
180    public static function register_oembed_providers() {
181        $host = rawurlencode( home_url() );
182        // videopress.com/v is already registered in core.
183        // By explicitly declaring the provider here, we can speed things up by not relying on oEmbed discovery.
184        wp_oembed_add_provider( '#^https?://video.wordpress.com/v/.*#', 'https://public-api.wordpress.com/oembed/?for=' . $host, true );
185        // This is needed as it's not supported in oEmbed discovery
186        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
187
188        add_filter( 'embed_oembed_html', array( __CLASS__, 'video_enqueue_bridge_when_oembed_present' ), 10, 4 );
189    }
190
191    /**
192     * Enqueues VideoPress token bridge when a VideoPress oembed is present on the current page.
193     *
194     * @param string|false $cache   The cached HTML result, stored in post meta.
195     * @param string       $url     The attempted embed URL.
196     * @param array        $attr    An array of shortcode attributes.
197     * @param int          $post_ID Post ID.
198     *
199     * @return string|false
200     */
201    public static function video_enqueue_bridge_when_oembed_present( $cache, $url, $attr, $post_ID = null ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
202        if ( Utils::is_videopress_url( $url ) ) {
203            Jwt_Token_Bridge::enqueue_jwt_token_bridge();
204        }
205
206        return $cache;
207    }
208
209    /**
210     * Register all VideoPress blocks
211     *
212     * @return void
213     */
214    public static function register_videopress_blocks() {
215        // Register VideoPress Video block.
216        self::register_videopress_video_block();
217    }
218
219    /**
220     * VideoPress video block render method
221     *
222     * @global \WP_Embed $wp_embed WordPress embed handler.
223     *
224     * @param array     $block_attributes Block attributes.
225     * @param string    $content          Current block markup.
226     * @param \WP_Block $block            Current block.
227     *
228     * @return string Block markup.
229     */
230    public static function render_videopress_video_block( $block_attributes, $content, $block ) {
231        global $wp_embed;
232
233        // CSS classes.
234        $align        = $block_attributes['align'] ?? null;
235        $align_class  = $align ? ' align' . $align : '';
236        $custom_class = isset( $block_attributes['className'] ) ? ' ' . $block_attributes['className'] : '';
237        $classes      = 'wp-block-jetpack-videopress jetpack-videopress-player' . $custom_class . $align_class;
238
239        // Inline style.
240        $style     = '';
241        $max_width = $block_attributes['maxWidth'] ?? null;
242
243        if ( $max_width && $max_width !== '100%' ) {
244            $style    = sprintf( 'max-width: %s;', $max_width );
245            $classes .= ' wp-block-jetpack-videopress--has-max-width';
246        }
247
248        /*
249         * <figcaption /> element
250         * Caption is stored into the block attributes,
251         * but also it was stored into the <figcaption /> element,
252         * meaning that it could be stored in two different places.
253         */
254        $figcaption = '';
255
256        // Caption from block attributes.
257        $caption = $block_attributes['caption'] ?? null;
258
259        /*
260         * If the caption is not stored into the block attributes,
261         * try to get it from the <figcaption /> element.
262         */
263        if ( $caption === null ) {
264            preg_match( '/<figcaption>(.*?)<\/figcaption>/', $content, $matches );
265            $caption = $matches[1] ?? null;
266        }
267
268        // If we have a caption, create the <figcaption /> element.
269        if ( $caption !== null ) {
270            $figcaption = sprintf( '<figcaption>%s</figcaption>', wp_kses_post( $caption ) );
271        }
272
273        // Custom anchor from block content.
274        $id_attribute = '';
275
276        // Try to get the custom anchor from the block attributes.
277        if ( isset( $block_attributes['anchor'] ) && $block_attributes['anchor'] ) {
278            $id_attribute = sprintf( 'id="%s"', esc_attr( $block_attributes['anchor'] ) );
279        } elseif ( preg_match( '/<figure[^>]*id="([^"]+)"/', $content, $matches ) ) {
280            // Otherwise, try to get the custom anchor from the <figure /> element.
281            $id_attribute = sprintf( 'id="%s"', esc_attr( $matches[1] ) );
282        }
283
284        // Preview On Hover data.
285        $is_poh_enabled =
286            isset( $block_attributes['posterData']['previewOnHover'] ) &&
287            $block_attributes['posterData']['previewOnHover'];
288
289        $autoplay = $block_attributes['autoplay'] ?? false;
290        $controls = $block_attributes['controls'] ?? false;
291        $poster   = $block_attributes['posterData']['url'] ?? null;
292
293        $preview_on_hover = '';
294
295        if ( $is_poh_enabled ) {
296            $preview_on_hover = array(
297                'previewAtTime'       => $block_attributes['posterData']['previewAtTime'],
298                'previewLoopDuration' => $block_attributes['posterData']['previewLoopDuration'],
299                'autoplay'            => $autoplay,
300                'showControls'        => $controls,
301            );
302
303            // Create inline style in case video has a custom poster.
304            $inline_style = '';
305            if ( $poster ) {
306                $inline_style = sprintf(
307                    'style="background-image: url(%s); background-size: cover; background-position: center center;"',
308                    esc_attr( $poster )
309                );
310            }
311
312            // Expose the preview on hover data to the client.
313            $preview_on_hover = sprintf(
314                '<div class="jetpack-videopress-player__overlay" %s></div><script type="application/json">%s</script>',
315                $inline_style,
316                wp_json_encode( $preview_on_hover, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP )
317            );
318
319            // Set `autoplay` and `muted` attributes to the video element.
320            $block_attributes['autoplay'] = true;
321            $block_attributes['muted']    = true;
322        }
323
324        $figure_template = '
325        <figure class="%1$s" style="%2$s" %3$s>
326            %4$s
327            %5$s
328            %6$s
329        </figure>
330        ';
331
332        // VideoPress URL.
333        $guid           = $block_attributes['guid'] ?? null;
334        $videopress_url = Utils::get_video_press_url( $guid, $block_attributes );
335
336        $video_wrapper         = '';
337        $video_wrapper_classes = 'jetpack-videopress-player__wrapper';
338
339        if ( $videopress_url ) {
340            $videopress_url = wp_kses_post( $videopress_url );
341
342            /*
343             * Provide a fallback iframe for when the oEmbed endpoint fails, e.g.
344             * when the VideoPress backend isn't ready for a freshly uploaded video.
345             * This prevents the published page from showing a bare link.
346             */
347            $fallback = function ( $output, $url ) use ( $videopress_url ) {
348                if ( $url !== html_entity_decode( $videopress_url, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ) ) {
349                    return $output;
350                }
351
352                return sprintf(
353                    '<iframe title="%1$s" aria-label="%1$s" src="%2$s" width="640" height="360" allowfullscreen data-resize-to-parent="true" allow="clipboard-write"></iframe>',
354                    esc_attr__( 'VideoPress Video Player', 'jetpack-videopress-pkg' ),
355                    esc_url( preg_replace( '#/v/#', '/embed/', $url, 1 ) )
356                );
357            };
358
359            add_filter( 'embed_maybe_make_link', $fallback, 10, 2 );
360            $oembed_html = apply_filters( 'video_embed_html', $wp_embed->shortcode( array(), $videopress_url ) );
361            remove_filter( 'embed_maybe_make_link', $fallback );
362
363            $video_wrapper = sprintf(
364                '<div class="%s">%s %s</div>',
365                $video_wrapper_classes,
366                $preview_on_hover,
367                $oembed_html
368            );
369
370            /*
371             * Self-heal failed oEmbed cache for VideoPress URLs.
372             *
373             * When the VideoPress backend isn't ready for a freshly uploaded video,
374             * WordPress caches '{{unknown}}' in post meta with a TTL that is too long
375             * for this use case. Clear recent failures so the next page render retries
376             * oEmbed discovery, keeping the fallback iframe above temporary.
377             */
378            $post_id = $block->context['postId'] ?? get_the_ID();
379
380            if ( $post_id ) {
381                $key_suffix   = md5( $videopress_url . serialize( wp_embed_defaults( $videopress_url ) ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize -- Matching WP_Embed cache key format.
382                $oembed_value = get_post_meta( $post_id, '_oembed_' . $key_suffix, true );
383                $oembed_time  = (int) get_post_meta( $post_id, '_oembed_time_' . $key_suffix, true );
384
385                /*
386                 * Only clear the '{{unknown}}' cache entry when it is recent, to avoid
387                 * disabling WordPress's oEmbed backoff for persistent provider failures.
388                 */
389                if (
390                    '{{unknown}}' === $oembed_value
391                    && ( ! $oembed_time || ( time() - $oembed_time ) < MINUTE_IN_SECONDS )
392                ) {
393                    delete_post_meta( $post_id, '_oembed_' . $key_suffix );
394                    delete_post_meta( $post_id, '_oembed_time_' . $key_suffix );
395                }
396            }
397        }
398
399        // Get premium content from block context.
400        $premium_block_plan_id    = isset( $block->context['premium-content/planId'] ) ? intval( $block->context['premium-content/planId'] ) : 0;
401        $is_premium_content_child = isset( $block->context['isPremiumContentChild'] ) ? (bool) $block->context['isPremiumContentChild'] : false;
402        $maybe_premium_script     = '';
403        if ( $is_premium_content_child ) {
404            Access_Control::instance()->set_guid_subscription( $guid, $premium_block_plan_id );
405            $escaped_guid         = wp_json_encode( $guid, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP );
406            $script_content       = "if ( ! window.__guidsToPlanIds ) { window.__guidsToPlanIds = {}; }; window.__guidsToPlanIds[$escaped_guid] = $premium_block_plan_id;";
407            $maybe_premium_script = '<script>' . $script_content . '</script>';
408        }
409
410        // $id_attribute, $video_wrapper, $figcaption properly escaped earlier in the code.
411        return sprintf(
412            $figure_template,
413            esc_attr( $classes ),
414            esc_attr( $style ),
415            $id_attribute,
416            $video_wrapper,
417            $figcaption,
418            $maybe_premium_script
419        );
420    }
421
422    /**
423     * Register the VideoPress block editor block,
424     * AKA "VideoPress Block v6".
425     *
426     * @return void
427     */
428    public static function register_videopress_video_block() {
429        /*
430         * If only Jetpack is active, and if the VideoPress module is not active,
431         * we can register the block just to display a placeholder to turn on the module.
432         * That invitation is only useful for admins though.
433         */
434        if (
435            Status::is_jetpack_plugin_without_videopress_module_active()
436            && ! Status::is_standalone_plugin_active()
437            && ! current_user_can( 'jetpack_activate_modules' )
438        ) {
439            return;
440        }
441
442        $videopress_video_metadata_file        = __DIR__ . '/../build/block-editor/blocks/video/block.json';
443        $videopress_video_metadata_file_exists = file_exists( $videopress_video_metadata_file );
444        if ( ! $videopress_video_metadata_file_exists ) {
445            return;
446        }
447
448        $videopress_video_metadata = json_decode(
449            // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
450            file_get_contents( $videopress_video_metadata_file )
451        );
452
453        // Pick the block name straight from the block metadata .json file.
454        $videopress_video_block_name = $videopress_video_metadata->name;
455
456        // Is the block already registered?
457        $is_block_registered = \WP_Block_Type_Registry::get_instance()->is_registered( $videopress_video_block_name );
458
459        // Do not register if the block is already registered.
460        if ( $is_block_registered ) {
461            return;
462        }
463
464        $registration = register_block_type(
465            $videopress_video_metadata_file,
466            array(
467                'render_callback'       => array( __CLASS__, 'render_videopress_video_block' ),
468                'render_email_callback' => array( Video_Block_Email_Renderer::class, 'render' ),
469                'uses_context'          => array( 'premium-content/planId', 'isPremiumContentChild', 'selectedPlanId' ),
470            )
471        );
472
473        // Do not enqueue scripts if the block could not be registered.
474        if ( empty( $registration ) || empty( $registration->editor_script_handles ) ) {
475            return;
476        }
477
478        // Extensions use Connection_Initial_State::render_script with script handle as parameter.
479        if ( is_array( $registration->editor_script_handles ) ) {
480            $script_handle = $registration->editor_script_handles[0];
481        } else {
482            $script_handle = $registration->editor_script_handles;
483        }
484
485        // Register and enqueue scripts used by the VideoPress video block.
486        Block_Editor_Extensions::init( $script_handle );
487    }
488
489    /**
490     * Enqueue the VideoPress Iframe API script
491     * when the URL of oEmbed HTML is a VideoPress URL.
492     *
493     * @param string|false $cache   The cached HTML result, stored in post meta.
494     * @param string       $url     The attempted embed URL.
495     * @param array        $attr    An array of shortcode attributes.
496     * @param int          $post_ID Post ID.
497     *
498     * @return string|false
499     */
500    public static function enqueue_videopress_iframe_api_script( $cache, $url, $attr, $post_ID ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
501        if ( Utils::is_videopress_url( $url ) ) {
502            // Enqueue the VideoPress IFrame API in the front-end.
503            wp_enqueue_script(
504                self::JETPACK_VIDEOPRESS_IFRAME_API_HANDLER,
505                'https://s0.wp.com/wp-content/plugins/video/assets/js/videojs/videopress-iframe-api.js',
506                array(),
507                gmdate( 'YW' ),
508                false
509            );
510        }
511
512        return $cache;
513    }
514}