PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.5
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.5
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
← All changes | modules/woocommerce-analytics/classes/class-jetpack-woocommerce-analytics-universal.php +254 -328 12.1.3 → 16.3-a.5 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,117 +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 - $all_props = array_merge(
147 - $properties,
148 - $this->get_common_properties()
149 - );
150 -
151 - $js = "{
152 - '_en': '" . esc_js( $event_name ) . "',
153 - 'pi': '" . esc_js( $product_id ) . "',
154 - 'pn': '" . esc_js( $product_details['name'] ) . "',
155 - 'pc': '" . esc_js( $product_details['category'] ) . "',
156 - 'pp': '" . esc_js( $product_details['price'] ) . "',
157 - 'pt': '" . esc_js( $product_details['type'] ) . "'," .
158 - $this->render_properties_as_js( $all_props ) . '
159 - }';
160 -
161 - return $js;
162 - }
163 -
164 - /**
165 - * On product lists or other non-product pages, add an event listener to "Add to Cart" button click
166 - */
167 72 public function loop_session_events() {
168 73 // Check for previous events queued in session data.
169 74 if ( is_object( WC()->session ) ) {
170 75 $data = WC()->session->get( 'wca_session_data' );
@@ -171,12 +76,12 @@
171 76 if ( ! empty( $data ) ) {
172 77 foreach ( $data as $data_instance ) {
173 78 $this->record_event(
174 79 $data_instance['event'],
175 - $data_instance['product_id'],
176 80 array(
177 81 'pq' => $data_instance['quantity'],
178 - )
82 + ),
83 + $data_instance['product_id']
179 84 );
180 85 }
181 86 // Clear data, now that these events have been recorded.
182 87 WC()->session->set( 'wca_session_data', '' );
@@ -185,12 +90,20 @@
185 90 }
186 91
187 92 /**
188 93 * On the cart page, add an event listener for removal of product click
94 + *
95 + * @deprecated 13.3
189 96 */
190 97 public function remove_from_cart() {
191 - $common_props = $this->render_properties_as_js(
192 - $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
193 106 );
194 107
195 108 // We listen at div.woocommerce because the cart 'form' contents get forcibly
196 109 // updated and subsequent removals from cart would then not have this click
@@ -198,19 +111,13 @@
198 111 wc_enqueue_js(
199 112 "jQuery( 'div.woocommerce' ).on( 'click', 'a.remove', function() {
200 113 var productID = jQuery( this ).data( 'product_id' );
201 114 var quantity = jQuery( this ).parent().parent().find( '.qty' ).val()
202 - var productDetails = {
203 - 'id': productID,
204 - 'quantity': quantity ? quantity : '1',
205 - };
206 - _wca.push( {
207 - '_en': 'woocommerceanalytics_remove_from_cart',
208 - 'pi': productDetails.id,
209 - 'pq': productDetails.quantity, " .
210 - $common_props . '
211 - } );
212 - } );'
115 + var common_props = $common_props;
116 + common_props.pi = productID;
117 + common_props.pq = quantity ? quantity : '1';
118 + _wca.push( common_props );
119 + } );"
213 120 );
214 121 }
215 122
216 123 /**
@@ -215,15 +122,17 @@
215 122
216 123 /**
217 124 * Adds the product ID to the remove product link (for use by remove_from_cart above) if not present
218 125 *
126 + * @deprecated 13.3
127 + *
219 128 * @param string $url Full HTML a tag of the link to remove an item from the cart.
220 129 * @param string $key Unique Key ID for a cart item.
221 130 *
222 - * @return mixed.
131 + * @return string
223 132 */
224 133 public function remove_from_cart_attributes( $url, $key ) {
225 - if ( false !== strpos( $url, 'data-product_id' ) ) {
134 + if ( str_contains( $url, 'data-product_id' ) ) {
226 135 return $url;
227 136 }
228 137
229 138 $item = WC()->cart->get_cart_item( $key );
@@ -238,43 +147,63 @@
238 147 return $url;
239 148 }
240 149
241 150 /**
242 - * 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.
243 153 *
244 - * @param array $product product.
245 - * @return array
154 + * @deprecated 13.3
155 + *
156 + * @param string $cart_item_key the cart item key.
157 + *
158 + * @return mixed|bool
246 159 */
247 - public function get_product_details( $product ) {
248 - return array(
249 - 'id' => $product->get_id(),
250 - 'name' => $product->get_title(),
251 - 'category' => $this->get_product_categories_concatenated( $product ),
252 - 'price' => $product->get_price(),
253 - 'type' => $product->get_type(),
254 - );
255 - }
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' );
256 163
257 - /**
258 - * Track a product page view
259 - */
260 - public function capture_product_view() {
261 - global $product;
262 - $this->record_event(
263 - 'woocommerceanalytics_product_view',
264 - $product->get_id()
265 - );
164 + if ( ! is_array( $packages ) || ! is_array( $selected_options ) ) {
165 + return false;
166 + }
167 +
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;
266 194 }
267 195
268 196 /**
269 197 * On the Checkout page, trigger an event for each product in the cart
198 + *
199 + * @deprecated 13.3
270 200 */
271 201 public function checkout_process() {
272 - $cart = WC()->cart->get_cart();
202 + global $post;
203 + $checkout_page_id = wc_get_page_id( 'checkout' );
204 + $cart = WC()->cart->get_cart();
273 205
274 - $guest_checkout = ucfirst( get_option( 'woocommerce_enable_guest_checkout', 'No' ) );
275 - $create_account = ucfirst( get_option( 'woocommerce_enable_signup_and_login_from_checkout', 'No' ) );
276 -
277 206 $enabled_payment_options = array_filter(
278 207 WC()->payment_gateways->get_available_payment_gateways(),
279 208 function ( $payment_gateway ) {
280 209 if ( ! $payment_gateway instanceof WC_Payment_Gateway ) {
@@ -285,18 +214,14 @@
285 214 }
286 215 );
287 216
288 217 $enabled_payment_options = array_keys( $enabled_payment_options );
289 - $include_express_payment = false;
290 218
291 - $wcpay_version = get_option( 'woocommerce_woocommerce_payments_version' );
292 - $has_required_wcpay_version = version_compare( $wcpay_version, '2.9.0', '>=' );
293 - // Check express payment availablity only if WC Pay is enabled and express checkout (payment request) is enabled.
294 - if ( in_array( 'woocommerce_payments', $enabled_payment_options, true ) && $has_required_wcpay_version ) {
295 - $wcpay_settings = get_option( 'woocommerce_woocommerce_payments_settings', array() );
296 - if ( array_key_exists( 'payment_request', $wcpay_settings ) && 'yes' === $wcpay_settings['payment_request'] ) {
297 - $include_express_payment = true;
298 - }
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();
299 224 }
300 225
301 226 foreach ( $cart as $cart_item_key => $cart_item ) {
302 227 /**
@@ -303,55 +228,83 @@
303 228 * This filter is already documented in woocommerce/templates/cart/cart.php
304 229 */
305 230 $product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
306 231
307 - if ( ! $product ) {
232 + if ( ! $product || ! $product instanceof WC_Product ) {
308 233 continue;
309 234 }
310 235
311 - if ( true === $include_express_payment ) {
312 - $properties = $this->process_event_properties(
313 - 'woocommerceanalytics_product_checkout',
314 - $product->get_id(),
315 - array(
316 - 'pq' => $cart_item['quantity'],
317 - 'payment_options' => $enabled_payment_options,
318 - 'device' => wp_is_mobile() ? 'mobile' : 'desktop',
319 - 'guest_checkout' => 'Yes' === $guest_checkout ? 'Yes' : 'No',
320 - 'create_account' => 'Yes' === $create_account ? 'Yes' : 'No',
321 - 'express_checkout' => 'null',
322 - )
323 - );
324 - wc_enqueue_js(
325 - "
326 - // wcpay.payment-request.availability event gets fired twice.
327 - // make sure we push only one event.
328 - var cartItem_{$cart_item_key}_logged = false;
329 - wp.hooks.addAction('wcpay.payment-request.availability', 'wcpay', function(args) {
330 - if ( true === cartItem_{$cart_item_key}_logged ) {
331 - return;
332 - }
333 - var properties = {$properties};
334 - properties.express_checkout = args.paymentRequestType;
335 - _wca.push(properties);
336 - cartItem_{$cart_item_key}_logged = true;
337 - });
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(
338 257 "
339 - );
340 - } else {
341 - $this->record_event(
342 - 'woocommerceanalytics_product_checkout',
343 - $product->get_id(),
344 - array(
345 - 'pq' => $cart_item['quantity'],
346 - 'payment_options' => $enabled_payment_options,
347 - 'device' => wp_is_mobile() ? 'mobile' : 'desktop',
348 - 'guest_checkout' => 'Yes' === $guest_checkout ? 'Yes' : 'No',
349 - 'create_account' => 'Yes' === $create_account ? 'Yes' : 'No',
350 - 'express_checkout' => 'null',
351 - )
352 - );
353 - }
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 +
354 307 }
355 308 }
356 309
357 310 /**
@@ -356,22 +309,34 @@
356 309
357 310 /**
358 311 * After the checkout process, fire an event for each item in the order
359 312 *
313 + * @deprecated 13.3
314 + *
360 315 * @param string $order_id Order Id.
361 316 */
362 317 public function order_process( $order_id ) {
363 318 $order = wc_get_order( $order_id );
364 319
320 + if (
321 + ! $order
322 + || ! $order instanceof WC_Order
323 + ) {
324 + return;
325 + }
326 +
365 327 $payment_option = $order->get_payment_method();
366 328
367 329 if ( is_object( WC()->session ) ) {
368 - $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 +
369 333 } else {
370 - $create_account = 'N';
334 + $create_account = 'No';
335 + $checkout_page_used = 'No';
371 336 }
372 337
373 - $guest_checkout = $order->get_user() ? 'N' : 'Y';
338 + $guest_checkout = $order->get_user() ? 'No' : 'Yes';
374 339
375 340 $express_checkout = 'null';
376 341 // When the payment option is woocommerce_payment
377 342 // See if Google Pay or Apple Pay was used.
@@ -383,24 +348,50 @@
383 348 $express_checkout = array( 'apple_pay' );
384 349 }
385 350 }
386 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 +
387 362 // loop through products in the order and queue a purchase event.
388 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.
389 365 $product_id = is_callable( array( $order_item, 'get_product_id' ) ) ? $order_item->get_product_id() : -1;
390 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 + }
391 377 $this->record_event(
392 378 'woocommerceanalytics_product_purchase',
393 - $product_id,
394 379 array(
395 380 'oi' => $order->get_order_number(),
396 381 'pq' => $order_item->get_quantity(),
397 - 'device' => wp_is_mobile() ? 'mobile' : 'desktop',
398 382 'payment_option' => $payment_option,
399 383 'create_account' => $create_account,
400 384 'guest_checkout' => $guest_checkout,
401 385 'express_checkout' => $express_checkout,
402 - )
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
403 394 );
404 395 }
405 396 }
406 397
@@ -406,12 +397,20 @@
406 397
407 398 /**
408 399 * Listen for clicks on the "Update Cart" button to know if an item has been removed by
409 400 * updating its quantity to zero
401 + *
402 + * @deprecated 13.3
410 403 */
411 404 public function remove_from_cart_via_quantity() {
412 - $common_props = $this->render_properties_as_js(
413 - $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
414 413 );
415 414
416 415 wc_enqueue_js(
417 416 "
@@ -420,36 +419,42 @@
420 419 cartItems.each( function( item ) {
421 420 var qty = jQuery( this ).find( 'input.qty' );
422 421 if ( qty && qty.val() === '0' ) {
423 422 var productID = jQuery( this ).find( '.product-remove a' ).data( 'product_id' );
424 - _wca.push( {
425 - '_en': 'woocommerceanalytics_remove_from_cart',
426 - 'pi': productID, " .
427 - $common_props . '
428 - } );
423 + var common_props = $common_props;
424 + common_props.pi = productID;
425 + _wca.push( common_props );
429 426 }
430 427 } );
431 - } );'
428 + } );"
432 429 );
433 430 }
434 431
435 432 /**
436 - * Get the current user id
433 + * Gets the inner blocks of a block.
437 434 *
438 - * @return int
435 + * @deprecated 13.3
436 + *
437 + * @param array $inner_blocks The inner blocks.
438 + *
439 + * @return array
439 440 */
440 - public function get_user_id() {
441 - if ( is_user_logged_in() ) {
442 - $blogid = Jetpack::get_option( 'id' );
443 - $userid = get_current_user_id();
444 - 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'];
445 445 }
446 - 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;
447 450 }
448 451
449 452 /**
450 453 * Track adding items to the cart.
451 454 *
455 + * @deprecated 13.3
456 + *
452 457 * @param string $cart_item_key Cart item key.
453 458 * @param int $product_id Product added to cart.
454 459 * @param int $quantity Quantity added to cart.
455 460 * @param int $variation_id Product variation.
@@ -470,8 +475,10 @@
470 475
471 476 /**
472 477 * Track in-session data.
473 478 *
479 + * @deprecated 13.3
480 + *
474 481 * @param int $product_id Product ID.
475 482 * @param int $quantity Quantity.
476 483 * @param string $event Fired event.
477 484 */
@@ -507,122 +514,41 @@
507 514 WC()->session->set( 'wca_session_data', $data );
508 515 }
509 516
510 517 /**
511 - * Gets product categories or varation attributes as a formatted concatenated string
518 + * Save createaccount post data to be used in $this->order_process.
512 519 *
513 - * @param object $product WC_Product.
514 - * @return string
515 - */
516 - public function get_product_categories_concatenated( $product ) {
517 -
518 - if ( ! $product instanceof WC_Product ) {
519 - return '';
520 - }
521 -
522 - $variation_data = $product->is_type( 'variation' ) ? wc_get_product_variation_attributes( $product->get_id() ) : '';
523 - if ( is_array( $variation_data ) && ! empty( $variation_data ) ) {
524 - $line = wc_get_formatted_variation( $variation_data, true );
525 - } else {
526 - $out = array();
527 - $categories = get_the_terms( $product->get_id(), 'product_cat' );
528 - if ( $categories ) {
529 - foreach ( $categories as $category ) {
530 - $out[] = $category->name;
531 - }
532 - }
533 - $line = join( '/', $out );
534 - }
535 - return $line;
536 - }
537 -
538 - /**
539 - * Search a specific post for text content.
520 + * @deprecated 13.3
540 521 *
541 - * Note: similar code is in a WooCommerce core PR:
542 - * https://github.com/woocommerce/woocommerce/pull/25932
522 + * @param array $data post data from the checkout page.
543 523 *
544 - * @param integer $post_id The id of the post to search.
545 - * @param string $text The text to search for.
546 - * @return integer 1 if post contains $text (otherwise 0).
547 - */
548 - public static function post_contains_text( $post_id, $text ) {
549 - global $wpdb;
550 -
551 - // Search for the text anywhere in the post.
552 - $wildcarded = "%{$text}%";
553 -
554 - $result = $wpdb->get_var(
555 - $wpdb->prepare(
556 - "
557 - SELECT COUNT( * ) FROM {$wpdb->prefix}posts
558 - WHERE ID=%d
559 - AND {$wpdb->prefix}posts.post_content LIKE %s
560 - ",
561 - array( $post_id, $wildcarded )
562 - )
563 - );
564 -
565 - return ( '0' !== $result ) ? 1 : 0;
566 - }
567 -
568 - /**
569 - * Get info about the cart & checkout pages, in particular
570 - * whether the store is using shortcodes or Gutenberg blocks.
571 - * This info is cached in a transient.
572 - *
573 - * Note: similar code is in a WooCommerce core PR:
574 - * https://github.com/woocommerce/woocommerce/pull/25932
575 - *
576 524 * @return array
577 525 */
578 - public static function get_cart_checkout_info() {
579 - $transient_name = 'jetpack_woocommerce_analytics_cart_checkout_info_cache';
580 -
581 - $info = get_transient( $transient_name );
582 - if ( false === $info ) {
583 - $cart_page_id = wc_get_page_id( 'cart' );
584 - $checkout_page_id = wc_get_page_id( 'checkout' );
585 -
586 - $info = array(
587 - 'cart_page_contains_cart_block' => self::post_contains_text(
588 - $cart_page_id,
589 - '<!-- wp:woocommerce/cart'
590 - ),
591 - 'cart_page_contains_cart_shortcode' => self::post_contains_text(
592 - $cart_page_id,
593 - '[woocommerce_cart]'
594 - ),
595 - 'checkout_page_contains_checkout_block' => self::post_contains_text(
596 - $checkout_page_id,
597 - '<!-- wp:woocommerce/checkout'
598 - ),
599 - 'checkout_page_contains_checkout_shortcode' => self::post_contains_text(
600 - $checkout_page_id,
601 - '[woocommerce_checkout]'
602 - ),
603 - );
604 -
605 - 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 + }
606 533 }
607 -
608 - return $info;
534 + return $data;
609 535 }
610 536
611 537 /**
612 - * 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.
613 539 *
614 - * @param array $data post data from the checkout page.
540 + * @deprecated 13.3
615 541 *
616 - * @return array
542 + * @param int $customer_id Customer ID.
543 + * @param array $new_customer_data New customer data.
617 544 */
618 - public function save_checkout_post_data( array $data ) {
545 + public function capture_created_customer( $customer_id, $new_customer_data ) {
619 546 $session = WC()->session;
620 547 if ( is_object( $session ) ) {
621 - if ( isset( $data['createaccount'] ) && ! empty( $data['createaccount'] ) ) {
548 + if ( str_contains( $new_customer_data['source'], 'store-api' ) ) {
622 549 $session->set( 'wc_checkout_createaccount_used', true );
623 550 $session->save_data();
624 551 }
625 552 }
626 - return $data;
627 553 }
628 554 }