').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('')
+ .append(
+ jQuery('').addClass('number'),
+ jQuery('').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();
diff --git a/includes/class-admin-settings.php b/includes/class-admin-settings.php
index 839714a..65092a1 100644
--- a/includes/class-admin-settings.php
+++ b/includes/class-admin-settings.php
@@ -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' ),
]
);
}
diff --git a/includes/class-controller.php b/includes/class-controller.php
index e537455..7d65780 100644
--- a/includes/class-controller.php
+++ b/includes/class-controller.php
@@ -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' ] );
}
/**
@@ -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() {
diff --git a/includes/class-debug.php b/includes/class-debug.php
index e2a0f8e..8548108 100644
--- a/includes/class-debug.php
+++ b/includes/class-debug.php
@@ -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' );
}
@@ -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();
@@ -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
+ );
}
}