Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
30.06% covered (danger)
30.06%
49 / 163
8.33% covered (danger)
8.33%
1 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
Initializer
30.06% covered (danger)
30.06%
49 / 163
8.33% covered (danger)
8.33%
1 / 12
1128.82
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 update_init_options
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 should_initialize_admin_ui
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 unconditional_initialization
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
 should_include_utilities
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 active_initialization
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
6
 register_oembed_providers
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 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
57.50% covered (warning)
57.50%
46 / 80
0.00% covered (danger)
0.00%
0 / 1
72.98
 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
10use WP_Block;
11
12/**
13 * Initialized the VideoPress package
14 */
15class Initializer {
16
17    const JETPACK_VIDEOPRESS_IFRAME_API_HANDLER = 'jetpack-videopress-iframe-api';
18
19    /**
20     * Initialization optinos
21     *
22     * @var array
23     */
24    protected static $init_options = array();
25
26    /**
27     * Initializes the VideoPress package
28     *
29     * This method is called by Config::ensure.
30     *
31     * @return void
32     */
33    public static function init() {
34        if ( ! did_action( 'videopress_init' ) ) {
35
36            self::unconditional_initialization();
37
38            if ( Status::is_active() ) {
39                self::active_initialization();
40            }
41        }
42
43        /**
44         * Fires after the VideoPress package is initialized
45         *
46         * @since 0.1.1
47         */
48        do_action( 'videopress_init' );
49    }
50
51    /**
52     * Update the initialization options
53     *
54     * This method is called by the Config class
55     *
56     * @param array $options The initialization options.
57     * @return void
58     */
59    public static function update_init_options( array $options ) {
60        if ( empty( $options['admin_ui'] ) || self::should_initialize_admin_ui() ) { // do not overwrite if already set to true.
61            return;
62        }
63
64        self::$init_options['admin_ui'] = $options['admin_ui'];
65    }
66
67    /**
68     * Checks the initialization options and returns whether the admin_ui should be initialized or not
69     *
70     * @return boolean
71     */
72    public static function should_initialize_admin_ui() {
73        return isset( self::$init_options['admin_ui'] ) && true === self::$init_options['admin_ui'];
74    }
75
76    /**
77     * Initialize VideoPress features that should be initialized whenever VideoPress is present, even if the module is not active
78     *
79     * @return void
80     */
81    private static function unconditional_initialization() {
82        if ( self::should_include_utilities() ) {
83            require_once __DIR__ . '/utility-functions.php';
84        }
85
86        // Set up package version hook.
87        add_filter( 'jetpack_package_versions', __NAMESPACE__ . '\Package_Version::send_package_version_to_tracker' );
88
89        Module_Control::init();
90
91        new WPCOM_REST_API_V2_Endpoint_VideoPress();
92        new WPCOM_REST_API_V2_Attachment_VideoPress_Field();
93        new WPCOM_REST_API_V2_Attachment_VideoPress_Data();
94
95        if ( is_admin() ) {
96            AJAX::init();
97        } else {
98            require_once __DIR__ . '/class-block-replacement.php';
99            Block_Replacement::init();
100        }
101    }
102
103    /**
104     * This avoids conflicts when running VideoPress plugin with older versions of the Jetpack plugin
105     *
106     * On version 11.3-a.7 utility functions include were removed from the plugin and it is safe to include it from the package
107     *
108     * @return boolean
109     */
110    private static function should_include_utilities() {
111        if ( ! class_exists( 'Jetpack' ) || ! defined( 'JETPACK__VERSION' ) ) {
112            return true;
113        }
114
115        return version_compare( JETPACK__VERSION, '11.3-a.7', '>=' );
116    }
117
118    /**
119     * Initialize VideoPress features that should be initialized only when the module is active
120     *
121     * @return void
122     */
123    private static function active_initialization() {
124        Attachment_Handler::init();
125        Jwt_Token_Bridge::init();
126        Uploader_Rest_Endpoints::init();
127        VideoPress_Rest_Api_V1_Stats::init();
128        VideoPress_Rest_Api_V1_Site::init();
129        VideoPress_Rest_Api_V1_Settings::init();
130        VideoPress_Rest_Api_V1_Features::init();
131        XMLRPC::init();
132        Block_Editor_Content::init();
133        self::register_oembed_providers();
134
135        // Enqueuethe VideoPress Iframe API script in the front-end.
136        add_filter( 'embed_oembed_html', array( __CLASS__, 'enqueue_videopress_iframe_api_script' ), 10, 4 );
137
138        if ( self::should_initialize_admin_ui() ) {
139            Admin_UI::init();
140        }
141
142        Divi::init();
143    }
144
145    /**
146     * Explicitly register VideoPress oembed provider for patterns not supported by core
147     *
148     * @return void
149     */
150    public static function register_oembed_providers() {
151        $host = rawurlencode( home_url() );
152        // videopress.com/v is already registered in core.
153        // By explicitly declaring the provider here, we can speed things up by not relying on oEmbed discovery.
154        wp_oembed_add_provider( '#^https?://video.wordpress.com/v/.*#', 'https://public-api.wordpress.com/oembed/?for=' . $host, true );
155        // This is needed as it's not supported in oEmbed discovery
156        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
157
158        add_filter( 'embed_oembed_html', array( __CLASS__, 'video_enqueue_bridge_when_oembed_present' ), 10, 4 );
159    }
160
161    /**
162     * Enqueues VideoPress token bridge when a VideoPress oembed is present on the current page.
163     *
164     * @param string|false $cache   The cached HTML result, stored in post meta.
165     * @param string       $url     The attempted embed URL.
166     * @param array        $attr    An array of shortcode attributes.
167     * @param int          $post_ID Post ID.
168     *
169     * @return string|false
170     */
171    public static function video_enqueue_bridge_when_oembed_present( $cache, $url, $attr, $post_ID = null ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
172        if ( Utils::is_videopress_url( $url ) ) {
173            Jwt_Token_Bridge::enqueue_jwt_token_bridge();
174        }
175
176        return $cache;
177    }
178
179    /**
180     * Register all VideoPress blocks
181     *
182     * @return void
183     */
184    public static function register_videopress_blocks() {
185        // Register VideoPress Video block.
186        self::register_videopress_video_block();
187    }
188
189    /**
190     * VideoPress video block render method
191     *
192     * @param array    $block_attributes - Block attributes.
193     * @param string   $content          - Current block markup.
194     * @param WP_Block $block            - Current block.
195     *
196     * @return string                    Block markup.
197     */
198    public static function render_videopress_video_block( $block_attributes, $content, $block ) {
199        global $wp_embed;
200
201        // CSS classes
202        $align        = isset( $block_attributes['align'] ) ? $block_attributes['align'] : null;
203        $align_class  = $align ? ' align' . $align : '';
204        $custom_class = isset( $block_attributes['className'] ) ? ' ' . $block_attributes['className'] : '';
205        $classes      = 'wp-block-jetpack-videopress jetpack-videopress-player' . $custom_class . $align_class;
206
207        // Inline style
208        $style     = '';
209        $max_width = isset( $block_attributes['maxWidth'] ) ? $block_attributes['maxWidth'] : null;
210
211        if ( $max_width && $max_width !== '100%' ) {
212            $style    = sprintf( 'max-width: %s;', $max_width );
213            $classes .= ' wp-block-jetpack-videopress--has-max-width';
214        }
215
216        /*
217         * <figcaption /> element
218         * Caption is stored into the block attributes,
219         * but also it was stored into the <figcaption /> element,
220         * meaning that it could be stored in two different places.
221         */
222        $figcaption = '';
223
224        // Caption from block attributes
225        $caption = isset( $block_attributes['caption'] ) ? $block_attributes['caption'] : null;
226
227        /*
228         * If the caption is not stored into the block attributes,
229         * try to get it from the <figcaption /> element.
230         */
231        if ( $caption === null ) {
232            preg_match( '/<figcaption>(.*?)<\/figcaption>/', $content, $matches );
233            $caption = isset( $matches[1] ) ? $matches[1] : null;
234        }
235
236        // If we have a caption, create the <figcaption /> element.
237        if ( $caption !== null ) {
238            $figcaption = sprintf( '<figcaption>%s</figcaption>', wp_kses_post( $caption ) );
239        }
240
241        // Custom anchor from block content
242        $id_attribute = '';
243
244        // Try to get the custom anchor from the block attributes.
245        if ( isset( $block_attributes['anchor'] ) && $block_attributes['anchor'] ) {
246            $id_attribute = sprintf( 'id="%s"', esc_attr( $block_attributes['anchor'] ) );
247        } elseif ( preg_match( '/<figure[^>]*id="([^"]+)"/', $content, $matches ) ) {
248            // Othwerwise, try to get the custom anchor from the <figure /> element.
249            $id_attribute = sprintf( 'id="%s"', $matches[1] );
250        }
251
252        // Preview On Hover data
253        $is_poh_enabled =
254            isset( $block_attributes['posterData']['previewOnHover'] ) &&
255            $block_attributes['posterData']['previewOnHover'];
256
257        $autoplay = isset( $block_attributes['autoplay'] ) ? $block_attributes['autoplay'] : false;
258        $controls = isset( $block_attributes['controls'] ) ? $block_attributes['controls'] : false;
259        $poster   = isset( $block_attributes['posterData']['url'] ) ? $block_attributes['posterData']['url'] : null;
260
261        $preview_on_hover = '';
262
263        if ( $is_poh_enabled ) {
264            $preview_on_hover = array(
265                'previewAtTime'       => $block_attributes['posterData']['previewAtTime'],
266                'previewLoopDuration' => $block_attributes['posterData']['previewLoopDuration'],
267                'autoplay'            => $autoplay,
268                'showControls'        => $controls,
269            );
270
271            // Create inlione style in case video has a custom poster.
272            $inline_style = '';
273            if ( $poster ) {
274                $inline_style = sprintf(
275                    'style="background-image: url(%s); background-size: cover; background-position: center center;"',
276                    esc_attr( $poster )
277                );
278            }
279
280            // Expose the preview on hover data to the client.
281            $preview_on_hover = sprintf(
282                '<div class="jetpack-videopress-player__overlay" %s></div><script type="application/json">%s</script>',
283                $inline_style,
284                wp_json_encode( $preview_on_hover, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP )
285            );
286
287            // Set `autoplay` and `muted` attributes to the video element.
288            $block_attributes['autoplay'] = true;
289            $block_attributes['muted']    = true;
290        }
291
292        $figure_template = '
293        <figure class="%1$s" style="%2$s" %3$s>
294            %4$s
295            %5$s
296            %6$s
297        </figure>
298        ';
299
300        // VideoPress URL
301        $guid           = isset( $block_attributes['guid'] ) ? $block_attributes['guid'] : null;
302        $videopress_url = Utils::get_video_press_url( $guid, $block_attributes );
303
304        $video_wrapper         = '';
305        $video_wrapper_classes = 'jetpack-videopress-player__wrapper';
306
307        if ( $videopress_url ) {
308            $videopress_url = wp_kses_post( $videopress_url );
309            $oembed_html    = apply_filters( 'video_embed_html', $wp_embed->shortcode( array(), $videopress_url ) );
310            $video_wrapper  = sprintf(
311                '<div class="%s">%s %s</div>',
312                $video_wrapper_classes,
313                $preview_on_hover,
314                $oembed_html
315            );
316        }
317
318        // Get premium content from block context
319        $premium_block_plan_id    = isset( $block->context['premium-content/planId'] ) ? intval( $block->context['premium-content/planId'] ) : 0;
320        $is_premium_content_child = isset( $block->context['isPremiumContentChild'] ) ? (bool) $block->context['isPremiumContentChild'] : false;
321        $maybe_premium_script     = '';
322        if ( $is_premium_content_child ) {
323            Access_Control::instance()->set_guid_subscription( $guid, $premium_block_plan_id );
324            $escaped_guid         = wp_json_encode( $guid, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP );
325            $script_content       = "if ( ! window.__guidsToPlanIds ) { window.__guidsToPlanIds = {}; }; window.__guidsToPlanIds[$escaped_guid] = $premium_block_plan_id;";
326            $maybe_premium_script = '<script>' . $script_content . '</script>';
327        }
328
329        // $id_attribute, $video_wrapper, $figcaption properly escaped earlier on the code
330        return sprintf(
331            $figure_template,
332            esc_attr( $classes ),
333            esc_attr( $style ),
334            $id_attribute,
335            $video_wrapper,
336            $figcaption,
337            $maybe_premium_script
338        );
339    }
340
341    /**
342     * Register the VideoPress block editor block,
343     * AKA "VideoPress Block v6".
344     *
345     * @return void
346     */
347    public static function register_videopress_video_block() {
348        /*
349         * If only Jetpack is active, and if the VideoPress module is not active,
350         * we can register the block just to display a placeholder to turn on the module.
351         * That invitation is only useful for admins though.
352         */
353        if (
354            Status::is_jetpack_plugin_without_videopress_module_active()
355            && ! Status::is_standalone_plugin_active()
356            && ! current_user_can( 'jetpack_activate_modules' )
357        ) {
358            return;
359        }
360
361        $videopress_video_metadata_file        = __DIR__ . '/../build/block-editor/blocks/video/block.json';
362        $videopress_video_metadata_file_exists = file_exists( $videopress_video_metadata_file );
363        if ( ! $videopress_video_metadata_file_exists ) {
364            return;
365        }
366
367        $videopress_video_metadata = json_decode(
368            // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
369            file_get_contents( $videopress_video_metadata_file )
370        );
371
372        // Pick the block name straight from the block metadata .json file.
373        $videopress_video_block_name = $videopress_video_metadata->name;
374
375        // Is the block already registered?
376        $is_block_registered = \WP_Block_Type_Registry::get_instance()->is_registered( $videopress_video_block_name );
377
378        // Do not register if the block is already registered.
379        if ( $is_block_registered ) {
380            return;
381        }
382
383        $registration = register_block_type(
384            $videopress_video_metadata_file,
385            array(
386                'render_callback'       => array( __CLASS__, 'render_videopress_video_block' ),
387                'render_email_callback' => array( Video_Block_Email_Renderer::class, 'render' ),
388                'uses_context'          => array( 'premium-content/planId', 'isPremiumContentChild', 'selectedPlanId' ),
389            )
390        );
391
392        // Do not enqueue scripts if the block could not be registered.
393        if ( empty( $registration ) || empty( $registration->editor_script_handles ) ) {
394            return;
395        }
396
397        // Extensions use Connection_Initial_State::render_script with script handle as parameter.
398        if ( is_array( $registration->editor_script_handles ) ) {
399            $script_handle = $registration->editor_script_handles[0];
400        } else {
401            $script_handle = $registration->editor_script_handles;
402        }
403
404        // Register and enqueue scripts used by the VideoPress video block.
405        Block_Editor_Extensions::init( $script_handle );
406    }
407
408    /**
409     * Enqueue the VideoPress Iframe API script
410     * when the URL of oEmbed HTML is a VideoPress URL.
411     *
412     * @param string|false $cache   The cached HTML result, stored in post meta.
413     * @param string       $url     The attempted embed URL.
414     * @param array        $attr    An array of shortcode attributes.
415     * @param int          $post_ID Post ID.
416     *
417     * @return string|false
418     */
419    public static function enqueue_videopress_iframe_api_script( $cache, $url, $attr, $post_ID ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
420        if ( Utils::is_videopress_url( $url ) ) {
421            // Enqueue the VideoPress IFrame API in the front-end.
422            wp_enqueue_script(
423                self::JETPACK_VIDEOPRESS_IFRAME_API_HANDLER,
424                'https://s0.wp.com/wp-content/plugins/video/assets/js/videojs/videopress-iframe-api.js',
425                array(),
426                gmdate( 'YW' ),
427                false
428            );
429        }
430
431        return $cache;
432    }
433}