Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
89.19% |
33 / 37 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Jwt_Token_Bridge | |
89.19% |
33 / 37 |
|
60.00% |
3 / 5 |
10.13 | |
0.00% |
0 / 1 |
| init | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
| enqueue_jwt_token_bridge_for_editor_canvas | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| get_bridge_url | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| enqueue_jwt_token_bridge | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
4 | |||
| enqueue_script | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * VideoPress Jwt_Token_Bridge |
| 4 | * |
| 5 | * @package automattic/jetpack-videopress |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\VideoPress; |
| 9 | |
| 10 | /** |
| 11 | * VideoPress Jwt_Token_Bridge class. |
| 12 | */ |
| 13 | class 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 | wp_localize_script( |
| 90 | self::SCRIPT_HANDLE, |
| 91 | 'videopressUpload', |
| 92 | array( |
| 93 | 'url' => sprintf( 'https://public-api.wordpress.com/rest/v1.1/video-uploads/%d/', VideoPressToken::blog_id() ), |
| 94 | 'nonce' => current_user_can( 'upload_files' ) ? wp_create_nonce( 'videopress-upload' ) : '', |
| 95 | 'attachmentFirst' => ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ), |
| 96 | ) |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Enqueues only the JS script |
| 102 | * |
| 103 | * @param string $handle The script handle to identify the script. |
| 104 | * @return bool True if the script was successfully localized, false otherwise. |
| 105 | */ |
| 106 | public static function enqueue_script( $handle = self::SCRIPT_HANDLE ) { |
| 107 | return wp_enqueue_script( |
| 108 | $handle, |
| 109 | self::get_bridge_url(), |
| 110 | array(), |
| 111 | Package_Version::PACKAGE_VERSION, |
| 112 | false |
| 113 | ); |
| 114 | } |
| 115 | } |