From e60f0c0255c21e92ecd69706e17543966ae8e84a Mon Sep 17 00:00:00 2001 From: Kevin Date: Sun, 25 Feb 2024 21:31:27 +0100 Subject: [PATCH] v 0.3.0 - Read last 6 days of logs. - Fix previous log check. - Better display for long errors. - Various fixes. --- inc/WPUBaseToolbox/WPUBaseToolbox.php | 16 +++++-- lang/wpuerrorlogs-fr_FR.mo | Bin 929 -> 973 bytes lang/wpuerrorlogs-fr_FR.po | 20 +++++---- wpuerrorlogs.php | 62 +++++++++++++++++++------- 4 files changed, 71 insertions(+), 27 deletions(-) diff --git a/inc/WPUBaseToolbox/WPUBaseToolbox.php b/inc/WPUBaseToolbox/WPUBaseToolbox.php index 5d3ed1b..15ac84e 100644 --- a/inc/WPUBaseToolbox/WPUBaseToolbox.php +++ b/inc/WPUBaseToolbox/WPUBaseToolbox.php @@ -4,7 +4,7 @@ /* Class Name: WPU Base Toolbox Description: Cool helpers for WordPress Plugins -Version: 0.12.0 +Version: 0.12.1 Class URI: https://github.com/WordPressUtilities/wpubaseplugin Author: Darklg Author URI: https://darklg.me/ @@ -15,7 +15,7 @@ defined('ABSPATH') || die; class WPUBaseToolbox { - private $plugin_version = '0.12.0'; + private $plugin_version = '0.12.1'; public function __construct() { add_action('wp_enqueue_scripts', array(&$this, 'form_scripts' @@ -405,6 +405,8 @@ function array_to_html_table($array, $args = array()) { /* Fix args */ $default_args = array( 'table_classname' => 'widefat', + 'htmlspecialchars_td' => true, + 'htmlspecialchars_th' => true, 'colnames' => array() ); if (!is_array($args)) { @@ -421,7 +423,10 @@ function array_to_html_table($array, $args = array()) { if (isset($args['colnames'][$key])) { $label = $args['colnames'][$key]; } - $html .= '' . htmlspecialchars($label) . ''; + if ($args['htmlspecialchars_th']) { + $label = htmlspecialchars($label); + } + $html .= '' . $label . ''; } $html .= ''; @@ -430,7 +435,10 @@ function array_to_html_table($array, $args = array()) { foreach ($array as $line) { $html .= ''; foreach ($line as $value) { - $html .= '' . htmlspecialchars($value) . ''; + if ($args['htmlspecialchars_td']) { + $value = htmlspecialchars($value); + } + $html .= '' . $value . ''; } $html .= ''; } diff --git a/lang/wpuerrorlogs-fr_FR.mo b/lang/wpuerrorlogs-fr_FR.mo index b15b2e6d293cd9073e9b2190d1f3cbac18188b2f..5d60116c54e71600fcf96f5ed8697d7dffe13db5 100644 GIT binary patch delta 341 zcmXxeF-`(e5XSLYSX~hmgoH?duwr3jM*+PE6(v1{Y`3`Nh3!aa=q$y;g2IGC8XCC) z4GlLioPa~{ACO7D{C2W$=Dlxc>x_44`%|a{DUcDlBY#B8wL~uP0d0K3YkbBeR`CX# zsPk>SM}t@Rjyg9*o%{Ol=jdB9mIWV|{K!U62aA}ZFJlfXn8p@9Vh2k&!5Gd_AGpK} zu23)BAghn17waA8((_sgshyU@X(B{l>=0&;OF-l$2;YWM?+!g_gud^3#sua!Z|$wy TS7W@;xV@l1@Yg>dc64?C6-6P$ delta 299 zcmXxfKMny=5XbS^KRkaT>?)9~h-h_0pNRppT2t%Do+9u5s%!7R>=}cp@%M3u!sQ`u#I}Jk5wFE1{bLPYs}&1 z_r1p{IgqqSEN}bUXgeY`8V&l`#1?k3fg{Y~3U#3^mT`wV=z_eNln&IMf9biF{-iFa g#aJ;?Pqq;5CXefZg!APrlEFBd+ 'wpuerrorlogs', 'name' => 'WPU Error Logs' @@ -139,6 +139,12 @@ public function page_content__main() { $errors = $this->get_logs(); + /* Prepare for display */ + $errors = array_map(function ($item) { + $item['text'] = $this->display_content_with_toggle($item['text']); + return $item; + }, $errors); + /* Keep only first five and extract data */ $colnames = array( 'count' => __('Count', 'wpuerrorlogs'), @@ -151,7 +157,8 @@ public function page_content__main() { $top_errors = $this->sort_errors_by_top($errors, 10); echo '

' . __('Top errors', 'wpuerrorlogs') . '

'; $html_errors = $this->basetoolbox->array_to_html_table($top_errors, array( - 'table_classname' => 'widefat', + 'table_classname' => 'widefat striped', + 'htmlspecialchars_td' => false, 'colnames' => $colnames )); echo $html_errors ? $html_errors : '

' . __('No errors at the moment.', 'wpuerrorlogs') . '

'; @@ -160,7 +167,8 @@ public function page_content__main() { $latest_errors = $this->sort_errors_by_latest($errors, 10); echo '

' . __('Latest errors', 'wpuerrorlogs') . '

'; $html_errors = $this->basetoolbox->array_to_html_table($latest_errors, array( - 'table_classname' => 'widefat', + 'table_classname' => 'widefat striped', + 'htmlspecialchars_td' => false, 'colnames' => $colnames )); echo $html_errors ? $html_errors : '

' . __('No errors at the moment.', 'wpuerrorlogs') . '

'; @@ -216,29 +224,34 @@ function get_logs() { } $errors = $this->get_logs_from_file($file); - $previous_file = $this->find_previous_log_file($file); - if ($previous_file) { + $previous_files = $this->find_previous_log_files($file, 5); + foreach ($previous_files as $previous_file) { $errors_previous = $this->get_logs_from_file($previous_file); - $errors = $errors + $errors_previous; + foreach ($errors_previous as $error) { + $errors[] = $error; + } } return $errors; } - function find_previous_log_file($file) { + function find_previous_log_files($file, $number_of_days = 5) { $date_formats = array('Ymd', 'dmY'); + $previous_files = array(); foreach ($date_formats as $date_format) { $now_date = date($date_format); if (strpos($file, $now_date) === false) { continue; } - $previous_date = date($date_format, time() - 86400); - $previous_file = str_replace($now_date, $previous_date, $file); - if (is_readable($previous_file)) { - return $previous_file; + for ($i = 1; $i <= $number_of_days; $i++) { + $previous_date = date($date_format, time() - 86400 * $i); + $previous_file = str_replace($now_date, $previous_date, $file); + if (is_readable($previous_file)) { + $previous_files[] = $previous_file; + } } } - return false; + return $previous_files; } function get_logs_from_file($file) { @@ -269,7 +282,6 @@ function get_logs_from_file($file) { $currentError['text'] = $this->minimize_error_text($currentError['text']); $errors[] = $currentError; } - return $errors; } @@ -277,7 +289,7 @@ function get_error_from_line($line) { /* Extract values */ $date_parts = explode(']', $line); $date = str_replace('[', '', $date_parts[0]); - $text = trim(substr($line, strlen('[' . $date . ']'), -1)); + $text = trim(substr($line, strlen('[' . $date . ']'))); /* Extract type */ $type = 'none'; @@ -310,6 +322,26 @@ function get_error_from_line($line) { Helpers ---------------------------------------------------------- */ + /* Display content + -------------------------- */ + + function display_content_with_toggle($content) { + $content = strip_tags($content); + if (strpos($content, "\n") === false) { + return $content; + } + $content_parts = explode("\n", $content); + if (!isset($content_parts[1])) { + return $content; + } + $content = $content_parts[0]; + $content .= '
' . __('Full error', 'wpuerrorlogs') . '
' . implode("\n", $content_parts) . '
'; + return $content; + } + + /* Minimize text + -------------------------- */ + function minimize_get_correspondances() { return array( 'abs' => ABSPATH,