array(), 'removed' => array(), 'before' => array(), 'after' => array(), 'source_flags' => self::flags( false, false, false, false ), ); if ( ! is_object( $request ) ) { return $empty; } $request_id = (int) $request->id; $created = strtotime( (string) $request->created_at . ' UTC' ); if ( ! $created ) { $created = time(); } $window = AIEO_Product_Request_Settings::trail_window(); $requested = array(); foreach ( AIEO_Product_Request_Store::items( $request_id ) as $row ) { $requested[] = self::decorate( (int) $row->product_id, array( 'variation_id' => (int) $row->variation_id, 'qty' => (int) $row->qty, 'source' => (string) $row->source, 'at' => (string) $row->added_at, ) ); } $removed = array(); foreach ( AIEO_Product_Request_Store::removed_items( $request_id ) as $row ) { $removed[] = self::decorate( (int) $row->product_id, array( 'variation_id' => (int) $row->variation_id, 'qty' => (int) $row->qty, 'source' => (string) $row->source, 'at' => (string) $row->added_at, 'removed_at' => (string) $row->removed_at, ) ); } $asked_ids = AIEO_Product_Request_Store::item_product_ids( $request_id ); $beacon_before = array(); $beacon_after = array(); $have_beacon = false; foreach ( AIEO_Product_Request_Store::views( $request_id ) as $v ) { $have_beacon = true; $pid = (int) $v->product_id; if ( in_array( $pid, $asked_ids, true ) ) { continue; } $ts = strtotime( (string) $v->viewed_at . ' UTC' ); $bucket = ( $ts && $ts < $created ) ? 'before' : 'after'; if ( 'before' === $bucket ) { $beacon_before[ $pid ] = array( 'at' => (string) $v->viewed_at, 'src' => 'beacon' ); } else { $beacon_after[ $pid ] = array( 'at' => (string) $v->viewed_at, 'src' => 'beacon' ); } } $sess_before = array(); $sess_after = array(); $sess_queried = false; $sess_hit = false; $session_ids = AIEO_Product_Request_Store::session_ids( $request ); if ( $session_ids && self::sessions_available() ) { $sess_queried = true; foreach ( self::session_views( $session_ids, $created, $window ) as $pid => $meta ) { $sess_hit = true; if ( in_array( (int) $pid, $asked_ids, true ) ) { continue; } if ( $meta['ts'] < $created ) { $sess_before[ (int) $pid ] = array( 'at' => $meta['at'], 'src' => 'sessions', 'dwell' => $meta['dwell'] ); } else { $sess_after[ (int) $pid ] = array( 'at' => $meta['at'], 'src' => 'sessions', 'dwell' => $meta['dwell'] ); } } } $recent_before = array(); $snapshot = json_decode( (string) $request->seen_before, true ); $have_recent = is_array( $snapshot ) && ! empty( $snapshot ); if ( $have_recent ) { foreach ( array_reverse( $snapshot ) as $entry ) { $pid = 0; $t = 0; if ( is_array( $entry ) ) { $pid = isset( $entry['id'] ) ? absint( $entry['id'] ) : 0; $t = isset( $entry['t'] ) ? (int) $entry['t'] : 0; } elseif ( is_numeric( $entry ) ) { $pid = absint( $entry ); } if ( $pid <= 0 || in_array( $pid, $asked_ids, true ) || isset( $recent_before[ $pid ] ) ) { continue; } if ( $t > 0 && $t < $created - $window ) { continue; } $recent_before[ $pid ] = array( 'at' => ( $t > 0 && $t <= $created ) ? gmdate( 'Y-m-d H:i:s', $t ) : '', 'src' => 'recent', ); } } $before = self::merge_and_decorate( array( $sess_before, $beacon_before, $recent_before ) ); $after = self::merge_and_decorate( array( $sess_after, $beacon_after ) ); return array( 'requested' => $requested, 'removed' => $removed, 'before' => $before, 'after' => $after, 'source_flags' => self::flags( $have_beacon, $sess_hit, $sess_queried, $have_recent ), ); } private static function flags( $beacon, $sessions_hit, $sessions_queried, $recent ) { return AIEO_Submission_Trail::flags( $beacon, $sessions_hit, $sessions_queried, $recent ); } public static function tracking_enabled() { return AIEO_Submission_Trail::tracking_enabled(); } private static function consent_gated() { return AIEO_Submission_Trail::consent_gated(); } private static function sessions_available() { return AIEO_Submission_Trail::sessions_available(); } private static function session_views( array $session_ids, $created, $window ) { return AIEO_Submission_Trail::product_views( $session_ids, $created, $window ); } private static function merge_and_decorate( array $maps ) { $merged = array(); foreach ( $maps as $map ) { foreach ( $map as $pid => $meta ) { if ( ! isset( $merged[ (int) $pid ] ) ) { $merged[ (int) $pid ] = $meta; } } } uasort( $merged, static function ( $a, $b ) { return strcmp( (string) ( $b['at'] ?? '' ), (string) ( $a['at'] ?? '' ) ); } ); $out = array(); foreach ( $merged as $pid => $meta ) { $out[] = self::decorate( (int) $pid, $meta ); if ( count( $out ) >= self::CAP ) { break; } } return $out; } private static function decorate( $product_id, array $meta = array() ) { $prod = function_exists( 'wc_get_product' ) ? wc_get_product( $product_id ) : null; $vid = (int) ( $meta['variation_id'] ?? 0 ); $var = $vid > 0 && function_exists( 'wc_get_product' ) ? wc_get_product( $vid ) : null; $prod = $var ? $var : $prod; return array_merge( array( 'product_id' => (int) $product_id, 'name' => $prod ? $prod->get_name() : sprintf( __( 'Deleted product #%d', 'ai-eshop-optimizer' ), (int) $product_id ), 'permalink' => $prod ? $prod->get_permalink() : '', 'edit_link' => $product_id > 0 ? get_edit_post_link( $product_id, 'raw' ) : '', 'price_html' => $prod ? $prod->get_price_html() : '', 'sku' => $prod && method_exists( $prod, 'get_sku' ) ? (string) $prod->get_sku() : '', 'at' => '', 'dwell' => 0, 'src' => 'item', ), $meta ); } private static function qty_shown() { if ( ! class_exists( 'AIEO_Product_Request_Settings' ) ) { return true; } $s = AIEO_Product_Request_Settings::get(); return 'yes' === (string) ( isset( $s['field_qty'] ) ? $s['field_qty'] : 'yes' ); } public static function items_heading( $type ) { switch ( (string) $type ) { case 'contact': return __( 'Asked about', 'ai-eshop-optimizer' ); case 'callback': return __( 'Call back about', 'ai-eshop-optimizer' ); } return __( 'Requested for quotation', 'ai-eshop-optimizer' ); } public static function as_text( $request ) { $t = self::for_request( $request ); $lines = array(); $lines[] = self::items_heading( is_object( $request ) && isset( $request->request_type ) ? (string) $request->request_type : 'quote' ) . ':'; if ( $t['requested'] ) { foreach ( $t['requested'] as $p ) { $lines[] = self::qty_shown() ? sprintf( ' - %s x%d %s', $p['name'], max( 1, (int) $p['qty'] ), $p['permalink'] ) : sprintf( ' - %s %s', $p['name'], $p['permalink'] ); } } else { $lines[] = ' ' . __( '(none)', 'ai-eshop-optimizer' ); } foreach ( array( 'before' => __( 'Viewed before asking:', 'ai-eshop-optimizer' ), 'after' => __( 'Viewed since asking:', 'ai-eshop-optimizer' ), ) as $key => $heading ) { if ( empty( $t[ $key ] ) ) { continue; } $lines[] = ''; $lines[] = $heading; foreach ( array_slice( $t[ $key ], 0, 15 ) as $p ) { $lines[] = ' - ' . $p['name'] . ' ' . $p['permalink']; } } $flags = $t['source_flags']; if ( ! $flags['tracking_on'] ) { if ( empty( $t['before'] ) ) { $lines[] = ''; $lines[] = __( 'Note: session tracking is off on this site, so the browsing trail is limited to products viewed after the request was sent.', 'ai-eshop-optimizer' ); } } elseif ( ! $flags['sessions'] ) { $lines[] = ''; $lines[] = $flags['consent_gated'] ? __( 'Note: no session activity was recorded for this visitor. Tracking on this site waits for consent, so they may have declined.', 'ai-eshop-optimizer' ) : __( 'Note: no session activity was recorded for this visitor around the time of the request.', 'ai-eshop-optimizer' ); } return implode( "\n", $lines ); } } }