activity_types = include plugin_dir_path(ATKAI_PLUGIN_FILE) . '/activities/types.php'; // Set up logger $this->logger = $logger; // Hook into the http_api_debug action to log HTTP requests add_action('http_api_debug', array($this, 'atk_log_http_requests'), 10, 5); } public function atk_log_http_requests($response, $context, $class, $args, $url) { if ( $this->logging || 'response' !== $context || ! empty( $args['atkai_internal_request'] ) ) return; // Only proceed for external requests if ($this->is_external_request($url)) { $user_id = get_current_user_id(); // Get the current user ID $method = isset($args['method']) ? strtolower($args['method']) : 'other'; $method = sanitize_text_field( $method ); // Determine activity type based on the request method $activity_type = isset($this->activity_types['api'][$method]) ? $this->activity_types['api'][$method] : 'unknown'; // Construct a description of the request $description = sprintf( "Response Code: %s; %s", is_wp_error($response) ? 'N/A' : '' . wp_remote_retrieve_response_code($response) . '', is_wp_error($response) ? 'Request failed' : (wp_remote_retrieve_response_code($response) == 200 ? 'Request was successful' : 'No error occurred') ); // Log the activity $this->logging = true; try { $this->logger->insert_log( $user_id, 'Api', $activity_type, $description, $method, self::redacted_url( $url ) ); } finally { $this->logging = false; } } } public static function redacted_url( $url ) { $parts = wp_parse_url( $url ); if ( ! is_array( $parts ) || empty( $parts['host'] ) || ! in_array( strtolower( $parts['scheme'] ?? '' ), [ 'http', 'https' ], true ) ) return ''; $safe = $parts['scheme'] . '://' . $parts['host'] . ( isset( $parts['port'] ) ? ':' . (int) $parts['port'] : '' ) . ( $parts['path'] ?? '/' ); if ( ! empty( $parts['query'] ) ) { parse_str( $parts['query'], $query ); $query = array_fill_keys( array_keys( $query ), '[redacted]' ); $safe .= '?' . http_build_query( $query ); } return $safe; } private function is_external_request($url) { // Check if the request URL is external by comparing it to the site URL return strpos($url, home_url()) !== 0; } }