Skip to content

Commit

Permalink
Clear and View Logs
Browse files Browse the repository at this point in the history
Clear and View Logs
  • Loading branch information
namithj committed Nov 8, 2024
1 parent 5b8efdf commit 631572d
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 55 deletions.
81 changes: 44 additions & 37 deletions assets/js/aspire-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ jQuery(document).ready(function () {
new ApiRewrites();
new ApiDebug();

new ClearLog();
new ViewLog();
});

Expand All @@ -15,15 +16,32 @@ class ClearLog {
field: jQuery('#aspireupdate-button-clearlog'),
init() {
ClearLog.clearlog_button.field.click(function () {

ClearLog.clearlog_button.clear();
});
},
show() {
ClearLog.clearlog_button.field.show();
},
hide() {
ClearLog.clearlog_button.field.hide();
}
},
clear() {
let parameters = {
"url": aspireupdate.ajax_url,
"type": "POST",
"data": {
"nonce": aspireupdate.nonce,
"action": "aspireupdate_clear_log"
}
};
jQuery.ajax(parameters)
.done(function (response) {
alert(aspireupdate.clear_log_success_message);
})
.fail(function (response) {
alert(aspireupdate.clear_log_failed_message);
});
},
}
}

Expand Down Expand Up @@ -64,41 +82,30 @@ class ViewLog {
});
},
show() {
let lines = jQuery('#voltron').html().split('\n');

jQuery.each(lines, function (index, line) {
jQuery('<div>')
.append(
jQuery('<span>').addClass('number'),
jQuery('<span>').addClass('content').text(line)
)
.appendTo(ViewLog.viewlog_popup.popup_inner);
jQuery('<div>')
.append(
jQuery('<span>').addClass('number'),
jQuery('<span>').addClass('content').text(line)
)
.appendTo(ViewLog.viewlog_popup.popup_inner);
jQuery('<div>')
.append(
jQuery('<span>').addClass('number'),
jQuery('<span>').addClass('content').text(line)
)
.appendTo(ViewLog.viewlog_popup.popup_inner);
jQuery('<div>')
.append(
jQuery('<span>').addClass('number'),
jQuery('<span>').addClass('content').text(line)
)
.appendTo(ViewLog.viewlog_popup.popup_inner);
jQuery('<div>')
.append(
jQuery('<span>').addClass('number'),
jQuery('<span>').addClass('content').text(line)
)
.appendTo(ViewLog.viewlog_popup.popup_inner);
});
ViewLog.viewlog_popup.field.show();
let parameters = {
"url": aspireupdate.ajax_url,
"type": "POST",
"data": {
"nonce": aspireupdate.nonce,
"action": "aspireupdate_read_log"
}
};
jQuery.ajax(parameters)
.done(function (response) {
let lines = response.data.content.split(aspireupdate.line_ending);
jQuery.each(lines, function (index, line) {
jQuery('<div>')
.append(
jQuery('<span>').addClass('number'),
jQuery('<span>').addClass('content').text(line)
)
.appendTo(ViewLog.viewlog_popup.popup_inner);
});
ViewLog.viewlog_popup.field.show();
})
.fail(function (response) {
alert(aspireupdate.read_log_failed_message);
});
},
close() {
ViewLog.viewlog_popup.field.hide();
Expand Down
12 changes: 8 additions & 4 deletions includes/class-admin-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,14 @@ public function admin_enqueue_scripts( $hook ) {
'aspire_update_settings_js',
'aspireupdate',
[
'ajax_url' => network_admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'aspireupdate-ajax' ),
'domain' => Utilities::get_top_level_domain(),
'string_unexpected_error' => esc_html__( 'Unexpected Error:', 'AspireUpdate' ),
'ajax_url' => network_admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'aspireupdate-ajax' ),
'domain' => Utilities::get_top_level_domain(),
'line_ending' => PHP_EOL,
'string_unexpected_error' => esc_html__( 'Unexpected Error:', 'AspireUpdate' ),
'clear_log_success_message' => esc_html__( 'Log file successfully cleared.', 'AspireUpdate' ),
'clear_log_failed_message' => esc_html__( 'Clearing Log file failed.', 'AspireUpdate' ),
'read_log_failed_message' => esc_html__( 'Reading Log file failed.', 'AspireUpdate' ),
]
);
}
Expand Down
32 changes: 32 additions & 0 deletions includes/class-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public function __construct() {
$this->api_rewrite();

add_action( 'init', [ $this, 'load_textdomain' ] );
add_action( 'wp_ajax_aspireupdate_clear_log', [ $this, 'clear_log' ] );
add_action( 'wp_ajax_aspireupdate_read_log', [ $this, 'read_log' ] );
}

