Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
16.67% covered (danger)
16.67%
4 / 24
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Divi_5
13.64% covered (danger)
13.64%
3 / 22
0.00% covered (danger)
0.00%
0 / 3
21.10
0.00% covered (danger)
0.00%
0 / 1
 init
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 register_module
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 enqueue_visual_builder_assets
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Divi 5 integration bootstrap for VideoPress.
4 *
5 * @package automattic/jetpack-videopress
6 */
7
8declare( strict_types = 1 );
9
10namespace Automattic\Jetpack\VideoPress\Divi5;
11
12use Automattic\Jetpack\VideoPress\Package_Version;
13use ET\Builder\Packages\ModuleLibrary\ModuleRegistration;
14use ET\Builder\VisualBuilder\Assets\PackageBuildManager;
15
16if ( ! defined( 'ABSPATH' ) ) {
17    exit( 0 );
18}
19
20/**
21 * Wires the VideoPress module into the Divi 5 framework.
22 *
23 * Both hooks below are only fired by Divi 5, so this integration stays inert on
24 * Divi 4 (where the legacy module continues to handle rendering).
25 */
26class Divi_5 {
27
28    /**
29     * Registers the Divi 5 hooks.
30     *
31     * @return void
32     */
33    public static function init() {
34        add_action( 'divi_module_library_modules_dependency_tree', array( __CLASS__, 'register_module' ) );
35        add_action( 'divi_visual_builder_assets_before_enqueue_scripts', array( __CLASS__, 'enqueue_visual_builder_assets' ) );
36
37        /*
38         * Divi builds its module library during theme bootstrap, which fires the
39         * dependency-tree action above (once) before this integration loads on
40         * `init` — so the callback misses it on requests like the Divi 5 Migrator's
41         * admin-ajax calls. Register directly here too; VideoPress_Module::load()
42         * is idempotent, so the two paths never double-register.
43         */
44        if ( class_exists( ModuleRegistration::class, false ) ) {
45            ( new VideoPress_Module() )->load();
46        }
47    }
48
49    /**
50     * Adds the VideoPress module to the Divi 5 module dependency tree.
51     *
52     * @param object $dependency_tree The Divi 5 module dependency tree.
53     *
54     * @return void
55     */
56    public static function register_module( $dependency_tree ) {
57        $dependency_tree->add_dependency( new VideoPress_Module() );
58    }
59
60    /**
61     * Registers the Visual Builder bundle that powers the module's editing UI.
62     *
63     * @return void
64     */
65    public static function enqueue_visual_builder_assets() {
66        $asset_file = dirname( __DIR__, 2 ) . '/build/divi-5/index.asset.php';
67        $asset      = file_exists( $asset_file ) ? require $asset_file : array();
68
69        PackageBuildManager::register_package_build(
70            array(
71                'name'    => 'jetpack-videopress-divi5-visual-builder',
72                // The build content hash, so each build busts the browser cache.
73                'version' => $asset['version'] ?? Package_Version::PACKAGE_VERSION,
74                'script'  => array(
75                    'src'                => plugins_url( '../../build/divi-5/index.js', __FILE__ ),
76
77                    /*
78                     * The handles the build emits (react, react-jsx-runtime and the
79                     * Divi-vendored @wordpress/* instances), plus the Divi builder
80                     * handles that provide the window.divi.* globals.
81                     */
82                    'deps'               => array_merge(
83                        array( 'divi-module-library', 'divi-rest' ),
84                        $asset['dependencies'] ?? array()
85                    ),
86                    'enqueue_top_window' => false,
87                    'enqueue_app_window' => true,
88                ),
89            )
90        );
91    }
92}