url( [ 'account' => $this->number( 'account' ), 'order' => $this->number( 'order' ), 'paged' => max( 1, $this->number( 'paged' ) ) ] ) ); exit; } public function styles( $hook ) { if ( 'toplevel_page_activity-track-ai' === $hook && ATKAI_Admin_Menus::is_woocommerce_tab() ) { wp_enqueue_style( 'atk-security', plugins_url( 'assets/admin/css/atk.security.css', ATKAI_PLUGIN_FILE ), [], filemtime( ATKAI_ABSPATH . 'assets/admin/css/atk.security.css' ) ); wp_enqueue_script( 'atk-security-overview', plugins_url( 'assets/admin/js/atk.security.js', ATKAI_PLUGIN_FILE ), [], filemtime( ATKAI_ABSPATH . 'assets/admin/js/atk.security.js' ), false ); wp_localize_script( 'atk-security-overview', 'atkSecurityOverview', [ 'key' => 'atk-woocommerce-overview-' . get_current_blog_id() . '-' . get_current_user_id(), 'show' => __( 'Show overview', 'activity-track' ), 'hide' => __( 'Hide overview', 'activity-track' ), ] ); } } private function number( $name ) { return isset( $_GET[ $name ] ) && is_scalar( $_GET[ $name ] ) ? absint( $_GET[ $name ] ) : 0; } private function url( $args = [] ) { return add_query_arg( array_merge( [ 'page' => 'activity-track-ai', 'tab' => 'woocommerce' ], $args ), admin_url( 'admin.php' ) ); } public static function label( $type ) { $labels = [ 'account_created' => __( 'Account created', 'activity-track' ), 'login' => __( 'Account signed in', 'activity-track' ), 'profile_updated' => __( 'Account profile updated', 'activity-track' ), 'role_changed' => __( 'Account role changed', 'activity-track' ), 'checkout_submitted' => __( 'Checkout submitted', 'activity-track' ), 'order_paid' => __( 'WooCommerce recorded payment complete', 'activity-track' ), 'payment_error' => __( 'Stripe checkout reported an error', 'activity-track' ), 'payment_method_added' => __( 'Saved payment method added', 'activity-track' ), 'stripe_payment' => __( 'Stripe payment outcome', 'activity-track' ), 'card_setup' => __( 'Stripe card setup outcome', 'activity-track' ), ]; return $labels[ $type ] ?? __( 'Account activity', 'activity-track' ); } public static function profile_description( $reason ) { $labels = [ 'contact' => __( 'Contact details', 'activity-track' ), 'name' => __( 'Name', 'activity-track' ), 'password' => __( 'Password', 'activity-track' ), 'billing_address' => __( 'Billing address', 'activity-track' ), 'shipping_address' => __( 'Shipping address', 'activity-track' ) ]; $changed = array_intersect_key( $labels, array_flip( explode( ',', $reason ) ) ); return $changed ? sprintf( __( 'Changed: %s. Values are not stored.', 'activity-track' ), implode( ', ', $changed ) ) : __( 'Changed fields were not captured for this event.', 'activity-track' ); } /** Group only adjacent profile records for the same subject, actor, source and mode within one minute. */ public static function event_groups( $rows ) { $groups = []; foreach ( $rows as $row ) { $index = count( $groups ) - 1; $first = $index >= 0 ? $groups[ $index ][0] : null; if ( $first && 'profile_updated' === $row->event_type && $first->event_type === $row->event_type && $row->subject_id && $first->subject_id === $row->subject_id && $first->actor_id === $row->actor_id && $first->source === $row->source && $first->mode === $row->mode && $first->order_id === $row->order_id && abs( strtotime( $first->occurred_at ) - strtotime( $row->occurred_at ) ) <= 60 ) { $groups[ $index ][] = $row; } else { $groups[] = [ $row ]; } } return $groups; } private function summaries( $rows ) { $groups = []; foreach ( $rows as $row ) { if ( ! $row->subject_id && ! $row->order_id ) continue; $key = ( $row->subject_id ? 'user-' . $row->subject_id : 'order-' . $row->order_id ) . '-' . ( 'test' === $row->mode ? 'test' : 'live' ); $groups[ $key ][] = $row; } if ( ! $groups ) return; echo '

' . esc_html__( 'Customer and order summaries', 'activity-track' ) . '

' . esc_html__( 'Events on this page only. One profile save can produce multiple records.', 'activity-track' ) . '

'; foreach ( $groups as $events ) { $first = $events[0]; $profile_count = 0; $paid_count = 0; $orders = []; $sequence = []; foreach ( array_reverse( $events ) as $event ) { if ( 'profile_updated' === $event->event_type ) $profile_count++; if ( 'order_paid' === $event->event_type ) $paid_count++; if ( $event->order_id ) $orders[ $event->order_id ][] = $event; if ( in_array( $event->event_type, [ 'account_created', 'checkout_submitted', 'payment_method_added', 'order_paid', 'stripe_payment', 'card_setup', 'payment_error' ], true ) ) { $sequence[] = self::label( $event->event_type ) . ( in_array( $event->event_type, [ 'stripe_payment', 'card_setup' ], true ) ? ' (' . str_replace( '_', ' ', $event->outcome ) . ')' : '' ) . ( $event->order_id ? ' · #' . $event->order_id : '' ); } } $title = $first->subject_id ? sprintf( __( 'User Id %d', 'activity-track' ), $first->subject_id ) : sprintf( __( 'Guest · Order #%d', 'activity-track' ), $first->order_id ); $overview = []; foreach ( array_keys( $orders ) as $id ) $overview[] = sprintf( __( 'Order #%d', 'activity-track' ), $id ); if ( $profile_count ) $overview[] = sprintf( _n( '%d profile update record', '%d profile update records', $profile_count, 'activity-track' ), $profile_count ); // Account-only summaries describe the event, without unrelated payment warnings. $headline = $paid_count ? __( 'Payment completed', 'activity-track' ) : self::label( $first->event_type ); echo '
' . esc_html( $title ) . ' ' . esc_html( sprintf( _n( '%d record', '%d records', count( $events ), 'activity-track' ), count( $events ) ) ) . '' . ( 'test' === $first->mode ? ' · ' . esc_html__( 'Test mode', 'activity-track' ) : '' ) . ''; if ( $paid_count ) { echo '' . esc_html( $headline ) . ' '; } else { echo '' . esc_html( $headline ) . ' '; } echo esc_html( implode( ' · ', $overview ) ) . ''; foreach ( $orders as $id => $order_events ) { $paid = false; $stripe = null; foreach ( $order_events as $event ) { if ( 'order_paid' === $event->event_type ) $paid = true; if ( 'stripe_payment' === $event->event_type && 'Stripe webhook' === $event->source ) $stripe = $event; } echo '

' . esc_html( sprintf( __( 'Order #%d', 'activity-track' ), $id ) ) . ' — ' . esc_html( $paid ? __( 'Payment completion recorded by WooCommerce.', 'activity-track' ) : __( 'No WooCommerce payment-completion record in this view.', 'activity-track' ) ) . ' ' . esc_html( $stripe ? sprintf( __( 'Latest recorded Stripe payment outcome: %s.', 'activity-track' ), str_replace( '_', ' ', $stripe->outcome ) ) : __( 'Stripe webhook: not recorded in this view.', 'activity-track' ) ) . '

'; } if ( $sequence ) echo '

' . esc_html( implode( ' → ', $sequence ) ) . '

'; if ( $first->subject_id ) echo '' . esc_html__( 'View user timeline', 'activity-track' ) . ''; echo '
'; } echo '
'; } public function render( $embedded = false ) { if ( ! current_user_can( 'manage_options' ) ) wp_die( esc_html__( 'You cannot view this timeline.', 'activity-track' ) ); if ( ! ATKAI_Security_Events::enabled( 'woocommerce' ) ) wp_die( esc_html__( 'Enable WooCommerce under Activity Track settings to view WooCommerce Activity.', 'activity-track' ) ); global $wpdb; $subject = $this->number( 'account' ); $order = $this->number( 'order' ); $page = max( 1, $this->number( 'paged' ) ); $table = ATKAI_Security_Events::table(); $heading = $embedded ? 'h2' : 'h1'; echo '
<' . $heading . '>' . esc_html__( 'WooCommerce Activity', 'activity-track' ) . '

' . esc_html__( 'Follow customer accounts, checkout activity, and payment outcomes.', 'activity-track' ) . '

' . esc_html__( 'Monitoring only', 'activity-track' ) . '
'; if ( ATKAI_Security_Events::VERSION !== get_option( 'atk_security_db_version' ) ) { echo '

' . esc_html__( 'Security event storage is unavailable. Existing activity logging and checkout continue to work.', 'activity-track' ) . '

'; return; } $tracking_users = ATKAI_Security_Events::enabled( 'user' ); $tracking_woo = ATKAI_Security_Events::enabled( 'woocommerce' ); $woo_active = class_exists( 'WooCommerce' ); $woo_status = ! $woo_active ? __( 'plugin not active on this site', 'activity-track' ) : ( $tracking_woo ? __( 'tracking enabled', 'activity-track' ) : __( 'installed and active; tracking disabled in Activity Track settings', 'activity-track' ) ); if ( ! $woo_active ) { $stripe_status = __( 'requires active WooCommerce', 'activity-track' ); } elseif ( ! $tracking_woo ) { $stripe_status = __( 'tracking paused; enable WooCommerce tracking in Activity Track settings', 'activity-track' ); } elseif ( ! class_exists( 'WC_Stripe' ) ) { $stripe_status = __( 'supported WooCommerce Stripe gateway not detected; other Stripe plugins are not yet supported', 'activity-track' ); } else { $stripe_status = __( 'gateway detected; webhook delivery required', 'activity-track' ); } $stripe_detected = $woo_active && $tracking_woo && class_exists( 'WC_Stripe' ); $last_stripe_event = $stripe_detected ? $wpdb->get_var( $wpdb->prepare( "SELECT MAX(recorded_at) FROM %i WHERE source = 'Stripe webhook' AND mode = 'live'", $table ) ) : null; $coverage = [ [ __( 'Account activity', 'activity-track' ), $tracking_users ? 'active' : 'inactive', $tracking_users ? __( 'Active', 'activity-track' ) : __( 'Inactive', 'activity-track' ), $tracking_users ? __( 'Registration, sign-ins, and profile changes are enabled.', 'activity-track' ) : __( 'Enable User under Activity Track settings.', 'activity-track' ) ], [ __( 'WooCommerce', 'activity-track' ), $woo_active && $tracking_woo ? 'active' : 'inactive', $woo_active && $tracking_woo ? __( 'Active', 'activity-track' ) : __( 'Inactive', 'activity-track' ), $woo_active && $tracking_woo ? __( 'Checkout and payment-completion tracking is enabled.', 'activity-track' ) : $woo_status ], [ __( 'Stripe', 'activity-track' ), $stripe_detected ? ( $last_stripe_event ? 'active' : 'pending' ) : 'inactive', $stripe_detected ? ( $last_stripe_event ? __( 'Events received', 'activity-track' ) : __( 'Webhook not yet confirmed', 'activity-track' ) ) : __( 'Inactive', 'activity-track' ), $last_stripe_event ? sprintf( __( 'Latest live event received: %s. This is not a real-time connection check.', 'activity-track' ), get_date_from_gmt( $last_stripe_event, 'M j, Y H:i' ) . ' ' . wp_timezone_string() ) : ( $stripe_detected ? __( 'WooCommerce payment tracking is active. A live Stripe webhook has not yet been recorded.', 'activity-track' ) : $stripe_status ) ], ]; echo '
' . esc_html__( 'Recording coverage', 'activity-track' ) . '
'; foreach ( $coverage as [ $name, $state, $label, $description ] ) { echo '
' . esc_html( $name ) . '' . esc_html( $label ) . '

' . esc_html( $description ) . '

'; } echo '
'; if ( ! $tracking_users || ( $woo_active && ! $tracking_woo ) ) { echo '

' . esc_html__( 'Open Activity Track settings', 'activity-track' ) . ' — ' . esc_html__( 'Under Enable Activity Types, select User and WooCommerce as needed, then save settings.', 'activity-track' ) . '

'; } echo '

' . esc_html__( 'Monitoring only — no customers are blocked. Only activity captured since tracking was enabled appears here.', 'activity-track' ) . '

'; echo '
' . esc_html__( 'All events', 'activity-track' ) . '
'; $where = '1=1'; if ( $subject ) $where .= $wpdb->prepare( ' AND subject_id = %d', $subject ); if ( $order ) $where .= $wpdb->prepare( ' AND order_id = %d', $order ); $cutoff = gmdate( 'Y-m-d H:i:s', time() - 600 ); // Count only distinct, live Stripe failure notifications, not checkout errors or webhook replays. $signals = $wpdb->get_results( $wpdb->prepare( "SELECT subject_id, COUNT(*) AS failures FROM %i WHERE $where AND subject_id > 0 AND outcome = 'failed' AND mode = 'live' AND event_type IN ('stripe_payment','card_setup') AND occurred_at >= %s GROUP BY subject_id HAVING COUNT(*) >= 5 ORDER BY failures DESC LIMIT 5", $table, $cutoff ) ); foreach ( $signals as $signal ) { echo '
' . esc_html__( 'Repeated payment failures — review recommended', 'activity-track' ) . '

' . esc_html( sprintf( __( 'User Id %1$d has %2$d Stripe failure events in the last 10 minutes. This may indicate card testing or legitimate retries. Review the timeline and Stripe before taking action.', 'activity-track' ), $signal->subject_id, $signal->failures ) ) . '

' . esc_html__( 'Review user timeline', 'activity-track' ) . '
'; } if ( $subject ) { echo '

' . esc_html( sprintf( __( 'Timeline for User Id %d', 'activity-track' ), $subject ) ) . ' · ' . esc_html__( 'View user', 'activity-track' ) . '

'; } $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM %i WHERE $where ORDER BY occurred_at DESC, event_id DESC LIMIT 51 OFFSET %d", $table, ( $page - 1 ) * 50 ) ); if ( ! $rows ) { echo '

' . esc_html__( 'No recorded events in this view', 'activity-track' ) . '

' . esc_html__( 'New account activity and supported payment events will appear here as they happen. Earlier activity remains in the main Activity Track log.', 'activity-track' ) . '

'; } else { $this->summaries( array_slice( $rows, 0, 50 ) ); echo '

' . esc_html( sprintf( __( 'Newest first · Timezone: %s. User Id identifies the affected user, not necessarily the acting user.', 'activity-track' ), wp_timezone_string() ) ) . '

    '; foreach ( self::event_groups( array_slice( $rows, 0, 50 ) ) as $group ) { $row = $group[0]; $outcomes = [ 'recorded' => __( 'Recorded', 'activity-track' ), 'error' => __( 'Processing error', 'activity-track' ), 'failed' => __( 'Failed', 'activity-track' ), 'succeeded' => __( 'Succeeded', 'activity-track' ), 'requires_action' => __( 'Authentication needed', 'activity-track' ), 'processing' => __( 'Processing', 'activity-track' ) ]; $outcome = $outcomes[ $row->outcome ] ?? __( 'Unknown', 'activity-track' ); $date = get_date_from_gmt( $row->occurred_at, 'M j, Y H:i:s' ); $icons = [ 'account_created' => 'admin-users', 'login' => 'unlock', 'profile_updated' => 'edit', 'role_changed' => 'shield', 'checkout_submitted' => 'cart', 'order_paid' => 'yes-alt', 'stripe_payment' => 'money-alt', 'card_setup' => 'credit-card', 'payment_method_added' => 'credit-card', 'payment_error' => 'warning' ]; $relative = sprintf( __( '%s ago', 'activity-track' ), human_time_diff( strtotime( $row->occurred_at . ' UTC' ), time() ) ); echo '
  1. ' . esc_html( self::label( $row->event_type ) ) . ( count( $group ) > 1 ? ' · ' . esc_html( sprintf( _n( '%d record', '%d records', count( $group ), 'activity-track' ), count( $group ) ) ) : '' ) . '' . esc_html( $outcome ) . '

    ' . esc_html( $relative . ' · ' . $date . ' · ' . $row->source ) . '

    '; if ( $row->subject_id ) { echo '' . esc_html( sprintf( __( 'User Id %d', 'activity-track' ), $row->subject_id ) ) . ''; } else { echo esc_html__( 'Guest or account not linked', 'activity-track' ); } if ( $row->order_id ) echo ' · ' . esc_html( sprintf( __( 'Order #%d', 'activity-track' ), $row->order_id ) ) . ''; if ( 'test' === $row->mode ) echo ' · ' . esc_html__( 'Test mode', 'activity-track' ) . ''; echo '

    '; if ( 'profile_updated' === $row->event_type ) { $reasons = array_unique( array_map( function( $event ) { return self::profile_description( $event->reason ); }, $group ) ); foreach ( $reasons as $description ) echo '

    ' . esc_html( $description ) . '

    '; } if ( $row->reason && 'profile_updated' !== $row->event_type ) echo '

    ' . esc_html__( 'Stripe reason:', 'activity-track' ) . ' ' . esc_html( $row->reason ) . '

    '; if ( 'payment_error' === $row->event_type ) echo '

    ' . esc_html__( 'The gateway reported an error. A bank decline or fraud is not confirmed by this event.', 'activity-track' ) . '

    '; echo '
    ' . esc_html__( 'Evidence details', 'activity-track' ) . '

    ' . esc_html( $row->actor_id ? sprintf( __( 'Acting User Id: %d', 'activity-track' ), $row->actor_id ) : __( 'Acting user not recorded', 'activity-track' ) ) . '

    '; if ( count( $group ) > 1 ) { echo '
      '; foreach ( $group as $event ) echo '
    • ' . esc_html( '#' . $event->event_id . ' · ' . get_date_from_gmt( $event->occurred_at, 'M j, Y H:i:s' ) . ' · ' . self::profile_description( $event->reason ) ) . '
    • '; echo '
    '; } if ( $row->gateway_ref ) echo '

    ' . esc_html__( 'Stripe reference:', 'activity-track' ) . ' ' . esc_html( $row->gateway_ref ) . '

    '; echo '

    ' . esc_html__( 'Recorded at:', 'activity-track' ) . ' ' . esc_html( get_date_from_gmt( $row->recorded_at, 'M j, Y H:i:s' ) ) . '

  2. '; } echo '
'; } echo ''; } }