← All changes
|
modules/woocommerce-analytics/classes/class-jetpack-woocommerce-analytics-universal.php
+136
-58
13.0.2
→
16.3-a.5
View file →
| @@ -1,8 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * Jetpack_WooCommerce_Analytics_Universal |
| 4 | 4 | * |
| 5 | + * @deprecated 13.3 | |
| 6 | + * | |
| 5 | 7 | * @package automattic/jetpack |
| 6 | 8 | * @author Automattic |
| 7 | 9 | */ |
| 8 | 10 | |
| @@ -9,14 +11,16 @@ | ||
| 9 | 11 | /** |
| 10 | 12 | * Bail if accessed directly |
| 11 | 13 | */ |
| 12 | 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | - exit; | |
| 15 | + exit( 0 ); | |
| 14 | 16 | } |
| 15 | 17 | |
| 16 | 18 | /** |
| 17 | 19 | * Class Jetpack_WooCommerce_Analytics_Universal |
| 18 | 20 | * Filters and Actions added to Store pages to perform analytics |
| 21 | + * | |
| 22 | + * @deprecated 13.3 | |
| 19 | 23 | */ |
| 20 | 24 | class Jetpack_WooCommerce_Analytics_Universal { |
| 21 | 25 | |
| 22 | 26 | /** |
| @@ -25,8 +29,10 @@ | ||
| 25 | 29 | use Jetpack_WooCommerce_Analytics_Trait; |
| 26 | 30 | |
| 27 | 31 | /** |
| 28 | 32 | * Jetpack_WooCommerce_Analytics_Universal constructor. |
| 33 | + * | |
| 34 | + * @deprecated 13.3 | |
| 29 | 35 | */ |
| 30 | 36 | public function __construct() { |
| 31 | 37 | $this->find_cart_checkout_content_sources(); |
| 32 | 38 | $this->additional_blocks_on_cart_page = $this->get_additional_blocks_on_page( 'cart' ); |
| @@ -59,8 +65,10 @@ | ||
| 59 | 65 | } |
| 60 | 66 | |
| 61 | 67 | /** |
| 62 | 68 | * On product lists or other non-product pages, add an event listener to "Add to Cart" button click |
| 69 | + * | |
| 70 | + * @deprecated 13.3 | |
| 63 | 71 | */ |
| 64 | 72 | public function loop_session_events() { |
| 65 | 73 | // Check for previous events queued in session data. |
| 66 | 74 | if ( is_object( WC()->session ) ) { |
| @@ -82,12 +90,20 @@ | ||
| 82 | 90 | } |
| 83 | 91 | |
| 84 | 92 | /** |
| 85 | 93 | * On the cart page, add an event listener for removal of product click |
| 94 | + * | |
| 95 | + * @deprecated 13.3 | |
| 86 | 96 | */ |
| 87 | 97 | public function remove_from_cart() { |
| 88 | - $common_props = $this->render_properties_as_js( | |
| 89 | - $this->get_common_properties() | |
| 98 | + $common_props = wp_json_encode( | |
| 99 | + array_merge( | |
| 100 | + array( | |
| 101 | + '_en' => 'woocommerceanalytics_remove_from_cart', | |
| 102 | + ), | |
| 103 | + $this->get_common_properties() | |
| 104 | + ), | |
| 105 | + JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP | |
| 90 | 106 | ); |
| 91 | 107 | |
| 92 | 108 | // We listen at div.woocommerce because the cart 'form' contents get forcibly |
| 93 | 109 | // updated and subsequent removals from cart would then not have this click |
| @@ -95,19 +111,13 @@ | ||
| 95 | 111 | wc_enqueue_js( |
| 96 | 112 | "jQuery( 'div.woocommerce' ).on( 'click', 'a.remove', function() { |
| 97 | 113 | var productID = jQuery( this ).data( 'product_id' ); |
| 98 | 114 | var quantity = jQuery( this ).parent().parent().find( '.qty' ).val() |
| 99 | - var productDetails = { | |
| 100 | - 'id': productID, | |
| 101 | - 'quantity': quantity ? quantity : '1', | |
| 102 | - }; | |
| 103 | - _wca.push( { | |
| 104 | - '_en': 'woocommerceanalytics_remove_from_cart', | |
| 105 | - 'pi': productDetails.id, | |
| 106 | - 'pq': productDetails.quantity, " . | |
| 107 | - $common_props . ' | |
| 108 | - } ); | |
| 109 | - } );' | |
| 115 | + var common_props = $common_props; | |
| 116 | + common_props.pi = productID; | |
| 117 | + common_props.pq = quantity ? quantity : '1'; | |
| 118 | + _wca.push( common_props ); | |
| 119 | + } );" | |
| 110 | 120 | ); |
| 111 | 121 | } |
| 112 | 122 | |
| 113 | 123 | /** |
| @@ -112,12 +122,14 @@ | ||
| 112 | 122 | |
| 113 | 123 | /** |
| 114 | 124 | * Adds the product ID to the remove product link (for use by remove_from_cart above) if not present |
| 115 | 125 | * |
| 126 | + * @deprecated 13.3 | |
| 127 | + * | |
| 116 | 128 | * @param string $url Full HTML a tag of the link to remove an item from the cart. |
| 117 | 129 | * @param string $key Unique Key ID for a cart item. |
| 118 | 130 | * |
| 119 | - * @return mixed. | |
| 131 | + * @return string | |
| 120 | 132 | */ |
| 121 | 133 | public function remove_from_cart_attributes( $url, $key ) { |
| 122 | 134 | if ( str_contains( $url, 'data-product_id' ) ) { |
| 123 | 135 | return $url; |
| @@ -138,8 +150,10 @@ | ||
| 138 | 150 | /** |
| 139 | 151 | * Get the selected shipping option for a cart item. If the name cannot be found in the options table, the method's |
| 140 | 152 | * ID will be used. |
| 141 | 153 | * |
| 154 | + * @deprecated 13.3 | |
| 155 | + * | |
| 142 | 156 | * @param string $cart_item_key the cart item key. |
| 143 | 157 | * |
| 144 | 158 | * @return mixed|bool |
| 145 | 159 | */ |
| @@ -180,11 +194,15 @@ | ||
| 180 | 194 | } |
| 181 | 195 | |
| 182 | 196 | /** |
| 183 | 197 | * On the Checkout page, trigger an event for each product in the cart |
| 198 | + * | |
| 199 | + * @deprecated 13.3 | |
| 184 | 200 | */ |
| 185 | 201 | public function checkout_process() { |
| 186 | - $cart = WC()->cart->get_cart(); | |
| 202 | + global $post; | |
| 203 | + $checkout_page_id = wc_get_page_id( 'checkout' ); | |
| 204 | + $cart = WC()->cart->get_cart(); | |
| 187 | 205 | |
| 188 | 206 | $enabled_payment_options = array_filter( |
| 189 | 207 | WC()->payment_gateways->get_available_payment_gateways(), |
| 190 | 208 | function ( $payment_gateway ) { |
| @@ -196,18 +214,14 @@ | ||
| 196 | 214 | } |
| 197 | 215 | ); |
| 198 | 216 | |
| 199 | 217 | $enabled_payment_options = array_keys( $enabled_payment_options ); |
| 200 | - $include_express_payment = false; | |
| 201 | 218 | |
| 202 | - $wcpay_version = get_option( 'woocommerce_woocommerce_payments_version' ); | |
| 203 | - $has_required_wcpay_version = version_compare( $wcpay_version, '2.9.0', '>=' ); | |
| 204 | - // Check express payment availablity only if WC Pay is enabled and express checkout (payment request) is enabled. | |
| 205 | - if ( in_array( 'woocommerce_payments', $enabled_payment_options, true ) && $has_required_wcpay_version ) { | |
| 206 | - $wcpay_settings = get_option( 'woocommerce_woocommerce_payments_settings', array() ); | |
| 207 | - if ( array_key_exists( 'payment_request', $wcpay_settings ) && 'yes' === $wcpay_settings['payment_request'] ) { | |
| 208 | - $include_express_payment = true; | |
| 209 | - } | |
| 219 | + $is_in_checkout_page = $checkout_page_id === $post->ID ? 'Yes' : 'No'; | |
| 220 | + $session = WC()->session; | |
| 221 | + if ( is_object( $session ) ) { | |
| 222 | + $session->set( 'checkout_page_used', true ); | |
| 223 | + $session->save_data(); | |
| 210 | 224 | } |
| 211 | 225 | |
| 212 | 226 | foreach ( $cart as $cart_item_key => $cart_item ) { |
| 213 | 227 | /** |
| @@ -220,8 +234,10 @@ | ||
| 220 | 234 | } |
| 221 | 235 | |
| 222 | 236 | $data = $this->get_cart_checkout_shared_data(); |
| 223 | 237 | |
| 238 | + $data['from_checkout'] = $is_in_checkout_page; | |
| 239 | + | |
| 224 | 240 | if ( ! empty( $data['products'] ) ) { |
| 225 | 241 | unset( $data['products'] ); |
| 226 | 242 | } |
| 227 | 243 | |
| @@ -236,32 +252,59 @@ | ||
| 236 | 252 | $data, |
| 237 | 253 | $product->get_id() |
| 238 | 254 | ); |
| 239 | 255 | |
| 240 | - if ( true === $include_express_payment ) { | |
| 241 | - wc_enqueue_js( | |
| 242 | - " | |
| 243 | - // wcpay.payment-request.availability event gets fired twice. | |
| 244 | - // make sure we push only one event. | |
| 245 | - var cartItem_{$cart_item_key}_logged = false; | |
| 246 | - wp.hooks.addAction('wcpay.payment-request.availability', 'wcpay', function(args) { | |
| 247 | - if ( true === cartItem_{$cart_item_key}_logged ) { | |
| 248 | - return; | |
| 249 | - } | |
| 250 | - var properties = {$properties}; | |
| 251 | - properties.express_checkout = args.paymentRequestType; | |
| 252 | - _wca.push(properties); | |
| 253 | - cartItem_{$cart_item_key}_logged = true; | |
| 254 | - }); | |
| 256 | + wc_enqueue_js( | |
| 255 | 257 | " |
| 256 | - ); | |
| 257 | - } else { | |
| 258 | - $this->record_event( | |
| 259 | - 'woocommerceanalytics_product_checkout', | |
| 260 | - $data, | |
| 261 | - $product->get_id() | |
| 262 | - ); | |
| 263 | - } | |
| 258 | + var cartItem_{$cart_item_key}_logged = false; | |
| 259 | + var properties = {$properties}; | |
| 260 | + // Check if jQuery is available | |
| 261 | + if ( typeof jQuery !== 'undefined' ) { | |
| 262 | + // This is only triggered on the checkout shortcode. | |
| 263 | + jQuery( document.body ).on( 'init_checkout', function () { | |
| 264 | + if ( true === cartItem_{$cart_item_key}_logged ) { | |
| 265 | + return; | |
| 266 | + } | |
| 267 | + wp.hooks.addAction( 'wcpay.payment-request.availability', 'wcpay', function ( args ) { | |
| 268 | + properties.express_checkout = args.paymentRequestType; | |
| 269 | + } ); | |
| 270 | + properties.checkout_page_contains_checkout_block = '0'; | |
| 271 | + properties.checkout_page_contains_checkout_shortcode = '1'; | |
| 272 | + | |
| 273 | + _wca.push( properties ); | |
| 274 | + cartItem_{$cart_item_key}_logged = true; | |
| 275 | + | |
| 276 | + } ); | |
| 277 | + } | |
| 278 | + | |
| 279 | + if ( | |
| 280 | + typeof wp !== 'undefined' && | |
| 281 | + typeof wp.data !== 'undefined' && | |
| 282 | + typeof wp.data.subscribe !== 'undefined' | |
| 283 | + ) { | |
| 284 | + wp.data.subscribe( function () { | |
| 285 | + if ( true === cartItem_{$cart_item_key}_logged ) { | |
| 286 | + return; | |
| 287 | + } | |
| 288 | + | |
| 289 | + const checkoutDataStore = wp.data.select( 'wc/store/checkout' ); | |
| 290 | + // Ensures we're not in Cart, but in Checkout page. | |
| 291 | + if ( | |
| 292 | + typeof checkoutDataStore !== 'undefined' && | |
| 293 | + checkoutDataStore.getOrderId() !== 0 | |
| 294 | + ) { | |
| 295 | + properties.express_checkout = Object.keys( wc.wcBlocksRegistry.getExpressPaymentMethods() ); | |
| 296 | + properties.checkout_page_contains_checkout_block = '1'; | |
| 297 | + properties.checkout_page_contains_checkout_shortcode = '0'; | |
| 298 | + | |
| 299 | + _wca.push( properties ); | |
| 300 | + cartItem_{$cart_item_key}_logged = true; | |
| 301 | + } | |
| 302 | + } ); | |
| 303 | + } | |
| 304 | + " | |
| 305 | + ); | |
| 306 | + | |
| 264 | 307 | } |
| 265 | 308 | } |
| 266 | 309 | |
| 267 | 310 | /** |
| @@ -266,8 +309,10 @@ | ||
| 266 | 309 | |
| 267 | 310 | /** |
| 268 | 311 | * After the checkout process, fire an event for each item in the order |
| 269 | 312 | * |
| 313 | + * @deprecated 13.3 | |
| 314 | + * | |
| 270 | 315 | * @param string $order_id Order Id. |
| 271 | 316 | */ |
| 272 | 317 | public function order_process( $order_id ) { |
| 273 | 318 | $order = wc_get_order( $order_id ); |
| @@ -281,11 +326,14 @@ | ||
| 281 | 326 | |
| 282 | 327 | $payment_option = $order->get_payment_method(); |
| 283 | 328 | |
| 284 | 329 | if ( is_object( WC()->session ) ) { |
| 285 | - $create_account = true === WC()->session->get( 'wc_checkout_createaccount_used' ) ? 'Yes' : 'No'; | |
| 330 | + $create_account = true === WC()->session->get( 'wc_checkout_createaccount_used' ) ? 'Yes' : 'No'; | |
| 331 | + $checkout_page_used = true === WC()->session->get( 'checkout_page_used' ) ? 'Yes' : 'No'; | |
| 332 | + | |
| 286 | 333 | } else { |
| 287 | - $create_account = 'No'; | |
| 334 | + $create_account = 'No'; | |
| 335 | + $checkout_page_used = 'No'; | |
| 288 | 336 | } |
| 289 | 337 | |
| 290 | 338 | $guest_checkout = $order->get_user() ? 'No' : 'Yes'; |
| 291 | 339 | |
| @@ -300,10 +348,21 @@ | ||
| 300 | 348 | $express_checkout = array( 'apple_pay' ); |
| 301 | 349 | } |
| 302 | 350 | } |
| 303 | 351 | |
| 352 | + $checkout_page_contains_checkout_block = '0'; | |
| 353 | + $checkout_page_contains_checkout_shortcode = '0'; | |
| 354 | + | |
| 355 | + $order_source = $order->get_created_via(); | |
| 356 | + if ( 'store-api' === $order_source ) { | |
| 357 | + $checkout_page_contains_checkout_block = '1'; | |
| 358 | + } elseif ( 'checkout' === $order_source ) { | |
| 359 | + $checkout_page_contains_checkout_shortcode = '1'; | |
| 360 | + } | |
| 361 | + | |
| 304 | 362 | // loop through products in the order and queue a purchase event. |
| 305 | 363 | foreach ( $order->get_items() as $order_item ) { |
| 364 | + // @phan-suppress-next-line PhanUndeclaredMethod -- Checked before being called. See also https://github.com/phan/phan/issues/1204. | |
| 306 | 365 | $product_id = is_callable( array( $order_item, 'get_product_id' ) ) ? $order_item->get_product_id() : -1; |
| 307 | 366 | |
| 308 | 367 | $order_items = $order->get_items(); |
| 309 | 368 | $order_items_count = 0; |
| @@ -326,8 +385,11 @@ | ||
| 326 | 385 | 'express_checkout' => $express_checkout, |
| 327 | 386 | 'products_count' => $order_items_count, |
| 328 | 387 | 'coupon_used' => $order_coupons_count, |
| 329 | 388 | 'order_value' => $order->get_total(), |
| 389 | + 'from_checkout' => $checkout_page_used, | |
| 390 | + 'checkout_page_contains_checkout_block' => $checkout_page_contains_checkout_block, | |
| 391 | + 'checkout_page_contains_checkout_shortcode' => $checkout_page_contains_checkout_shortcode, | |
| 330 | 392 | ), |
| 331 | 393 | $product_id |
| 332 | 394 | ); |
| 333 | 395 | } |
| @@ -335,12 +397,20 @@ | ||
| 335 | 397 | |
| 336 | 398 | /** |
| 337 | 399 | * Listen for clicks on the "Update Cart" button to know if an item has been removed by |
| 338 | 400 | * updating its quantity to zero |
| 401 | + * | |
| 402 | + * @deprecated 13.3 | |
| 339 | 403 | */ |
| 340 | 404 | public function remove_from_cart_via_quantity() { |
| 341 | - $common_props = $this->render_properties_as_js( | |
| 342 | - $this->get_common_properties() | |
| 405 | + $common_props = wp_json_encode( | |
| 406 | + array_merge( | |
| 407 | + array( | |
| 408 | + '_en' => 'woocommerceanalytics_remove_from_cart', | |
| 409 | + ), | |
| 410 | + $this->get_common_properties() | |
| 411 | + ), | |
| 412 | + JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP | |
| 343 | 413 | ); |
| 344 | 414 | |
| 345 | 415 | wc_enqueue_js( |
| 346 | 416 | " |
| @@ -349,16 +419,14 @@ | ||
| 349 | 419 | cartItems.each( function( item ) { |
| 350 | 420 | var qty = jQuery( this ).find( 'input.qty' ); |
| 351 | 421 | if ( qty && qty.val() === '0' ) { |
| 352 | 422 | var productID = jQuery( this ).find( '.product-remove a' ).data( 'product_id' ); |
| 353 | - _wca.push( { | |
| 354 | - '_en': 'woocommerceanalytics_remove_from_cart', | |
| 355 | - 'pi': productID, " . | |
| 356 | - $common_props . ' | |
| 357 | - } ); | |
| 423 | + var common_props = $common_props; | |
| 424 | + common_props.pi = productID; | |
| 425 | + _wca.push( common_props ); | |
| 358 | 426 | } |
| 359 | 427 | } ); |
| 360 | - } );' | |
| 428 | + } );" | |
| 361 | 429 | ); |
| 362 | 430 | } |
| 363 | 431 | |
| 364 | 432 | /** |
| @@ -363,8 +431,10 @@ | ||
| 363 | 431 | |
| 364 | 432 | /** |
| 365 | 433 | * Gets the inner blocks of a block. |
| 366 | 434 | * |
| 435 | + * @deprecated 13.3 | |
| 436 | + * | |
| 367 | 437 | * @param array $inner_blocks The inner blocks. |
| 368 | 438 | * |
| 369 | 439 | * @return array |
| 370 | 440 | */ |
| @@ -381,8 +451,10 @@ | ||
| 381 | 451 | |
| 382 | 452 | /** |
| 383 | 453 | * Track adding items to the cart. |
| 384 | 454 | * |
| 455 | + * @deprecated 13.3 | |
| 456 | + * | |
| 385 | 457 | * @param string $cart_item_key Cart item key. |
| 386 | 458 | * @param int $product_id Product added to cart. |
| 387 | 459 | * @param int $quantity Quantity added to cart. |
| 388 | 460 | * @param int $variation_id Product variation. |
| @@ -403,8 +475,10 @@ | ||
| 403 | 475 | |
| 404 | 476 | /** |
| 405 | 477 | * Track in-session data. |
| 406 | 478 | * |
| 479 | + * @deprecated 13.3 | |
| 480 | + * | |
| 407 | 481 | * @param int $product_id Product ID. |
| 408 | 482 | * @param int $quantity Quantity. |
| 409 | 483 | * @param string $event Fired event. |
| 410 | 484 | */ |
| @@ -442,8 +516,10 @@ | ||
| 442 | 516 | |
| 443 | 517 | /** |
| 444 | 518 | * Save createaccount post data to be used in $this->order_process. |
| 445 | 519 | * |
| 520 | + * @deprecated 13.3 | |
| 521 | + * | |
| 446 | 522 | * @param array $data post data from the checkout page. |
| 447 | 523 | * |
| 448 | 524 | * @return array |
| 449 | 525 | */ |
| @@ -459,8 +535,10 @@ | ||
| 459 | 535 | } |
| 460 | 536 | |
| 461 | 537 | /** |
| 462 | 538 | * Capture the create account event. Similar to save_checkout_post_data but works with Store API. |
| 539 | + * | |
| 540 | + * @deprecated 13.3 | |
| 463 | 541 | * |
| 464 | 542 | * @param int $customer_id Customer ID. |
| 465 | 543 | * @param array $new_customer_data New customer data. |
| 466 | 544 | */ |