| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPCoder\Dashboard; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
use WPCoder\WPCoder; |
| 8 |
|
| 9 |
class DebugLog { |
| 10 |
|
| 11 |
public static function init(): void { |
| 12 |
self::send(); |
| 13 |
$log_file = WP_CONTENT_DIR . '/debug.log'; |
| 14 |
$enabled = (bool) get_option('_wpcoder_enable_debug_log'); |
| 15 |
|
| 16 |
if ( ! $enabled ) { |
| 17 |
$content = 'Debug log is disabled.'; // Feature turned off |
| 18 |
} elseif ( ! file_exists($log_file) || ! is_readable($log_file) ) { |
| 19 |
$content = 'No debug log entries found.'; // No file (as you wanted: empty output) |
| 20 |
} else { |
| 21 |
clearstatcache(true, $log_file); // Ensure fresh file stats |
| 22 |
if ( (int) filesize($log_file) === 0 ) { |
| 23 |
$content = 'No debug log entries found.'; // Empty file |
| 24 |
} else { |
| 25 |
$data = file_get_contents($log_file); // Read file |
| 26 |
$content = ($data === false) ? 'Failed to read debug.log.' : $data; |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
?> |
| 31 |
|
| 32 |
|
| 33 |
<form method="post"> |
| 34 |
<div class="wowp-settings"> |
| 35 |
|
| 36 |
<div class="wowp-settings__page"> |
| 37 |
|
| 38 |
|
| 39 |
<div class="wowp-settings__page-sidebar"> |
| 40 |
|
| 41 |
<div class="wowp-field has-checkbox is-reverse"> |
| 42 |
<span class="label"> |
| 43 |
<?php esc_html_e( 'Enable Debug Log', 'wp-coder' ); ?> |
| 44 |
</span> |
| 45 |
<label class="switch"> |
| 46 |
<input type="checkbox" value="1" name="wp_coder_enable_debug_log" |
| 47 |
id="wpcoder-debug-enable"<?php checked( $enabled ); ?>> |
| 48 |
<span class="slider"></span> |
| 49 |
</label> |
| 50 |
</div> |
| 51 |
|
| 52 |
<?php submit_button( __( 'Save', 'wp-coder' ), 'wowp-button button button-dark', 'submit', false ); ?> |
| 53 |
<?php submit_button( __( 'Clear Log ', 'wp-coder' ), 'wowp-button button button-red', 'clear-log', false ); ?> |
| 54 |
|
| 55 |
|
| 56 |
</div> |
| 57 |
|
| 58 |
<div class="wowp-settings__page-content wowp-field"> |
| 59 |
<textarea name="wp_coder_debug_log" cols="40" rows="5" |
| 60 |
readonly><?php echo esc_textarea( $content ); ?></textarea> |
| 61 |
</div> |
| 62 |
|
| 63 |
|
| 64 |
</div> |
| 65 |
|
| 66 |
<?php wp_nonce_field( WPCoder::PREFIX . '_debug_action', WPCoder::PREFIX . '_debug_name' ); ?> |
| 67 |
</div> |
| 68 |
</form> |
| 69 |
|
| 70 |
|
| 71 |
<?php |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
private static function send(): void { |
| 76 |
if ( ! self::verify() ) { |
| 77 |
return; |
| 78 |
} |
| 79 |
$checked = ! empty( $_POST['wp_coder_enable_debug_log'] ) ? 1 : 0; |
| 80 |
$log_file = WP_CONTENT_DIR . '/debug.log'; |
| 81 |
|
| 82 |
if ( isset( $_POST['submit'] ) ) { |
| 83 |
update_option( '_wpcoder_enable_debug_log', $checked ); |
| 84 |
} |
| 85 |
|
| 86 |
if ( isset( $_POST['clear-log'] ) && !empty($checked) && file_exists($log_file)) { |
| 87 |
file_put_contents( $log_file, '' ); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
private static function verify(): bool { |
| 92 |
$nonce_name = WPCoder::PREFIX . '_debug_name'; |
| 93 |
$nonce_action = WPCoder::PREFIX . '_debug_action'; |
| 94 |
if ( empty( $_POST[ $nonce_name ] ) ) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
return wp_verify_nonce( $_POST[ $nonce_name ], $nonce_action ) && current_user_can( 'unfiltered_html' ); |
| 99 |
} |
| 100 |
} |
| 101 |
|