| 1 |
<?php |
| 2 |
defined('ABSPATH') or die('Unauthorized Access'); |
| 3 |
|
| 4 |
if (!Phpinfo_WP_License::is_valid()) { |
| 5 |
phpinfowp_render_feature_lock([ |
| 6 |
'feature' => 'PHP Error Log Viewer', |
| 7 |
'icon' => 'dashicons-warning', |
| 8 |
'tagline' => 'Browse, filter, and clear your PHP error log — without FTP, SSH, or asking your host.', |
| 9 |
'previews' => [ |
| 10 |
'<strong>[—] PHP Warning:</strong> Undefined variable $foo in /wp-content/…', |
| 11 |
'<strong>[—] PHP Deprecated:</strong> strpos() expects string …', |
| 12 |
'<strong>[—] PHP Fatal error:</strong> Allowed memory size exhausted …', |
| 13 |
], |
| 14 |
]); |
| 15 |
return; |
| 16 |
} |
| 17 |
|
| 18 |
$message = ''; |
| 19 |
$msg_type = 'success'; |
| 20 |
|
| 21 |
if (isset($_POST['phpinfowp_log_action']) && check_admin_referer('phpinfowp_log_nonce')) { |
| 22 |
if ($_POST['phpinfowp_log_action'] === 'clear') { |
| 23 |
$path = Phpinfo_WP_Error_Log::find_path(); |
| 24 |
if ($path && Phpinfo_WP_Error_Log::clear($path)) { |
| 25 |
$message = 'Log file cleared.'; |
| 26 |
} else { |
| 27 |
$message = 'Could not clear the log file (check file permissions).'; |
| 28 |
$msg_type = 'error'; |
| 29 |
} |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
$path = Phpinfo_WP_Error_Log::find_path(); |
| 34 |
$lines = $path ? Phpinfo_WP_Error_Log::tail($path, 200) : []; |
| 35 |
$size = $path ? Phpinfo_WP_Error_Log::format_bytes(Phpinfo_WP_Error_Log::size($path)) : '0 B'; |
| 36 |
$search = sanitize_text_field($_GET['log_search'] ?? ''); |
| 37 |
?> |
| 38 |
|
| 39 |
<div class="phpinfowp-pro-page"> |
| 40 |
<div class="phpinfowp-page-header"> |
| 41 |
<div> |
| 42 |
<h1>PHP Error Log <span class="phpinfowp-pro-badge"><?php _e('PRO', 'phpinfo-wp'); ?></span></h1> |
| 43 |
<p class="phpinfowp-page-subtitle"><?php _e('Browse, filter, and clear your PHP error log — without FTP, SSH, or asking your host.', 'phpinfo-wp'); ?></p> |
| 44 |
</div> |
| 45 |
</div> |
| 46 |
|
| 47 |
<?php if ($message): ?> |
| 48 |
<div class="notice notice-<?php echo $msg_type; ?> inline is-dismissible"><p><?php echo esc_html($message); ?></p></div> |
| 49 |
<?php endif; ?> |
| 50 |
|
| 51 |
<?php if (!$path): |
| 52 |
$diag = Phpinfo_WP_Error_Log::diagnose(); |
| 53 |
$verdict_color = $diag['verdict'] === 'empty' ? '#00a32a' : ($diag['verdict'] === 'disabled' ? '#dba617' : '#d63638'); |
| 54 |
$verdict_label = $diag['verdict'] === 'empty' ? 'No errors yet' : ($diag['verdict'] === 'disabled' ? 'Logging disabled' : 'Not found'); |
| 55 |
?> |
| 56 |
<div class="phpinfowp-errlog-diag"> |
| 57 |
<div class="phpinfowp-errlog-diag-head"> |
| 58 |
<div> |
| 59 |
<span class="phpinfowp-errlog-diag-pill" style="background:<?php echo $verdict_color; ?>20;color:<?php echo $verdict_color; ?>;border-color:<?php echo $verdict_color; ?>40"> |
| 60 |
<?php echo esc_html($verdict_label); ?> |
| 61 |
</span> |
| 62 |
<h2 style="margin:8px 0 4px;font-size:17px;color:#1d2327"><?php _e('No log file discovered', 'phpinfo-wp'); ?></h2> |
| 63 |
</div> |
| 64 |
</div> |
| 65 |
<p style="margin:6px 0 18px;color:#3c434a;line-height:1.5"><?php echo esc_html($diag['summary']); ?></p> |
| 66 |
|
| 67 |
<h3 style="font-size:11px;font-weight:700;letter-spacing:.5px;text-transform:uppercase;color:#646970;margin:0 0 8px"><?php _e('WordPress debug constants', 'phpinfo-wp'); ?></h3> |
| 68 |
<table class="phpinfowp-errlog-diag-table"> |
| 69 |
<tr> |
| 70 |
<td><code>WP_DEBUG</code></td> |
| 71 |
<td><?php echo $diag['constants']['WP_DEBUG'] ? '<span style="color:#00a32a">✓ on</span>' : '<span style="color:#d63638">✗ off</span>'; ?></td> |
| 72 |
<td>Master switch — must be on for WP_DEBUG_LOG to work.</td> |
| 73 |
</tr> |
| 74 |
<tr> |
| 75 |
<td><code>WP_DEBUG_LOG</code></td> |
| 76 |
<td><?php echo $diag['constants']['WP_DEBUG_LOG'] ? '<span style="color:#00a32a">✓ on</span>' : '<span style="color:#d63638">✗ off</span>'; ?></td> |
| 77 |
<td>Writes errors to <code>wp-content/debug.log</code> (or a custom path if you set one).</td> |
| 78 |
</tr> |
| 79 |
<tr> |
| 80 |
<td><code>WP_DEBUG_DISPLAY</code></td> |
| 81 |
<td><?php echo $diag['constants']['WP_DEBUG_DISPLAY'] ? '<span style="color:#d63638">✗ on</span>' : '<span style="color:#00a32a">✓ off</span>'; ?></td> |
| 82 |
<td><?php _e('Should be <strong>off</strong> in production — leaking errors to visitors is a security risk.', 'phpinfo-wp'); ?></td> |
| 83 |
</tr> |
| 84 |
</table> |
| 85 |
|
| 86 |
<h3 style="font-size:11px;font-weight:700;letter-spacing:.5px;text-transform:uppercase;color:#646970;margin:18px 0 8px"><?php _e('Paths checked', 'phpinfo-wp'); ?></h3> |
| 87 |
<table class="phpinfowp-errlog-diag-table phpinfowp-errlog-diag-paths"> |
| 88 |
<?php foreach ($diag['checks'] as $c): |
| 89 |
$sym = $c['status'] === 'found' ? '✓' : ($c['status'] === 'unreadable' ? '!' : '✗'); |
| 90 |
$col = $c['status'] === 'found' ? '#00a32a' : ($c['status'] === 'unreadable' ? '#dba617' : '#a7aaad'); |
| 91 |
?> |
| 92 |
<tr> |
| 93 |
<td style="width:24px;text-align:center;color:<?php echo $col; ?>;font-weight:700"><?php echo $sym; ?></td> |
| 94 |
<td><code style="font-size:11px;color:#3c434a"><?php echo esc_html($c['path']); ?></code></td> |
| 95 |
<td style="color:#646970;font-size:12px"><?php echo esc_html($c['note'] ?: ucfirst($c['status'])); ?></td> |
| 96 |
</tr> |
| 97 |
<?php endforeach; ?> |
| 98 |
</table> |
| 99 |
|
| 100 |
<details style="margin-top:18px"> |
| 101 |
<summary style="cursor:pointer;font-weight:600;color:#1d2327">Enable WordPress logging</summary> |
| 102 |
<p style="margin:8px 0 6px;color:#3c434a">Add to <code>wp-config.php</code> <em>above</em> the <code>/* That's all, stop editing! */</code> line:</p> |
| 103 |
<pre style="background:#f6f7f7;border:1px solid #e0e0e6;border-radius:6px;padding:12px 14px;font-size:12px;color:#1d2327;margin:0">define('WP_DEBUG', true); |
| 104 |
define('WP_DEBUG_LOG', true); |
| 105 |
define('WP_DEBUG_DISPLAY', false); |
| 106 |
@ini_set('display_errors', '0');</pre> |
| 107 |
<p style="margin:10px 0 0;color:#646970;font-size:12px">The log file (<code>wp-content/debug.log</code>) only appears once an error has been logged. On a healthy site that may take days. Many managed hosts (SiteGround, Kinsta, WP Engine, Cloudways) also expose a log viewer in their control panel — that's a separate file from this one.</p> |
| 108 |
</details> |
| 109 |
</div> |
| 110 |
<?php else: ?> |
| 111 |
|
| 112 |
<div class="phpinfowp-log-toolbar"> |
| 113 |
<div class="phpinfowp-log-meta"> |
| 114 |
<strong><?php _e('File:', 'phpinfo-wp'); ?></strong> <code><?php echo esc_html($path); ?></code> |
| 115 |
· <strong><?php _e('Size:', 'phpinfo-wp'); ?></strong> <?php echo esc_html($size); ?> |
| 116 |
· <strong><?php _e('Showing:', 'phpinfo-wp'); ?></strong> last <?php echo count($lines); ?> lines |
| 117 |
</div> |
| 118 |
<div style="display:flex;gap:8px;align-items:center;flex-wrap:wrap"> |
| 119 |
<input type="text" id="phpinfowp-log-search" placeholder="<?php echo esc_attr__('Filter lines...', 'phpinfo-wp'); ?>" |
| 120 |
value="<?php echo esc_attr($search); ?>" class="regular-text" |
| 121 |
oninput="phpinfowpFilterLog(this.value)"> |
| 122 |
<form method="post" onsubmit="return confirm('Clear the entire log file? This cannot be undone.')"> |
| 123 |
<?php wp_nonce_field('phpinfowp_log_nonce'); ?> |
| 124 |
<input type="hidden" name="phpinfowp_log_action" value="clear"> |
| 125 |
<button type="submit" class="button button-secondary"><?php _e('Clear Log', 'phpinfo-wp'); ?></button> |
| 126 |
</form> |
| 127 |
</div> |
| 128 |
</div> |
| 129 |
|
| 130 |
<div id="phpinfowp-log-viewer"> |
| 131 |
<?php if (empty($lines)): ?> |
| 132 |
<p style="padding:20px;color:#666;text-align:center"><?php _e('Log is empty — no errors recorded.', 'phpinfo-wp'); ?></p> |
| 133 |
<?php else: ?> |
| 134 |
<?php foreach ($lines as $line): ?> |
| 135 |
<div class="log-line <?php echo esc_attr(Phpinfo_WP_Error_Log::classify($line)); ?>" data-line="<?php echo esc_attr(strtolower($line)); ?>"> |
| 136 |
<?php echo esc_html($line); ?> |
| 137 |
</div> |
| 138 |
<?php endforeach; ?> |
| 139 |
<?php endif; ?> |
| 140 |
</div> |
| 141 |
|
| 142 |
<script> |
| 143 |
function phpinfowpFilterLog(q) { |
| 144 |
q = q.toLowerCase(); |
| 145 |
document.querySelectorAll('#phpinfowp-log-viewer .log-line').forEach(function(el) { |
| 146 |
el.style.display = (!q || el.dataset.line.includes(q)) ? '' : 'none'; |
| 147 |
}); |
| 148 |
} |
| 149 |
<?php if ($search): ?> |
| 150 |
phpinfowpFilterLog(<?php echo json_encode($search); ?>); |
| 151 |
<?php endif; ?> |
| 152 |
</script> |
| 153 |
|
| 154 |
<?php endif; ?> |
| 155 |
</div> |
| 156 |
|