session; if ( ! $s['enabled'] || ! $s['events']['add_to_cart'] || ! $session ) { return; } $context = Consent::current(); if ( ! $context['marketing'] && ! $context['statistics'] ) { return; } $p = wc_get_product( $variation_id ? $variation_id : $product_id ); if ( ! $p ) { return; } $key = 'woo:cart:' . wp_generate_uuid4(); $snapshot = Data::product( $p, $quantity ); if ( 'hook' === $s['add_to_cart_source'] ) { Delivery::enqueue( $key, 'woo_add_to_cart', $snapshot, $context ); } $pending = (array) \PixelPair\Settings\Migrations::session( WC()->session, 'pixelpair_pending', array() ); $pending[] = array( 'key' => $key, 'snapshot' => $snapshot, 'expires' => time() + 600, ); WC()->session->set( 'pixelpair_pending', array_slice( $pending, -20 ) ); if ( method_exists( $session, 'set_customer_session_cookie' ) ) { $session->set_customer_session_cookie( true ); } } /** * Provide validated commerce data. * * @param \WC_Order $order Created checkout order. */ public function capture_order( $order ): void { $s = Settings::get( 'woo' ); if ( ! $s['enabled'] ) { return; } $context = Consent::current(); if ( ! $context['marketing'] && ! $context['statistics'] ) { return; } if ( $context['marketing'] && ( new \PixelPair\Settings\Repository() )->get()['meta_advanced_matching'] ) { $billing = array( 'em' => $order->get_billing_email(), 'ph' => $order->get_billing_phone(), 'fn' => $order->get_billing_first_name(), 'ln' => $order->get_billing_last_name(), 'ct' => $order->get_billing_city(), 'st' => $order->get_billing_state(), 'zp' => $order->get_billing_postcode(), 'country' => $order->get_billing_country(), ); $context['matching'] = array_replace( (array) ( $context['matching'] ?? array() ), \PixelPair\UserData\Normalizer::hashes( $billing ) ); } if ( $context['marketing'] && ( new \PixelPair\Settings\Repository() )->get()['tiktok_advanced_matching'] ) { $context['tiktok_matching'] = array_replace( (array) ( $context['tiktok_matching'] ?? array() ), \PixelPair\UserData\Normalizer::hashes( array( 'em' => $order->get_billing_email(), 'ph' => $order->get_billing_phone(), ) ) ); } try { $order->update_meta_data( '_pixelpair_context', Crypto::encrypt( (string) wp_json_encode( $context ) ) ); } catch ( \Throwable $error ) { return; } /** * Orders can also be created without a browser session. * * @var \WC_Session|null $session Optional checkout session. */ $session = WC()->session; if ( $s['attribution'] && $session ) { $a = \PixelPair\Settings\Migrations::session( $session, 'pixelpair_attribution', array() ); $order->update_meta_data( '_pixelpair_attribution', Consent::attribution( is_array( $a ) ? $a : array(), $context['marketing'] ) ); } $order->save(); } /** * Provide validated commerce data. * * @param int $id Paid order. */ public function schedule_purchase( int $id ): void { if ( Settings::get( 'woo' )['safety_net'] && ! wp_next_scheduled( 'pixelpair_woo_purchase', array( $id ) ) ) { wp_schedule_single_event( time() + 60, 'pixelpair_woo_purchase', array( $id ) ); } } /** * Provide validated commerce data. * * @param int $id Paid order. */ public function purchase( int $id ): void { if ( ! function_exists( 'wc_get_order' ) || ! Settings::get( 'woo' )['enabled'] ) { return; } $o = wc_get_order( $id ); if ( ! $o || ! $o->is_paid() || ! $this->renewal_allowed( $o ) ) { return; } $context = self::context( $o ); if ( ! Consent::retained( $context, 'marketing' ) ) { return; } Delivery::enqueue( 'woo:purchase:' . $id, 'woo_purchase', Data::order( $o ), $context ); } /** * Provide validated commerce data. * * @param \WC_Order $order Order. * @return bool Renewal permitted. */ public function renewal_allowed( $order ): bool { return Settings::get( 'woo' )['renewals'] || ! ( $order->get_meta( '_subscription_renewal' ) || ( function_exists( 'wcs_order_contains_renewal' ) && wcs_order_contains_renewal( $order ) ) ); } /** * Provide validated commerce data. * * @param \WC_Order $order Order. * @return array Protected captured context. */ public static function context( $order ): array { $raw = \PixelPair\Settings\Migrations::meta( 'woo', $order, '_pixelpair_context' ); $plain = is_string( $raw ) ? Crypto::decrypt( $raw ) : null; $context = $plain ? json_decode( $plain, true ) : array(); return is_array( $context ) ? $context : array(); } /** * Provide validated commerce data. * * @param \WC_Order $order Order currently viewed. */ public function attribution_box( $order ): void { if ( Settings::get( 'woo' )['hide_attribution'] || ! current_user_can( 'edit_shop_orders' ) ) { return; } $data = \PixelPair\Settings\Migrations::meta( 'woo', $order, '_pixelpair_attribution' ); if ( ! is_array( $data ) || ! $data ) { return; } echo '

' . esc_html__( 'PixelPair attribution', 'pixelpair' ) . '

'; foreach ( $data as $key => $value ) { echo '
' . esc_html( $key ) . '
' . esc_html( (string) $value ) . '
'; } echo '
'; } }