timeout = 30; $this->max_retries = 3; } /** * Get the current API base URL from settings */ private function get_api_base_url() { return ovena_get_api_base_url(); } /** * Send product feed data to Ovena API */ public function send_feed_data( $feed_data ) { $transmission_mode = get_option( 'ovena_acp_transmission_mode', 'stream' ); $uuid = ovena_generate_uuid(); if ( $transmission_mode === 'stream' ) { return $this->send_feed_data_as_stream( $feed_data, $uuid ); } else { return $this->send_feed_data_as_json( $feed_data, $uuid ); } } /** * Send feed data as JSON with idempotency key */ private function send_feed_data_as_json( $feed_data, $uuid ) { $endpoint = $this->get_api_base_url() . 'products'; $response = $this->make_request( $endpoint, 'POST', $feed_data, array( 'Idempotency-Key' => $uuid ) ); if ( is_wp_error( $response ) ) { $this->log_error( __( 'API Request Failed', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), $response->get_error_message() ); return array( 'success' => false, 'error' => $response->get_error_message(), 'uuid' => $uuid ); } $response_code = wp_remote_retrieve_response_code( $response ); $response_body = wp_remote_retrieve_body( $response ); if ( $response_code >= 200 && $response_code < 300 ) { /* translators: %1$s: UUID, %2$d: HTTP response code */ $this->log_success( __( 'Feed data sent successfully (JSON)', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), sprintf( __( 'UUID: %1$s, Status: %2$d', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), $uuid, $response_code ) ); return array( 'success' => true, 'response' => json_decode( $response_body, true ), 'status_code' => $response_code, 'uuid' => $uuid, 'mode' => 'json' ); } else { /* translators: %1$s: UUID, %2$d: HTTP response code, %3$s: response body */ $this->log_error( __( 'API Error Response (JSON)', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), sprintf( __( 'UUID: %1$s, Status: %2$d, Body: %3$s', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), $uuid, $response_code, $response_body ) ); return array( 'success' => false, /* translators: %d: HTTP response code */ 'error' => sprintf( __( 'API returned status code: %d', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), $response_code ), 'response' => $response_body, 'status_code' => $response_code, 'uuid' => $uuid ); } } /** * Send feed data as stream with UUID filename */ private function send_feed_data_as_stream( $feed_data, $uuid ) { $endpoint = $this->get_api_base_url() . 'file/upload'; $filename = $uuid . '.json'; // Create temporary file $temp_file = wp_tempnam( $filename ); if ( ! $temp_file ) { $this->log_error( __( 'Stream Upload Failed', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), __( 'Could not create temporary file', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ) ); return array( 'success' => false, 'error' => __( 'Could not create temporary file', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), 'uuid' => $uuid ); } // Write JSON data to temporary file $json_data = wp_json_encode( $feed_data, JSON_PRETTY_PRINT ); if ( file_put_contents( $temp_file, $json_data ) === false ) { wp_delete_file( $temp_file ); $this->log_error( __( 'Stream Upload Failed', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), __( 'Could not write data to temporary file', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ) ); return array( 'success' => false, 'error' => __( 'Could not write data to temporary file', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), 'uuid' => $uuid ); } // Prepare multipart form data $boundary = wp_generate_password( 24, false ); $body = $this->build_multipart_body( $temp_file, $filename, $boundary ); $response = $this->make_stream_request( $endpoint, $body, $boundary, array( 'Idempotency-Key' => $uuid ) ); // Clean up temporary file wp_delete_file( $temp_file ); if ( is_wp_error( $response ) ) { $this->log_error( __( 'Stream Upload Failed', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), $response->get_error_message() ); return array( 'success' => false, 'error' => $response->get_error_message(), 'uuid' => $uuid ); } $response_code = wp_remote_retrieve_response_code( $response ); $response_body = wp_remote_retrieve_body( $response ); if ( $response_code >= 200 && $response_code < 300 ) { /* translators: %1$s: filename, %2$s: UUID, %3$d: HTTP response code */ $this->log_success( __( 'Stream upload successful', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), sprintf( __( 'File: %1$s, UUID: %2$s, Status: %3$d', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), $filename, $uuid, $response_code ) ); return array( 'success' => true, 'response' => json_decode( $response_body, true ), 'status_code' => $response_code, 'filename' => $filename, 'uuid' => $uuid, 'mode' => 'stream' ); } else { /* translators: %1$s: filename, %2$s: UUID, %3$d: HTTP response code, %4$s: response body */ $this->log_error( __( 'Stream Upload Error', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), sprintf( __( 'File: %1$s, UUID: %2$s, Status: %3$d, Body: %4$s', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), $filename, $uuid, $response_code, $response_body ) ); return array( 'success' => false, /* translators: %d: HTTP response code */ 'error' => sprintf( __( 'Stream upload failed with status %d', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), $response_code ), 'response' => $response_body, 'status_code' => $response_code, 'uuid' => $uuid ); } } /** * Send individual product data to Ovena API */ public function send_product_data( $product_data ) { $endpoint = $this->get_api_base_url() . 'product'; $response = $this->make_request( $endpoint, 'POST', $product_data ); if ( is_wp_error( $response ) ) { return array( 'success' => false, 'error' => $response->get_error_message() ); } $response_code = wp_remote_retrieve_response_code( $response ); $response_body = wp_remote_retrieve_body( $response ); return array( 'success' => $response_code >= 200 && $response_code < 300, 'response' => json_decode( $response_body, true ), 'status_code' => $response_code ); } /** * Test API connection */ public function test_connection() { $endpoint = $this->get_api_base_url() . 'health'; $this->log_success( __( 'Testing API Connection', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), __( 'Endpoint: ***REDACTED***/health', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ) ); $response = $this->make_request( $endpoint, 'GET' ); if ( is_wp_error( $response ) ) { $error_msg = $response->get_error_message(); $this->log_error( __( 'API Connection Test Failed', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), $error_msg ); return array( 'success' => false, 'error' => $error_msg ); } $response_code = wp_remote_retrieve_response_code( $response ); $response_body = wp_remote_retrieve_body( $response ); if ( $response_code >= 200 && $response_code < 300 ) { /* translators: %d: HTTP response code */ $this->log_success( __( 'API Connection Test Successful', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), sprintf( __( 'Status: %d', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), $response_code ) ); return array( 'success' => true, 'status' => $response_code, 'message' => __( 'API connection successful', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), 'response' => json_decode( $response_body, true ) ); } else { /* translators: %d: HTTP response code */ $this->log_error( __( 'API Connection Test Failed', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), sprintf( __( 'Status: %d', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), $response_code ) ); return array( 'success' => false, 'status' => $response_code, /* translators: %d: HTTP response code */ 'error' => sprintf( __( 'API returned status code: %d', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), $response_code ) ); } } /** * Test API authentication * Calls health/apikey endpoint to verify API key is valid */ public function test_authentication() { // Ensure no double slashes in URL construction $base_url = rtrim( $this->get_api_base_url(), '/' ); $endpoint = $base_url . '/health/apikey'; $this->log_success( __( 'Testing API Authentication', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), __( 'Endpoint: ***REDACTED***/health/apikey', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ) ); $response = $this->make_request( $endpoint, 'GET' ); if ( is_wp_error( $response ) ) { $error_msg = $response->get_error_message(); $this->log_error( __( 'API Authentication Test Failed', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), $error_msg ); return array( 'success' => false, 'error' => $error_msg ); } $response_code = wp_remote_retrieve_response_code( $response ); $response_body = wp_remote_retrieve_body( $response ); if ( $response_code === 200 ) { $this->log_success( __( 'API Authentication Test Successful', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), __( 'API key is valid and authenticated', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ) ); return array( 'success' => true, 'status' => $response_code, 'message' => __( 'Authentication successful! API key is valid.', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), 'response' => json_decode( $response_body, true ) ); } elseif ( $response_code === 401 ) { $this->log_error( __( 'API Authentication Test Failed', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), __( 'Status: 401 Unauthorized - Invalid API key', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ) ); return array( 'success' => false, 'status' => $response_code, 'error' => __( 'Authentication failed: Invalid or missing API key', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ) ); } elseif ( $response_code === 403 ) { $this->log_error( __( 'API Authentication Test Failed', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), __( 'Status: 403 Forbidden - API key lacks permissions', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ) ); return array( 'success' => false, 'status' => $response_code, 'error' => __( 'Authentication failed: API key does not have required permissions', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ) ); } else { /* translators: %d: HTTP response code */ $this->log_error( __( 'API Authentication Test Failed', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), sprintf( __( 'Status: %d', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), $response_code ) ); return array( 'success' => false, 'status' => $response_code, /* translators: %d: HTTP response code */ 'error' => sprintf( __( 'API returned status code: %d', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), $response_code ) ); } } /** * Make HTTP request with retries */ private function make_request( $url, $method = 'GET', $data = null, $custom_headers = array() ) { $args = array( 'method' => $method, 'timeout' => $this->timeout, 'headers' => array( 'Content-Type' => 'application/json', 'User-Agent' => 'Ovena WooCommerce Plugin/' . $this->get_plugin_version(), 'X-Source' => 'woocommerce', 'X-Site-URL' => home_url() ) ); // Add custom headers if ( ! empty( $custom_headers ) ) { $args['headers'] = array_merge( $args['headers'], $custom_headers ); } // Add authentication if configured $api_key = get_option( 'ovena_acp_api_key' ); if ( ! empty( $api_key ) ) { $args['headers']['X-Api-Key'] = $api_key; } // Add request body for POST/PUT requests if ( $data && in_array( $method, array( 'POST', 'PUT', 'PATCH' ) ) ) { $args['body'] = wp_json_encode( $data ); } // Log detailed request information if debug mode is enabled $this->log_api_request( $url, $method, $args, $data ); // Retry logic $last_error = null; for ( $i = 0; $i < $this->max_retries; $i++ ) { $response = wp_remote_request( $url, $args ); if ( ! is_wp_error( $response ) ) { // Log successful response details $this->log_api_response( $url, $method, $response, $i + 1 ); return $response; } $last_error = $response; // Log retry attempt $this->log_api_retry( $url, $method, $response, $i + 1 ); // Wait before retry (exponential backoff) if ( $i < $this->max_retries - 1 ) { sleep( pow( 2, $i ) ); } } // Log final failure $this->log_api_failure( $url, $method, $last_error ); return $last_error; } /** * Get plugin version */ private function get_plugin_version() { if ( ! function_exists( 'get_plugin_data' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } $plugin_data = get_plugin_data( OVENA_ACP_FILE ); return $plugin_data['Version'] ?? '1.0.0'; } /** * Make stream request for file upload */ private function make_stream_request( $url, $body, $boundary, $custom_headers = array() ) { $args = array( 'method' => 'POST', 'timeout' => $this->timeout * 2, // Longer timeout for file uploads 'headers' => array( 'Content-Type' => 'multipart/form-data; boundary=' . $boundary, 'User-Agent' => 'Ovena WooCommerce Plugin/' . $this->get_plugin_version(), 'X-Source' => 'woocommerce', 'X-Site-URL' => home_url() ), 'body' => $body ); // Add custom headers if ( ! empty( $custom_headers ) ) { $args['headers'] = array_merge( $args['headers'], $custom_headers ); } // Add authentication if configured $api_key = get_option( 'ovena_acp_api_key' ); if ( ! empty( $api_key ) ) { $args['headers']['X-Api-Key'] = $api_key; } else { } // Log detailed request information if debug mode is enabled $this->log_api_request( $url, 'POST', $args, 'Stream upload' ); $response = wp_remote_post( $url, $args ); // Log response if debug mode is enabled $this->log_api_response( $url, 'POST', $response, 1 ); return $response; } /** * Build multipart body for file upload */ private function build_multipart_body( $file_path, $filename, $boundary ) { $file_contents = file_get_contents( $file_path ); $body = "--{$boundary}\r\n"; $body .= "Content-Disposition: form-data; name=\"file\"; filename=\"{$filename}\"\r\n"; $body .= "Content-Type: application/json\r\n\r\n"; $body .= $file_contents . "\r\n"; $body .= "--{$boundary}--\r\n"; return $body; } /** * Log successful API calls */ public function log_success( $action, $details = '' ) { $this->write_log( 'SUCCESS', $action, $details ); } /** * Log API errors */ public function log_error( $action, $details = '' ) { $this->write_log( 'ERROR', $action, $details ); } /** * Log info messages */ public function log_info( $action, $details = '' ) { $this->write_log( 'INFO', $action, $details ); } /** * Log detailed API request information for debugging */ private function log_api_request( $url, $method, $args, $data = null ) { if ( ! $this->is_debug_mode_enabled() ) { return; } $request_details = array( 'url' => $this->sanitize_url_for_log( $url ), 'method' => $method, 'headers' => $this->sanitize_headers_for_log( $args['headers'] ?? array() ), 'timeout' => $args['timeout'] ?? 30, 'body_size' => isset( $args['body'] ) ? strlen( $args['body'] ) : 0, 'raw_data' => $this->sanitize_data_for_log( $data ), 'timestamp' => gmdate( 'Y-m-d H:i:s' ) ); $this->write_debug_log( 'API_REQUEST', 'Outgoing API Request', $request_details ); } /** * Log detailed API response information for debugging */ private function log_api_response( $url, $method, $response, $attempt ) { if ( ! $this->is_debug_mode_enabled() ) { return; } $response_code = wp_remote_retrieve_response_code( $response ); $response_body = wp_remote_retrieve_body( $response ); $response_headers = wp_remote_retrieve_headers( $response ); $response_details = array( 'url' => $this->sanitize_url_for_log( $url ), 'method' => $method, 'attempt' => $attempt, 'status_code' => $response_code, 'response_headers' => $this->sanitize_headers_for_log( $response_headers ), 'response_body' => $this->sanitize_response_for_log( $response_body ), 'response_size' => strlen( $response_body ), 'timestamp' => gmdate( 'Y-m-d H:i:s' ) ); $level = ( $response_code >= 200 && $response_code < 300 ) ? 'SUCCESS' : 'WARNING'; $this->write_debug_log( 'API_RESPONSE', 'API Response Received', $response_details, $level ); } /** * Log API retry attempts */ private function log_api_retry( $url, $method, $error, $attempt ) { if ( ! $this->is_debug_mode_enabled() ) { return; } $retry_details = array( 'url' => $this->sanitize_url_for_log( $url ), 'method' => $method, 'attempt' => $attempt, 'error_message' => is_wp_error( $error ) ? $error->get_error_message() : 'Unknown error', 'error_code' => is_wp_error( $error ) ? $error->get_error_code() : '', 'will_retry' => $attempt < $this->max_retries, 'timestamp' => gmdate( 'Y-m-d H:i:s' ) ); $this->write_debug_log( 'API_RETRY', 'API Request Retry', $retry_details, 'WARNING' ); } /** * Log final API failures */ private function log_api_failure( $url, $method, $error ) { if ( ! $this->is_debug_mode_enabled() ) { return; } $failure_details = array( 'url' => $this->sanitize_url_for_log( $url ), 'method' => $method, 'error_message' => is_wp_error( $error ) ? $error->get_error_message() : 'Unknown error', 'error_code' => is_wp_error( $error ) ? $error->get_error_code() : '', 'max_retries_reached' => true, 'timestamp' => gmdate( 'Y-m-d H:i:s' ) ); $this->write_debug_log( 'API_FAILURE', 'API Request Failed (Max Retries)', $failure_details, 'ERROR' ); } /** * Check if debug mode is enabled */ private function is_debug_mode_enabled() { return get_option( 'ovena_acp_debug_mode', false ); } /** * Sanitize headers for logging (remove sensitive information) */ private function sanitize_headers_for_log( $headers ) { $sanitized = array(); $sensitive_keys = array( 'authorization', 'x-api-key', 'api-key', 'x-api-token', 'api-token', 'x-auth-token', 'auth-token', 'x-access-token', 'access-token', ); foreach ( $headers as $key => $value ) { $lk = strtolower( $key ); if ( in_array( $lk, $sensitive_keys, true ) ) { $sanitized[ $key ] = '***REDACTED***'; } else { $sanitized[ $key ] = $value; } } return $sanitized; } /** * Sanitize URL for logging (redact base URL but keep path) */ private function sanitize_url_for_log( $url ) { $base_url = $this->get_api_base_url(); // Replace the base URL with redacted placeholder, keeping the endpoint path if ( strpos( $url, $base_url ) === 0 ) { $endpoint = str_replace( $base_url, '', $url ); return '***REDACTED_BASE_URL***/' . ltrim( $endpoint, '/' ); } // If URL doesn't match base URL pattern, redact the domain but keep path $parsed = wp_parse_url( $url ); if ( isset( $parsed['scheme'] ) && isset( $parsed['host'] ) ) { $path = isset( $parsed['path'] ) ? $parsed['path'] : ''; $query = isset( $parsed['query'] ) ? '?' . $parsed['query'] : ''; return '***REDACTED_DOMAIN***' . $path . $query; } return '***REDACTED_URL***'; } /** * Sanitize request data for logging */ private function sanitize_data_for_log( $data ) { if ( ! $data ) { return null; } // Redact sensitive fields like api_key, token, authorization within arrays if ( is_array( $data ) ) { $data = $this->redact_sensitive_data_in_array( $data ); } else if ( is_string( $data ) ) { // Basic redaction for common patterns in JSON strings $data = preg_replace_callback( '/("(api[_-]?key|authorization|access[_-]?token|api[_-]?token)"\s*:\s*")([^"]+)(")/i', function( $m ) { return $m[1] . '***REDACTED***' . $m[4]; }, $data ); } // If data is too large, truncate it $json_data = wp_json_encode( $data ); if ( strlen( $json_data ) > 5000 ) { return array( 'truncated' => true, 'original_size' => strlen( $json_data ), 'sample' => substr( $json_data, 0, 1000 ) . '...[TRUNCATED]' ); } return $data; } /** * Recursively redact sensitive keys in array data. * * @param array $data * @return array */ private function redact_sensitive_data_in_array( $data ) { if ( ! is_array( $data ) ) { return $data; } $redacted = array(); foreach ( $data as $key => $value ) { $lk = strtolower( (string) $key ); if ( is_array( $value ) ) { $redacted[ $key ] = $this->redact_sensitive_data_in_array( $value ); continue; } // Redact if the key looks like an API key or token if ( ( strpos( $lk, 'api' ) !== false && strpos( $lk, 'key' ) !== false ) || strpos( $lk, 'token' ) !== false || strpos( $lk, 'authorization' ) !== false || strpos( $lk, 'password' ) !== false ) { $redacted[ $key ] = '***REDACTED***'; } else { $redacted[ $key ] = $value; } } return $redacted; } /** * Sanitize response for logging */ private function sanitize_response_for_log( $response_body ) { if ( strlen( $response_body ) > 2000 ) { return array( 'truncated' => true, 'original_size' => strlen( $response_body ), 'preview' => substr( $response_body, 0, 500 ) . '...[TRUNCATED]' ); } return $response_body; } /** * Write detailed debug log entry */ private function write_debug_log( $type, $action, $details, $level = 'DEBUG' ) { if ( ! $this->is_debug_mode_enabled() ) { return; } $log_entry = array( 'timestamp' => gmdate( 'Y-m-d H:i:s' ), 'level' => $level, 'type' => $type, 'action' => $action, 'details' => $details ); // Store debug logs separately from regular logs $debug_logs = get_option( 'ovena_acp_debug_logs', array() ); // Keep only last 50 debug entries (they are more detailed) if ( count( $debug_logs ) >= 50 ) { $debug_logs = array_slice( $debug_logs, -25 ); } $debug_logs[] = $log_entry; update_option( 'ovena_acp_debug_logs', $debug_logs ); // Also log to WordPress debug log if enabled if ( defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { $log_message = "Ovena Debug [{$level}] {$type} - {$action}: " . wp_json_encode( $details );} } /** * Write to log file */ private function write_log( $level, $action, $details = '' ) { if ( ! get_option( 'ovena_acp_enable_logging', true ) ) { return; } $log_entry = array( 'timestamp' => gmdate( 'Y-m-d H:i:s' ), 'level' => $level, 'action' => $action, 'details' => $details, 'url' => '***REDACTED***' ); // Store in WordPress option (rotate when it gets too large) $logs = get_option( 'ovena_acp_logs', array() ); // Keep only last 100 entries if ( count( $logs ) >= 100 ) { $logs = array_slice( $logs, -50 ); } $logs[] = $log_entry; update_option( 'ovena_acp_logs', $logs ); // Also log to WordPress debug log if enabled if ( defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) {} } /** * Get recent logs */ public function get_logs( $limit = 50 ) { $logs = get_option( 'ovena_acp_logs', array() ); return array_slice( array_reverse( $logs ), 0, $limit ); } /** * Clear logs */ public function clear_logs() { delete_option( 'ovena_acp_logs' ); } /** * Get debug logs */ public function get_debug_logs( $limit = 25 ) { $logs = get_option( 'ovena_acp_debug_logs', array() ); return array_slice( array_reverse( $logs ), 0, $limit ); } /** * Clear debug logs */ public function clear_debug_logs() { delete_option( 'ovena_acp_debug_logs' ); } /** * Get debug logs by type */ public function get_debug_logs_by_type( $type, $limit = 10 ) { $all_logs = $this->get_debug_logs( 50 ); $filtered_logs = array_filter( $all_logs, function( $log ) use ( $type ) { return isset( $log['type'] ) && $log['type'] === $type; }); return array_slice( $filtered_logs, 0, $limit ); } /** * Enable debug mode */ public function enable_debug_mode() { update_option( 'ovena_acp_debug_mode', true ); $this->write_debug_log( 'SYSTEM', 'Debug mode enabled', array( 'enabled_at' => gmdate( 'Y-m-d H:i:s' ) ), 'INFO' ); } /** * Disable debug mode */ public function disable_debug_mode() { $this->write_debug_log( 'SYSTEM', 'Debug mode disabled', array( 'disabled_at' => gmdate( 'Y-m-d H:i:s' ) ), 'INFO' ); update_option( 'ovena_acp_debug_mode', false ); } /** * Get API statistics */ public function get_api_stats() { $stats = get_option( 'ovena_acp_api_stats', array( 'total_requests' => 0, 'successful_requests' => 0, 'failed_requests' => 0, 'last_sync' => null, 'last_success' => null, 'last_failure' => null ) ); return $stats; } /** * Update API statistics */ public function update_api_stats( $success = true ) { $stats = $this->get_api_stats(); $stats['total_requests']++; $stats['last_sync'] = current_time( 'mysql' ); if ( $success ) { $stats['successful_requests']++; $stats['last_success'] = current_time( 'mysql' ); } else { $stats['failed_requests']++; $stats['last_failure'] = current_time( 'mysql' ); } update_option( 'ovena_acp_api_stats', $stats ); } }