Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.00% |
19 / 20 |
|
85.71% |
6 / 7 |
CRAP | |
0.00% |
0 / 1 |
| Checkout_Blocks | |
95.00% |
19 / 20 |
|
85.71% |
6 / 7 |
9 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_name | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| initialize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| register_script | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
3.00 | |||
| get_script_handles | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_editor_script_handles | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_script_data | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Register the Blocks checkout JavaScript with WooCommerce. |
| 4 | * |
| 5 | * @package brianhenryie/bh-wc-postcode-address-autofill |
| 6 | */ |
| 7 | |
| 8 | namespace BrianHenryIE\WC_Postcode_Address_Autofill\WooCommerce; |
| 9 | |
| 10 | use Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface; |
| 11 | use BrianHenryIE\WC_Postcode_Address_Autofill\Settings_Interface; |
| 12 | |
| 13 | /** |
| 14 | * Conventional way to register scripts for WooCommerce Blocks. |
| 15 | * |
| 16 | * @see \Automattic\WooCommerce\Blocks\BlockTypes\AbstractBlock::register_block_type_assets() |
| 17 | */ |
| 18 | class Checkout_Blocks implements IntegrationInterface { |
| 19 | |
| 20 | /** |
| 21 | * Plugin settings for assets URL and sometimes for caching version. |
| 22 | * |
| 23 | * @uses Settings_Interface::get_plugin_basename() |
| 24 | * @uses Settings_Interface::get_plugin_version() |
| 25 | */ |
| 26 | protected Settings_Interface $settings; |
| 27 | |
| 28 | /** |
| 29 | * Constructor |
| 30 | * |
| 31 | * @param Settings_Interface $settings The plugin settings. |
| 32 | */ |
| 33 | public function __construct( Settings_Interface $settings ) { |
| 34 | $this->settings = $settings; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Return a unique name for the integration. |
| 39 | * |
| 40 | * @see IntegrationInterface::get_name() |
| 41 | * @return string |
| 42 | */ |
| 43 | public function get_name() { |
| 44 | return 'bh-wc-postcode-address-autofill-checkout-blocks'; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Initialize the integration – in this case, just register the script. |
| 49 | * |
| 50 | * @see IntegrationInterface::initialize() |
| 51 | * @return void |
| 52 | */ |
| 53 | public function initialize() { |
| 54 | $this->register_script(); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Register the checkout script with WordPress, to later be enqueued by WooCommerce. |
| 59 | */ |
| 60 | protected function register_script(): void { |
| 61 | $script_asset_path = realpath( __DIR__ . '/../../build/bh-wc-postcode-address-autofill-checkout-blocks.asset.php' ); |
| 62 | $script_asset = $script_asset_path && file_exists( $script_asset_path ) |
| 63 | ? require $script_asset_path |
| 64 | : array( |
| 65 | 'dependencies' => array(), |
| 66 | 'version' => $this->settings->get_plugin_version(), |
| 67 | ); |
| 68 | |
| 69 | wp_register_script( |
| 70 | 'bh-wc-postcode-address-autofill-checkout-blocks', |
| 71 | plugin_dir_url( $this->settings->get_plugin_basename() ) . 'build/bh-wc-postcode-address-autofill-checkout-blocks.js', |
| 72 | $script_asset['dependencies'], |
| 73 | $script_asset['version'], |
| 74 | true |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Return the handle of the script registered above. |
| 80 | * |
| 81 | * @see IntegrationInterface::get_script_handles() |
| 82 | * @return string[] |
| 83 | */ |
| 84 | public function get_script_handles() { |
| 85 | return array( 'bh-wc-postcode-address-autofill-checkout-blocks' ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * The script does not run in the admin editor. |
| 90 | * |
| 91 | * @see IntegrationInterface::get_editor_script_handles() |
| 92 | * @return string[] |
| 93 | */ |
| 94 | public function get_editor_script_handles() { |
| 95 | return array(); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * The script does not need any data. |
| 100 | * |
| 101 | * @see IntegrationInterface::get_script_data() |
| 102 | * @return array{} |
| 103 | */ |
| 104 | public function get_script_data() { |
| 105 | return array(); |
| 106 | } |
| 107 | } |