Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

View Logs page #17

Merged
merged 3 commits into from
May 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 76 additions & 3 deletions includes/classes/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public function __construct() {
add_action( 'admin_init', array( $this, 'fields' ), 20 );

add_filter( 'sanitize_option_' . SETTINGS_GROUP, array( $this, 'sanitize' ), 10, 2 );

add_action( 'admin_menu', array( $this, 'logs_menu' ) );
}

/**
Expand Down Expand Up @@ -57,6 +59,73 @@ public function fields() {
);
}

/**
* Add logs page under Tools
*
* @return void
*/
public function logs_menu() {
add_management_page(
__( 'Sophi Logs', 'debug-bar-for-sophi' ),
__( 'Sophi Logs', 'debug-bar-for-sophi' ),
'manage_options',
'sophi-logs',
array( $this, 'logs_page' )
);
}

/**
* Render Sophi logs page
*
* @return void
*/
public function logs_page() {
$files = glob( trailingslashit( SOPHI_DEBUG_BAR_LOG_PATH ) . 'sophi-*.log' );
$dates = array();

foreach ( $files as $file ) {
if ( preg_match( '/.*?sophi-([\d\-]+)\.log/', $file, $match ) ) {
$dates[] = $match[1];
}
}

$dates = array_reverse( $dates );

if ( isset( $_REQUEST['date'] ) ) {
check_admin_referer( 'sophi-logs' );
$current = sanitize_file_name( $_REQUEST['date'] );
} else {
$current = reset( $dates );
}

$log_file = trailingslashit( SOPHI_DEBUG_BAR_LOG_PATH ) . "sophi-{$current}.log";

if ( ! file_exists( $log_file ) ) {
esc_html_e( 'There is no log from Sophi.io available yet.', 'debug-bar-for-sophi' );
return;
}

//phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$log_content = file_get_contents( $log_file );

?>
<div class="wrap">
<h1><?php esc_html_e( 'Sophi Logs' ); ?></h1>
<form method="post" action="">
<select name="date" id="sophi_logs_date">
<?php foreach ( $dates as $date ) : ?>
<option value="<?php echo esc_attr( $date ); ?>" <?php echo selected( $date === $current ); ?>><?php echo esc_html( "sophi-{$date}.log" ); ?></option>
<?php endforeach; ?>
</select>
<?php wp_nonce_field( 'sophi-logs' ); ?>
<button type="submit" class="button"><?php esc_html_e( 'Show', 'debug-bar-for-sophi' ); ?></button>
</form>

<pre style="white-space: pre-wrap; word-wrap: break-word;"><?php echo wp_kses_post( $log_content ); ?></pre>
</div>
<?php
}

/**
* Return combined Sophi settings with debug
*
Expand Down Expand Up @@ -100,7 +169,7 @@ public function render_debug_log_field() {
<?php checked( $is_writable && 'yes' === $settings['enable_debug_log'] ); ?>
<?php disabled( ! $is_writable ); ?>
/>
<?php esc_html_e( 'Enable debug log', 'sophi-debug-log' ); ?>
<?php esc_html_e( 'Enable debug log', 'debug-bar-for-sophi' ); ?>
</label>
<?php

Expand All @@ -109,11 +178,15 @@ public function render_debug_log_field() {
echo wp_kses_post(
sprintf(
/* translators: logs directory path */
__( 'The logs directory <code>%s</code> is not writable.', 'sophi-debug-log' ),
__( 'The logs directory <code>%s</code> is not writable.', 'debug-bar-for-sophi' ),
esc_attr( SOPHI_DEBUG_BAR_LOG_PATH )
)
);
echo '</p>';
} else {
echo '<p class="description">';
echo '<a href="tools.php?page=sophi-logs">' . esc_html__( 'View Logs', 'debug-bar-for-sophi' ) . '</a>';
echo '</p>';
}
}

Expand All @@ -134,7 +207,7 @@ public function render_disable_sophi_caching() {
value="yes"
<?php checked( 'yes' === $settings['disable_sophi_caching'] ); ?>
/>
<?php esc_html_e( 'Disable WordPress caching of Sophi content', 'sophi-debug-log' ); ?>
<?php esc_html_e( 'Disable WordPress caching of Sophi content', 'debug-bar-for-sophi' ); ?>
</label>
<?php
}
Expand Down