Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
35.71% |
5 / 14 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| Shared_Modules | |
35.71% |
5 / 14 |
|
0.00% |
0 / 2 |
20.02 | |
0.00% |
0 / 1 |
| init | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
3.04 | |||
| register | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| 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 | * Libraries every dashboard used to bundle (DataViews, `@wordpress/ui`, Jetpack components, …) |
| 12 | * are built once by `wp-build-shared` and loaded once per page. |
| 13 | */ |
| 14 | class Shared_Modules { |
| 15 | |
| 16 | /** |
| 17 | * Whether the modules were registered. |
| 18 | * |
| 19 | * @var bool |
| 20 | */ |
| 21 | private static $initialized = false; |
| 22 | |
| 23 | /** |
| 24 | * Hook the registration. |
| 25 | */ |
| 26 | public static function init() { |
| 27 | if ( self::$initialized ) { |
| 28 | return; |
| 29 | } |
| 30 | self::$initialized = true; |
| 31 | if ( did_action( 'admin_init' ) ) { |
| 32 | self::register(); |
| 33 | } else { |
| 34 | add_action( 'admin_init', array( __CLASS__, 'register' ), 1 ); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Register each shared module; the import map only lists the ones a page ends up importing. |
| 40 | */ |
| 41 | public static function register() { |
| 42 | $dir = dirname( __DIR__ ) . '/build/shared-modules/'; |
| 43 | if ( ! file_exists( $dir . 'modules.php' ) || ! function_exists( 'wp_register_script_module' ) ) { |
| 44 | return; |
| 45 | } |
| 46 | $build = require $dir . 'modules.php'; |
| 47 | $asset = require $dir . 'shared.min.asset.php'; |
| 48 | $url = plugins_url( 'build/shared-modules/', __DIR__ ); |
| 49 | foreach ( $build['modules'] as $id => $file ) { |
| 50 | wp_register_script_module( $id, $url . $file, $asset['module_dependencies'] ?? array(), $build['version'] ); |
| 51 | } |
| 52 | } |
| 53 | } |