/**
Expand Down Expand Up @@ -49,8 +51,38 @@ private function api_rewrite() {
}
}

/**
* Ajax action to clear the Log file.
*
* @return void
*/
public function clear_log() {
if ( isset( $_POST['nonce'] ) || wp_verify_nonce( $_POST['nonce'], 'aspireupdate-ajax' ) ) {
Debug::clear();
wp_send_json_success();
}
wp_send_json_error();
}

/**
* Ajax action to read the Log file.
*
* @return void
*/
public function read_log() {
if ( isset( $_POST['nonce'] ) || wp_verify_nonce( $_POST['nonce'], 'aspireupdate-ajax' ) ) {
wp_send_json_success(
[
'content' => Debug::read( 1000 ),
]
);
}
wp_send_json_error();
}

/**
* Load translations.
*
* @return void
*/
public function load_textdomain() {
Expand Down
42 changes: 28 additions & 14 deletions includes/class-debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,35 @@ private static function verify_filesystem( $wp_filesystem ) {
/**
* Get the content of the log file truncated upto N number of lines.
*
* @param integer $limit Max no of lines to return.
* @param integer $limit Max no of lines to return. Defaults to a 1000 lines.
*
* @return string The File content truncate upto the number of lines set in the limit parameter.
*/
public static function read( $limit ) {
public static function read( $limit = 1000 ) {
$wp_filesystem = self::init_filesystem();
if ( self::verify_filesystem( $wp_filesystem ) ) {
$file_path = self::get_file_path();

if ( $wp_filesystem->exists( $file_path ) && $wp_filesystem->is_readable( $file_path ) ) {
$file_handle = $wp_filesystem->get_contents_array( $file_path );
return implode( "\n", array_slice( $file_handle, 0, $limit ) );
$file_content = $wp_filesystem->get_contents_array( $file_path );
$content = '';
$index = 0;
foreach ( $file_content as $file_content_lines ) {
if ( ( $index < $limit ) ) {
$content .= $file_content_lines . PHP_EOL;
++$index;
}
}
if ( '' === trim( $content ) ) {
$content = esc_html__( '*****Log file is empty.*****', 'AspireUpdate' );
} elseif ( $limit < count( $file_content ) ) {
$content .= PHP_EOL . sprintf(
/* translators: 1: The number of lines at which the content was truncated. */
esc_html__( '*****Log truncated at %s lines.*****', 'AspireUpdate' ),
$limit
);
}
return $content;
} else {
return esc_html__( 'Error: Unable to read the log file.', 'AspireUpdate' );
}
Expand Down Expand Up @@ -124,11 +141,11 @@ public static function log( $message, $type = 'string' ) {
if ( self::verify_filesystem( $wp_filesystem ) ) {
$timestamp = gmdate( 'Y-m-d H:i:s' );
$formatted_message = sprintf(
"[%s] [%s]: %s\n" . PHP_EOL,
'[%s] [%s]: %s',
$timestamp,
strtoupper( $type ),
self::format_message( $message )
);
) . PHP_EOL;

$file_path = self::get_file_path();

Expand All @@ -138,14 +155,11 @@ public static function log( $message, $type = 'string' ) {
$content = $wp_filesystem->get_contents( $file_path );
}
}

if ( $wp_filesystem->is_writable( $file_path ) ) {
$wp_filesystem->put_contents(
$file_path,
$formatted_message . $content,
FS_CHMOD_FILE
);
}
$wp_filesystem->put_contents(
$file_path,
$formatted_message . $content,
FS_CHMOD_FILE
);
}
}

Expand Down

0 comments on commit 631572d

Please sign in to comment.