1919namespace GratisAiAgent \Bootstrap ;
2020
2121use GratisAiAgent \Core \ProviderTraceLogger ;
22+ use GratisAiAgent \Models \ProviderTrace ;
2223use XWP \DI \Decorators \Filter ;
2324use XWP \DI \Decorators \Handler ;
2425
3233 * CTX_GLOBAL ensures the filters are active in every request context — AI
3334 * calls can originate from admin (manual runs), REST (webhook triggers), CLI,
3435 * and cron (scheduled tasks).
36+ *
37+ * Both filter callbacks are no-ops when WP_DEBUG is not active. The DI
38+ * container always instantiates this handler, but the actual recording never
39+ * happens on production sites where WP_DEBUG is false or undefined.
3540 */
3641#[Handler(
3742 container: 'gratis-ai-agent ' ,
@@ -44,7 +49,8 @@ final class HttpTraceHandler {
4449 * Capture outgoing request details before the HTTP call is made.
4550 *
4651 * Returns `$preempt` unchanged — this filter is used only for its
47- * side-effect of recording in-flight request metadata.
52+ * side-effect of recording in-flight request metadata. No-op when
53+ * WP_DEBUG is not active.
4854 *
4955 * @param false|array<string,mixed>|\WP_Error $preempt A preemptive return value. Default false.
5056 * @param array<string,mixed> $parsed_args HTTP request arguments.
@@ -53,14 +59,18 @@ final class HttpTraceHandler {
5359 */
5460 #[Filter( tag: 'pre_http_request ' , priority: 10 )]
5561 public function on_pre_http_request ( mixed $ preempt , array $ parsed_args , string $ url ): mixed {
62+ if ( ! ProviderTrace::is_debug_mode () ) {
63+ return $ preempt ;
64+ }
5665 return ProviderTraceLogger::on_pre_http_request ( $ preempt , $ parsed_args , $ url );
5766 }
5867
5968 /**
6069 * Capture response details and write a trace record.
6170 *
6271 * Returns `$response` unchanged — this filter is used only for its
63- * side-effect of persisting the completed trace row.
72+ * side-effect of persisting the completed trace row. No-op when
73+ * WP_DEBUG is not active.
6474 *
6575 * @param array<string,mixed> $response HTTP response array.
6676 * @param array<string,mixed> $parsed_args HTTP request arguments.
@@ -69,6 +79,9 @@ public function on_pre_http_request( mixed $preempt, array $parsed_args, string
6979 */
7080 #[Filter( tag: 'http_response ' , priority: 10 )]
7181 public function on_http_response ( array $ response , array $ parsed_args , string $ url ): array {
82+ if ( ! ProviderTrace::is_debug_mode () ) {
83+ return $ response ;
84+ }
7285 return ProviderTraceLogger::on_http_response ( $ response , $ parsed_args , $ url );
7386 }
7487}
0 commit comments