PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.1
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.1
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 13.9.2 14.0.1 All 504 releases
← All changes | modules/woocommerce-analytics/classes/class-jetpack-woocommerce-analytics-universal.php +244 -329 12.2.3 → 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,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,47 +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 - if ( ! $product instanceof WC_Product ) {
263 - return;
164 + if ( ! is_array( $packages ) || ! is_array( $selected_options ) ) {
165 + return false;
264 166 }
265 167
266 - $this->record_event(
267 - 'woocommerceanalytics_product_view',
268 - $product->get_id()
269 - );
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;
270 194 }
271 195
272 196 /**
273 197 * On the Checkout page, trigger an event for each product in the cart
198 + *
199 + * @deprecated 13.3
274 200 */
275 201 public function checkout_process() {
276 - $cart = WC()->cart->get_cart();
202 + global $post;
203 + $checkout_page_id = wc_get_page_id( 'checkout' );
204 + $cart = WC()->cart->get_cart();
277 205
278 - $guest_checkout = ucfirst( get_option( 'woocommerce_enable_guest_checkout', 'No' ) );
279 - $create_account = ucfirst( get_option( 'woocommerce_enable_signup_and_login_from_checkout', 'No' ) );
280 -
281 206 $enabled_payment_options = array_filter(
282 207 WC()->payment_gateways->get_available_payment_gateways(),
283 208 function ( $payment_gateway ) {
284 209 if ( ! $payment_gateway instanceof WC_Payment_Gateway ) {
@@ -289,18 +214,14 @@
289 214 }
290 215 );
291 216
292 217 $enabled_payment_options = array_keys( $enabled_payment_options );
293 - $include_express_payment = false;
294 218
295 - $wcpay_version = get_option( 'woocommerce_woocommerce_payments_version' );
296 - $has_required_wcpay_version = version_compare( $wcpay_version, '2.9.0', '>=' );
297 - // Check express payment availablity only if WC Pay is enabled and express checkout (payment request) is enabled.
298 - if ( in_array( 'woocommerce_payments', $enabled_payment_options, true ) && $has_required_wcpay_version ) {
299 - $wcpay_settings = get_option( 'woocommerce_woocommerce_payments_settings', array() );
300 - if ( array_key_exists( 'payment_request', $wcpay_settings ) && 'yes' === $wcpay_settings['payment_request'] ) {
301 - $include_express_payment = true;
302 - }
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();
303 224 }
304 225
305 226 foreach ( $cart as $cart_item_key => $cart_item ) {
306 227 /**
@@ -311,51 +232,79 @@
311 232 if ( ! $product || ! $product instanceof WC_Product ) {
312 233 continue;
313 234 }
314 235
315 - if ( true === $include_express_payment ) {
316 - $properties = $this->process_event_properties(
317 - 'woocommerceanalytics_product_checkout',
318 - $product->get_id(),
319 - array(
320 - 'pq' => $cart_item['quantity'],
321 - 'payment_options' => $enabled_payment_options,
322 - 'device' => wp_is_mobile() ? 'mobile' : 'desktop',
323 - 'guest_checkout' => 'Yes' === $guest_checkout ? 'Yes' : 'No',
324 - 'create_account' => 'Yes' === $create_account ? 'Yes' : 'No',
325 - 'express_checkout' => 'null',
326 - )
327 - );
328 - wc_enqueue_js(
329 - "
330 - // wcpay.payment-request.availability event gets fired twice.
331 - // make sure we push only one event.
332 - var cartItem_{$cart_item_key}_logged = false;
333 - wp.hooks.addAction('wcpay.payment-request.availability', 'wcpay', function(args) {
334 - if ( true === cartItem_{$cart_item_key}_logged ) {
335 - return;
336 - }
337 - var properties = {$properties};
338 - properties.express_checkout = args.paymentRequestType;
339 - _wca.push(properties);
340 - cartItem_{$cart_item_key}_logged = true;
341 - });
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(
342 257 "
343 - );
344 - } else {
345 - $this->record_event(
346 - 'woocommerceanalytics_product_checkout',
347 - $product->get_id(),
348 - array(
349 - 'pq' => $cart_item['quantity'],
350 - 'payment_options' => $enabled_payment_options,
351 - 'device' => wp_is_mobile() ? 'mobile' : 'desktop',
352 - 'guest_checkout' => 'Yes' === $guest_checkout ? 'Yes' : 'No',
353 - 'create_account' => 'Yes' === $create_account ? 'Yes' : 'No',
354 - 'express_checkout' => 'null',
355 - )
356 - );
357 - }
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 +
358 307 }
359 308 }
360 309
361 310 /**
@@ -360,8 +309,10 @@
360 309
361 310 /**
362 311 * After the checkout process, fire an event for each item in the order
363 312 *
313 + * @deprecated 13.3
314 + *
364 315 * @param string $order_id Order Id.
365 316 */
366 317 public function order_process( $order_id ) {
367 318 $order = wc_get_order( $order_id );
@@ -375,14 +326,17 @@
375 326
376 327 $payment_option = $order->get_payment_method();
377 328
378 329 if ( is_object( WC()->session ) ) {
379 - $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 +
380 333 } else {
381 - $create_account = 'N';
334 + $create_account = 'No';
335 + $checkout_page_used = 'No';
382 336 }
383 337
384 - $guest_checkout = $order->get_user() ? 'N' : 'Y';
338 + $guest_checkout = $order->get_user() ? 'No' : 'Yes';
385 339
386 340 $express_checkout = 'null';
387 341 // When the payment option is woocommerce_payment
388 342 // See if Google Pay or Apple Pay was used.
@@ -394,24 +348,50 @@
394 348 $express_checkout = array( 'apple_pay' );
395 349 }
396 350 }
397 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 +
398 362 // loop through products in the order and queue a purchase event.
399 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.
400 365 $product_id = is_callable( array( $order_item, 'get_product_id' ) ) ? $order_item->get_product_id() : -1;
401 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 + }
402 377 $this->record_event(
403 378 'woocommerceanalytics_product_purchase',
404 - $product_id,
405 379 array(
406 380 'oi' => $order->get_order_number(),
407 381 'pq' => $order_item->get_quantity(),
408 - 'device' => wp_is_mobile() ? 'mobile' : 'desktop',
409 382 'payment_option' => $payment_option,
410 383 'create_account' => $create_account,
411 384 'guest_checkout' => $guest_checkout,
412 385 'express_checkout' => $express_checkout,
413 - )
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
414 394 );
415 395 }
416 396 }
417 397
@@ -417,12 +397,20 @@
417 397
418 398 /**
419 399 * Listen for clicks on the "Update Cart" button to know if an item has been removed by
420 400 * updating its quantity to zero
401 + *
402 + * @deprecated 13.3
421 403 */
422 404 public function remove_from_cart_via_quantity() {
423 - $common_props = $this->render_properties_as_js(
424 - $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
425 413 );
426 414
427 415 wc_enqueue_js(
428 416 "
@@ -431,36 +419,42 @@
431 419 cartItems.each( function( item ) {
432 420 var qty = jQuery( this ).find( 'input.qty' );
433 421 if ( qty && qty.val() === '0' ) {
434 422 var productID = jQuery( this ).find( '.product-remove a' ).data( 'product_id' );
435 - _wca.push( {
436 - '_en': 'woocommerceanalytics_remove_from_cart',
437 - 'pi': productID, " .
438 - $common_props . '
439 - } );
423 + var common_props = $common_props;
424 + common_props.pi = productID;
425 + _wca.push( common_props );
440 426 }
441 427 } );
442 - } );'
428 + } );"
443 429 );
444 430 }
445 431
446 432 /**
447 - * Get the current user id
433 + * Gets the inner blocks of a block.
448 434 *
449 - * @return int
435 + * @deprecated 13.3
436 + *
437 + * @param array $inner_blocks The inner blocks.
438 + *
439 + * @return array
450 440 */
451 - public function get_user_id() {
452 - if ( is_user_logged_in() ) {
453 - $blogid = Jetpack::get_option( 'id' );
454 - $userid = get_current_user_id();
455 - 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'];
456 445 }
457 - 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;
458 450 }
459 451
460 452 /**
461 453 * Track adding items to the cart.
462 454 *
455 + * @deprecated 13.3
456 + *
463 457 * @param string $cart_item_key Cart item key.
464 458 * @param int $product_id Product added to cart.
465 459 * @param int $quantity Quantity added to cart.
466 460 * @param int $variation_id Product variation.
@@ -481,8 +475,10 @@
481 475
482 476 /**
483 477 * Track in-session data.
484 478 *
479 + * @deprecated 13.3
480 + *
485 481 * @param int $product_id Product ID.
486 482 * @param int $quantity Quantity.
487 483 * @param string $event Fired event.
488 484 */
@@ -518,122 +514,41 @@
518 514 WC()->session->set( 'wca_session_data', $data );
519 515 }
520 516
521 517 /**
522 - * Gets product categories or varation attributes as a formatted concatenated string
518 + * Save createaccount post data to be used in $this->order_process.
523 519 *
524 - * @param object $product WC_Product.
525 - * @return string
526 - */
527 - public function get_product_categories_concatenated( $product ) {
528 -
529 - if ( ! $product instanceof WC_Product ) {
530 - return '';
531 - }
532 -
533 - $variation_data = $product->is_type( 'variation' ) ? wc_get_product_variation_attributes( $product->get_id() ) : '';
534 - if ( is_array( $variation_data ) && ! empty( $variation_data ) ) {
535 - $line = wc_get_formatted_variation( $variation_data, true );
536 - } else {
537 - $out = array();
538 - $categories = get_the_terms( $product->get_id(), 'product_cat' );
539 - if ( $categories ) {
540 - foreach ( $categories as $category ) {
541 - $out[] = $category->name;
542 - }
543 - }
544 - $line = implode( '/', $out );
545 - }
546 - return $line;
547 - }
548 -
549 - /**
550 - * Search a specific post for text content.
520 + * @deprecated 13.3
551 521 *
552 - * Note: similar code is in a WooCommerce core PR:
553 - * https://github.com/woocommerce/woocommerce/pull/25932
522 + * @param array $data post data from the checkout page.
554 523 *
555 - * @param integer $post_id The id of the post to search.
556 - * @param string $text The text to search for.
557 - * @return integer 1 if post contains $text (otherwise 0).
558 - */
559 - public static function post_contains_text( $post_id, $text ) {
560 - global $wpdb;
561 -
562 - // Search for the text anywhere in the post.
563 - $wildcarded = "%{$text}%";
564 -
565 - $result = $wpdb->get_var(
566 - $wpdb->prepare(
567 - "
568 - SELECT COUNT( * ) FROM {$wpdb->prefix}posts
569 - WHERE ID=%d
570 - AND {$wpdb->prefix}posts.post_content LIKE %s
571 - ",
572 - array( $post_id, $wildcarded )
573 - )
574 - );
575 -
576 - return ( '0' !== $result ) ? 1 : 0;
577 - }
578 -
579 - /**
580 - * Get info about the cart & checkout pages, in particular
581 - * whether the store is using shortcodes or Gutenberg blocks.
582 - * This info is cached in a transient.
583 - *
584 - * Note: similar code is in a WooCommerce core PR:
585 - * https://github.com/woocommerce/woocommerce/pull/25932
586 - *
587 524 * @return array
588 525 */
589 - public static function get_cart_checkout_info() {
590 - $transient_name = 'jetpack_woocommerce_analytics_cart_checkout_info_cache';
591 -
592 - $info = get_transient( $transient_name );
593 - if ( false === $info ) {
594 - $cart_page_id = wc_get_page_id( 'cart' );
595 - $checkout_page_id = wc_get_page_id( 'checkout' );
596 -
597 - $info = array(
598 - 'cart_page_contains_cart_block' => self::post_contains_text(
599 - $cart_page_id,
600 - '<!-- wp:woocommerce/cart'
601 - ),
602 - 'cart_page_contains_cart_shortcode' => self::post_contains_text(
603 - $cart_page_id,
604 - '[woocommerce_cart]'
605 - ),
606 - 'checkout_page_contains_checkout_block' => self::post_contains_text(
607 - $checkout_page_id,
608 - '<!-- wp:woocommerce/checkout'
609 - ),
610 - 'checkout_page_contains_checkout_shortcode' => self::post_contains_text(
611 - $checkout_page_id,
612 - '[woocommerce_checkout]'
613 - ),
614 - );
615 -
616 - 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 + }
617 533 }
618 -
619 - return $info;
534 + return $data;
620 535 }
621 536
622 537 /**
623 - * 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.
624 539 *
625 - * @param array $data post data from the checkout page.
540 + * @deprecated 13.3
626 541 *
627 - * @return array
542 + * @param int $customer_id Customer ID.
543 + * @param array $new_customer_data New customer data.
628 544 */
629 - public function save_checkout_post_data( array $data ) {
545 + public function capture_created_customer( $customer_id, $new_customer_data ) {
630 546 $session = WC()->session;
631 547 if ( is_object( $session ) ) {
632 - if ( isset( $data['createaccount'] ) && ! empty( $data['createaccount'] ) ) {
548 + if ( str_contains( $new_customer_data['source'], 'store-api' ) ) {
633 549 $session->set( 'wc_checkout_createaccount_used', true );
634 550 $session->save_data();
635 551 }
636 552 }
637 - return $data;
638 553 }
639 554 }