'defer' ) ); $entries = self::page_entries(); if ( array() !== $entries ) { wp_add_inline_script( self::HANDLE, self::queue_script( $entries ), 'before' ); } } public static function adapter_url(): string { return (string) apply_filters( 'flowfy_adapter_url', FLOWFY_ADAPTER_URL ); } public static function add_attributes( $attributes ) { if ( ! is_array( $attributes ) || ! isset( $attributes['id'] ) || self::HANDLE . '-js' !== $attributes['id'] ) { return $attributes; } $attributes['data-source-id'] = Connection::site_id(); return $attributes; } public static function page_entries(): array { $entries = array_merge( self::identify_entries(), self::context_entries(), CartQueue::drain() ); $purchase = self::purchase_payload(); if ( null !== $purchase ) { $entries[] = array( 'purchase', $purchase ); } return $entries; } public static function queue_script( array $entries ): string { $script = 'window.flowfyWooQ = window.flowfyWooQ || [];'; foreach ( $entries as $entry ) { $json = wp_json_encode( $entry, self::JSON_FLAGS ); if ( false !== $json ) { $script .= 'window.flowfyWooQ.push(' . $json . ');'; } } return $script; } private static function identify_entries(): array { $user = wp_get_current_user(); if ( ! $user->exists() || user_can( $user, 'manage_woocommerce' ) ) { return array(); } wc_maybe_define_constant( 'DONOTCACHEPAGE', true ); $customer = new WC_Customer( $user->ID ); $traits = array( 'user_id' => (string) $user->ID, 'email' => $user->user_email, 'phone' => $customer->get_billing_phone(), 'firstname' => $user->first_name, 'lastname' => $user->last_name, ); return array( array( 'identify', array_filter( $traits ) ) ); } private static function context_entries(): array { $candidates = array( 'product_view' => self::product_view_payload(), 'search' => self::search_payload(), 'list_view' => self::list_view_payload(), 'cart_view' => self::cart_view_payload(), 'checkout_start' => self::checkout_start_payload(), ); $entries = array(); foreach ( array_filter( $candidates ) as $name => $payload ) { $entries[] = array( $name, $payload ); } return $entries; } private static function product_view_payload(): ?array { $product = is_product() ? wc_get_product( get_queried_object_id() ) : null; return $product instanceof WC_Product ? Payloads::product_viewed( $product ) : null; } private static function search_payload(): ?array { return is_search() && 'product' === get_query_var( 'post_type' ) ? Payloads::products_searched( get_search_query( false ) ) : null; } private static function list_view_payload(): ?array { global $wp_query; if ( ! is_shop() && ! is_product_taxonomy() ) { return null; } $products = array_filter( array_map( 'wc_get_product', wp_list_pluck( (array) $wp_query->posts, 'ID' ) ) ); $term = get_queried_object(); $category = $term instanceof WP_Term && 'product_cat' === $term->taxonomy ? $term->name : ''; $list_id = $term instanceof WP_Term ? $term->slug : 'shop'; return Payloads::product_list_viewed( $products, $category, $list_id ); } private static function cart_view_payload(): ?array { return is_cart() && WC()->cart ? Payloads::cart_viewed( WC()->cart, CartQueue::cart_id() ) : null; } private static function checkout_start_payload(): ?array { if ( ! is_checkout() || is_order_received_page() || is_checkout_pay_page() || ! WC()->cart ) { return null; } return Payloads::checkout_started( WC()->cart, CartQueue::cart_id() ); } private static function purchase_payload(): ?array { $order = self::received_order(); if ( null === $order || '' !== (string) $order->get_meta( self::META_BROWSER_SENT ) ) { return null; } $payload = Payloads::order_completed( $order ); if ( null === $payload ) { return null; } $order->update_meta_data( self::META_BROWSER_SENT, (string) time() ); $order->save_meta_data(); return $payload; } private static function received_order(): ?WC_Order { if ( ! is_order_received_page() ) { return null; } $order = wc_get_order( absint( get_query_var( 'order-received' ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- the order key is the proof; nonces break on cached thank-you pages. $key = isset( $_GET['key'] ) && is_string( $_GET['key'] ) ? sanitize_text_field( wp_unslash( $_GET['key'] ) ) : ''; if ( ! $order instanceof WC_Order || '' === $key || ! hash_equals( $order->get_order_key(), $key ) || $order->has_status( 'failed' ) ) { return null; } return $order; } }