get_id() : ( $subject instanceof \WP_Post ? $subject->ID : 0 ); $rows = $order_id > 0 ? self::rows( $order_id ) : array(); $ledger = Api::ledger_url( '' ); if ( array() === $rows ) { echo '

' . esc_html__( 'No AI agent has acted on this order.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ) . '

'; return; } echo ''; foreach ( array( __( 'When', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), __( 'Agent', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), __( 'Action', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), __( 'Outcome', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ) ) as $heading ) { echo ''; } echo ''; foreach ( $rows as $row ) { $at = strtotime( (string) ( $row['ts'] ?? '' ) . ' UTC' ); $when = false === $at ? false : wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $at ); $agent = Api::agent( (int) ( $row['agent_id'] ?? 0 ) ); echo ''; echo ''; echo ''; // The ledger narrowed to this kind of row about this order (AF-25.16). echo ''; echo ''; echo ''; } echo '
' . esc_html( $heading ) . '
' . esc_html( false === $when ? '' : $when ) . '' . esc_html( null === $agent ? '#' . (int) ( $row['agent_id'] ?? 0 ) : $agent['name'] ) . '' . esc_html( self::action( $row ) ) . '' . esc_html( self::status( (string) ( $row['status'] ?? '' ) ) ) . '
'; echo '

' . esc_html__( 'Open the Activity Ledger', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ) . '

'; } /** * The order's agent rows, newest first. * * @param int $order_id Order id. * @return list> */ private static function rows( int $order_id ): array { $rows = array(); foreach ( self::TYPES as $type ) { $request = new WP_REST_Request( 'GET', '/agenticdaisy-firewall/v1/ledger' ); $request->set_query_params( array( 'entity_type' => $type, 'entity_id' => $order_id, 'per_page' => self::ROWS, ) ); $response = rest_do_request( $request ); $found = $response->is_error() ? array() : $response->get_data(); foreach ( is_array( $found ) ? $found : array() as $row ) { if ( is_array( $row ) && (int) ( $row['agent_id'] ?? 0 ) > 0 ) { $rows[] = $row; } } } usort( $rows, static fn( array $a, array $b ): int => (int) ( $b['id'] ?? 0 ) <=> (int) ( $a['id'] ?? 0 ) ); return array_slice( $rows, 0, self::ROWS ); } /** * What the agent did, in words. * * @param array $row A ledger row. */ private static function action( array $row ): string { $facts = is_array( $row['facts'] ?? null ) ? $row['facts'] : array(); $type = (string) ( $row['entity_type'] ?? '' ); $cat = (string) ( $row['action_cat'] ?? '' ); if ( 'refund' === $type ) { $amount = $facts['refund.amount_minor'] ?? null; return is_int( $amount ) /* translators: %s: amount. */ ? sprintf( __( 'Refund of %s', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), self::money( $amount ) ) : __( 'Refund', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ); } if ( 'order' === $type && is_string( $facts['order.status_to'] ?? null ) && '' !== $facts['order.status_to'] ) { /* translators: %s: order status. */ return sprintf( __( 'Status set to %s', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), $facts['order.status_to'] ); } return match ( $type . ' ' . $cat ) { 'order delete' => __( 'Order deleted', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), 'order_note create' => __( 'Note added', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), 'order_fulfillment create' => __( 'Fulfillment added', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), default => __( 'Order changed', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), }; } /** * A ledger status, in words. * * @param string $status Ledger status. */ private static function status( string $status ): string { return match ( $status ) { 'allowed' => __( 'Allowed', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), 'pending' => __( 'Waiting for approval', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), 'denied' => __( 'Denied', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), 'undone' => __( 'Undone', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), 'interrupted' => __( 'Interrupted', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), default => $status, }; } /** * Minor units of the store's currency, as the store prints them. * * @param int $minor Amount. */ private static function money( int $minor ): string { $decimals = ( new StoreCurrency() )->decimals(); return html_entity_decode( wp_strip_all_tags( wc_price( $minor / 10 ** $decimals, array( 'decimals' => $decimals ) ) ), ENT_QUOTES, 'UTF-8' ); } }