Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
50.00% |
13 / 26 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Shared_Modules | |
50.00% |
13 / 26 |
|
25.00% |
1 / 4 |
53.12 | |
0.00% |
0 / 1 |
| init | |
71.43% |
5 / 7 |
|
0.00% |
0 / 1 |
3.21 | |||
| manifest | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| register | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
| enqueue_script_dependencies | |
37.50% |
3 / 8 |
|
0.00% |
0 / 1 |
14.79 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Registers the script modules that Jetpack's wp-build dashboards share. |
| 4 | * |
| 5 | * @package automattic/jetpack-admin-ui |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Admin_UI; |
| 9 | |
| 10 | /** |
| 11 | * Registers `build/shared-modules/` so dashboards built with `sharedModules` can import them. |
| 12 | */ |
| 13 | class Shared_Modules { |
| 14 | |
| 15 | /** |
| 16 | * Whether init() ran. |
| 17 | * |
| 18 | * @var bool |
| 19 | */ |
| 20 | private static $initialized = false; |
| 21 | |
| 22 | /** |
| 23 | * Hook the registration. Each opted-in dashboard calls it when it loads its build, as well as |
| 24 | * Admin_Shell::init(): another plugin can load an older Admin_Shell early, one that doesn't call it. |
| 25 | * |
| 26 | * @return void |
| 27 | */ |
| 28 | public static function init() { |
| 29 | if ( self::$initialized ) { |
| 30 | return; |
| 31 | } |
| 32 | self::$initialized = true; |
| 33 | |
| 34 | // Before the wp-build pages and the shell build the import map on admin_enqueue_scripts. |
| 35 | if ( did_action( 'admin_init' ) ) { |
| 36 | self::register(); |
| 37 | } else { |
| 38 | add_action( 'admin_init', array( __CLASS__, 'register' ), 1 ); |
| 39 | } |
| 40 | add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_script_dependencies' ), 100 ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Read `modules.php`, the manifest `wp-build-shared` writes. |
| 45 | * |
| 46 | * @return array|null |
| 47 | */ |
| 48 | private static function manifest() { |
| 49 | static $manifest = false; |
| 50 | if ( false === $manifest ) { |
| 51 | $file = dirname( __DIR__ ) . '/build/shared-modules/modules.php'; |
| 52 | $manifest = is_readable( $file ) ? require $file : null; |
| 53 | } |
| 54 | return is_array( $manifest ) ? $manifest : null; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Register each shared module. The import map only lists those a page's modules depend on. |
| 59 | * |
| 60 | * @return void |
| 61 | */ |
| 62 | public static function register() { |
| 63 | $manifest = self::manifest(); |
| 64 | if ( ! $manifest || ! function_exists( 'wp_register_script_module' ) ) { |
| 65 | return; |
| 66 | } |
| 67 | $url = plugins_url( 'build/shared-modules/', __DIR__ ); |
| 68 | foreach ( $manifest['modules'] as $id => $file ) { |
| 69 | wp_register_script_module( $id, $url . $file, $manifest['module_dependencies'] ?? array(), $manifest['version'] ); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Load the classic scripts the shared modules read globals from, on wp-build pages. |
| 75 | * |
| 76 | * @return void |
| 77 | */ |
| 78 | public static function enqueue_script_dependencies() { |
| 79 | $manifest = self::manifest(); |
| 80 | if ( ! $manifest || empty( $manifest['dependencies'] ) ) { |
| 81 | return; |
| 82 | } |
| 83 | // wp-build enqueues one `<page>-wp-admin` module per page; the shell's guests ride on the host's. |
| 84 | foreach ( wp_script_modules()->get_queue() as $id ) { |
| 85 | if ( str_ends_with( $id, '-wp-admin' ) ) { |
| 86 | foreach ( $manifest['dependencies'] as $handle ) { |
| 87 | wp_enqueue_script( $handle ); |
| 88 | } |
| 89 | return; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | } |