← All changes
|
modules/woocommerce-analytics/classes/class-jetpack-woocommerce-analytics-trait.php
+53
-44
13.0.2
→
16.3-a.7
View file →
| @@ -1,8 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * Jetpack_WooCommerce_Analytics_Trait |
| 4 | 4 | * |
| 5 | + * @deprecated 13.3 | |
| 6 | + * | |
| 5 | 7 | * @package automattic/jetpack |
| 6 | 8 | * @author Automattic |
| 7 | 9 | */ |
| 8 | 10 | |
| @@ -9,14 +11,16 @@ | ||
| 9 | 11 | /** |
| 10 | 12 | * Bail if accessed directly |
| 11 | 13 | */ |
| 12 | 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | - exit; | |
| 15 | + exit( 0 ); | |
| 14 | 16 | } |
| 15 | 17 | |
| 16 | 18 | /** |
| 17 | 19 | * Jetpack_WooCommerce_Analytics_Trait |
| 18 | 20 | * Common functionality for WooCommerce Analytics classes. |
| 21 | + * | |
| 22 | + * @deprecated 13.3 | |
| 19 | 23 | */ |
| 20 | 24 | trait Jetpack_WooCommerce_Analytics_Trait { |
| 21 | 25 | |
| 22 | 26 | /** |
| @@ -56,8 +60,10 @@ | ||
| 56 | 60 | |
| 57 | 61 | /** |
| 58 | 62 | * Format Cart Items or Order Items to an array |
| 59 | 63 | * |
| 64 | + * @deprecated 13.3 | |
| 65 | + * | |
| 60 | 66 | * @param array|WC_Order_Item[] $items Cart Items or Order Items. |
| 61 | 67 | */ |
| 62 | 68 | protected function format_items_to_json( $items ) { |
| 63 | 69 | $products = array(); |
| @@ -67,9 +73,15 @@ | ||
| 67 | 73 | $product = wc_get_product( $item->get_product_id() ); |
| 68 | 74 | } else { |
| 69 | 75 | $product = $item['data']; |
| 70 | 76 | } |
| 77 | + | |
| 78 | + if ( ! $product || ! $product instanceof WC_Product ) { | |
| 79 | + continue; | |
| 80 | + } | |
| 81 | + | |
| 71 | 82 | $data = $this->get_product_details( $product ); |
| 83 | + | |
| 72 | 84 | if ( $item instanceof WC_Order_Item_Product ) { |
| 73 | 85 | $data['pq'] = $item->get_quantity(); |
| 74 | 86 | } else { |
| 75 | 87 | $data['pq'] = $item['quantity']; |
| @@ -76,13 +88,15 @@ | ||
| 76 | 88 | } |
| 77 | 89 | $products[] = $data; |
| 78 | 90 | } |
| 79 | 91 | |
| 80 | - return wp_json_encode( $products ); | |
| 92 | + return wp_json_encode( $products, JSON_UNESCAPED_SLASHES ); | |
| 81 | 93 | } |
| 82 | 94 | |
| 83 | 95 | /** |
| 84 | 96 | * Get Cart/Checkout page view shared data |
| 97 | + * | |
| 98 | + * @deprecated 13.3 | |
| 85 | 99 | */ |
| 86 | 100 | protected function get_cart_checkout_shared_data() { |
| 87 | 101 | $cart = WC()->cart; |
| 88 | 102 | |
| @@ -106,16 +120,16 @@ | ||
| 106 | 120 | } |
| 107 | 121 | ); |
| 108 | 122 | |
| 109 | 123 | $enabled_payment_options = array_keys( $enabled_payment_options ); |
| 110 | - | |
| 111 | - $shared_data = array( | |
| 124 | + $cart_total = wc_prices_include_tax() ? $cart->get_cart_contents_total() + $cart->get_cart_contents_tax() : $cart->get_cart_contents_total(); | |
| 125 | + $shared_data = array( | |
| 112 | 126 | 'products' => $this->format_items_to_json( $cart->get_cart() ), |
| 113 | 127 | 'create_account' => $create_account, |
| 114 | 128 | 'guest_checkout' => $guest_checkout, |
| 115 | 129 | 'express_checkout' => 'null', // TODO: not solved yet. |
| 116 | 130 | 'products_count' => $cart->get_cart_contents_count(), |
| 117 | - 'order_value' => $cart->get_cart_total(), | |
| 131 | + 'order_value' => $cart_total, | |
| 118 | 132 | 'shipping_options_count' => 'null', // TODO: not solved yet. |
| 119 | 133 | 'coupon_used' => $coupon_used, |
| 120 | 134 | 'payment_options' => $enabled_payment_options, |
| 121 | 135 | ); |
| @@ -126,8 +140,10 @@ | ||
| 126 | 140 | /** |
| 127 | 141 | * Gets the content of the cart/checkout page or where the cart/checkout page is ultimately derived from if using a template. |
| 128 | 142 | * This method sets the class properties $checkout_content_source and $cart_content_source. |
| 129 | 143 | * |
| 144 | + * @deprecated 13.3 | |
| 145 | + * | |
| 130 | 146 | * @return void Does not return, but sets class properties. |
| 131 | 147 | */ |
| 132 | 148 | public function find_cart_checkout_content_sources() { |
| 133 | 149 | |
| @@ -197,21 +213,8 @@ | ||
| 197 | 213 | $cart_template = get_block_template( 'woocommerce/woocommerce//cart' ); |
| 198 | 214 | } |
| 199 | 215 | } |
| 200 | 216 | |
| 201 | - if ( function_exists( 'gutenberg_get_block_template' ) ) { | |
| 202 | - $checkout_template = gutenberg_get_block_template( 'woocommerce/woocommerce//page-checkout' ); | |
| 203 | - $cart_template = gutenberg_get_block_template( 'woocommerce/woocommerce//page-cart' ); | |
| 204 | - | |
| 205 | - if ( ! $checkout_template ) { | |
| 206 | - $checkout_template = gutenberg_get_block_template( 'woocommerce/woocommerce//checkout' ); | |
| 207 | - } | |
| 208 | - | |
| 209 | - if ( ! $cart_template ) { | |
| 210 | - $cart_template = gutenberg_get_block_template( 'woocommerce/woocommerce//cart' ); | |
| 211 | - } | |
| 212 | - } | |
| 213 | - | |
| 214 | 217 | if ( ! empty( $checkout_template->content ) ) { |
| 215 | 218 | // Checkout template is in use, but we need to see if the page-content-wrapper is in use, or if the template is being used directly. |
| 216 | 219 | $this->checkout_content_source = $checkout_template->content; |
| 217 | 220 | $is_using_page_content = str_contains( $checkout_template->content, '<!-- wp:woocommerce/page-content-wrapper {"page":"checkout"}' ); |
| @@ -253,8 +256,10 @@ | ||
| 253 | 256 | |
| 254 | 257 | /** |
| 255 | 258 | * Default event properties which should be included with all events. |
| 256 | 259 | * |
| 260 | + * @deprecated 13.3 | |
| 261 | + * | |
| 257 | 262 | * @return array Array of standard event props. |
| 258 | 263 | */ |
| 259 | 264 | public function get_common_properties() { |
| 260 | 265 | $site_info = array( |
| @@ -261,9 +266,9 @@ | ||
| 261 | 266 | 'blog_id' => Jetpack::get_option( 'id' ), |
| 262 | 267 | 'ui' => $this->get_user_id(), |
| 263 | 268 | 'url' => home_url(), |
| 264 | 269 | 'woo_version' => WC()->version, |
| 265 | - 'store_admin' => in_array( 'administrator', wp_get_current_user()->roles, true ) ? 1 : 0, | |
| 270 | + 'store_admin' => in_array( array( 'administrator', 'shop_manager' ), wp_get_current_user()->roles, true ) ? 1 : 0, | |
| 266 | 271 | 'device' => wp_is_mobile() ? 'mobile' : 'desktop', |
| 267 | 272 | 'template_used' => $this->cart_checkout_templates_in_use ? '1' : '0', |
| 268 | 273 | 'additional_blocks_on_cart_page' => $this->additional_blocks_on_cart_page, |
| 269 | 274 | 'additional_blocks_on_checkout_page' => $this->additional_blocks_on_checkout_page, |
| @@ -272,29 +277,13 @@ | ||
| 272 | 277 | $cart_checkout_info = $this->get_cart_checkout_info(); |
| 273 | 278 | return array_merge( $site_info, $cart_checkout_info ); |
| 274 | 279 | } |
| 275 | 280 | |
| 276 | - /** | |
| 277 | - * Render tracks event properties as string of JavaScript object props. | |
| 278 | - * | |
| 279 | - * @param array $properties Array of key/value pairs. | |
| 280 | - * @return string String of the form "key1: value1, key2: value2, " (etc). | |
| 281 | - */ | |
| 282 | - private function render_properties_as_js( $properties ) { | |
| 283 | - $js_args_string = ''; | |
| 284 | - foreach ( $properties as $key => $value ) { | |
| 285 | - if ( is_array( $value ) ) { | |
| 286 | - $js_args_string = $js_args_string . "'$key': " . wp_json_encode( $value ) . ','; | |
| 287 | - } else { | |
| 288 | - $js_args_string = $js_args_string . "'$key': '" . esc_js( $value ) . "', "; | |
| 289 | - } | |
| 290 | - } | |
| 291 | - return $js_args_string; | |
| 292 | - } | |
| 293 | - | |
| 294 | 281 | /** |
| 295 | 282 | * Record an event with optional product and custom properties. |
| 296 | 283 | * |
| 284 | + * @deprecated 13.3 | |
| 285 | + * | |
| 297 | 286 | * @param string $event_name The name of the event to record. |
| 298 | 287 | * @param array $properties Optional array of (key => value) event properties. |
| 299 | 288 | * @param integer $product_id The id of the product relating to the event. |
| 300 | 289 | * |
| @@ -307,8 +296,10 @@ | ||
| 307 | 296 | |
| 308 | 297 | /** |
| 309 | 298 | * Gather relevant product information |
| 310 | 299 | * |
| 300 | + * @deprecated 13.3 | |
| 301 | + * | |
| 311 | 302 | * @param \WC_Product $product product. |
| 312 | 303 | * @return array |
| 313 | 304 | */ |
| 314 | 305 | public function get_product_details( $product ) { |
| @@ -323,8 +314,10 @@ | ||
| 323 | 314 | |
| 324 | 315 | /** |
| 325 | 316 | * Gets product categories or varation attributes as a formatted concatenated string |
| 326 | 317 | * |
| 318 | + * @deprecated 13.3 | |
| 319 | + * | |
| 327 | 320 | * @param object $product WC_Product. |
| 328 | 321 | * @return string |
| 329 | 322 | */ |
| 330 | 323 | public function get_product_categories_concatenated( $product ) { |
| @@ -351,8 +344,10 @@ | ||
| 351 | 344 | |
| 352 | 345 | /** |
| 353 | 346 | * Compose event properties. |
| 354 | 347 | * |
| 348 | + * @deprecated 13.3 | |
| 349 | + * | |
| 355 | 350 | * @param string $event_name The name of the event to record. |
| 356 | 351 | * @param array $properties Optional array of (key => value) event properties. |
| 357 | 352 | * @param integer $product_id Optional id of the product relating to the event. |
| 358 | 353 | * |
| @@ -380,27 +375,31 @@ | ||
| 380 | 375 | */ |
| 381 | 376 | $all_props = apply_filters( |
| 382 | 377 | 'jetpack_woocommerce_analytics_event_props', |
| 383 | 378 | array_merge( |
| 384 | - $properties, | |
| 385 | - $this->get_common_properties() | |
| 379 | + $this->get_common_properties(), // We put this here to allow override of common props. | |
| 380 | + $properties | |
| 386 | 381 | ) |
| 387 | 382 | ); |
| 388 | 383 | |
| 389 | - $js = "{'_en': '" . esc_js( $event_name ) . "'"; | |
| 390 | - | |
| 391 | 384 | if ( isset( $product_details ) ) { |
| 392 | 385 | $all_props = array_merge( $all_props, $product_details ); |
| 393 | 386 | } |
| 394 | 387 | |
| 395 | - $js .= ',' . $this->render_properties_as_js( $all_props ) . '}'; | |
| 396 | - | |
| 397 | - return $js; | |
| 388 | + return wp_json_encode( | |
| 389 | + array_merge( | |
| 390 | + array( '_en' => $event_name ), | |
| 391 | + $all_props | |
| 392 | + ), | |
| 393 | + JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP | |
| 394 | + ); | |
| 398 | 395 | } |
| 399 | 396 | |
| 400 | 397 | /** |
| 401 | 398 | * Get the current user id |
| 402 | 399 | * |
| 400 | + * @deprecated 13.3 | |
| 401 | + * | |
| 403 | 402 | * @return int |
| 404 | 403 | */ |
| 405 | 404 | public function get_user_id() { |
| 406 | 405 | if ( is_user_logged_in() ) { |
| @@ -413,8 +412,10 @@ | ||
| 413 | 412 | |
| 414 | 413 | /** |
| 415 | 414 | * Gets the IDs of additional blocks on the Cart/Checkout pages or templates. |
| 416 | 415 | * |
| 416 | + * @deprecated 13.3 | |
| 417 | + * | |
| 417 | 418 | * @param string $cart_or_checkout Whether to get blocks on the cart or checkout page. |
| 418 | 419 | * @return array All inner blocks on the page. |
| 419 | 420 | */ |
| 420 | 421 | public function get_additional_blocks_on_page( $cart_or_checkout = 'cart' ) { |
| @@ -484,8 +485,10 @@ | ||
| 484 | 485 | |
| 485 | 486 | /** |
| 486 | 487 | * Gets an array containing the block or shortcode use properties for the Cart page. |
| 487 | 488 | * |
| 489 | + * @deprecated 13.3 | |
| 490 | + * | |
| 488 | 491 | * @return array An array containing the block or shortcode use properties for the Cart page. |
| 489 | 492 | */ |
| 490 | 493 | public function get_cart_page_block_usage() { |
| 491 | 494 | $new_info = array(); |
| @@ -502,8 +505,10 @@ | ||
| 502 | 505 | |
| 503 | 506 | /** |
| 504 | 507 | * Gets an array containing the block or shortcode use properties for the Checkout page. |
| 505 | 508 | * |
| 509 | + * @deprecated 13.3 | |
| 510 | + * | |
| 506 | 511 | * @return array An array containing the block or shortcode use properties for the Checkout page. |
| 507 | 512 | */ |
| 508 | 513 | public function get_checkout_page_block_usage() { |
| 509 | 514 | $new_info = array(); |
| @@ -525,8 +530,10 @@ | ||
| 525 | 530 | * |
| 526 | 531 | * Note: similar code is in a WooCommerce core PR: |
| 527 | 532 | * https://github.com/woocommerce/woocommerce/pull/25932 |
| 528 | 533 | * |
| 534 | + * @deprecated 13.3 | |
| 535 | + * | |
| 529 | 536 | * @return array |
| 530 | 537 | */ |
| 531 | 538 | public function get_cart_checkout_info() { |
| 532 | 539 | $info = array_merge( |
| @@ -540,8 +547,10 @@ | ||
| 540 | 547 | * Search a specific post for text content. |
| 541 | 548 | * |
| 542 | 549 | * Note: similar code is in a WooCommerce core PR: |
| 543 | 550 | * https://github.com/woocommerce/woocommerce/pull/25932 |
| 551 | + * | |
| 552 | + * @deprecated 13.3 | |
| 544 | 553 | * |
| 545 | 554 | * @param integer $post_id The id of the post to search. |
| 546 | 555 | * @param string $text The text to search for. |
| 547 | 556 | * @return integer 1 if post contains $text (otherwise 0). |