← All changes
|
modules/woocommerce-analytics/classes/class-jetpack-woocommerce-analytics-trait.php
+422
-102
12.6.4
→
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,55 +11,279 @@ | ||
| 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 | /** |
| 23 | - * Default event properties which should be included with all events. | |
| 27 | + * Saves whether the cart/checkout templates are in use based on WC Blocks version. | |
| 24 | 28 | * |
| 25 | - * @return array Array of standard event props. | |
| 29 | + * @var bool true if the templates are in use. | |
| 26 | 30 | */ |
| 27 | - public function get_common_properties() { | |
| 28 | - $site_info = array( | |
| 29 | - 'blog_id' => Jetpack::get_option( 'id' ), | |
| 30 | - 'ui' => $this->get_user_id(), | |
| 31 | - 'url' => home_url(), | |
| 32 | - 'woo_version' => WC()->version, | |
| 33 | - 'store_admin' => in_array( 'administrator', wp_get_current_user()->roles, true ) ? 1 : 0, | |
| 31 | + protected $cart_checkout_templates_in_use; | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * The content of the cart page or where the cart page is ultimately derived from if using a template. | |
| 35 | + * | |
| 36 | + * @var string | |
| 37 | + */ | |
| 38 | + protected $cart_content_source = ''; | |
| 39 | + | |
| 40 | + /** | |
| 41 | + * The content of the checkout page or where the cart page is ultimately derived from if using a template. | |
| 42 | + * | |
| 43 | + * @var string | |
| 44 | + */ | |
| 45 | + protected $checkout_content_source = ''; | |
| 46 | + | |
| 47 | + /** | |
| 48 | + * Tracks any additional blocks loaded on the Cart page. | |
| 49 | + * | |
| 50 | + * @var array | |
| 51 | + */ | |
| 52 | + protected $additional_blocks_on_cart_page; | |
| 53 | + | |
| 54 | + /** | |
| 55 | + * Tracks any additional blocks loaded on the Checkout page. | |
| 56 | + * | |
| 57 | + * @var array | |
| 58 | + */ | |
| 59 | + protected $additional_blocks_on_checkout_page; | |
| 60 | + | |
| 61 | + /** | |
| 62 | + * Format Cart Items or Order Items to an array | |
| 63 | + * | |
| 64 | + * @deprecated 13.3 | |
| 65 | + * | |
| 66 | + * @param array|WC_Order_Item[] $items Cart Items or Order Items. | |
| 67 | + */ | |
| 68 | + protected function format_items_to_json( $items ) { | |
| 69 | + $products = array(); | |
| 70 | + | |
| 71 | + foreach ( $items as $item ) { | |
| 72 | + if ( $item instanceof WC_Order_Item_Product ) { | |
| 73 | + $product = wc_get_product( $item->get_product_id() ); | |
| 74 | + } else { | |
| 75 | + $product = $item['data']; | |
| 76 | + } | |
| 77 | + | |
| 78 | + if ( ! $product || ! $product instanceof WC_Product ) { | |
| 79 | + continue; | |
| 80 | + } | |
| 81 | + | |
| 82 | + $data = $this->get_product_details( $product ); | |
| 83 | + | |
| 84 | + if ( $item instanceof WC_Order_Item_Product ) { | |
| 85 | + $data['pq'] = $item->get_quantity(); | |
| 86 | + } else { | |
| 87 | + $data['pq'] = $item['quantity']; | |
| 88 | + } | |
| 89 | + $products[] = $data; | |
| 90 | + } | |
| 91 | + | |
| 92 | + return wp_json_encode( $products, JSON_UNESCAPED_SLASHES ); | |
| 93 | + } | |
| 94 | + | |
| 95 | + /** | |
| 96 | + * Get Cart/Checkout page view shared data | |
| 97 | + * | |
| 98 | + * @deprecated 13.3 | |
| 99 | + */ | |
| 100 | + protected function get_cart_checkout_shared_data() { | |
| 101 | + $cart = WC()->cart; | |
| 102 | + | |
| 103 | + $guest_checkout = ucfirst( get_option( 'woocommerce_enable_guest_checkout', 'No' ) ); | |
| 104 | + $create_account = ucfirst( get_option( 'woocommerce_enable_signup_and_login_from_checkout', 'No' ) ); | |
| 105 | + | |
| 106 | + $coupons = $cart->get_coupons(); | |
| 107 | + $coupon_used = 0; | |
| 108 | + if ( is_countable( $coupons ) ) { | |
| 109 | + $coupon_used = count( $coupons ) ? 1 : 0; | |
| 110 | + } | |
| 111 | + | |
| 112 | + $enabled_payment_options = array_filter( | |
| 113 | + WC()->payment_gateways->get_available_payment_gateways(), | |
| 114 | + function ( $payment_gateway ) { | |
| 115 | + if ( ! $payment_gateway instanceof WC_Payment_Gateway ) { | |
| 116 | + return false; | |
| 117 | + } | |
| 118 | + | |
| 119 | + return $payment_gateway->is_available(); | |
| 120 | + } | |
| 34 | 121 | ); |
| 35 | - $cart_checkout_info = $this->get_cart_checkout_info(); | |
| 36 | - return array_merge( $site_info, $cart_checkout_info ); | |
| 122 | + | |
| 123 | + $enabled_payment_options = array_keys( $enabled_payment_options ); | |
| 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( | |
| 126 | + 'products' => $this->format_items_to_json( $cart->get_cart() ), | |
| 127 | + 'create_account' => $create_account, | |
| 128 | + 'guest_checkout' => $guest_checkout, | |
| 129 | + 'express_checkout' => 'null', // TODO: not solved yet. | |
| 130 | + 'products_count' => $cart->get_cart_contents_count(), | |
| 131 | + 'order_value' => $cart_total, | |
| 132 | + 'shipping_options_count' => 'null', // TODO: not solved yet. | |
| 133 | + 'coupon_used' => $coupon_used, | |
| 134 | + 'payment_options' => $enabled_payment_options, | |
| 135 | + ); | |
| 136 | + | |
| 137 | + return $shared_data; | |
| 37 | 138 | } |
| 38 | 139 | |
| 39 | 140 | /** |
| 40 | - * Render tracks event properties as string of JavaScript object props. | |
| 141 | + * Gets the content of the cart/checkout page or where the cart/checkout page is ultimately derived from if using a template. | |
| 142 | + * This method sets the class properties $checkout_content_source and $cart_content_source. | |
| 41 | 143 | * |
| 42 | - * @param array $properties Array of key/value pairs. | |
| 43 | - * @return string String of the form "key1: value1, key2: value2, " (etc). | |
| 144 | + * @deprecated 13.3 | |
| 145 | + * | |
| 146 | + * @return void Does not return, but sets class properties. | |
| 44 | 147 | */ |
| 45 | - private function render_properties_as_js( $properties ) { | |
| 46 | - $js_args_string = ''; | |
| 47 | - foreach ( $properties as $key => $value ) { | |
| 48 | - if ( is_array( $value ) ) { | |
| 49 | - $js_args_string = $js_args_string . "'$key': " . wp_json_encode( $value ) . ','; | |
| 50 | - } else { | |
| 51 | - $js_args_string = $js_args_string . "'$key': '" . esc_js( $value ) . "', "; | |
| 148 | + public function find_cart_checkout_content_sources() { | |
| 149 | + | |
| 150 | + /** | |
| 151 | + * The steps we take to find the content are: | |
| 152 | + * 1. Check the transient, if that contains content and is not expired, return that. | |
| 153 | + * 2. Check if the cart/checkout templates are in use. If *not in use*, get the content from the pages and | |
| 154 | + * return it, there is no need to dig further. | |
| 155 | + * 3. If the templates *are* in use, check if the `page-content-wrapper` block is in use. If so, get the content | |
| 156 | + * from the pages (same as step 2) and return it. | |
| 157 | + * 4. If the templates are in use but `page-content-wrapper` is not, then get the content directly from the | |
| 158 | + * template and return it. | |
| 159 | + * 5. At the end of each step, assign the found content to the relevant class properties and save them in a | |
| 160 | + * transient with a 1-day lifespan. This will prevent us from having to do this work on every page load. | |
| 161 | + */ | |
| 162 | + | |
| 163 | + $cart_checkout_content_cache_transient_name = 'jetpack_woocommerce_analytics_cart_checkout_content_sources'; | |
| 164 | + | |
| 165 | + $transient_value = get_transient( $cart_checkout_content_cache_transient_name ); | |
| 166 | + | |
| 167 | + if ( | |
| 168 | + false !== $transient_value && | |
| 169 | + ! empty( $transient_value['checkout_content_source'] ) && | |
| 170 | + ! empty( $transient_value['cart_content_source'] ) | |
| 171 | + ) { | |
| 172 | + $this->cart_content_source = $transient_value['cart_content_source']; | |
| 173 | + $this->checkout_content_source = $transient_value['checkout_content_source']; | |
| 174 | + return; | |
| 175 | + } | |
| 176 | + | |
| 177 | + $this->cart_checkout_templates_in_use = wp_is_block_theme() && class_exists( 'Automattic\WooCommerce\Blocks\Package' ) && version_compare( Automattic\WooCommerce\Blocks\Package::get_version(), '10.6.0', '>=' ); | |
| 178 | + | |
| 179 | + // Cart/Checkout *pages* are in use if the templates are not in use. Return their content and do nothing else. | |
| 180 | + if ( ! $this->cart_checkout_templates_in_use ) { | |
| 181 | + $cart_page = get_post( wc_get_page_id( 'cart' ) ); | |
| 182 | + $checkout_page = get_post( wc_get_page_id( 'checkout' ) ); | |
| 183 | + | |
| 184 | + if ( $cart_page && isset( $cart_page->post_content ) ) { | |
| 185 | + $this->cart_content_source = $cart_page->post_content; | |
| 52 | 186 | } |
| 187 | + | |
| 188 | + if ( $checkout_page && isset( $checkout_page->post_content ) ) { | |
| 189 | + $this->checkout_content_source = $checkout_page->post_content; | |
| 190 | + } | |
| 191 | + | |
| 192 | + set_transient( | |
| 193 | + $cart_checkout_content_cache_transient_name, | |
| 194 | + array( | |
| 195 | + 'cart_content_source' => $this->cart_content_source, | |
| 196 | + 'checkout_content_source' => $this->checkout_content_source, | |
| 197 | + ), | |
| 198 | + DAY_IN_SECONDS | |
| 199 | + ); | |
| 200 | + return; | |
| 53 | 201 | } |
| 54 | - return $js_args_string; | |
| 202 | + | |
| 203 | + // We are in a Block theme - so we need to find out if the templates are being used. | |
| 204 | + if ( function_exists( 'get_block_template' ) ) { | |
| 205 | + $checkout_template = get_block_template( 'woocommerce/woocommerce//page-checkout' ); | |
| 206 | + $cart_template = get_block_template( 'woocommerce/woocommerce//page-cart' ); | |
| 207 | + | |
| 208 | + if ( ! $checkout_template ) { | |
| 209 | + $checkout_template = get_block_template( 'woocommerce/woocommerce//checkout' ); | |
| 210 | + } | |
| 211 | + | |
| 212 | + if ( ! $cart_template ) { | |
| 213 | + $cart_template = get_block_template( 'woocommerce/woocommerce//cart' ); | |
| 214 | + } | |
| 215 | + } | |
| 216 | + | |
| 217 | + if ( ! empty( $checkout_template->content ) ) { | |
| 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. | |
| 219 | + $this->checkout_content_source = $checkout_template->content; | |
| 220 | + $is_using_page_content = str_contains( $checkout_template->content, '<!-- wp:woocommerce/page-content-wrapper {"page":"checkout"}' ); | |
| 221 | + | |
| 222 | + if ( $is_using_page_content ) { | |
| 223 | + // The page-content-wrapper is in use, so we need to get the page content. | |
| 224 | + $checkout_page = get_post( wc_get_page_id( 'checkout' ) ); | |
| 225 | + | |
| 226 | + if ( $checkout_page && isset( $checkout_page->post_content ) ) { | |
| 227 | + $this->checkout_content_source = $checkout_page->post_content; | |
| 228 | + } | |
| 229 | + } | |
| 230 | + } | |
| 231 | + | |
| 232 | + if ( ! empty( $cart_template->content ) ) { | |
| 233 | + // Cart 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. | |
| 234 | + $this->cart_content_source = $cart_template->content; | |
| 235 | + $is_using_page_content = str_contains( $cart_template->content, '<!-- wp:woocommerce/page-content-wrapper {"page":"cart"}' ); | |
| 236 | + | |
| 237 | + if ( $is_using_page_content ) { | |
| 238 | + // The page-content-wrapper is in use, so we need to get the page content. | |
| 239 | + $cart_page = get_post( wc_get_page_id( 'cart' ) ); | |
| 240 | + | |
| 241 | + if ( $cart_page && isset( $cart_page->post_content ) ) { | |
| 242 | + $this->cart_content_source = $cart_page->post_content; | |
| 243 | + } | |
| 244 | + } | |
| 245 | + } | |
| 246 | + | |
| 247 | + set_transient( | |
| 248 | + $cart_checkout_content_cache_transient_name, | |
| 249 | + array( | |
| 250 | + 'cart_content_source' => $this->cart_content_source, | |
| 251 | + 'checkout_content_source' => $this->checkout_content_source, | |
| 252 | + ), | |
| 253 | + DAY_IN_SECONDS | |
| 254 | + ); | |
| 55 | 255 | } |
| 56 | 256 | |
| 257 | + /** | |
| 258 | + * Default event properties which should be included with all events. | |
| 259 | + * | |
| 260 | + * @deprecated 13.3 | |
| 261 | + * | |
| 262 | + * @return array Array of standard event props. | |
| 263 | + */ | |
| 264 | + public function get_common_properties() { | |
| 265 | + $site_info = array( | |
| 266 | + 'blog_id' => Jetpack::get_option( 'id' ), | |
| 267 | + 'ui' => $this->get_user_id(), | |
| 268 | + 'url' => home_url(), | |
| 269 | + 'woo_version' => WC()->version, | |
| 270 | + 'store_admin' => in_array( array( 'administrator', 'shop_manager' ), wp_get_current_user()->roles, true ) ? 1 : 0, | |
| 271 | + 'device' => wp_is_mobile() ? 'mobile' : 'desktop', | |
| 272 | + 'template_used' => $this->cart_checkout_templates_in_use ? '1' : '0', | |
| 273 | + 'additional_blocks_on_cart_page' => $this->additional_blocks_on_cart_page, | |
| 274 | + 'additional_blocks_on_checkout_page' => $this->additional_blocks_on_checkout_page, | |
| 275 | + 'store_currency' => get_woocommerce_currency(), | |
| 276 | + ); | |
| 277 | + $cart_checkout_info = $this->get_cart_checkout_info(); | |
| 278 | + return array_merge( $site_info, $cart_checkout_info ); | |
| 279 | + } | |
| 280 | + | |
| 57 | 281 | /** |
| 58 | 282 | * Record an event with optional product and custom properties. |
| 59 | 283 | * |
| 284 | + * @deprecated 13.3 | |
| 285 | + * | |
| 60 | 286 | * @param string $event_name The name of the event to record. |
| 61 | 287 | * @param array $properties Optional array of (key => value) event properties. |
| 62 | 288 | * @param integer $product_id The id of the product relating to the event. |
| 63 | 289 | * |
| @@ -67,11 +293,61 @@ | ||
| 67 | 293 | $js = $this->process_event_properties( $event_name, $properties, $product_id ); |
| 68 | 294 | wc_enqueue_js( "_wca.push({$js});" ); |
| 69 | 295 | } |
| 70 | 296 | |
| 297 | + /** | |
| 298 | + * Gather relevant product information | |
| 299 | + * | |
| 300 | + * @deprecated 13.3 | |
| 301 | + * | |
| 302 | + * @param \WC_Product $product product. | |
| 303 | + * @return array | |
| 304 | + */ | |
| 305 | + public function get_product_details( $product ) { | |
| 306 | + return array( | |
| 307 | + 'pi' => $product->get_id(), | |
| 308 | + 'pn' => $product->get_title(), | |
| 309 | + 'pc' => $this->get_product_categories_concatenated( $product ), | |
| 310 | + 'pp' => $product->get_price(), | |
| 311 | + 'pt' => $product->get_type(), | |
| 312 | + ); | |
| 313 | + } | |
| 314 | + | |
| 315 | + /** | |
| 316 | + * Gets product categories or varation attributes as a formatted concatenated string | |
| 317 | + * | |
| 318 | + * @deprecated 13.3 | |
| 319 | + * | |
| 320 | + * @param object $product WC_Product. | |
| 321 | + * @return string | |
| 322 | + */ | |
| 323 | + public function get_product_categories_concatenated( $product ) { | |
| 324 | + | |
| 325 | + if ( ! $product instanceof WC_Product ) { | |
| 326 | + return ''; | |
| 327 | + } | |
| 328 | + | |
| 329 | + $variation_data = $product->is_type( 'variation' ) ? wc_get_product_variation_attributes( $product->get_id() ) : ''; | |
| 330 | + if ( is_array( $variation_data ) && ! empty( $variation_data ) ) { | |
| 331 | + $line = wc_get_formatted_variation( $variation_data, true ); | |
| 332 | + } else { | |
| 333 | + $out = array(); | |
| 334 | + $categories = get_the_terms( $product->get_id(), 'product_cat' ); | |
| 335 | + if ( $categories ) { | |
| 336 | + foreach ( $categories as $category ) { | |
| 337 | + $out[] = $category->name; | |
| 338 | + } | |
| 339 | + } | |
| 340 | + $line = implode( '/', $out ); | |
| 341 | + } | |
| 342 | + return $line; | |
| 343 | + } | |
| 344 | + | |
| 71 | 345 | /** |
| 72 | 346 | * Compose event properties. |
| 73 | 347 | * |
| 348 | + * @deprecated 13.3 | |
| 349 | + * | |
| 74 | 350 | * @param string $event_name The name of the event to record. |
| 75 | 351 | * @param array $properties Optional array of (key => value) event properties. |
| 76 | 352 | * @param integer $product_id Optional id of the product relating to the event. |
| 77 | 353 | * |
| @@ -99,31 +375,31 @@ | ||
| 99 | 375 | */ |
| 100 | 376 | $all_props = apply_filters( |
| 101 | 377 | 'jetpack_woocommerce_analytics_event_props', |
| 102 | 378 | array_merge( |
| 103 | - $properties, | |
| 104 | - $this->get_common_properties() | |
| 379 | + $this->get_common_properties(), // We put this here to allow override of common props. | |
| 380 | + $properties | |
| 105 | 381 | ) |
| 106 | 382 | ); |
| 107 | 383 | |
| 108 | - $js = "{'_en': '" . esc_js( $event_name ) . "'"; | |
| 109 | - | |
| 110 | 384 | if ( isset( $product_details ) ) { |
| 111 | - $js .= ",'pi': '" . esc_js( $product_id ) . "'"; | |
| 112 | - $js .= ",'pn': '" . esc_js( $product_details['name'] ) . "'"; | |
| 113 | - $js .= ",'pc': '" . esc_js( $product_details['category'] ) . "'"; | |
| 114 | - $js .= ",'pp': '" . esc_js( $product_details['price'] ) . "'"; | |
| 115 | - $js .= ",'pt': '" . esc_js( $product_details['type'] ) . "'"; | |
| 385 | + $all_props = array_merge( $all_props, $product_details ); | |
| 116 | 386 | } |
| 117 | 387 | |
| 118 | - $js .= ',' . $this->render_properties_as_js( $all_props ) . '}'; | |
| 119 | - | |
| 120 | - 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 | + ); | |
| 121 | 395 | } |
| 122 | 396 | |
| 123 | 397 | /** |
| 124 | 398 | * Get the current user id |
| 125 | 399 | * |
| 400 | + * @deprecated 13.3 | |
| 401 | + * | |
| 126 | 402 | * @return int |
| 127 | 403 | */ |
| 128 | 404 | public function get_user_id() { |
| 129 | 405 | if ( is_user_logged_in() ) { |
| @@ -134,95 +410,137 @@ | ||
| 134 | 410 | return 'null'; |
| 135 | 411 | } |
| 136 | 412 | |
| 137 | 413 | /** |
| 138 | - * Get info about the cart & checkout pages, in particular | |
| 139 | - * whether the store is using shortcodes or Gutenberg blocks. | |
| 140 | - * This info is cached in a transient. | |
| 414 | + * Gets the IDs of additional blocks on the Cart/Checkout pages or templates. | |
| 141 | 415 | * |
| 142 | - * Note: similar code is in a WooCommerce core PR: | |
| 143 | - * https://github.com/woocommerce/woocommerce/pull/25932 | |
| 416 | + * @deprecated 13.3 | |
| 144 | 417 | * |
| 145 | - * @return array | |
| 418 | + * @param string $cart_or_checkout Whether to get blocks on the cart or checkout page. | |
| 419 | + * @return array All inner blocks on the page. | |
| 146 | 420 | */ |
| 147 | - public function get_cart_checkout_info() { | |
| 148 | - $transient_name = 'jetpack_woocommerce_analytics_cart_checkout_info_cache'; | |
| 421 | + public function get_additional_blocks_on_page( $cart_or_checkout = 'cart' ) { | |
| 149 | 422 | |
| 150 | - $info = get_transient( $transient_name ); | |
| 423 | + $additional_blocks_on_page_transient_name = 'jetpack_woocommerce_analytics_additional_blocks_on_' . $cart_or_checkout . '_page'; | |
| 424 | + $additional_blocks_on_page = get_transient( $additional_blocks_on_page_transient_name ); | |
| 151 | 425 | |
| 152 | - // Return cached data early to prevent additional processing, the transient lasts for 1 day. | |
| 153 | - if ( false !== $info ) { | |
| 154 | - return $info; | |
| 426 | + if ( false !== $additional_blocks_on_page ) { | |
| 427 | + return $additional_blocks_on_page; | |
| 155 | 428 | } |
| 156 | 429 | |
| 157 | - // If WC Blocks is version 10.6.0 or above, it's using the Cart/Checkout templates. | |
| 158 | - $cart_checkout_templates_in_use = class_exists( 'Automattic\WooCommerce\Blocks\Package' ) && version_compare( Automattic\WooCommerce\Blocks\Package::get_version(), '10.6.0', '>=' ); | |
| 430 | + $content = $this->cart_content_source; | |
| 159 | 431 | |
| 160 | - if ( ! $cart_checkout_templates_in_use ) { | |
| 161 | - $cart_page_id = wc_get_page_id( 'cart' ); | |
| 162 | - $checkout_page_id = wc_get_page_id( 'checkout' ); | |
| 432 | + if ( 'checkout' === $cart_or_checkout ) { | |
| 433 | + $content = $this->checkout_content_source; | |
| 434 | + } | |
| 163 | 435 | |
| 164 | - $info = array( | |
| 165 | - 'cart_page_contains_cart_block' => $this->post_contains_text( | |
| 166 | - $cart_page_id, | |
| 167 | - '<!-- wp:woocommerce/cart' | |
| 168 | - ), | |
| 169 | - 'cart_page_contains_cart_shortcode' => $this->post_contains_text( | |
| 170 | - $cart_page_id, | |
| 171 | - '[woocommerce_cart]' | |
| 172 | - ), | |
| 173 | - 'checkout_page_contains_checkout_block' => $this->post_contains_text( | |
| 174 | - $checkout_page_id, | |
| 175 | - '<!-- wp:woocommerce/checkout' | |
| 176 | - ), | |
| 177 | - 'checkout_page_contains_checkout_shortcode' => $this->post_contains_text( | |
| 178 | - $checkout_page_id, | |
| 179 | - '[woocommerce_checkout]' | |
| 180 | - ), | |
| 181 | - ); | |
| 182 | - set_transient( $transient_name, $info, DAY_IN_SECONDS ); | |
| 183 | - return $info; | |
| 184 | - } | |
| 436 | + $parsed_blocks = parse_blocks( $content ); | |
| 437 | + $other_blocks = array_filter( | |
| 438 | + $parsed_blocks, | |
| 439 | + function ( $block ) use ( $cart_or_checkout ) { | |
| 440 | + if ( ! isset( $block['blockName'] ) ) { | |
| 441 | + return false; | |
| 442 | + } | |
| 185 | 443 | |
| 186 | - // We will only reach here if the Cart/Checkout templates are in use. | |
| 444 | + if ( 'woocommerce/classic-shortcode' === $block['blockName'] ) { | |
| 445 | + return false; | |
| 446 | + } | |
| 187 | 447 | |
| 188 | - $cart_template = null; | |
| 189 | - $checkout_template = null; | |
| 190 | - $cart_template_id = null; | |
| 191 | - $checkout_template_id = null; | |
| 192 | - $block_controller = Automattic\WooCommerce\Blocks\Package::container()->get( Automattic\WooCommerce\Blocks\BlockTemplatesController::class ); | |
| 193 | - $templates = $block_controller->get_block_templates( array( 'cart', 'checkout' ) ); | |
| 448 | + if ( 'core/shortcode' === $block['blockName'] ) { | |
| 449 | + return false; | |
| 450 | + } | |
| 194 | 451 | |
| 195 | - foreach ( $templates as $template ) { | |
| 196 | - if ( 'cart' === $template->slug ) { | |
| 197 | - $cart_template_id = ( $template->id ); | |
| 452 | + if ( 'checkout' === $cart_or_checkout && 'woocommerce/checkout' !== $block['blockName'] ) { | |
| 453 | + return true; | |
| 454 | + } | |
| 455 | + | |
| 456 | + if ( 'cart' === $cart_or_checkout && 'woocommerce/cart' !== $block['blockName'] ) { | |
| 457 | + return true; | |
| 458 | + } | |
| 459 | + | |
| 460 | + return false; | |
| 461 | + } | |
| 462 | + ); | |
| 463 | + | |
| 464 | + $all_inner_blocks = array(); | |
| 465 | + | |
| 466 | + // Loop over each "block group". In templates the blocks are grouped up. | |
| 467 | + foreach ( $other_blocks as $block ) { | |
| 468 | + | |
| 469 | + // This check is necessary because sometimes this is null when using templates. | |
| 470 | + if ( ! empty( $block['blockName'] ) ) { | |
| 471 | + $all_inner_blocks[] = $block['blockName']; | |
| 472 | + } | |
| 473 | + | |
| 474 | + if ( ! isset( $block['innerBlocks'] ) || ! is_array( $block['innerBlocks'] ) || 0 === count( $block['innerBlocks'] ) ) { | |
| 198 | 475 | continue; |
| 199 | 476 | } |
| 200 | - if ( 'checkout' === $template->slug ) { | |
| 201 | - $checkout_template_id = ( $template->id ); | |
| 477 | + | |
| 478 | + foreach ( $block['innerBlocks'] as $inner_content ) { | |
| 479 | + $all_inner_blocks = array_merge( $all_inner_blocks, $this->get_inner_blocks( $inner_content ) ); | |
| 202 | 480 | } |
| 203 | 481 | } |
| 482 | + set_transient( $additional_blocks_on_page_transient_name, $all_inner_blocks, DAY_IN_SECONDS ); | |
| 483 | + return $all_inner_blocks; | |
| 484 | + } | |
| 204 | 485 | |
| 205 | - // Get the template and its contents from the IDs we found above. | |
| 206 | - if ( function_exists( 'get_block_template' ) ) { | |
| 207 | - $cart_template = get_block_template( $cart_template_id ); | |
| 208 | - $checkout_template = get_block_template( $checkout_template_id ); | |
| 209 | - } | |
| 486 | + /** | |
| 487 | + * Gets an array containing the block or shortcode use properties for the Cart page. | |
| 488 | + * | |
| 489 | + * @deprecated 13.3 | |
| 490 | + * | |
| 491 | + * @return array An array containing the block or shortcode use properties for the Cart page. | |
| 492 | + */ | |
| 493 | + public function get_cart_page_block_usage() { | |
| 494 | + $new_info = array(); | |
| 210 | 495 | |
| 211 | - if ( function_exists( 'gutenberg_get_block_template' ) ) { | |
| 212 | - $cart_template = get_block_template( $cart_template_id ); | |
| 213 | - $checkout_template = get_block_template( $checkout_template_id ); | |
| 214 | - } | |
| 496 | + $content = $this->cart_content_source; | |
| 497 | + $block_presence = str_contains( $content, '<!-- wp:woocommerce/cart' ); | |
| 498 | + $shortcode_presence = str_contains( $content, '[woocommerce_cart]' ); | |
| 499 | + $classic_shortcode_presence = str_contains( $content, '<!-- wp:woocommerce/classic-shortcode' ); | |
| 215 | 500 | |
| 216 | - // Update the info transient with data we got from the templates, if the site isn't using WC Blocks we | |
| 217 | - // won't be doing this so no concern about overwriting. | |
| 218 | - $info = array( | |
| 219 | - 'cart_page_contains_cart_block' => str_contains( $cart_template->content, '<!-- wp:woocommerce/cart' ) ? 1 : 0, | |
| 220 | - 'cart_page_contains_cart_shortcode' => str_contains( $cart_template->content, '[woocommerce_cart]' ) ? 1 : 0, | |
| 221 | - 'checkout_page_contains_checkout_block' => str_contains( $checkout_template->content, '<!-- wp:woocommerce/checkout' ) ? 1 : 0, | |
| 222 | - 'checkout_page_contains_checkout_shortcode' => str_contains( $checkout_template->content, '[woocommerce_checkout]' ) ? 1 : 0, | |
| 501 | + $new_info['cart_page_contains_cart_block'] = $block_presence ? '1' : '0'; | |
| 502 | + $new_info['cart_page_contains_cart_shortcode'] = $shortcode_presence || $classic_shortcode_presence ? '1' : '0'; | |
| 503 | + return $new_info; | |
| 504 | + } | |
| 505 | + | |
| 506 | + /** | |
| 507 | + * Gets an array containing the block or shortcode use properties for the Checkout page. | |
| 508 | + * | |
| 509 | + * @deprecated 13.3 | |
| 510 | + * | |
| 511 | + * @return array An array containing the block or shortcode use properties for the Checkout page. | |
| 512 | + */ | |
| 513 | + public function get_checkout_page_block_usage() { | |
| 514 | + $new_info = array(); | |
| 515 | + | |
| 516 | + $content = $this->checkout_content_source; | |
| 517 | + $block_presence = str_contains( $content, '<!-- wp:woocommerce/checkout' ); | |
| 518 | + $shortcode_presence = str_contains( $content, '[woocommerce_checkout]' ); | |
| 519 | + $classic_shortcode_presence = str_contains( $content, '<!-- wp:woocommerce/classic-shortcode' ); | |
| 520 | + | |
| 521 | + $new_info['checkout_page_contains_checkout_block'] = $block_presence ? '1' : '0'; | |
| 522 | + $new_info['checkout_page_contains_checkout_shortcode'] = $shortcode_presence || $classic_shortcode_presence ? '1' : '0'; | |
| 523 | + return $new_info; | |
| 524 | + } | |
| 525 | + | |
| 526 | + /** | |
| 527 | + * Get info about the cart & checkout pages, in particular | |
| 528 | + * whether the store is using shortcodes or Gutenberg blocks. | |
| 529 | + * This info is cached in a transient. | |
| 530 | + * | |
| 531 | + * Note: similar code is in a WooCommerce core PR: | |
| 532 | + * https://github.com/woocommerce/woocommerce/pull/25932 | |
| 533 | + * | |
| 534 | + * @deprecated 13.3 | |
| 535 | + * | |
| 536 | + * @return array | |
| 537 | + */ | |
| 538 | + public function get_cart_checkout_info() { | |
| 539 | + $info = array_merge( | |
| 540 | + $this->get_cart_page_block_usage(), | |
| 541 | + $this->get_checkout_page_block_usage() | |
| 223 | 542 | ); |
| 224 | - set_transient( $transient_name, $info, DAY_IN_SECONDS ); | |
| 225 | 543 | return $info; |
| 226 | 544 | } |
| 227 | 545 | |
| 228 | 546 | /** |
| @@ -229,8 +547,10 @@ | ||
| 229 | 547 | * Search a specific post for text content. |
| 230 | 548 | * |
| 231 | 549 | * Note: similar code is in a WooCommerce core PR: |
| 232 | 550 | * https://github.com/woocommerce/woocommerce/pull/25932 |
| 551 | + * | |
| 552 | + * @deprecated 13.3 | |
| 233 | 553 | * |
| 234 | 554 | * @param integer $post_id The id of the post to search. |
| 235 | 555 | * @param string $text The text to search for. |
| 236 | 556 | * @return integer 1 if post contains $text (otherwise 0). |