Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
89.74% |
35 / 39 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
Frontend_Assets | |
89.74% |
35 / 39 |
|
66.67% |
2 / 3 |
14.21 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
enqueue_styles | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
5 | |||
enqueue_scripts | |
86.21% |
25 / 29 |
|
0.00% |
0 / 1 |
8.17 |
1 | <?php |
2 | /** |
3 | * The public-facing functionality of the plugin. |
4 | * |
5 | * @link http://example.com |
6 | * @since 1.0.0 |
7 | * |
8 | * @package brianhenryie/bh-wp-bitcoin-gateway |
9 | */ |
10 | |
11 | namespace BrianHenryIE\WP_Bitcoin_Gateway\Frontend; |
12 | |
13 | use BrianHenryIE\WP_Bitcoin_Gateway\API_Interface; |
14 | use BrianHenryIE\WP_Bitcoin_Gateway\Settings_Interface; |
15 | use Psr\Log\LoggerAwareTrait; |
16 | use Psr\Log\LoggerInterface; |
17 | use WC_Order; |
18 | |
19 | /** |
20 | * Enqueue CSS, JS and JSON order details on the order-received page. |
21 | */ |
22 | class Frontend_Assets { |
23 | use LoggerAwareTrait; |
24 | |
25 | /** |
26 | * Get the plugin version for caching. |
27 | */ |
28 | protected Settings_Interface $settings; |
29 | |
30 | /** |
31 | * Check is the order a Bitcoin order. |
32 | * Get the order details. |
33 | */ |
34 | protected API_Interface $api; |
35 | |
36 | /** |
37 | * Constructor |
38 | * |
39 | * @param API_Interface $api The main plugin functions. |
40 | * @param Settings_Interface $settings The plugin settings. |
41 | * @param LoggerInterface $logger A PSR logger. |
42 | */ |
43 | public function __construct( API_Interface $api, Settings_Interface $settings, LoggerInterface $logger ) { |
44 | $this->setLogger( $logger ); |
45 | $this->settings = $settings; |
46 | $this->api = $api; |
47 | } |
48 | |
49 | /** |
50 | * Register the stylesheets for the frontend-facing side of the site. |
51 | * |
52 | * @hooked wp_enqueue_scripts |
53 | * |
54 | * @since 1.0.0 |
55 | */ |
56 | public function enqueue_styles(): void { |
57 | |
58 | $order_id = isset( $GLOBALS['order-received'] ) ? $GLOBALS['order-received'] : ( isset( $GLOBALS['view-order'] ) ? $GLOBALS['view-order'] : 0 ); |
59 | $order_id = intval( $order_id ); |
60 | |
61 | if ( empty( $order_id ) || ! $this->api->is_order_has_bitcoin_gateway( $order_id ) ) { |
62 | return; |
63 | } |
64 | |
65 | $version = $this->settings->get_plugin_version(); |
66 | wp_enqueue_style( 'bh-wp-bitcoin-gateway', $this->settings->get_plugin_url() . 'assets/css/bh-wp-bitcoin-gateway.css', array(), $version, 'all' ); |
67 | |
68 | wp_enqueue_style( 'dashicons' ); |
69 | } |
70 | |
71 | /** |
72 | * Register the JavaScript for the frontend-facing side of the site. |
73 | * |
74 | * @hooked wp_enqueue_scripts |
75 | * |
76 | * @since 1.0.0 |
77 | */ |
78 | public function enqueue_scripts(): void { |
79 | |
80 | $order_id = isset( $GLOBALS['order-received'] ) ? $GLOBALS['order-received'] : ( isset( $GLOBALS['view-order'] ) ? $GLOBALS['view-order'] : 0 ); |
81 | $order_id = intval( $order_id ); |
82 | |
83 | if ( empty( $order_id ) || ! $this->api->is_order_has_bitcoin_gateway( $order_id ) ) { |
84 | return; |
85 | } |
86 | |
87 | /** |
88 | * We confirmed this is a shop_order in the previous line. |
89 | * |
90 | * @var WC_Order $order |
91 | */ |
92 | $order = wc_get_order( $order_id ); |
93 | |
94 | try { |
95 | $order_details = $this->api->get_order_details( $order ); |
96 | } catch ( \Exception $exception ) { |
97 | $this->logger->error( 'Failed to get order details when enqueuing scripts: ' . $exception->getMessage(), array( 'exception' => $exception ) ); |
98 | return; |
99 | } |
100 | |
101 | $version = $this->settings->get_plugin_version(); |
102 | |
103 | $script_url = $this->settings->get_plugin_url() . 'assets/js/frontend/bh-wp-bitcoin-gateway.min.js'; |
104 | |
105 | if ( defined( 'SCRIPT_DEBUG' ) && constant( 'SCRIPT_DEBUG' ) ) { |
106 | $script_url = str_replace( '.min', '', $script_url ); |
107 | } |
108 | |
109 | wp_enqueue_script( 'bh-wp-bitcoin-gateway', $script_url, array( 'jquery' ), $version, true ); |
110 | |
111 | // TODO: For security, filter array to explicit allow-list containing only the required keys. |
112 | $order_details_json = wp_json_encode( $order_details, JSON_PRETTY_PRINT ); |
113 | |
114 | $ajax_data = array( |
115 | 'ajax_url' => admin_url( 'admin-ajax.php' ), |
116 | 'nonce' => wp_create_nonce( self::class ), |
117 | ); |
118 | $ajax_data_json = wp_json_encode( $ajax_data, JSON_PRETTY_PRINT ); |
119 | |
120 | $script = <<<EOD |
121 | var bh_wp_bitcoin_gateway_ajax_data = $ajax_data_json; |
122 | var bh_wp_bitcoin_gateway_order_details = $order_details_json; |
123 | EOD; |
124 | |
125 | wp_add_inline_script( |
126 | 'bh-wp-bitcoin-gateway', |
127 | $script, |
128 | 'before' |
129 | ); |
130 | } |
131 | } |