Instantly share code, notes, and snippets.
Last active
September 9, 2025 08:24
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
-
Save mehrshaddarzi/fb3acf029cfdd16e0e6e2f105595ae6d to your computer and use it in GitHub Desktop.
WP Remote Request Log at WordPress Footer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // کلاس مدیریت برای لاگهای wp_remote_request | |
| class WP_Remote_Request_Logger { | |
| private $option_name = 'wp_remote_request_log'; | |
| private $max_logs = 50; | |
| public function __construct() { | |
| add_action('init', array($this, 'init')); | |
| add_action('wp_footer', array($this, 'display_logs')); | |
| } | |
| public function init() { | |
| if (is_admin()) { | |
| return; | |
| } | |
| add_filter('pre_http_request', array($this, 'log_request_pre'), 10, 3); | |
| add_action('http_api_debug', array($this, 'log_response'), 10, 5); | |
| } | |
| public function log_request_pre($preempt, $args, $url) { | |
| if ($preempt !== false) { | |
| return $preempt; | |
| } | |
| $log_data = array( | |
| 'type' => 'request', | |
| 'url' => $url, | |
| 'method' => $args['method'] ?? 'GET', | |
| 'time' => current_time('mysql'), | |
| 'timestamp' => time(), | |
| 'args' => $this->sanitize_args($args) | |
| ); | |
| $this->save_log($log_data); | |
| return $preempt; | |
| } | |
| public function log_response($response, $context, $class, $args, $url) { | |
| $log_data = array( | |
| 'type' => 'response', | |
| 'url' => $url, | |
| 'context' => $context, | |
| 'class' => $class, | |
| 'time' => current_time('mysql'), | |
| 'timestamp' => time(), | |
| 'response' => $this->sanitize_response($response), | |
| 'args' => $this->sanitize_args($args) | |
| ); | |
| $this->save_log($log_data); | |
| } | |
| private function sanitize_args($args) { | |
| // حذف اطلاعات حساس از آرگومانها | |
| $sanitized = $args; | |
| if (isset($sanitized['headers']['Authorization'])) { | |
| $sanitized['headers']['Authorization'] = '***REDACTED***'; | |
| } | |
| if (isset($sanitized['body'])) { | |
| if (is_array($sanitized['body'])) { | |
| foreach ($sanitized['body'] as $key => $value) { | |
| if (strpos(strtolower($key), 'password') !== false || | |
| strpos(strtolower($key), 'token') !== false || | |
| strpos(strtolower($key), 'secret') !== false) { | |
| $sanitized['body'][$key] = '***REDACTED***'; | |
| } | |
| } | |
| } | |
| } | |
| return $sanitized; | |
| } | |
| private function sanitize_response($response) { | |
| // سانیتیزیشن پاسخ برای امنیت | |
| if (is_wp_error($response)) { | |
| return $response; | |
| } | |
| $sanitized = $response; | |
| // حذف اطلاعات حساس از هدرها | |
| if (isset($sanitized['headers'])) { | |
| foreach ($sanitized['headers'] as $key => $value) { | |
| if (strpos(strtolower($key), 'authorization') !== false || | |
| strpos(strtolower($key), 'token') !== false || | |
| strpos(strtolower($key), 'secret') !== false) { | |
| $sanitized['headers'][$key] = '***REDACTED***'; | |
| } | |
| } | |
| } | |
| return $sanitized; | |
| } | |
| private function save_log($log_data) { | |
| $logs = get_option($this->option_name, array()); | |
| // محدودیت تعداد لاگها | |
| if (count($logs) >= $this->max_logs) { | |
| array_shift($logs); | |
| } | |
| $logs[] = $log_data; | |
| update_option($this->option_name, $logs, 'no'); | |
| } | |
| public function display_logs() { | |
| if (!current_user_can('manage_options')) { | |
| return; | |
| } | |
| $logs = get_option($this->option_name, array()); | |
| if (empty($logs)) { | |
| return; | |
| } | |
| echo '<div style="background: #f8f8f8; padding: 20px; margin: 20px; border: 2px solid #0073aa; border-radius: 5px; font-family: monospace; font-size: 12px;">'; | |
| echo '<h3 style="color: #0073aa; margin-top: 0;">WP Remote Request Logs</h3>'; | |
| foreach ($logs as $index => $log) { | |
| $bg_color = $log['type'] === 'request' ? '#e7f4e4' : '#e4f0f7'; | |
| echo '<div style="background: ' . $bg_color . '; margin-bottom: 15px; padding: 15px; border: 1px solid #ddd; border-radius: 3px;">'; | |
| echo '<strong style="color: #333;">#' . ($index + 1) . ' - ' . strtoupper($log['type']) . '</strong><br>'; | |
| echo '<span style="color: #666;">' . esc_html($log['time']) . '</span><br>'; | |
| echo '<strong>URL:</strong> ' . esc_url($log['url']) . '<br>'; | |
| if ($log['type'] === 'request') { | |
| echo '<strong>Method:</strong> ' . esc_html($log['method']) . '<br>'; | |
| } else { | |
| echo '<strong>Context:</strong> ' . esc_html($log['context']) . '<br>'; | |
| echo '<strong>Class:</strong> ' . esc_html($log['class']) . '<br>'; | |
| } | |
| echo '<strong>Details:</strong><br>'; | |
| echo '<pre style="background: #fff; padding: 10px; border: 1px solid #ccc; overflow: auto; max-height: 300px;">'; | |
| if ($log['type'] === 'request') { | |
| var_dump($log['args']); | |
| } else { | |
| var_dump($log['response']); | |
| } | |
| echo '</pre>'; | |
| echo '</div>'; | |
| } | |
| echo '</div>'; | |
| if(isset($_GET['delete_logs'])) { | |
| $this->clear_logs(); | |
| } | |
| } | |
| // متد برای پاک کردن لاگها | |
| public function clear_logs() { | |
| delete_option($this->option_name); | |
| } | |
| } | |
| // راهاندازی لاگر | |
| new WP_Remote_Request_Logger(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment