class-jetpack-woocommerce-analytics-checkout-flow.php
2 years ago
class-jetpack-woocommerce-analytics-my-account.php
2 years ago
class-jetpack-woocommerce-analytics-trait.php
2 years ago
class-jetpack-woocommerce-analytics-universal.php
2 years ago
class-jetpack-woocommerce-analytics-trait.php
611 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Jetpack_WooCommerce_Analytics_Trait |
| 4 | * |
| 5 | * @deprecated 13.3 |
| 6 | * |
| 7 | * @package automattic/jetpack |
| 8 | * @author Automattic |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * Bail if accessed directly |
| 13 | */ |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit; |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Jetpack_WooCommerce_Analytics_Trait |
| 20 | * Common functionality for WooCommerce Analytics classes. |
| 21 | * |
| 22 | * @deprecated 13.3 |
| 23 | */ |
| 24 | trait Jetpack_WooCommerce_Analytics_Trait { |
| 25 | |
| 26 | /** |
| 27 | * Saves whether the cart/checkout templates are in use based on WC Blocks version. |
| 28 | * |
| 29 | * @var bool true if the templates are in use. |
| 30 | */ |
| 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 ); |
| 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 | } |
| 121 | ); |
| 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; |
| 138 | } |
| 139 | |
| 140 | /** |
| 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. |
| 143 | * |
| 144 | * @deprecated 13.3 |
| 145 | * |
| 146 | * @return void Does not return, but sets class properties. |
| 147 | */ |
| 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; |
| 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; |
| 201 | } |
| 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 ( function_exists( 'gutenberg_get_block_template' ) ) { |
| 218 | $checkout_template = gutenberg_get_block_template( 'woocommerce/woocommerce//page-checkout' ); |
| 219 | $cart_template = gutenberg_get_block_template( 'woocommerce/woocommerce//page-cart' ); |
| 220 | |
| 221 | if ( ! $checkout_template ) { |
| 222 | $checkout_template = gutenberg_get_block_template( 'woocommerce/woocommerce//checkout' ); |
| 223 | } |
| 224 | |
| 225 | if ( ! $cart_template ) { |
| 226 | $cart_template = gutenberg_get_block_template( 'woocommerce/woocommerce//cart' ); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | if ( ! empty( $checkout_template->content ) ) { |
| 231 | // 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. |
| 232 | $this->checkout_content_source = $checkout_template->content; |
| 233 | $is_using_page_content = str_contains( $checkout_template->content, '<!-- wp:woocommerce/page-content-wrapper {"page":"checkout"}' ); |
| 234 | |
| 235 | if ( $is_using_page_content ) { |
| 236 | // The page-content-wrapper is in use, so we need to get the page content. |
| 237 | $checkout_page = get_post( wc_get_page_id( 'checkout' ) ); |
| 238 | |
| 239 | if ( $checkout_page && isset( $checkout_page->post_content ) ) { |
| 240 | $this->checkout_content_source = $checkout_page->post_content; |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | if ( ! empty( $cart_template->content ) ) { |
| 246 | // 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. |
| 247 | $this->cart_content_source = $cart_template->content; |
| 248 | $is_using_page_content = str_contains( $cart_template->content, '<!-- wp:woocommerce/page-content-wrapper {"page":"cart"}' ); |
| 249 | |
| 250 | if ( $is_using_page_content ) { |
| 251 | // The page-content-wrapper is in use, so we need to get the page content. |
| 252 | $cart_page = get_post( wc_get_page_id( 'cart' ) ); |
| 253 | |
| 254 | if ( $cart_page && isset( $cart_page->post_content ) ) { |
| 255 | $this->cart_content_source = $cart_page->post_content; |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | set_transient( |
| 261 | $cart_checkout_content_cache_transient_name, |
| 262 | array( |
| 263 | 'cart_content_source' => $this->cart_content_source, |
| 264 | 'checkout_content_source' => $this->checkout_content_source, |
| 265 | ), |
| 266 | DAY_IN_SECONDS |
| 267 | ); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Default event properties which should be included with all events. |
| 272 | * |
| 273 | * @deprecated 13.3 |
| 274 | * |
| 275 | * @return array Array of standard event props. |
| 276 | */ |
| 277 | public function get_common_properties() { |
| 278 | $site_info = array( |
| 279 | 'blog_id' => Jetpack::get_option( 'id' ), |
| 280 | 'ui' => $this->get_user_id(), |
| 281 | 'url' => home_url(), |
| 282 | 'woo_version' => WC()->version, |
| 283 | 'store_admin' => in_array( array( 'administrator', 'shop_manager' ), wp_get_current_user()->roles, true ) ? 1 : 0, |
| 284 | 'device' => wp_is_mobile() ? 'mobile' : 'desktop', |
| 285 | 'template_used' => $this->cart_checkout_templates_in_use ? '1' : '0', |
| 286 | 'additional_blocks_on_cart_page' => $this->additional_blocks_on_cart_page, |
| 287 | 'additional_blocks_on_checkout_page' => $this->additional_blocks_on_checkout_page, |
| 288 | 'store_currency' => get_woocommerce_currency(), |
| 289 | ); |
| 290 | $cart_checkout_info = $this->get_cart_checkout_info(); |
| 291 | return array_merge( $site_info, $cart_checkout_info ); |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Render tracks event properties as string of JavaScript object props. |
| 296 | * |
| 297 | * @deprecated 13.3 |
| 298 | * |
| 299 | * @param array $properties Array of key/value pairs. |
| 300 | * @return string String of the form "key1: value1, key2: value2, " (etc). |
| 301 | */ |
| 302 | private function render_properties_as_js( $properties ) { |
| 303 | $js_args_string = ''; |
| 304 | foreach ( $properties as $key => $value ) { |
| 305 | if ( is_array( $value ) ) { |
| 306 | $js_args_string = $js_args_string . "'$key': " . wp_json_encode( $value ) . ','; |
| 307 | } else { |
| 308 | $js_args_string = $js_args_string . "'$key': '" . esc_js( $value ) . "', "; |
| 309 | } |
| 310 | } |
| 311 | return $js_args_string; |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Record an event with optional product and custom properties. |
| 316 | * |
| 317 | * @deprecated 13.3 |
| 318 | * |
| 319 | * @param string $event_name The name of the event to record. |
| 320 | * @param array $properties Optional array of (key => value) event properties. |
| 321 | * @param integer $product_id The id of the product relating to the event. |
| 322 | * |
| 323 | * @return string|void |
| 324 | */ |
| 325 | public function record_event( $event_name, $properties = array(), $product_id = null ) { |
| 326 | $js = $this->process_event_properties( $event_name, $properties, $product_id ); |
| 327 | wc_enqueue_js( "_wca.push({$js});" ); |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Gather relevant product information |
| 332 | * |
| 333 | * @deprecated 13.3 |
| 334 | * |
| 335 | * @param \WC_Product $product product. |
| 336 | * @return array |
| 337 | */ |
| 338 | public function get_product_details( $product ) { |
| 339 | return array( |
| 340 | 'pi' => $product->get_id(), |
| 341 | 'pn' => $product->get_title(), |
| 342 | 'pc' => $this->get_product_categories_concatenated( $product ), |
| 343 | 'pp' => $product->get_price(), |
| 344 | 'pt' => $product->get_type(), |
| 345 | ); |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Gets product categories or varation attributes as a formatted concatenated string |
| 350 | * |
| 351 | * @deprecated 13.3 |
| 352 | * |
| 353 | * @param object $product WC_Product. |
| 354 | * @return string |
| 355 | */ |
| 356 | public function get_product_categories_concatenated( $product ) { |
| 357 | |
| 358 | if ( ! $product instanceof WC_Product ) { |
| 359 | return ''; |
| 360 | } |
| 361 | |
| 362 | $variation_data = $product->is_type( 'variation' ) ? wc_get_product_variation_attributes( $product->get_id() ) : ''; |
| 363 | if ( is_array( $variation_data ) && ! empty( $variation_data ) ) { |
| 364 | $line = wc_get_formatted_variation( $variation_data, true ); |
| 365 | } else { |
| 366 | $out = array(); |
| 367 | $categories = get_the_terms( $product->get_id(), 'product_cat' ); |
| 368 | if ( $categories ) { |
| 369 | foreach ( $categories as $category ) { |
| 370 | $out[] = $category->name; |
| 371 | } |
| 372 | } |
| 373 | $line = implode( '/', $out ); |
| 374 | } |
| 375 | return $line; |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Compose event properties. |
| 380 | * |
| 381 | * @deprecated 13.3 |
| 382 | * |
| 383 | * @param string $event_name The name of the event to record. |
| 384 | * @param array $properties Optional array of (key => value) event properties. |
| 385 | * @param integer $product_id Optional id of the product relating to the event. |
| 386 | * |
| 387 | * @return string|void |
| 388 | */ |
| 389 | public function process_event_properties( $event_name, $properties = array(), $product_id = null ) { |
| 390 | |
| 391 | // Only set product details if we have a product id. |
| 392 | if ( $product_id ) { |
| 393 | $product = wc_get_product( $product_id ); |
| 394 | if ( ! $product instanceof WC_Product ) { |
| 395 | return; |
| 396 | } |
| 397 | $product_details = $this->get_product_details( $product ); |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Allow defining custom event properties in WooCommerce Analytics. |
| 402 | * |
| 403 | * @module woocommerce-analytics |
| 404 | * |
| 405 | * @since 12.5 |
| 406 | * |
| 407 | * @param array $all_props Array of event props to be filtered. |
| 408 | */ |
| 409 | $all_props = apply_filters( |
| 410 | 'jetpack_woocommerce_analytics_event_props', |
| 411 | array_merge( |
| 412 | $this->get_common_properties(), // We put this here to allow override of common props. |
| 413 | $properties |
| 414 | ) |
| 415 | ); |
| 416 | |
| 417 | $js = "{'_en': '" . esc_js( $event_name ) . "'"; |
| 418 | |
| 419 | if ( isset( $product_details ) ) { |
| 420 | $all_props = array_merge( $all_props, $product_details ); |
| 421 | } |
| 422 | |
| 423 | $js .= ',' . $this->render_properties_as_js( $all_props ) . '}'; |
| 424 | |
| 425 | return $js; |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * Get the current user id |
| 430 | * |
| 431 | * @deprecated 13.3 |
| 432 | * |
| 433 | * @return int |
| 434 | */ |
| 435 | public function get_user_id() { |
| 436 | if ( is_user_logged_in() ) { |
| 437 | $blogid = Jetpack::get_option( 'id' ); |
| 438 | $userid = get_current_user_id(); |
| 439 | return $blogid . ':' . $userid; |
| 440 | } |
| 441 | return 'null'; |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * Gets the IDs of additional blocks on the Cart/Checkout pages or templates. |
| 446 | * |
| 447 | * @deprecated 13.3 |
| 448 | * |
| 449 | * @param string $cart_or_checkout Whether to get blocks on the cart or checkout page. |
| 450 | * @return array All inner blocks on the page. |
| 451 | */ |
| 452 | public function get_additional_blocks_on_page( $cart_or_checkout = 'cart' ) { |
| 453 | |
| 454 | $additional_blocks_on_page_transient_name = 'jetpack_woocommerce_analytics_additional_blocks_on_' . $cart_or_checkout . '_page'; |
| 455 | $additional_blocks_on_page = get_transient( $additional_blocks_on_page_transient_name ); |
| 456 | |
| 457 | if ( false !== $additional_blocks_on_page ) { |
| 458 | return $additional_blocks_on_page; |
| 459 | } |
| 460 | |
| 461 | $content = $this->cart_content_source; |
| 462 | |
| 463 | if ( 'checkout' === $cart_or_checkout ) { |
| 464 | $content = $this->checkout_content_source; |
| 465 | } |
| 466 | |
| 467 | $parsed_blocks = parse_blocks( $content ); |
| 468 | $other_blocks = array_filter( |
| 469 | $parsed_blocks, |
| 470 | function ( $block ) use ( $cart_or_checkout ) { |
| 471 | if ( ! isset( $block['blockName'] ) ) { |
| 472 | return false; |
| 473 | } |
| 474 | |
| 475 | if ( 'woocommerce/classic-shortcode' === $block['blockName'] ) { |
| 476 | return false; |
| 477 | } |
| 478 | |
| 479 | if ( 'core/shortcode' === $block['blockName'] ) { |
| 480 | return false; |
| 481 | } |
| 482 | |
| 483 | if ( 'checkout' === $cart_or_checkout && 'woocommerce/checkout' !== $block['blockName'] ) { |
| 484 | return true; |
| 485 | } |
| 486 | |
| 487 | if ( 'cart' === $cart_or_checkout && 'woocommerce/cart' !== $block['blockName'] ) { |
| 488 | return true; |
| 489 | } |
| 490 | |
| 491 | return false; |
| 492 | } |
| 493 | ); |
| 494 | |
| 495 | $all_inner_blocks = array(); |
| 496 | |
| 497 | // Loop over each "block group". In templates the blocks are grouped up. |
| 498 | foreach ( $other_blocks as $block ) { |
| 499 | |
| 500 | // This check is necessary because sometimes this is null when using templates. |
| 501 | if ( ! empty( $block['blockName'] ) ) { |
| 502 | $all_inner_blocks[] = $block['blockName']; |
| 503 | } |
| 504 | |
| 505 | if ( ! isset( $block['innerBlocks'] ) || ! is_array( $block['innerBlocks'] ) || 0 === count( $block['innerBlocks'] ) ) { |
| 506 | continue; |
| 507 | } |
| 508 | |
| 509 | foreach ( $block['innerBlocks'] as $inner_content ) { |
| 510 | $all_inner_blocks = array_merge( $all_inner_blocks, $this->get_inner_blocks( $inner_content ) ); |
| 511 | } |
| 512 | } |
| 513 | set_transient( $additional_blocks_on_page_transient_name, $all_inner_blocks, DAY_IN_SECONDS ); |
| 514 | return $all_inner_blocks; |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * Gets an array containing the block or shortcode use properties for the Cart page. |
| 519 | * |
| 520 | * @deprecated 13.3 |
| 521 | * |
| 522 | * @return array An array containing the block or shortcode use properties for the Cart page. |
| 523 | */ |
| 524 | public function get_cart_page_block_usage() { |
| 525 | $new_info = array(); |
| 526 | |
| 527 | $content = $this->cart_content_source; |
| 528 | $block_presence = str_contains( $content, '<!-- wp:woocommerce/cart' ); |
| 529 | $shortcode_presence = str_contains( $content, '[woocommerce_cart]' ); |
| 530 | $classic_shortcode_presence = str_contains( $content, '<!-- wp:woocommerce/classic-shortcode' ); |
| 531 | |
| 532 | $new_info['cart_page_contains_cart_block'] = $block_presence ? '1' : '0'; |
| 533 | $new_info['cart_page_contains_cart_shortcode'] = $shortcode_presence || $classic_shortcode_presence ? '1' : '0'; |
| 534 | return $new_info; |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * Gets an array containing the block or shortcode use properties for the Checkout page. |
| 539 | * |
| 540 | * @deprecated 13.3 |
| 541 | * |
| 542 | * @return array An array containing the block or shortcode use properties for the Checkout page. |
| 543 | */ |
| 544 | public function get_checkout_page_block_usage() { |
| 545 | $new_info = array(); |
| 546 | |
| 547 | $content = $this->checkout_content_source; |
| 548 | $block_presence = str_contains( $content, '<!-- wp:woocommerce/checkout' ); |
| 549 | $shortcode_presence = str_contains( $content, '[woocommerce_checkout]' ); |
| 550 | $classic_shortcode_presence = str_contains( $content, '<!-- wp:woocommerce/classic-shortcode' ); |
| 551 | |
| 552 | $new_info['checkout_page_contains_checkout_block'] = $block_presence ? '1' : '0'; |
| 553 | $new_info['checkout_page_contains_checkout_shortcode'] = $shortcode_presence || $classic_shortcode_presence ? '1' : '0'; |
| 554 | return $new_info; |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * Get info about the cart & checkout pages, in particular |
| 559 | * whether the store is using shortcodes or Gutenberg blocks. |
| 560 | * This info is cached in a transient. |
| 561 | * |
| 562 | * Note: similar code is in a WooCommerce core PR: |
| 563 | * https://github.com/woocommerce/woocommerce/pull/25932 |
| 564 | * |
| 565 | * @deprecated 13.3 |
| 566 | * |
| 567 | * @return array |
| 568 | */ |
| 569 | public function get_cart_checkout_info() { |
| 570 | $info = array_merge( |
| 571 | $this->get_cart_page_block_usage(), |
| 572 | $this->get_checkout_page_block_usage() |
| 573 | ); |
| 574 | return $info; |
| 575 | } |
| 576 | |
| 577 | /** |
| 578 | * Search a specific post for text content. |
| 579 | * |
| 580 | * Note: similar code is in a WooCommerce core PR: |
| 581 | * https://github.com/woocommerce/woocommerce/pull/25932 |
| 582 | * |
| 583 | * @deprecated 13.3 |
| 584 | * |
| 585 | * @param integer $post_id The id of the post to search. |
| 586 | * @param string $text The text to search for. |
| 587 | * @return integer 1 if post contains $text (otherwise 0). |
| 588 | */ |
| 589 | public function post_contains_text( $post_id, $text ) { |
| 590 | global $wpdb; |
| 591 | |
| 592 | // Search for the text anywhere in the post. |
| 593 | $wildcarded = "%{$text}%"; |
| 594 | |
| 595 | // No better way to search post content without having filters expanding blocks. |
| 596 | // This is already cached up in the parent function. |
| 597 | $result = $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching |
| 598 | $wpdb->prepare( |
| 599 | " |
| 600 | SELECT COUNT( * ) FROM {$wpdb->prefix}posts |
| 601 | WHERE ID=%d |
| 602 | AND {$wpdb->prefix}posts.post_content LIKE %s |
| 603 | ", |
| 604 | array( $post_id, $wildcarded ) |
| 605 | ) |
| 606 | ); |
| 607 | |
| 608 | return ( '0' !== $result ) ? 1 : 0; |
| 609 | } |
| 610 | } |
| 611 |