Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jwt_Token_Bridge
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 4
42
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 get_bridge_url
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 enqueue_jwt_token_bridge
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
 enqueue_script
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
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
39    /**
40     * Gets the bridge script URL depending on the environment we are in
41     *
42     * @return string
43     */
44    public static function get_bridge_url() {
45        return plugins_url( '../build/lib/token-bridge.js', __FILE__ );
46    }
47
48    /**
49     * Enqueues the jwt bridge script.
50     */
51    public static function enqueue_jwt_token_bridge() {
52        global $post;
53        $post_id = isset( $post->ID ) ? absint( $post->ID ) : 0;
54
55        $bridge_url = self::get_bridge_url();
56
57        self::enqueue_script();
58
59        wp_localize_script(
60            self::SCRIPT_HANDLE,
61            'videopressAjax',
62            array(
63                'ajaxUrl'   => admin_url( 'admin-ajax.php' ),
64                'bridgeUrl' => $bridge_url,
65                'post_id'   => $post_id,
66            )
67        );
68    }
69
70    /**
71     * Enqueues only the JS script
72     *
73     * @param string $handle The script handle to identify the script.
74     * @return bool True if the script was successfully localized, false otherwise.
75     */
76    public static function enqueue_script( $handle = self::SCRIPT_HANDLE ) {
77        return wp_enqueue_script(
78            $handle,
79            self::get_bridge_url(),
80            array(),
81            Package_Version::PACKAGE_VERSION,
82            false
83        );
84    }
85}