Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.00% covered (warning)
85.00%
51 / 60
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Frontend_Assets
85.00% covered (warning)
85.00%
51 / 60
25.00% covered (danger)
25.00%
1 / 4
27.11
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_order_id_from_globals
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
30
 enqueue_styles
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
3.02
 enqueue_scripts
93.48% covered (success)
93.48%
43 / 46
0.00% covered (danger)
0.00%
0 / 1
16.07
1<?php
2/**
3 * The public-facing functionality of the plugin.
4 *
5 * @package    brianhenryie/bh-wp-bitcoin-gateway
6 */
7
8namespace BrianHenryIE\WP_Bitcoin_Gateway\Integrations\WooCommerce;
9
10use BrianHenryIE\WP_Bitcoin_Gateway\Brick\Money\Money;
11use BrianHenryIE\WP_Bitcoin_Gateway\Settings_Interface;
12use Psr\Log\LoggerAwareTrait;
13use Psr\Log\LoggerInterface;
14use WC_Order;
15
16/**
17 * Enqueue CSS, JS and JSON order details on the order-received page.
18 */
19class Frontend_Assets {
20    use LoggerAwareTrait;
21
22    /**
23     * Constructor
24     *
25     * @param API_WooCommerce_Interface $api Check is the order a Bitcoin order; get the order details.
26     * @param Settings_Interface        $settings Get the plugin version for caching.
27     * @param LoggerInterface           $logger A PSR logger.
28     */
29    public function __construct(
30        protected API_WooCommerce_Interface $api,
31        protected Settings_Interface $settings,
32        LoggerInterface $logger
33    ) {
34        $this->setLogger( $logger );
35    }
36
37    /**
38     * Try to find the order id as `order-received` or `view-order` in the PHP globals.
39     */
40    protected function get_order_id_from_globals(): ?int {
41
42        if ( isset( $GLOBALS['order-received'] ) && is_numeric( $GLOBALS['order-received'] ) ) {
43            return absint( $GLOBALS['order-received'] );
44        }
45
46        if ( isset( $GLOBALS['view-order'] ) && is_numeric( $GLOBALS['view-order'] ) ) {
47            return absint( $GLOBALS['view-order'] );
48        }
49
50        return null;
51    }
52
53    /**
54     * Register the stylesheets for the frontend-facing side of the site.
55     *
56     * @hooked wp_enqueue_scripts
57     *
58     * @since    1.0.0
59     */
60    public function enqueue_styles(): void {
61
62        $order_id = $this->get_order_id_from_globals();
63
64        if ( ! $order_id ) {
65            return;
66        }
67
68        if ( ! $this->api->is_order_has_bitcoin_gateway( $order_id ) ) {
69            // Although we're on the thank-you page, this isn't a Bitcoin order.
70            return;
71        }
72
73        $version = $this->settings->get_plugin_version();
74        wp_enqueue_style( 'bh-wp-bitcoin-gateway', $this->settings->get_plugin_url() . 'assets/css/bh-wp-bitcoin-gateway.css', array(), $version, 'all' );
75
76        wp_enqueue_style( 'dashicons' );
77    }
78
79    /**
80     * Register the JavaScript for the frontend-facing side of the site.
81     *
82     * @hooked wp_enqueue_scripts
83     *
84     * @since    1.0.0
85     */
86    public function enqueue_scripts(): void {
87
88        $order_id = $this->get_order_id_from_globals();
89
90        if ( empty( $order_id ) || ! $this->api->is_order_has_bitcoin_gateway( $order_id ) ) {
91            return;
92        }
93
94        /**
95         * We confirmed this is a shop_order in the previous line.
96         *
97         * @var WC_Order $order
98         */
99        $order = wc_get_order( $order_id );
100
101        try {
102            $order_details = $this->api->get_formatted_order_details( $order );
103        } catch ( \Exception $exception ) {
104            $this->logger->error( 'Failed to get order details when enqueuing scripts: ' . $exception->getMessage(), array( 'exception' => $exception ) );
105            return;
106        }
107
108        $version = $this->settings->get_plugin_version();
109
110        $script_url = $this->settings->get_plugin_url() . 'assets/js/frontend/woocommerce/shortcode/thank-you/thank-you.min.js';
111
112        $webpack_manifest_path = $this->settings->get_plugin_dir()
113            . '/assets/js/frontend/woocommerce/shortcode/thank-you/thank-you.min.asset.php';
114
115        /** @var array{dependencies?: array<string>, version?:string} $webpack_manifest */
116        $webpack_manifest = (array) include $webpack_manifest_path;
117
118        wp_register_script(
119            'bh-wp-bitcoin-gateway-shortcode-thank-you',
120            $script_url,
121            $webpack_manifest['dependencies'] ?? array( 'jquery' ),
122            $webpack_manifest['version'] ?? $this->settings->get_plugin_version(),
123            array( 'in_footer' => true )
124        );
125
126        wp_enqueue_script( 'bh-wp-bitcoin-gateway-shortcode-thank-you' );
127
128        // Filter array to explicit allow-list containing only the required keys for frontend TypeScript.
129        $filtered_order_details = array(
130            'btc_address'                 => $order_details['btc_address'] ?? '',
131            'btc_total'                   => isset( $order_details['btc_total'] ) && ( $order_details['btc_total'] instanceof Money ) ? $order_details['btc_total']->getAmount()->toScale( 8 ) : '',
132            'order_id'                    => (string) $order->get_id(),
133            'btc_amount_received'         => isset( $order_details['btc_amount_received'] ) && is_string( $order_details['btc_amount_received'] ) ? $order_details['btc_amount_received'] : '',
134            'status'                      => isset( $order_details['payment_status'] ) && is_string( $order_details['payment_status'] ) ? $order_details['payment_status'] : '',
135            'amount_received'             => isset( $order_details['btc_amount_received_formatted'] ) && is_string( $order_details['btc_amount_received_formatted'] ) ? $order_details['btc_amount_received_formatted'] : '',
136            'order_status_formatted'      => isset( $order_details['order_status_formatted'] ) && is_string( $order_details['order_status_formatted'] ) ? $order_details['order_status_formatted'] : '',
137            'last_checked_time_formatted' => isset( $order_details['last_checked_time_formatted'] ) && is_string( $order_details['last_checked_time_formatted'] ) ? $order_details['last_checked_time_formatted'] : '',
138        );
139
140        $order_details_json = wp_json_encode( $filtered_order_details, JSON_PRETTY_PRINT );
141
142        $ajax_data      = array(
143            'ajax_url' => admin_url( 'admin-ajax.php' ),
144            'nonce'    => wp_create_nonce( self::class ),
145        );
146        $ajax_data_json = wp_json_encode( $ajax_data, JSON_PRETTY_PRINT );
147
148        $script = <<<EOD
149var bh_wp_bitcoin_gateway_ajax_data = $ajax_data_json;
150var bh_wp_bitcoin_gateway_order_details = $order_details_json;
151EOD;
152
153        wp_add_inline_script(
154        // TODO: move this into WooCommerce specific file, then page specific.
155        // 'bh-wp-bitcoin-gateway-shortcode-checkout-thank-you'.
156        // 'bh-wp-bitcoin-gateway-woocommerce-thank-you-classic-theme'.
157            'bh-wp-bitcoin-gateway-shortcode-thank-you',
158            $script,
159            'before'
160        );
161    }
162}