Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
34.29% covered (danger)
34.29%
12 / 35
0.00% covered (danger)
0.00%
0 / 1
CRAP
n/a
0 / 0
Automattic\Jetpack\Extensions\VideoPress_Video\register_videopress_blocks
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
90
1<?php
2/**
3 * Register VideoPress Video block.
4 *
5 * @package automattic/jetpack
6 **/
7
8namespace Automattic\Jetpack\Extensions\VideoPress_Video;
9
10use Automattic\Jetpack\VideoPress\Channel as VideoPress_Pkg_Channel;
11use Automattic\Jetpack\VideoPress\Channel_Blocks as VideoPress_Pkg_Channel_Blocks;
12use Automattic\Jetpack\VideoPress\Initializer as VideoPress_Pkg_Initializer;
13use Automattic\Jetpack\VideoPress\Playlist_Index as VideoPress_Pkg_Playlist_Index;
14
15if ( ! defined( 'ABSPATH' ) ) {
16    exit( 0 );
17}
18
19// Set the videopress/video block availability, depending on the site plan.
20add_action(
21    'jetpack_register_gutenberg_extensions',
22    function () {
23        \Jetpack_Gutenberg::set_availability_for_plan( 'videopress/video' );
24    }
25);
26
27/**
28 * Register the VideoPress blocks.
29 *
30 * Named so tests can invoke it directly without re-running the global init action.
31 *
32 * @param string|null $playlist_metadata_file         Path to the playlist block.json; null uses the package default.
33 * @param string|null $latest_videos_playlist_metadata_file Path to the latest videos playlist block.json; null uses the package default.
34 */
35function register_videopress_blocks( $playlist_metadata_file = null, $latest_videos_playlist_metadata_file = null ) {
36    $extensions                            = \Jetpack_Gutenberg::get_extensions();
37    $is_videopress_video_extension_enabled = in_array( 'videopress/video', $extensions, true );
38
39    if (
40        $is_videopress_video_extension_enabled &&
41        method_exists( 'Automattic\Jetpack\VideoPress\Initializer', 'register_videopress_video_block' )
42    ) {
43        VideoPress_Pkg_Initializer::register_videopress_video_block();
44    }
45
46    // The playlist block has its own VideoPress-module-active guard inside
47    // register_videopress_playlist_block(), so register it unconditionally here.
48    if ( method_exists( 'Automattic\Jetpack\VideoPress\Initializer', 'register_videopress_playlist_block' ) ) {
49        VideoPress_Pkg_Initializer::register_videopress_playlist_block( $playlist_metadata_file );
50    }
51
52    // Depends on the playlist block above: it registers only once that one is.
53    if ( method_exists( 'Automattic\Jetpack\VideoPress\Initializer', 'register_videopress_latest_videos_playlist_block' ) ) {
54        VideoPress_Pkg_Initializer::register_videopress_latest_videos_playlist_block( $latest_videos_playlist_metadata_file );
55    }
56
57    // The All Playlists block only registers once the playlist block has.
58    if ( method_exists( 'Automattic\Jetpack\VideoPress\Initializer', 'register_videopress_all_playlists_block' ) ) {
59        VideoPress_Pkg_Initializer::register_videopress_all_playlists_block();
60    }
61
62    // WordPress.com does not run the package initializer that keeps the playlist index current,
63    // so the index starts wherever the playlist block is; hooking it twice is a no-op.
64    if (
65        method_exists( 'Automattic\Jetpack\VideoPress\Playlist_Index', 'init' ) &&
66        \WP_Block_Type_Registry::get_instance()->is_registered( 'videopress/playlist' )
67    ) {
68        VideoPress_Pkg_Playlist_Index::init();
69    }
70
71    // The channel theme's blocks ( channel, video and playlist details, episode navigation ) and the playlist block's `source` also need the playlist block.
72    if ( method_exists( 'Automattic\Jetpack\VideoPress\Channel_Blocks', 'register' ) ) {
73        VideoPress_Pkg_Channel_Blocks::register();
74    }
75}
76// Ignore the empty argument supplied by do_action( 'init' ) so metadata uses the package default.
77add_action( 'init', __NAMESPACE__ . '\register_videopress_blocks', 10, 0 );
78
79// The channel feature ( video durations and views, Query Loop filters, channel block
80// bindings ) registers itself on init when the theme opts in; WordPress.com does not
81// run the package initializer, so it starts here as well.
82if ( method_exists( 'Automattic\Jetpack\VideoPress\Channel', 'init' ) ) {
83    VideoPress_Pkg_Channel::init();
84}
85
86// The searchable transcript index, for the same reason.
87if ( method_exists( 'Automattic\Jetpack\VideoPress\Transcript_Index', 'init' ) ) {
88    \Automattic\Jetpack\VideoPress\Transcript_Index::init();
89}
90
91// Register the `v6-video-frame-poster` extension.
92add_action(
93    'jetpack_register_gutenberg_extensions',
94    function () {
95        \Jetpack_Gutenberg::set_extension_available( 'v6-video-frame-poster' );
96    }
97);