← All changes
|
modules/woocommerce-analytics/classes/class-jetpack-woocommerce-analytics-universal.php
+244
-341
12.5.2
→
16.3-a.1
View file →
| @@ -1,45 +1,50 @@ | ||
| 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 | -use Automattic\Jetpack\Assets; | |
| 10 | - | |
| 11 | 11 | /** |
| 12 | 12 | * Bail if accessed directly |
| 13 | 13 | */ |
| 14 | 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | - exit; | |
| 15 | + exit( 0 ); | |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | /** |
| 19 | 19 | * Class Jetpack_WooCommerce_Analytics_Universal |
| 20 | 20 | * Filters and Actions added to Store pages to perform analytics |
| 21 | + * | |
| 22 | + * @deprecated 13.3 | |
| 21 | 23 | */ |
| 22 | 24 | class Jetpack_WooCommerce_Analytics_Universal { |
| 25 | + | |
| 23 | 26 | /** |
| 27 | + * Trait to handle common analytics functions. | |
| 28 | + */ | |
| 29 | + use Jetpack_WooCommerce_Analytics_Trait; | |
| 30 | + | |
| 31 | + /** | |
| 24 | 32 | * Jetpack_WooCommerce_Analytics_Universal constructor. |
| 33 | + * | |
| 34 | + * @deprecated 13.3 | |
| 25 | 35 | */ |
| 26 | 36 | public function __construct() { |
| 27 | - // loading _wca. | |
| 28 | - add_action( 'wp_head', array( $this, 'wp_head_top' ), 1 ); | |
| 37 | + $this->find_cart_checkout_content_sources(); | |
| 38 | + $this->additional_blocks_on_cart_page = $this->get_additional_blocks_on_page( 'cart' ); | |
| 39 | + $this->additional_blocks_on_checkout_page = $this->get_additional_blocks_on_page( 'checkout' ); | |
| 29 | 40 | |
| 30 | 41 | // add to carts from non-product pages or lists -- search, store etc. |
| 31 | 42 | add_action( 'wp_head', array( $this, 'loop_session_events' ), 2 ); |
| 32 | 43 | |
| 33 | - // loading s.js. | |
| 34 | - add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_tracking_script' ) ); | |
| 35 | - | |
| 36 | 44 | // Capture cart events. |
| 37 | 45 | add_action( 'woocommerce_add_to_cart', array( $this, 'capture_add_to_cart' ), 10, 6 ); |
| 38 | 46 | |
| 39 | - // single product page view. | |
| 40 | - add_action( 'woocommerce_after_single_product', array( $this, 'capture_product_view' ) ); | |
| 41 | - | |
| 42 | 47 | add_action( 'woocommerce_after_cart', array( $this, 'remove_from_cart' ) ); |
| 43 | 48 | add_action( 'woocommerce_after_mini_cart', array( $this, 'remove_from_cart' ) ); |
| 44 | 49 | add_action( 'wcct_before_cart_widget', array( $this, 'remove_from_cart' ) ); |
| 45 | 50 | add_filter( 'woocommerce_cart_item_remove_link', array( $this, 'remove_from_cart_attributes' ), 10, 2 ); |
| @@ -54,129 +59,17 @@ | ||
| 54 | 59 | add_action( 'woocommerce_thankyou', array( $this, 'order_process' ), 10, 1 ); |
| 55 | 60 | add_action( 'woocommerce_after_cart', array( $this, 'remove_from_cart_via_quantity' ), 10, 1 ); |
| 56 | 61 | |
| 57 | 62 | add_filter( 'woocommerce_checkout_posted_data', array( $this, 'save_checkout_post_data' ), 10, 1 ); |
| 58 | - } | |
| 59 | 63 | |
| 60 | - /** | |
| 61 | - * Make _wca available to queue events | |
| 62 | - */ | |
| 63 | - public function wp_head_top() { | |
| 64 | - if ( is_cart() || is_checkout() || is_checkout_pay_page() || is_order_received_page() || is_add_payment_method_page() ) { | |
| 65 | - echo '<script>window._wca_prevent_referrer = true;</script>' . "\r\n"; | |
| 66 | - } | |
| 67 | - echo '<script>window._wca = window._wca || [];</script>' . "\r\n"; | |
| 64 | + add_action( 'woocommerce_created_customer', array( $this, 'capture_created_customer' ), 10, 2 ); | |
| 68 | 65 | } |
| 69 | 66 | |
| 70 | 67 | /** |
| 71 | - * Place script to call s.js, Store Analytics. | |
| 72 | - */ | |
| 73 | - public function enqueue_tracking_script() { | |
| 74 | - $filename = sprintf( | |
| 75 | - 'https://stats.wp.com/s-%d.js', | |
| 76 | - gmdate( 'YW' ) | |
| 77 | - ); | |
| 78 | - | |
| 79 | - Assets::enqueue_async_script( 'woocommerce-analytics', esc_url( $filename ), esc_url( $filename ), array(), null, false ); | |
| 80 | - } | |
| 81 | - | |
| 82 | - /** | |
| 83 | - * Default event properties which should be included with all events. | |
| 68 | + * On product lists or other non-product pages, add an event listener to "Add to Cart" button click | |
| 84 | 69 | * |
| 85 | - * @return array Array of standard event props. | |
| 70 | + * @deprecated 13.3 | |
| 86 | 71 | */ |
| 87 | - public function get_common_properties() { | |
| 88 | - $site_info = array( | |
| 89 | - 'blog_id' => Jetpack::get_option( 'id' ), | |
| 90 | - 'ui' => $this->get_user_id(), | |
| 91 | - 'url' => home_url(), | |
| 92 | - 'woo_version' => WC()->version, | |
| 93 | - ); | |
| 94 | - $cart_checkout_info = self::get_cart_checkout_info(); | |
| 95 | - return array_merge( $site_info, $cart_checkout_info ); | |
| 96 | - } | |
| 97 | - | |
| 98 | - /** | |
| 99 | - * Render tracks event properties as string of JavaScript object props. | |
| 100 | - * | |
| 101 | - * @param array $properties Array of key/value pairs. | |
| 102 | - * @return string String of the form "key1: value1, key2: value2, " (etc). | |
| 103 | - */ | |
| 104 | - private function render_properties_as_js( $properties ) { | |
| 105 | - $js_args_string = ''; | |
| 106 | - foreach ( $properties as $key => $value ) { | |
| 107 | - if ( is_array( $value ) ) { | |
| 108 | - $js_args_string = $js_args_string . "'$key': " . wp_json_encode( $value ) . ','; | |
| 109 | - } else { | |
| 110 | - $js_args_string = $js_args_string . "'$key': '" . esc_js( $value ) . "', "; | |
| 111 | - } | |
| 112 | - } | |
| 113 | - return $js_args_string; | |
| 114 | - } | |
| 115 | - | |
| 116 | - /** | |
| 117 | - * Record an event with optional custom properties. | |
| 118 | - * | |
| 119 | - * @param string $event_name The name of the event to record. | |
| 120 | - * @param integer $product_id The id of the product relating to the event. | |
| 121 | - * @param array $properties Optional array of (key => value) event properties. | |
| 122 | - * | |
| 123 | - * @return string|void | |
| 124 | - */ | |
| 125 | - public function record_event( $event_name, $product_id, $properties = array() ) { | |
| 126 | - $js = $this->process_event_properties( $event_name, $product_id, $properties ); | |
| 127 | - wc_enqueue_js( "_wca.push({$js});" ); | |
| 128 | - } | |
| 129 | - | |
| 130 | - /** | |
| 131 | - * Compose event properties. | |
| 132 | - * | |
| 133 | - * @param string $event_name The name of the event to record. | |
| 134 | - * @param integer $product_id The id of the product relating to the event. | |
| 135 | - * @param array $properties Optional array of (key => value) event properties. | |
| 136 | - * | |
| 137 | - * @return string|void | |
| 138 | - */ | |
| 139 | - public function process_event_properties( $event_name, $product_id, $properties = array() ) { | |
| 140 | - $product = wc_get_product( $product_id ); | |
| 141 | - if ( ! $product instanceof WC_Product ) { | |
| 142 | - return; | |
| 143 | - } | |
| 144 | - $product_details = $this->get_product_details( $product ); | |
| 145 | - | |
| 146 | - /** | |
| 147 | - * Allow defining custom event properties in WooCommerce Analytics. | |
| 148 | - * | |
| 149 | - * @module woocommerce-analytics | |
| 150 | - * | |
| 151 | - * @since 12.5 | |
| 152 | - * | |
| 153 | - * @param array $all_props Array of event props to be filtered. | |
| 154 | - */ | |
| 155 | - $all_props = apply_filters( | |
| 156 | - 'jetpack_woocommerce_analytics_event_props', | |
| 157 | - array_merge( | |
| 158 | - $properties, | |
| 159 | - $this->get_common_properties() | |
| 160 | - ) | |
| 161 | - ); | |
| 162 | - | |
| 163 | - $js = "{ | |
| 164 | - '_en': '" . esc_js( $event_name ) . "', | |
| 165 | - 'pi': '" . esc_js( $product_id ) . "', | |
| 166 | - 'pn': '" . esc_js( $product_details['name'] ) . "', | |
| 167 | - 'pc': '" . esc_js( $product_details['category'] ) . "', | |
| 168 | - 'pp': '" . esc_js( $product_details['price'] ) . "', | |
| 169 | - 'pt': '" . esc_js( $product_details['type'] ) . "'," . | |
| 170 | - $this->render_properties_as_js( $all_props ) . ' | |
| 171 | - }'; | |
| 172 | - | |
| 173 | - return $js; | |
| 174 | - } | |
| 175 | - | |
| 176 | - /** | |
| 177 | - * On product lists or other non-product pages, add an event listener to "Add to Cart" button click | |
| 178 | - */ | |
| 179 | 72 | public function loop_session_events() { |
| 180 | 73 | // Check for previous events queued in session data. |
| 181 | 74 | if ( is_object( WC()->session ) ) { |
| 182 | 75 | $data = WC()->session->get( 'wca_session_data' ); |
| @@ -183,12 +76,12 @@ | ||
| 183 | 76 | if ( ! empty( $data ) ) { |
| 184 | 77 | foreach ( $data as $data_instance ) { |
| 185 | 78 | $this->record_event( |
| 186 | 79 | $data_instance['event'], |
| 187 | - $data_instance['product_id'], | |
| 188 | 80 | array( |
| 189 | 81 | 'pq' => $data_instance['quantity'], |
| 190 | - ) | |
| 82 | + ), | |
| 83 | + $data_instance['product_id'] | |
| 191 | 84 | ); |
| 192 | 85 | } |
| 193 | 86 | // Clear data, now that these events have been recorded. |
| 194 | 87 | WC()->session->set( 'wca_session_data', '' ); |
| @@ -197,12 +90,20 @@ | ||
| 197 | 90 | } |
| 198 | 91 | |
| 199 | 92 | /** |
| 200 | 93 | * On the cart page, add an event listener for removal of product click |
| 94 | + * | |
| 95 | + * @deprecated 13.3 | |
| 201 | 96 | */ |
| 202 | 97 | public function remove_from_cart() { |
| 203 | - $common_props = $this->render_properties_as_js( | |
| 204 | - $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 | |
| 205 | 106 | ); |
| 206 | 107 | |
| 207 | 108 | // We listen at div.woocommerce because the cart 'form' contents get forcibly |
| 208 | 109 | // updated and subsequent removals from cart would then not have this click |
| @@ -210,19 +111,13 @@ | ||
| 210 | 111 | wc_enqueue_js( |
| 211 | 112 | "jQuery( 'div.woocommerce' ).on( 'click', 'a.remove', function() { |
| 212 | 113 | var productID = jQuery( this ).data( 'product_id' ); |
| 213 | 114 | var quantity = jQuery( this ).parent().parent().find( '.qty' ).val() |
| 214 | - var productDetails = { | |
| 215 | - 'id': productID, | |
| 216 | - 'quantity': quantity ? quantity : '1', | |
| 217 | - }; | |
| 218 | - _wca.push( { | |
| 219 | - '_en': 'woocommerceanalytics_remove_from_cart', | |
| 220 | - 'pi': productDetails.id, | |
| 221 | - 'pq': productDetails.quantity, " . | |
| 222 | - $common_props . ' | |
| 223 | - } ); | |
| 224 | - } );' | |
| 115 | + var common_props = $common_props; | |
| 116 | + common_props.pi = productID; | |
| 117 | + common_props.pq = quantity ? quantity : '1'; | |
| 118 | + _wca.push( common_props ); | |
| 119 | + } );" | |
| 225 | 120 | ); |
| 226 | 121 | } |
| 227 | 122 | |
| 228 | 123 | /** |
| @@ -227,15 +122,17 @@ | ||
| 227 | 122 | |
| 228 | 123 | /** |
| 229 | 124 | * Adds the product ID to the remove product link (for use by remove_from_cart above) if not present |
| 230 | 125 | * |
| 126 | + * @deprecated 13.3 | |
| 127 | + * | |
| 231 | 128 | * @param string $url Full HTML a tag of the link to remove an item from the cart. |
| 232 | 129 | * @param string $key Unique Key ID for a cart item. |
| 233 | 130 | * |
| 234 | - * @return mixed. | |
| 131 | + * @return string | |
| 235 | 132 | */ |
| 236 | 133 | public function remove_from_cart_attributes( $url, $key ) { |
| 237 | - if ( false !== strpos( $url, 'data-product_id' ) ) { | |
| 134 | + if ( str_contains( $url, 'data-product_id' ) ) { | |
| 238 | 135 | return $url; |
| 239 | 136 | } |
| 240 | 137 | |
| 241 | 138 | $item = WC()->cart->get_cart_item( $key ); |
| @@ -250,47 +147,63 @@ | ||
| 250 | 147 | return $url; |
| 251 | 148 | } |
| 252 | 149 | |
| 253 | 150 | /** |
| 254 | - * Gather relevant product information | |
| 151 | + * Get the selected shipping option for a cart item. If the name cannot be found in the options table, the method's | |
| 152 | + * ID will be used. | |
| 255 | 153 | * |
| 256 | - * @param array $product product. | |
| 257 | - * @return array | |
| 154 | + * @deprecated 13.3 | |
| 155 | + * | |
| 156 | + * @param string $cart_item_key the cart item key. | |
| 157 | + * | |
| 158 | + * @return mixed|bool | |
| 258 | 159 | */ |
| 259 | - public function get_product_details( $product ) { | |
| 260 | - return array( | |
| 261 | - 'id' => $product->get_id(), | |
| 262 | - 'name' => $product->get_title(), | |
| 263 | - 'category' => $this->get_product_categories_concatenated( $product ), | |
| 264 | - 'price' => $product->get_price(), | |
| 265 | - 'type' => $product->get_type(), | |
| 266 | - ); | |
| 267 | - } | |
| 160 | + public function get_shipping_option_for_item( $cart_item_key ) { | |
| 161 | + $packages = wc()->shipping()->get_packages(); | |
| 162 | + $selected_options = wc()->session->get( 'chosen_shipping_methods' ); | |
| 268 | 163 | |
| 269 | - /** | |
| 270 | - * Track a product page view | |
| 271 | - */ | |
| 272 | - public function capture_product_view() { | |
| 273 | - global $product; | |
| 274 | - if ( ! $product instanceof WC_Product ) { | |
| 275 | - return; | |
| 164 | + if ( ! is_array( $packages ) || ! is_array( $selected_options ) ) { | |
| 165 | + return false; | |
| 276 | 166 | } |
| 277 | 167 | |
| 278 | - $this->record_event( | |
| 279 | - 'woocommerceanalytics_product_view', | |
| 280 | - $product->get_id() | |
| 281 | - ); | |
| 168 | + foreach ( $packages as $package_id => $package ) { | |
| 169 | + | |
| 170 | + if ( ! isset( $package['contents'] ) || ! is_array( $package['contents'] ) ) { | |
| 171 | + return false; | |
| 172 | + } | |
| 173 | + | |
| 174 | + foreach ( $package['contents'] as $package_item ) { | |
| 175 | + if ( ! isset( $package_item['key'] ) || $package_item['key'] !== $cart_item_key || ! isset( $selected_options[ $package_id ] ) ) { | |
| 176 | + continue; | |
| 177 | + } | |
| 178 | + $selected_rate_id = $selected_options[ $package_id ]; | |
| 179 | + $method_key_id = sanitize_text_field( str_replace( ':', '_', $selected_rate_id ) ); | |
| 180 | + $option_name = 'woocommerce_' . $method_key_id . '_settings'; | |
| 181 | + $option_value = get_option( $option_name ); | |
| 182 | + $title = ''; | |
| 183 | + if ( is_array( $option_value ) && isset( $option_value['title'] ) ) { | |
| 184 | + $title = $option_value['title']; | |
| 185 | + } | |
| 186 | + if ( ! $title ) { | |
| 187 | + return $selected_rate_id; | |
| 188 | + } | |
| 189 | + return $title; | |
| 190 | + } | |
| 191 | + } | |
| 192 | + | |
| 193 | + return false; | |
| 282 | 194 | } |
| 283 | 195 | |
| 284 | 196 | /** |
| 285 | 197 | * On the Checkout page, trigger an event for each product in the cart |
| 198 | + * | |
| 199 | + * @deprecated 13.3 | |
| 286 | 200 | */ |
| 287 | 201 | public function checkout_process() { |
| 288 | - $cart = WC()->cart->get_cart(); | |
| 202 | + global $post; | |
| 203 | + $checkout_page_id = wc_get_page_id( 'checkout' ); | |
| 204 | + $cart = WC()->cart->get_cart(); | |
| 289 | 205 | |
| 290 | - $guest_checkout = ucfirst( get_option( 'woocommerce_enable_guest_checkout', 'No' ) ); | |
| 291 | - $create_account = ucfirst( get_option( 'woocommerce_enable_signup_and_login_from_checkout', 'No' ) ); | |
| 292 | - | |
| 293 | 206 | $enabled_payment_options = array_filter( |
| 294 | 207 | WC()->payment_gateways->get_available_payment_gateways(), |
| 295 | 208 | function ( $payment_gateway ) { |
| 296 | 209 | if ( ! $payment_gateway instanceof WC_Payment_Gateway ) { |
| @@ -301,18 +214,14 @@ | ||
| 301 | 214 | } |
| 302 | 215 | ); |
| 303 | 216 | |
| 304 | 217 | $enabled_payment_options = array_keys( $enabled_payment_options ); |
| 305 | - $include_express_payment = false; | |
| 306 | 218 | |
| 307 | - $wcpay_version = get_option( 'woocommerce_woocommerce_payments_version' ); | |
| 308 | - $has_required_wcpay_version = version_compare( $wcpay_version, '2.9.0', '>=' ); | |
| 309 | - // Check express payment availablity only if WC Pay is enabled and express checkout (payment request) is enabled. | |
| 310 | - if ( in_array( 'woocommerce_payments', $enabled_payment_options, true ) && $has_required_wcpay_version ) { | |
| 311 | - $wcpay_settings = get_option( 'woocommerce_woocommerce_payments_settings', array() ); | |
| 312 | - if ( array_key_exists( 'payment_request', $wcpay_settings ) && 'yes' === $wcpay_settings['payment_request'] ) { | |
| 313 | - $include_express_payment = true; | |
| 314 | - } | |
| 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(); | |
| 315 | 224 | } |
| 316 | 225 | |
| 317 | 226 | foreach ( $cart as $cart_item_key => $cart_item ) { |
| 318 | 227 | /** |
| @@ -323,51 +232,79 @@ | ||
| 323 | 232 | if ( ! $product || ! $product instanceof WC_Product ) { |
| 324 | 233 | continue; |
| 325 | 234 | } |
| 326 | 235 | |
| 327 | - if ( true === $include_express_payment ) { | |
| 328 | - $properties = $this->process_event_properties( | |
| 329 | - 'woocommerceanalytics_product_checkout', | |
| 330 | - $product->get_id(), | |
| 331 | - array( | |
| 332 | - 'pq' => $cart_item['quantity'], | |
| 333 | - 'payment_options' => $enabled_payment_options, | |
| 334 | - 'device' => wp_is_mobile() ? 'mobile' : 'desktop', | |
| 335 | - 'guest_checkout' => 'Yes' === $guest_checkout ? 'Yes' : 'No', | |
| 336 | - 'create_account' => 'Yes' === $create_account ? 'Yes' : 'No', | |
| 337 | - 'express_checkout' => 'null', | |
| 338 | - ) | |
| 339 | - ); | |
| 340 | - wc_enqueue_js( | |
| 341 | - " | |
| 342 | - // wcpay.payment-request.availability event gets fired twice. | |
| 343 | - // make sure we push only one event. | |
| 344 | - var cartItem_{$cart_item_key}_logged = false; | |
| 345 | - wp.hooks.addAction('wcpay.payment-request.availability', 'wcpay', function(args) { | |
| 346 | - if ( true === cartItem_{$cart_item_key}_logged ) { | |
| 347 | - return; | |
| 348 | - } | |
| 349 | - var properties = {$properties}; | |
| 350 | - properties.express_checkout = args.paymentRequestType; | |
| 351 | - _wca.push(properties); | |
| 352 | - cartItem_{$cart_item_key}_logged = true; | |
| 353 | - }); | |
| 236 | + $data = $this->get_cart_checkout_shared_data(); | |
| 237 | + | |
| 238 | + $data['from_checkout'] = $is_in_checkout_page; | |
| 239 | + | |
| 240 | + if ( ! empty( $data['products'] ) ) { | |
| 241 | + unset( $data['products'] ); | |
| 242 | + } | |
| 243 | + | |
| 244 | + if ( ! empty( $data['shipping_options_count'] ) ) { | |
| 245 | + unset( $data['shipping_options_count'] ); | |
| 246 | + } | |
| 247 | + | |
| 248 | + $data['pq'] = $cart_item['quantity']; | |
| 249 | + | |
| 250 | + $properties = $this->process_event_properties( | |
| 251 | + 'woocommerceanalytics_product_checkout', | |
| 252 | + $data, | |
| 253 | + $product->get_id() | |
| 254 | + ); | |
| 255 | + | |
| 256 | + wc_enqueue_js( | |
| 354 | 257 | " |
| 355 | - ); | |
| 356 | - } else { | |
| 357 | - $this->record_event( | |
| 358 | - 'woocommerceanalytics_product_checkout', | |
| 359 | - $product->get_id(), | |
| 360 | - array( | |
| 361 | - 'pq' => $cart_item['quantity'], | |
| 362 | - 'payment_options' => $enabled_payment_options, | |
| 363 | - 'device' => wp_is_mobile() ? 'mobile' : 'desktop', | |
| 364 | - 'guest_checkout' => 'Yes' === $guest_checkout ? 'Yes' : 'No', | |
| 365 | - 'create_account' => 'Yes' === $create_account ? 'Yes' : 'No', | |
| 366 | - 'express_checkout' => 'null', | |
| 367 | - ) | |
| 368 | - ); | |
| 369 | - } | |
| 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 | + | |
| 370 | 307 | } |
| 371 | 308 | } |
| 372 | 309 | |
| 373 | 310 | /** |
| @@ -372,8 +309,10 @@ | ||
| 372 | 309 | |
| 373 | 310 | /** |
| 374 | 311 | * After the checkout process, fire an event for each item in the order |
| 375 | 312 | * |
| 313 | + * @deprecated 13.3 | |
| 314 | + * | |
| 376 | 315 | * @param string $order_id Order Id. |
| 377 | 316 | */ |
| 378 | 317 | public function order_process( $order_id ) { |
| 379 | 318 | $order = wc_get_order( $order_id ); |
| @@ -387,14 +326,17 @@ | ||
| 387 | 326 | |
| 388 | 327 | $payment_option = $order->get_payment_method(); |
| 389 | 328 | |
| 390 | 329 | if ( is_object( WC()->session ) ) { |
| 391 | - $create_account = true === WC()->session->get( 'wc_checkout_createaccount_used' ) ? 'Y' : 'N'; | |
| 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 | + | |
| 392 | 333 | } else { |
| 393 | - $create_account = 'N'; | |
| 334 | + $create_account = 'No'; | |
| 335 | + $checkout_page_used = 'No'; | |
| 394 | 336 | } |
| 395 | 337 | |
| 396 | - $guest_checkout = $order->get_user() ? 'N' : 'Y'; | |
| 338 | + $guest_checkout = $order->get_user() ? 'No' : 'Yes'; | |
| 397 | 339 | |
| 398 | 340 | $express_checkout = 'null'; |
| 399 | 341 | // When the payment option is woocommerce_payment |
| 400 | 342 | // See if Google Pay or Apple Pay was used. |
| @@ -406,24 +348,50 @@ | ||
| 406 | 348 | $express_checkout = array( 'apple_pay' ); |
| 407 | 349 | } |
| 408 | 350 | } |
| 409 | 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 | + | |
| 410 | 362 | // loop through products in the order and queue a purchase event. |
| 411 | 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. | |
| 412 | 365 | $product_id = is_callable( array( $order_item, 'get_product_id' ) ) ? $order_item->get_product_id() : -1; |
| 413 | 366 | |
| 367 | + $order_items = $order->get_items(); | |
| 368 | + $order_items_count = 0; | |
| 369 | + if ( is_array( $order_items ) ) { | |
| 370 | + $order_items_count = count( $order_items ); | |
| 371 | + } | |
| 372 | + $order_coupons = $order->get_coupons(); | |
| 373 | + $order_coupons_count = 0; | |
| 374 | + if ( is_array( $order_coupons ) ) { | |
| 375 | + $order_coupons_count = count( $order_coupons ); | |
| 376 | + } | |
| 414 | 377 | $this->record_event( |
| 415 | 378 | 'woocommerceanalytics_product_purchase', |
| 416 | - $product_id, | |
| 417 | 379 | array( |
| 418 | 380 | 'oi' => $order->get_order_number(), |
| 419 | 381 | 'pq' => $order_item->get_quantity(), |
| 420 | - 'device' => wp_is_mobile() ? 'mobile' : 'desktop', | |
| 421 | 382 | 'payment_option' => $payment_option, |
| 422 | 383 | 'create_account' => $create_account, |
| 423 | 384 | 'guest_checkout' => $guest_checkout, |
| 424 | 385 | 'express_checkout' => $express_checkout, |
| 425 | - ) | |
| 386 | + 'products_count' => $order_items_count, | |
| 387 | + 'coupon_used' => $order_coupons_count, | |
| 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, | |
| 392 | + ), | |
| 393 | + $product_id | |
| 426 | 394 | ); |
| 427 | 395 | } |
| 428 | 396 | } |
| 429 | 397 | |
| @@ -429,12 +397,20 @@ | ||
| 429 | 397 | |
| 430 | 398 | /** |
| 431 | 399 | * Listen for clicks on the "Update Cart" button to know if an item has been removed by |
| 432 | 400 | * updating its quantity to zero |
| 401 | + * | |
| 402 | + * @deprecated 13.3 | |
| 433 | 403 | */ |
| 434 | 404 | public function remove_from_cart_via_quantity() { |
| 435 | - $common_props = $this->render_properties_as_js( | |
| 436 | - $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 | |
| 437 | 413 | ); |
| 438 | 414 | |
| 439 | 415 | wc_enqueue_js( |
| 440 | 416 | " |
| @@ -443,36 +419,42 @@ | ||
| 443 | 419 | cartItems.each( function( item ) { |
| 444 | 420 | var qty = jQuery( this ).find( 'input.qty' ); |
| 445 | 421 | if ( qty && qty.val() === '0' ) { |
| 446 | 422 | var productID = jQuery( this ).find( '.product-remove a' ).data( 'product_id' ); |
| 447 | - _wca.push( { | |
| 448 | - '_en': 'woocommerceanalytics_remove_from_cart', | |
| 449 | - 'pi': productID, " . | |
| 450 | - $common_props . ' | |
| 451 | - } ); | |
| 423 | + var common_props = $common_props; | |
| 424 | + common_props.pi = productID; | |
| 425 | + _wca.push( common_props ); | |
| 452 | 426 | } |
| 453 | 427 | } ); |
| 454 | - } );' | |
| 428 | + } );" | |
| 455 | 429 | ); |
| 456 | 430 | } |
| 457 | 431 | |
| 458 | 432 | /** |
| 459 | - * Get the current user id | |
| 433 | + * Gets the inner blocks of a block. | |
| 460 | 434 | * |
| 461 | - * @return int | |
| 435 | + * @deprecated 13.3 | |
| 436 | + * | |
| 437 | + * @param array $inner_blocks The inner blocks. | |
| 438 | + * | |
| 439 | + * @return array | |
| 462 | 440 | */ |
| 463 | - public function get_user_id() { | |
| 464 | - if ( is_user_logged_in() ) { | |
| 465 | - $blogid = Jetpack::get_option( 'id' ); | |
| 466 | - $userid = get_current_user_id(); | |
| 467 | - return $blogid . ':' . $userid; | |
| 441 | + private function get_inner_blocks( $inner_blocks ) { | |
| 442 | + $block_names = array(); | |
| 443 | + if ( ! empty( $inner_blocks['blockName'] ) ) { | |
| 444 | + $block_names[] = $inner_blocks['blockName']; | |
| 468 | 445 | } |
| 469 | - return 'null'; | |
| 446 | + if ( isset( $inner_blocks['innerBlocks'] ) && is_array( $inner_blocks['innerBlocks'] ) ) { | |
| 447 | + $block_names = array_merge( $block_names, $this->get_inner_blocks( $inner_blocks['innerBlocks'] ) ); | |
| 448 | + } | |
| 449 | + return $block_names; | |
| 470 | 450 | } |
| 471 | 451 | |
| 472 | 452 | /** |
| 473 | 453 | * Track adding items to the cart. |
| 474 | 454 | * |
| 455 | + * @deprecated 13.3 | |
| 456 | + * | |
| 475 | 457 | * @param string $cart_item_key Cart item key. |
| 476 | 458 | * @param int $product_id Product added to cart. |
| 477 | 459 | * @param int $quantity Quantity added to cart. |
| 478 | 460 | * @param int $variation_id Product variation. |
| @@ -493,8 +475,10 @@ | ||
| 493 | 475 | |
| 494 | 476 | /** |
| 495 | 477 | * Track in-session data. |
| 496 | 478 | * |
| 479 | + * @deprecated 13.3 | |
| 480 | + * | |
| 497 | 481 | * @param int $product_id Product ID. |
| 498 | 482 | * @param int $quantity Quantity. |
| 499 | 483 | * @param string $event Fired event. |
| 500 | 484 | */ |
| @@ -530,122 +514,41 @@ | ||
| 530 | 514 | WC()->session->set( 'wca_session_data', $data ); |
| 531 | 515 | } |
| 532 | 516 | |
| 533 | 517 | /** |
| 534 | - * Gets product categories or varation attributes as a formatted concatenated string | |
| 518 | + * Save createaccount post data to be used in $this->order_process. | |
| 535 | 519 | * |
| 536 | - * @param object $product WC_Product. | |
| 537 | - * @return string | |
| 538 | - */ | |
| 539 | - public function get_product_categories_concatenated( $product ) { | |
| 540 | - | |
| 541 | - if ( ! $product instanceof WC_Product ) { | |
| 542 | - return ''; | |
| 543 | - } | |
| 544 | - | |
| 545 | - $variation_data = $product->is_type( 'variation' ) ? wc_get_product_variation_attributes( $product->get_id() ) : ''; | |
| 546 | - if ( is_array( $variation_data ) && ! empty( $variation_data ) ) { | |
| 547 | - $line = wc_get_formatted_variation( $variation_data, true ); | |
| 548 | - } else { | |
| 549 | - $out = array(); | |
| 550 | - $categories = get_the_terms( $product->get_id(), 'product_cat' ); | |
| 551 | - if ( $categories ) { | |
| 552 | - foreach ( $categories as $category ) { | |
| 553 | - $out[] = $category->name; | |
| 554 | - } | |
| 555 | - } | |
| 556 | - $line = implode( '/', $out ); | |
| 557 | - } | |
| 558 | - return $line; | |
| 559 | - } | |
| 560 | - | |
| 561 | - /** | |
| 562 | - * Search a specific post for text content. | |
| 520 | + * @deprecated 13.3 | |
| 563 | 521 | * |
| 564 | - * Note: similar code is in a WooCommerce core PR: | |
| 565 | - * https://github.com/woocommerce/woocommerce/pull/25932 | |
| 522 | + * @param array $data post data from the checkout page. | |
| 566 | 523 | * |
| 567 | - * @param integer $post_id The id of the post to search. | |
| 568 | - * @param string $text The text to search for. | |
| 569 | - * @return integer 1 if post contains $text (otherwise 0). | |
| 570 | - */ | |
| 571 | - public static function post_contains_text( $post_id, $text ) { | |
| 572 | - global $wpdb; | |
| 573 | - | |
| 574 | - // Search for the text anywhere in the post. | |
| 575 | - $wildcarded = "%{$text}%"; | |
| 576 | - | |
| 577 | - $result = $wpdb->get_var( | |
| 578 | - $wpdb->prepare( | |
| 579 | - " | |
| 580 | - SELECT COUNT( * ) FROM {$wpdb->prefix}posts | |
| 581 | - WHERE ID=%d | |
| 582 | - AND {$wpdb->prefix}posts.post_content LIKE %s | |
| 583 | - ", | |
| 584 | - array( $post_id, $wildcarded ) | |
| 585 | - ) | |
| 586 | - ); | |
| 587 | - | |
| 588 | - return ( '0' !== $result ) ? 1 : 0; | |
| 589 | - } | |
| 590 | - | |
| 591 | - /** | |
| 592 | - * Get info about the cart & checkout pages, in particular | |
| 593 | - * whether the store is using shortcodes or Gutenberg blocks. | |
| 594 | - * This info is cached in a transient. | |
| 595 | - * | |
| 596 | - * Note: similar code is in a WooCommerce core PR: | |
| 597 | - * https://github.com/woocommerce/woocommerce/pull/25932 | |
| 598 | - * | |
| 599 | 524 | * @return array |
| 600 | 525 | */ |
| 601 | - public static function get_cart_checkout_info() { | |
| 602 | - $transient_name = 'jetpack_woocommerce_analytics_cart_checkout_info_cache'; | |
| 603 | - | |
| 604 | - $info = get_transient( $transient_name ); | |
| 605 | - if ( false === $info ) { | |
| 606 | - $cart_page_id = wc_get_page_id( 'cart' ); | |
| 607 | - $checkout_page_id = wc_get_page_id( 'checkout' ); | |
| 608 | - | |
| 609 | - $info = array( | |
| 610 | - 'cart_page_contains_cart_block' => self::post_contains_text( | |
| 611 | - $cart_page_id, | |
| 612 | - '<!-- wp:woocommerce/cart' | |
| 613 | - ), | |
| 614 | - 'cart_page_contains_cart_shortcode' => self::post_contains_text( | |
| 615 | - $cart_page_id, | |
| 616 | - '[woocommerce_cart]' | |
| 617 | - ), | |
| 618 | - 'checkout_page_contains_checkout_block' => self::post_contains_text( | |
| 619 | - $checkout_page_id, | |
| 620 | - '<!-- wp:woocommerce/checkout' | |
| 621 | - ), | |
| 622 | - 'checkout_page_contains_checkout_shortcode' => self::post_contains_text( | |
| 623 | - $checkout_page_id, | |
| 624 | - '[woocommerce_checkout]' | |
| 625 | - ), | |
| 626 | - ); | |
| 627 | - | |
| 628 | - set_transient( $transient_name, $info, DAY_IN_SECONDS ); | |
| 526 | + public function save_checkout_post_data( array $data ) { | |
| 527 | + $session = WC()->session; | |
| 528 | + if ( is_object( $session ) ) { | |
| 529 | + if ( isset( $data['createaccount'] ) && ! empty( $data['createaccount'] ) ) { | |
| 530 | + $session->set( 'wc_checkout_createaccount_used', true ); | |
| 531 | + $session->save_data(); | |
| 532 | + } | |
| 629 | 533 | } |
| 630 | - | |
| 631 | - return $info; | |
| 534 | + return $data; | |
| 632 | 535 | } |
| 633 | 536 | |
| 634 | 537 | /** |
| 635 | - * Save createaccount post data to be used in $this->order_process. | |
| 538 | + * Capture the create account event. Similar to save_checkout_post_data but works with Store API. | |
| 636 | 539 | * |
| 637 | - * @param array $data post data from the checkout page. | |
| 540 | + * @deprecated 13.3 | |
| 638 | 541 | * |
| 639 | - * @return array | |
| 542 | + * @param int $customer_id Customer ID. | |
| 543 | + * @param array $new_customer_data New customer data. | |
| 640 | 544 | */ |
| 641 | - public function save_checkout_post_data( array $data ) { | |
| 545 | + public function capture_created_customer( $customer_id, $new_customer_data ) { | |
| 642 | 546 | $session = WC()->session; |
| 643 | 547 | if ( is_object( $session ) ) { |
| 644 | - if ( isset( $data['createaccount'] ) && ! empty( $data['createaccount'] ) ) { | |
| 548 | + if ( str_contains( $new_customer_data['source'], 'store-api' ) ) { | |
| 645 | 549 | $session->set( 'wc_checkout_createaccount_used', true ); |
| 646 | 550 | $session->save_data(); |
| 647 | 551 | } |
| 648 | 552 | } |
| 649 | - return $data; | |
| 650 | 553 | } |
| 651 | 554 | } |