Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
16.00% covered (danger)
16.00%
28 / 175
6.67% covered (danger)
6.67%
1 / 15
CRAP
0.00% covered (danger)
0.00%
0 / 1
Commerce
16.00% covered (danger)
16.00%
28 / 175
6.67% covered (danger)
6.67%
1 / 15
1472.08
0.00% covered (danger)
0.00%
0 / 1
 register_hooks
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
6
 on_payment
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 products_count
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
20
 orders_count
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
 site_cart_views
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 checkout_views
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 thank_you_page
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
 razorpay_connection
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
20
 shippo_connection
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
20
 stripe_connection
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
20
 paypal_connection
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
20
 ecomdash_connected
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
30
 product_created_or_updated
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 woocommerce_hpos_enabled
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
5
 woopay_connection
83.33% covered (warning)
83.33%
15 / 18
0.00% covered (danger)
0.00%
0 / 1
7.23
1<?php
2
3namespace NewfoldLabs\WP\Module\Data\Listeners;
4
5/**
6 * Monitors Yith events
7 */
8class Commerce extends Listener {
9
10    /**
11     * Register the hooks for the listener
12     *
13     * @return void
14     */
15    public function register_hooks() {
16        if ( is_plugin_active( 'woocommerce/woocommerce.php' ) ) {
17            add_filter( 'newfold_wp_data_module_cron_data_filter', array( $this, 'products_count' ) );
18            add_filter( 'newfold_wp_data_module_cron_data_filter', array( $this, 'orders_count' ) );
19
20            add_action( 'woocommerce_order_status_processing', array( $this, 'on_payment' ), 10, 2 );
21            add_filter( 'woocommerce_before_cart', array( $this, 'site_cart_views' ) );
22            add_filter( 'woocommerce_before_checkout_form', array( $this, 'checkout_views' ) );
23            add_filter( 'woocommerce_thankyou', array( $this, 'thank_you_page' ) );
24            add_filter( 'pre_update_option_nfd-ecommerce-captive-flow-razorpay', array( $this, 'razorpay_connection' ), 10, 2 );
25            add_filter( 'pre_update_option_nfd-ecommerce-captive-flow-shippo', array( $this, 'shippo_connection' ), 10, 2 );
26            add_filter( 'pre_update_option_nfd-ecommerce-captive-flow-stripe', array( $this, 'stripe_connection' ), 10, 2 );
27
28            // Paypal Connection
29            add_filter( 'pre_update_option_yith_ppwc_merchant_data_production', array( $this, 'paypal_connection' ), 10, 2 );
30            add_filter( 'update_option_ewc4wp_sso_account_status', array( $this, 'ecomdash_connected' ), 10, 2 );
31            add_filter( 'woocommerce_update_product', array( $this, 'product_created_or_updated' ), 100, 2 );
32            add_action( 'update_option_woocommerce_custom_orders_table_enabled', array( $this, 'woocommerce_hpos_enabled' ), 10, 3 );
33
34            // Hook into the update of the 'wcpay_account_data' option to trigger an event when WooPay is connected.
35            add_filter( 'pre_update_option_wcpay_account_data', array( $this, 'woopay_connection' ), 10, 2 );
36        }
37    }
38
39    /**
40     * On Payment, send data to Hiive
41     *
42     * @param  int       $order_id  the order id
43     * @param  \WC_Order $order  the order
44     *
45     * @return void
46     */
47    public function on_payment( $order_id, \WC_Order $order ) {
48
49        $data = array(
50            'order_currency'       => $order->get_currency(),
51            'order_total'          => $order->get_total(),
52            'payment_method'       => $order->get_payment_method(),
53            'payment_method_title' => $order->get_payment_method_title(),
54        );
55
56        $this->push( 'woocommerce_order_status_processing', $data );
57    }
58
59    /**
60     * Products Count
61     *
62     * @param  string $data  Array of data to be sent to Hiive
63     *
64     * @return string Array of data
65     */
66    public function products_count( $data ) {
67        if ( ! isset( $data['meta'] ) ) {
68            $data['meta'] = array();
69        }
70        $product_post_counts = wp_count_posts( 'product' );
71        if ( $product_post_counts && isset( $product_post_counts->publish ) ) {
72            $data['meta']['products_count'] = (int) $product_post_counts->publish;
73        }
74
75        return $data;
76    }
77
78    /**
79     * Orders Count
80     *
81     * @param  string $data  Array of data to be sent to Hiive
82     *
83     * @return string Array of data
84     */
85    public function orders_count( $data ) {
86        if ( ! isset( $data['meta'] ) ) {
87            $data['meta'] = array();
88        }
89
90        $args = array(
91            'status' => wc_get_is_paid_statuses(),
92            'limit'  => -1,
93            'return' => 'ids',
94        );
95
96        $order_ids = wc_get_orders( $args );
97
98        if ( ! empty( $order_ids ) ) {
99            $data['meta']['orders_count'] = (int) count( $order_ids );
100        }
101
102        return $data;
103    }
104
105    /**
106     * Site Cart View, send data to Hiive
107     *
108     * @return void
109     */
110    public function site_cart_views() {
111        if ( WC()->cart->get_cart_contents_count() !== 0 ) {
112            $data = array(
113                'product_count' => WC()->cart->get_cart_contents_count(),
114                'cart_total'    => floatval( WC()->cart->get_cart_contents_total() ),
115                'currency'      => get_woocommerce_currency(),
116            );
117
118            $this->push(
119                'site_cart_view',
120                $data
121            );
122        }
123    }
124
125
126    /**
127     * Checkout view, send data to Hiive
128     *
129     * @return void
130     */
131    public function checkout_views() {
132        $data = array(
133            'product_count'  => WC()->cart->get_cart_contents_count(),
134            'cart_total'     => floatval( WC()->cart->get_cart_contents_total() ),
135            'currency'       => get_woocommerce_currency(),
136            'payment_method' => array_keys( WC()->payment_gateways()->get_available_payment_gateways() ),
137        );
138
139        $this->push(
140            'site_checkout_view',
141            $data
142        );
143    }
144
145    /**
146     * Thank you page, send data to Hiive
147     *
148     * @param  int $order_id  the order id
149     *
150     * @return void
151     */
152    public function thank_you_page( $order_id ) {
153        $order      = wc_get_order( $order_id );
154        $line_items = $order->get_items();
155
156        // This loops over line items
157        foreach ( $line_items as $item ) {
158            $qty = $item['qty'];
159        }
160        $data = array(
161            'product_count' => $qty,
162            'order_total'   => floatval( $order->get_total() ),
163            'currency'      => get_woocommerce_currency(),
164        );
165
166        $this->push(
167            'site_thank_you_view',
168            $data
169        );
170    }
171
172    /**
173     * Razorpay connected
174     *
175     * @param string $new_option  New value of the razorpay_data_production option
176     * @param string $old_option  Old value of the razorpay_data_production option
177     *
178     * @return string The new option value
179     */
180    public function razorpay_connection( $new_option, $old_option ) {
181        $url  = is_ssl() ? 'https://' : 'http://';
182        $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
183        $data = array(
184            'label_key' => 'provider',
185            'provider'  => 'razorpay',
186            'page'      => $url,
187        );
188        if ( $new_option !== $old_option && ! empty( $new_option ) ) {
189            $this->push(
190                'payment_connected',
191                $data
192            );
193        }
194
195        return $new_option;
196    }
197
198    /**
199     * Shippo connected
200     *
201     * @param string $new_option  New value of the shippo_data option
202     * @param string $old_option  Old value of the shippo_data option
203     *
204     * @return string The new option value
205     */
206    public function shippo_connection( $new_option, $old_option ) {
207        $url  = is_ssl() ? 'https://' : 'http://';
208        $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
209        $data = array(
210            'label_key' => 'provider',
211            'provider'  => 'yith_shippo',
212            'page'      => $url,
213        );
214        if ( $new_option !== $old_option && ! empty( $new_option ) ) {
215            $this->push(
216                'shipping_connected',
217                $data
218            );
219        }
220
221        return $new_option;
222    }
223
224    /**
225     * Stripe connected
226     *
227     * @param string $new_option  New value of the stripe_data_production option
228     * @param string $old_option  Old value of the stripe_data_production option
229     *
230     * @return string The new option value
231     */
232    public function stripe_connection( $new_option, $old_option ) {
233        $url  = is_ssl() ? 'https://' : 'http://';
234        $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
235        $data = array(
236            'label_key' => 'provider',
237            'provider'  => 'yith_stripe',
238            'page'      => $url,
239        );
240        if ( $new_option !== $old_option && ! empty( $new_option ) ) {
241            $this->push(
242                'payment_connected',
243                $data
244            );
245        }
246
247        return $new_option;
248    }
249
250    /**
251     * PayPal connected
252     *
253     * @param string $new_option  New value of the yith_ppwc_merchant_data_production option
254     * @param string $old_option  Old value of the yith_ppwc_merchant_data_production option
255     *
256     * @return string The new option value
257     */
258    public function paypal_connection( $new_option, $old_option ) {
259        $url  = is_ssl() ? 'https://' : 'http://';
260        $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
261        $data = array(
262            'label_key' => 'provider',
263            'provider'  => 'yith_paypal',
264            'page'      => $url,
265        );
266        if ( $new_option !== $old_option && ! empty( $new_option ) ) {
267            $this->push(
268                'payment_connected',
269                $data
270            );
271        }
272
273        return $new_option;
274    }
275
276    /**
277     * Ecomdash connection, send data to Hiive
278     *
279     * @param string $new_option  New value of the update_option_ewc4wp_sso_account_status option
280     * @param string $old_option  Old value of the update_option_ewc4wp_sso_account_status option
281     *
282     * @return string The new option value
283     */
284    public function ecomdash_connected( $new_option, $old_option ) {
285        if ( $new_option !== $old_option && ! empty( $new_option ) && 'connected' === $new_option ) {
286            $url  = is_ssl() ? 'https://' : 'http://';
287            $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
288            $data = array(
289                'url' => $url,
290            );
291            $this->push(
292                'ecomdash_connected',
293                $data
294            );
295        }
296        return $new_option;
297    }
298
299    /**
300     * Product added, send data to Hiive
301     *
302     * @param string  $product_id  id of post which is being savedPost ObjectOld value of the yith_ppwc_merchant_data_production option
303     * @param WP_POST $product  details of the product
304     * @return void
305     */
306    public function product_created_or_updated( $product_id, $product ) {
307        $data = array(
308            'label_key'    => 'product_type',
309            'product_type' => $product->product_type,
310            'post_id'      => $product_id,
311        );
312
313        $this->push(
314            'product_created',
315            $data
316        );
317    }
318
319    /**
320     * HPOS (High Performance Order Storage) is enabled
321     * Send data to Hiive containing "hpos" or "legacy", and the page URL.
322     *
323     * @hooked update_option_woocommerce_custom_orders_table_enabled
324     *
325     * @param mixed|string $old_value  Old value of woocommerce_custom_orders_table_enabled.
326     * @param mixed|string $new_value  New value of woocommerce_custom_orders_table_enabled, 'yes'|'no'.
327     * @param string       $option  Name of the option being updated, always 'woocommerce_custom_orders_table_enabled'.
328     */
329    public function woocommerce_hpos_enabled( $old_value, $new_value, string $option ): void {
330        if ( $new_value !== $old_value && ! empty( $new_value ) ) {
331            $url  = is_ssl() ? 'https://' : 'http://';
332            $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
333            $type = ( 'yes' === $new_value ) ? 'hpos' : 'legacy';
334
335            $data = array(
336                'label_key' => 'type',
337                'type'      => $type,
338                'page'      => $url,
339            );
340
341            $this->push(
342                'changed_woo_order_storage_type',
343                $data
344            );
345        }
346    }
347
348    /**
349     * This method triggers a `payment_connected` event when WooPay is connected (when `wcpay_account_data` goes from not existing to existing)
350     *
351     * * Connection Data (from `wcpay_account_data`):
352     * - account_id: Unique WooPay account ID.
353     * - status: Connection status (e.g., 'connected', 'disconnected').
354     * - last_updated: timestamp, (e.g. '2025-01-08T12:34:56Z')
355     * - is_live
356     *
357     * @hooked update_option_wcpay_account_data
358     * @see \WC_Payments_Account::get_cached_account_data()
359     * @see \WCPay\Database_Cache::ACCOUNT_KEY
360     * @see \WCPay\Database_Cache::get_or_add()
361     * @see update_option()
362     *
363     * @param array{data:array{account_id:string,status:string,last_updated:string}} $new_option  New value of the woopay connection option
364     * @param array|false|string                                                     $old_option  Old value of the woopay connection option
365     */
366    public function woopay_connection($new_option, $old_option): array
367    {
368
369        // If the option has not changed, bail
370        if ($new_option === $old_option) {
371            return $new_option;
372        }
373
374        // If the option is empty, or the status is not set, bail
375        if (empty($new_option) || ! isset($new_option['data']['status'])) {
376            return $new_option;
377        }
378
379        // If the status is not changing, bail
380        if (isset($old_option['data']['status']) && ($new_option['data']['status'] === $old_option['data']['status'])) {
381            return $new_option;
382        }
383
384        $url  = is_ssl() ? 'https://' : 'http://';
385        $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
386
387        $this->push(
388            'payment_connected',
389            array(
390                'label_key' => 'provider',
391                'provider'  => 'woopay',
392                'status'    => $new_option['data']['status'],
393                'page'      => $url,
394            )
395        );
396
397        return $new_option;
398    }
399}