Status > Scheduled * Actions says whose action it follows. Its arguments are never changed: * the job's callback receives them. */ final class SchedulerAttribution { /** * Writes a line to a job's log. * * @var Closure(int, string): void */ private Closure $log; /** * Constructor. * * @param (Closure(int, string): void)|null $log Job log writer; defaults to Action Scheduler's logger. */ public function __construct( ?Closure $log = null ) { $this->log = $log ?? static function ( int $action_id, string $message ): void { \ActionScheduler_Logger::instance()->log( $action_id, $message ); }; } /** * Hook job storage. */ public function register(): void { add_action( 'action_scheduler_stored_action', array( $this, 'attribute' ), 10, 1 ); } /** * One line of plain text: no line breaks or control characters to forge * another log line with, and no more than 100 characters. * * @param string $text Agent name or key. */ private static function plain( string $text ): string { return mb_substr( (string) preg_replace( '/[\x00-\x1F\x7F]+/u', ' ', $text ), 0, 100 ); } /** * Label a job stored during an agent's request. * * @param mixed $action_id The stored job. */ public function attribute( $action_id ): void { $agent = Api::current_agent(); if ( null === $agent || ! is_numeric( $action_id ) ) { return; } $ledger = Api::request_ledger_id(); $name = self::plain( $agent['name'] ); $key = self::plain( $agent['key'] ); ( $this->log )( (int) $action_id, null === $ledger /* translators: 1: agent name, 2: agent key. */ ? sprintf( __( 'Scheduled during AI agent "%1$s" (%2$s)\'s request; runs as WooCommerce follow-up, not as the agent.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), $name, $key ) /* translators: 1: agent name, 2: agent key, 3: ledger row. */ : sprintf( __( 'Scheduled during AI agent "%1$s" (%2$s)\'s request, Agent Firewall ledger #%3$d; runs as WooCommerce follow-up, not as the agent.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), $name, $key, $ledger ) ); } }