Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
60.00% |
15 / 25 |
|
50.00% |
1 / 2 |
CRAP | n/a |
0 / 0 |
|
| Automattic\Jetpack\PremiumAnalytics\get_sdk_module_registration | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
5 | |||
| Automattic\Jetpack\PremiumAnalytics\register_sdk_script_module | |
30.77% |
4 / 13 |
|
0.00% |
0 / 1 |
13.30 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Registers the dashboard's SDK under the name of its package. |
| 4 | * |
| 5 | * Widgets built against `@automattic/jetpack-premium-analytics-sdk` import that name, and wp-build |
| 6 | * leaves it external, so the page import map must resolve it to the facade module this package builds. |
| 7 | * |
| 8 | * @package automattic/jetpack-premium-analytics |
| 9 | */ |
| 10 | |
| 11 | namespace Automattic\Jetpack\PremiumAnalytics; |
| 12 | |
| 13 | const SDK_MODULE_ID = '@automattic/jetpack-premium-analytics-sdk'; |
| 14 | |
| 15 | /** |
| 16 | * Id of the facade module wp-build builds from `packages/sdk`. |
| 17 | */ |
| 18 | const SDK_FACADE_MODULE_ID = '@jetpack-premium-analytics/sdk'; |
| 19 | |
| 20 | /** |
| 21 | * Resolve the facade module's registration from the build's module registry. |
| 22 | * |
| 23 | * @internal Feeds register_sdk_script_module() and its tests; not part of the package's public surface. |
| 24 | * |
| 25 | * @param array[] $modules The modules of `build/modules/registry.php`. |
| 26 | * @param array $constants The constants of `build/constants.php`. |
| 27 | * @param string $build_dir Directory of the build, which holds the asset files. |
| 28 | * @param bool $minified Whether to serve the minified bundle. |
| 29 | * @return array|null `src`, `deps` and `version` for `wp_register_script_module()`, or null when the registry has no facade. |
| 30 | */ |
| 31 | function get_sdk_module_registration( array $modules, array $constants, $build_dir, $minified = true ) { |
| 32 | foreach ( $modules as $module ) { |
| 33 | if ( SDK_FACADE_MODULE_ID !== $module['id'] ) { |
| 34 | continue; |
| 35 | } |
| 36 | |
| 37 | $asset_path = $build_dir . '/modules/' . $module['asset']; |
| 38 | $asset = file_exists( $asset_path ) ? require $asset_path : array(); |
| 39 | |
| 40 | return array( |
| 41 | 'src' => $constants['build_url'] . 'modules/' . $module['path'] . ( $minified ? '.min.js' : '.js' ), |
| 42 | 'deps' => $asset['module_dependencies'] ?? array(), |
| 43 | 'version' => $asset['version'] ?? false, |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | return null; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Register the facade module a second time, under the SDK package's name. |
| 52 | * |
| 53 | * @return void |
| 54 | */ |
| 55 | function register_sdk_script_module() { |
| 56 | $build_dir = dirname( __DIR__ ) . '/build'; |
| 57 | $registry_file = $build_dir . '/modules/registry.php'; |
| 58 | if ( ! file_exists( $build_dir . '/constants.php' ) || ! file_exists( $registry_file ) ) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | $registration = get_sdk_module_registration( |
| 63 | require $registry_file, |
| 64 | require $build_dir . '/constants.php', |
| 65 | $build_dir, |
| 66 | ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) |
| 67 | ); |
| 68 | if ( null === $registration ) { |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | wp_register_script_module( SDK_MODULE_ID, $registration['src'], $registration['deps'], $registration['version'] ); |
| 73 | } |
| 74 | |
| 75 | // After wp-build's own registration of the package's modules, at the default priority. |
| 76 | add_action( 'wp_default_scripts', __NAMESPACE__ . '\register_sdk_script_module', 20 ); |