Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.71% covered (warning)
85.71%
24 / 28
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jwt_Token_Bridge
85.71% covered (warning)
85.71%
24 / 28
60.00% covered (warning)
60.00%
3 / 5
8.19
0.00% covered (danger)
0.00%
0 / 1
 init
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
2.03
 enqueue_jwt_token_bridge_for_editor_canvas
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 get_bridge_url
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 enqueue_jwt_token_bridge
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
2
 enqueue_script
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * VideoPress Jwt_Token_Bridge
4 *
5 * @package automattic/jetpack-videopress
6 */
7
8namespace Automattic\Jetpack\VideoPress;
9
10/**
11 * VideoPress Jwt_Token_Bridge class.
12 */
13class Jwt_Token_Bridge {
14
15    /**
16     * The handle used to enqueue the script
17     *
18     * @var string
19     */
20    const SCRIPT_HANDLE = 'media-video-jwt-bridge';
21
22    /**
23     * Initializer
24     *
25     * This method should be called only once by the Initializer class. Do not call this method again.
26     */
27    public static function init() {
28        if ( ! Status::is_active() ) {
29            return;
30        }
31
32        // Expose the VideoPress token to the Block Editor context.
33        add_action( 'enqueue_block_editor_assets', array( __CLASS__, 'enqueue_jwt_token_bridge' ), 1 );
34
35        // Expose the VideoPress token to the WPAdmin context.
36        add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_jwt_token_bridge' ), 1 );
37
38        // Expose the VideoPress token to the block editor canvas. In iframed editors the
39        // canvas is a separate document that only receives assets enqueued during
40        // enqueue_block_assets, and an embedded player posts its token request to its
41        // direct parent — the canvas — so the bridge must be listening there for private
42        // videos to preview (e.g. the Video Playlist block's editor preview).
43        add_action( 'enqueue_block_assets', array( __CLASS__, 'enqueue_jwt_token_bridge_for_editor_canvas' ), 1 );
44    }
45
46    /**
47     * Enqueues the jwt bridge script for the block editor canvas.
48     *
49     * Front-end loads are excluded: there the bridge is enqueued per-render only when a
50     * VideoPress block is actually on the page.
51     */
52    public static function enqueue_jwt_token_bridge_for_editor_canvas() {
53        if ( ! is_admin() ) {
54            return;
55        }
56
57        self::enqueue_jwt_token_bridge();
58    }
59
60    /**
61     * Gets the bridge script URL depending on the environment we are in
62     *
63     * @return string
64     */
65    public static function get_bridge_url() {
66        return plugins_url( '../build/lib/token-bridge.js', __FILE__ );
67    }
68
69    /**
70     * Enqueues the jwt bridge script.
71     */
72    public static function enqueue_jwt_token_bridge() {
73        global $post;
74        $post_id = isset( $post->ID ) ? absint( $post->ID ) : 0;
75
76        $bridge_url = self::get_bridge_url();
77
78        self::enqueue_script();
79
80        wp_localize_script(
81            self::SCRIPT_HANDLE,
82            'videopressAjax',
83            array(
84                'ajaxUrl'   => admin_url( 'admin-ajax.php' ),
85                'bridgeUrl' => $bridge_url,
86                'post_id'   => $post_id,
87            )
88        );
89    }
90
91    /**
92     * Enqueues only the JS script
93     *
94     * @param string $handle The script handle to identify the script.
95     * @return bool True if the script was successfully localized, false otherwise.
96     */
97    public static function enqueue_script( $handle = self::SCRIPT_HANDLE ) {
98        return wp_enqueue_script(
99            $handle,
100            self::get_bridge_url(),
101            array(),
102            Package_Version::PACKAGE_VERSION,
103            false
104        );
105    }
106}