Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
94.74% |
18 / 19 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
Login_Form | |
94.74% |
18 / 19 |
|
50.00% |
1 / 2 |
3.00 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
enqueue_script | |
94.44% |
17 / 18 |
|
0.00% |
0 / 1 |
2.00 |
1 | <?php |
2 | /** |
3 | * Add "send magic login link" button to the WooCommerce login form. |
4 | * |
5 | * @package brianhenryie/bh-wp-autologin-urls |
6 | */ |
7 | |
8 | namespace BrianHenryIE\WP_Autologin_URLs\WooCommerce; |
9 | |
10 | use BrianHenryIE\WP_Autologin_URLs\Login\Login_Ajax; |
11 | use BrianHenryIE\WP_Autologin_URLs\Settings_Interface; |
12 | |
13 | /** |
14 | * Enqueue the WooCommerce login form specific JavaScript. |
15 | */ |
16 | class Login_Form { |
17 | |
18 | /** |
19 | * Needed for plugin basename and version for JS URL and versioning. |
20 | * |
21 | * @var Settings_Interface |
22 | */ |
23 | protected Settings_Interface $settings; |
24 | |
25 | /** |
26 | * Constructor |
27 | * |
28 | * @param Settings_Interface $settings Plugin settings for slug and version. |
29 | */ |
30 | public function __construct( Settings_Interface $settings ) { |
31 | $this->settings = $settings; |
32 | } |
33 | |
34 | /** |
35 | * Register the JavaScript when the WooCommerce login form is printed. |
36 | * |
37 | * @hooked woocommerce_before_customer_login_form. |
38 | * @hooked woocommerce_before_checkout_form |
39 | * |
40 | * @since 1.7.0 |
41 | */ |
42 | public function enqueue_script(): void { |
43 | |
44 | if ( ! $this->settings->is_magic_link_enabled() ) { |
45 | return; |
46 | } |
47 | |
48 | $url = plugin_dir_url( $this->settings->get_plugin_basename() ) . 'assets/bh-wp-autologin-urls-woocommerce-login.js'; |
49 | |
50 | $handle = 'bh-wp-autologin-urls-woocommerce-login-form'; |
51 | wp_enqueue_script( $handle, $url, array( 'jquery' ), $this->settings->get_plugin_version(), true ); |
52 | |
53 | $ajax_data = array( |
54 | 'ajaxurl' => admin_url( 'admin-ajax.php' ), |
55 | '_wp_nonce' => wp_create_nonce( Login_Ajax::class ), |
56 | ); |
57 | $ajax_data_json = wp_json_encode( $ajax_data, JSON_PRETTY_PRINT ); |
58 | |
59 | $script = <<<EOD |
60 | var bh_wp_autologin_urls = $ajax_data_json; |
61 | EOD; |
62 | |
63 | wp_add_inline_script( |
64 | $handle, |
65 | $script, |
66 | 'before' |
67 | ); |
68 | } |
69 | } |