Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
24.73% |
45 / 182 |
|
13.33% |
2 / 15 |
CRAP | |
0.00% |
0 / 1 |
| Commerce | |
24.73% |
45 / 182 |
|
13.33% |
2 / 15 |
1160.40 | |
0.00% |
0 / 1 |
| register_hooks | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
6 | |||
| on_payment | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| products_count | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
| orders_count | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 | |||
| site_cart_views | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
4 | |||
| checkout_views | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
| thank_you_page | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 | |||
| razorpay_connection | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
| shippo_connection | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
| stripe_connection | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
| paypal_connection | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
| ecomdash_connected | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
30 | |||
| product_created_or_updated | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| woocommerce_hpos_enabled | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
5 | |||
| woopay_connection | |
83.33% |
15 / 18 |
|
0.00% |
0 / 1 |
7.23 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Data\Listeners; |
| 4 | |
| 5 | /** |
| 6 | * Monitors Yith events |
| 7 | */ |
| 8 | class 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 | * @hooked woocommerce_before_cart |
| 109 | * @return void |
| 110 | */ |
| 111 | public function site_cart_views() { |
| 112 | if ( ! function_exists( 'WC' ) ) { |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | $cart = WC()->cart; |
| 117 | |
| 118 | if ( ! $cart ) { |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | $cart_contents_count = $cart->get_cart_contents_count(); |
| 123 | |
| 124 | if ( 0 === $cart_contents_count ) { |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | $data = array( |
| 129 | 'product_count' => $cart->get_cart_contents_count(), |
| 130 | 'cart_total' => floatval( $cart->get_cart_contents_total() ), |
| 131 | 'currency' => get_woocommerce_currency(), |
| 132 | ); |
| 133 | |
| 134 | $this->push( |
| 135 | 'site_cart_view', |
| 136 | $data |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Checkout view, send data to Hiive |
| 142 | * |
| 143 | * @return void |
| 144 | */ |
| 145 | public function checkout_views() { |
| 146 | $data = array( |
| 147 | 'product_count' => WC()->cart->get_cart_contents_count(), |
| 148 | 'cart_total' => floatval( WC()->cart->get_cart_contents_total() ), |
| 149 | 'currency' => get_woocommerce_currency(), |
| 150 | 'payment_method' => array_keys( WC()->payment_gateways()->get_available_payment_gateways() ), |
| 151 | ); |
| 152 | |
| 153 | $this->push( |
| 154 | 'site_checkout_view', |
| 155 | $data |
| 156 | ); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Thank you page, send data to Hiive |
| 161 | * |
| 162 | * @param int $order_id the order id |
| 163 | * |
| 164 | * @return void |
| 165 | */ |
| 166 | public function thank_you_page( $order_id ) { |
| 167 | $order = wc_get_order( $order_id ); |
| 168 | $line_items = $order->get_items(); |
| 169 | |
| 170 | // This loops over line items |
| 171 | foreach ( $line_items as $item ) { |
| 172 | $qty = $item['qty']; |
| 173 | } |
| 174 | $data = array( |
| 175 | 'product_count' => $qty, |
| 176 | 'order_total' => floatval( $order->get_total() ), |
| 177 | 'currency' => get_woocommerce_currency(), |
| 178 | ); |
| 179 | |
| 180 | $this->push( |
| 181 | 'site_thank_you_view', |
| 182 | $data |
| 183 | ); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Razorpay connected |
| 188 | * |
| 189 | * @param string $new_option New value of the razorpay_data_production option |
| 190 | * @param string $old_option Old value of the razorpay_data_production option |
| 191 | * |
| 192 | * @return string The new option value |
| 193 | */ |
| 194 | public function razorpay_connection( $new_option, $old_option ) { |
| 195 | $url = is_ssl() ? 'https://' : 'http://'; |
| 196 | $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
| 197 | $data = array( |
| 198 | 'label_key' => 'provider', |
| 199 | 'provider' => 'razorpay', |
| 200 | 'page' => $url, |
| 201 | ); |
| 202 | if ( $new_option !== $old_option && ! empty( $new_option ) ) { |
| 203 | $this->push( |
| 204 | 'payment_connected', |
| 205 | $data |
| 206 | ); |
| 207 | } |
| 208 | |
| 209 | return $new_option; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Shippo connected |
| 214 | * |
| 215 | * @param string $new_option New value of the shippo_data option |
| 216 | * @param string $old_option Old value of the shippo_data option |
| 217 | * |
| 218 | * @return string The new option value |
| 219 | */ |
| 220 | public function shippo_connection( $new_option, $old_option ) { |
| 221 | $url = is_ssl() ? 'https://' : 'http://'; |
| 222 | $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
| 223 | $data = array( |
| 224 | 'label_key' => 'provider', |
| 225 | 'provider' => 'yith_shippo', |
| 226 | 'page' => $url, |
| 227 | ); |
| 228 | if ( $new_option !== $old_option && ! empty( $new_option ) ) { |
| 229 | $this->push( |
| 230 | 'shipping_connected', |
| 231 | $data |
| 232 | ); |
| 233 | } |
| 234 | |
| 235 | return $new_option; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Stripe connected |
| 240 | * |
| 241 | * @param string $new_option New value of the stripe_data_production option |
| 242 | * @param string $old_option Old value of the stripe_data_production option |
| 243 | * |
| 244 | * @return string The new option value |
| 245 | */ |
| 246 | public function stripe_connection( $new_option, $old_option ) { |
| 247 | $url = is_ssl() ? 'https://' : 'http://'; |
| 248 | $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
| 249 | $data = array( |
| 250 | 'label_key' => 'provider', |
| 251 | 'provider' => 'yith_stripe', |
| 252 | 'page' => $url, |
| 253 | ); |
| 254 | if ( $new_option !== $old_option && ! empty( $new_option ) ) { |
| 255 | $this->push( |
| 256 | 'payment_connected', |
| 257 | $data |
| 258 | ); |
| 259 | } |
| 260 | |
| 261 | return $new_option; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * PayPal connected |
| 266 | * |
| 267 | * @param string $new_option New value of the yith_ppwc_merchant_data_production option |
| 268 | * @param string $old_option Old value of the yith_ppwc_merchant_data_production option |
| 269 | * |
| 270 | * @return string The new option value |
| 271 | */ |
| 272 | public function paypal_connection( $new_option, $old_option ) { |
| 273 | $url = is_ssl() ? 'https://' : 'http://'; |
| 274 | $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
| 275 | $data = array( |
| 276 | 'label_key' => 'provider', |
| 277 | 'provider' => 'yith_paypal', |
| 278 | 'page' => $url, |
| 279 | ); |
| 280 | if ( $new_option !== $old_option && ! empty( $new_option ) ) { |
| 281 | $this->push( |
| 282 | 'payment_connected', |
| 283 | $data |
| 284 | ); |
| 285 | } |
| 286 | |
| 287 | return $new_option; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Ecomdash connection, send data to Hiive |
| 292 | * |
| 293 | * @param string $new_option New value of the update_option_ewc4wp_sso_account_status option |
| 294 | * @param string $old_option Old value of the update_option_ewc4wp_sso_account_status option |
| 295 | * |
| 296 | * @return string The new option value |
| 297 | */ |
| 298 | public function ecomdash_connected( $new_option, $old_option ) { |
| 299 | if ( $new_option !== $old_option && ! empty( $new_option ) && 'connected' === $new_option ) { |
| 300 | $url = is_ssl() ? 'https://' : 'http://'; |
| 301 | $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
| 302 | $data = array( |
| 303 | 'url' => $url, |
| 304 | ); |
| 305 | $this->push( |
| 306 | 'ecomdash_connected', |
| 307 | $data |
| 308 | ); |
| 309 | } |
| 310 | return $new_option; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Product added, send data to Hiive |
| 315 | * |
| 316 | * @param string $product_id id of post which is being savedPost ObjectOld value of the yith_ppwc_merchant_data_production option |
| 317 | * @param WP_POST $product details of the product |
| 318 | * @return void |
| 319 | */ |
| 320 | public function product_created_or_updated( $product_id, $product ) { |
| 321 | $data = array( |
| 322 | 'label_key' => 'product_type', |
| 323 | 'product_type' => $product->product_type, |
| 324 | 'post_id' => $product_id, |
| 325 | ); |
| 326 | |
| 327 | $this->push( |
| 328 | 'product_created', |
| 329 | $data |
| 330 | ); |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * HPOS (High Performance Order Storage) is enabled |
| 335 | * Send data to Hiive containing "hpos" or "legacy", and the page URL. |
| 336 | * |
| 337 | * @hooked update_option_woocommerce_custom_orders_table_enabled |
| 338 | * |
| 339 | * @param mixed|string $old_value Old value of woocommerce_custom_orders_table_enabled. |
| 340 | * @param mixed|string $new_value New value of woocommerce_custom_orders_table_enabled, 'yes'|'no'. |
| 341 | * @param string $option Name of the option being updated, always 'woocommerce_custom_orders_table_enabled'. |
| 342 | */ |
| 343 | public function woocommerce_hpos_enabled( $old_value, $new_value, string $option ): void { |
| 344 | if ( $new_value !== $old_value && ! empty( $new_value ) ) { |
| 345 | $url = is_ssl() ? 'https://' : 'http://'; |
| 346 | $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
| 347 | $type = ( 'yes' === $new_value ) ? 'hpos' : 'legacy'; |
| 348 | |
| 349 | $data = array( |
| 350 | 'label_key' => 'type', |
| 351 | 'type' => $type, |
| 352 | 'page' => $url, |
| 353 | ); |
| 354 | |
| 355 | $this->push( |
| 356 | 'changed_woo_order_storage_type', |
| 357 | $data |
| 358 | ); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * This method triggers a `payment_connected` event when WooPay is connected (when `wcpay_account_data` goes from not existing to existing) |
| 364 | * |
| 365 | * * Connection Data (from `wcpay_account_data`): |
| 366 | * - account_id: Unique WooPay account ID. |
| 367 | * - status: Connection status (e.g., 'connected', 'disconnected'). |
| 368 | * - last_updated: timestamp, (e.g. '2025-01-08T12:34:56Z') |
| 369 | * - is_live |
| 370 | * |
| 371 | * @hooked update_option_wcpay_account_data |
| 372 | * @see \WC_Payments_Account::get_cached_account_data() |
| 373 | * @see \WCPay\Database_Cache::ACCOUNT_KEY |
| 374 | * @see \WCPay\Database_Cache::get_or_add() |
| 375 | * @see update_option() |
| 376 | * |
| 377 | * @param array{data:array{account_id:string,status:string,last_updated:string}} $new_option New value of the woopay connection option |
| 378 | * @param array|false|string $old_option Old value of the woopay connection option |
| 379 | */ |
| 380 | public function woopay_connection($new_option, $old_option): array |
| 381 | { |
| 382 | |
| 383 | // If the option has not changed, bail |
| 384 | if ($new_option === $old_option) { |
| 385 | return $new_option; |
| 386 | } |
| 387 | |
| 388 | // If the option is empty, or the status is not set, bail |
| 389 | if (empty($new_option) || ! isset($new_option['data']['status'])) { |
| 390 | return $new_option; |
| 391 | } |
| 392 | |
| 393 | // If the status is not changing, bail |
| 394 | if (isset($old_option['data']['status']) && ($new_option['data']['status'] === $old_option['data']['status'])) { |
| 395 | return $new_option; |
| 396 | } |
| 397 | |
| 398 | $url = is_ssl() ? 'https://' : 'http://'; |
| 399 | $url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
| 400 | |
| 401 | $this->push( |
| 402 | 'payment_connected', |
| 403 | array( |
| 404 | 'label_key' => 'provider', |
| 405 | 'provider' => 'woopay', |
| 406 | 'status' => $new_option['data']['status'], |
| 407 | 'page' => $url, |
| 408 | ) |
| 409 | ); |
| 410 | |
| 411 | return $new_option; |
| 412 | } |
| 413 | } |