Skip to content

Commit 574bade

Browse files
feat: try to replace the old system for import run details
1 parent 3c94e7d commit 574bade

File tree

8 files changed

+293
-139
lines changed

8 files changed

+293
-139
lines changed

includes/admin/feedzy-rss-feeds-import.php

Lines changed: 156 additions & 71 deletions
Large diffs are not rendered by default.

includes/admin/feedzy-rss-feeds-log.php

Lines changed: 60 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ class Feedzy_Rss_Feeds_Log {
4949
const ERROR = 400;
5050

5151
/**
52-
* Critical level.
52+
* Ignore level.
5353
*
54-
* @var int Critical level.
54+
* @var int Ignore level.
5555
*/
56-
const CRITICAL = 500;
56+
const NONE = 500;
5757

5858
/**
5959
* Log file name.
@@ -89,18 +89,18 @@ class Feedzy_Rss_Feeds_Log {
8989
* @var array<int, string> Log levels.
9090
*/
9191
private static $levels = array(
92-
self::DEBUG => 'DEBUG',
93-
self::INFO => 'INFO',
94-
self::WARNING => 'WARNING',
95-
self::ERROR => 'ERROR',
96-
self::CRITICAL => 'CRITICAL',
92+
self::DEBUG => 'DEBUG',
93+
self::INFO => 'INFO',
94+
self::WARNING => 'WARNING',
95+
self::ERROR => 'ERROR',
9796
);
9897

9998
const PRIORITIES_MAPPING = array(
10099
'debug' => self::DEBUG,
101100
'info' => self::INFO,
102101
'warning' => self::WARNING,
103102
'error' => self::ERROR,
103+
'none' => self::NONE,
104104
);
105105

106106
/**
@@ -138,6 +138,20 @@ class Feedzy_Rss_Feeds_Log {
138138
*/
139139
public $level_threshold = self::ERROR;
140140

141+
/**
142+
* Whether to retain error messages for import run errors meta.
143+
*
144+
* @var string[]
145+
*/
146+
private $error_messages_accumulator = array();
147+
148+
/**
149+
* Whether to retain error messages for import run errors meta.
150+
*
151+
* @var bool Whether to retain error messages.
152+
*/
153+
private $retain_error_messages = false;
154+
141155
/**
142156
* Whether email reports can be sent.
143157
*
@@ -268,6 +282,13 @@ public static function log( $level, $message, array $context = array() ) {
268282
* @return void
269283
*/
270284
private function write_log( $level, $message, array $context = array() ) {
285+
if ( self::ERROR === $level ) {
286+
$this->increment_log_stat( 'error_count' );
287+
if ( $this->retain_error_messages ) {
288+
$this->error_messages_accumulator[] = $message;
289+
}
290+
}
291+
271292
if ( ! $this->filesystem ) {
272293
return;
273294
}
@@ -276,10 +297,6 @@ private function write_log( $level, $message, array $context = array() ) {
276297
return;
277298
}
278299

279-
if ( self::ERROR <= $level ) {
280-
$this->increment_log_stat( 'error_count' );
281-
}
282-
283300
$record = array(
284301
'timestamp' => gmdate( 'c' ),
285302
'level' => isset( self::$levels[ $level ] ) ? self::$levels[ $level ] : 'UNKNOWN',
@@ -355,18 +372,6 @@ public static function error( $message, array $context = array() ) {
355372
self::log( self::ERROR, $message, $context );
356373
}
357374

358-
/**
359-
* Log a critical message.
360-
*
361-
* @since 5.1.0
362-
* @param string $message The log message.
363-
* @param array<string, mixed> $context The log context.
364-
* @return void
365-
*/
366-
public static function critical( $message, array $context = array() ) {
367-
self::log( self::CRITICAL, $message, $context );
368-
}
369-
370375
/**
371376
* Get all logs as raw entries.
372377
*
@@ -597,7 +602,7 @@ public function register_endpoints() {
597602
* REST API endpoint to export logs.
598603
*
599604
* @since 5.1.0
600-
* @param WP_REST_Request $request The REST request.
605+
* @param WP_REST_Request<array<string, mixed>> $request The REST request.
601606
* @return WP_Error|void
602607
*/
603608
public function export_logs_endpoint( $request ) {
@@ -628,8 +633,8 @@ public function export_logs_endpoint( $request ) {
628633
* REST API endpoint to delete log file.
629634
*
630635
* @since 5.1.0
631-
* @param WP_REST_Request $request The REST request.
632-
* @return WP_Error|array
636+
* @param WP_REST_Request<array<string, mixed>> $request The REST request.
637+
* @return WP_Error|array<string, mixed>
633638
*/
634639
public function delete_log_file_endpoint( $request ) {
635640
if ( ! $request instanceof WP_REST_Request ) {
@@ -803,4 +808,32 @@ private function get_log_file_path() {
803808
$log_dir = $upload_dir['basedir'] . '/feedzy-logs';
804809
return $log_dir . '/' . self::FILE_NAME . self::FILE_EXT;
805810
}
811+
812+
/**
813+
* Enable retention of error messages.
814+
*
815+
* @return void
816+
*/
817+
public function enable_error_messages_retention() {
818+
$this->retain_error_messages = true;
819+
}
820+
821+
/**
822+
* Disable retention of error messages.
823+
*
824+
* @return void
825+
*/
826+
public function disable_error_messages_retention() {
827+
$this->retain_error_messages = false;
828+
$this->error_messages_accumulator = array();
829+
}
830+
831+
/**
832+
* Get the accumulated error messages.
833+
*
834+
* @return string[]
835+
*/
836+
public function get_error_messages_accumulator() {
837+
return $this->error_messages_accumulator;
838+
}
806839
}

includes/admin/feedzy-rss-feeds-task-manager.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,17 @@ public function schedule_daily_tasks() {
7878
* Send error report email.
7979
*
8080
* @since 5.1.0
81-
* @return bool Whether the email was sent successfully.
81+
* @return void.
8282
*/
8383
public function send_error_report() {
8484
$log_instance = Feedzy_Rss_Feeds_Log::get_instance();
8585

8686
if ( ! $log_instance->can_send_email() ) {
87-
return false;
87+
return;
8888
}
8989

9090
if ( ! $log_instance->has_reportable_data() ) {
91-
return false;
91+
return;
9292
}
9393

9494
$logs_entries = $log_instance->get_error_logs_for_email( 50 );
@@ -100,21 +100,17 @@ public function send_error_report() {
100100
$headers = array( 'Content-Type: text/html; charset=UTF-8' );
101101

102102
// phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_mail_wp_mail
103-
$email_send = wp_mail( $log_instance->get_email_address(), $subject, $message, $headers );
104-
105-
if ( $email_send ) {
103+
if ( wp_mail( $log_instance->get_email_address(), $subject, $message, $headers ) ) {
106104
$log_instance->reset_log_stats();
107105
}
108-
109-
return $email_send;
110106
}
111107

112108
/**
113109
* Get the email report content using the layout template.
114110
*
115111
* @since 5.1.0
116-
* @param array $logs_entries The log entries to include in the report.
117-
* @param array $stats The log statistics.
112+
* @param array<int, array<string, mixed>> $logs_entries The log entries to include in the report.
113+
* @param array<string, mixed> $stats The log statistics.
118114
* @return string The rendered email content.
119115
*/
120116
private function get_email_report_content( $logs_entries, $stats ) {

includes/feedzy-rss-feeds.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,30 @@ function () {
280280
$plugin_slug = FEEDZY_DIRNAME . '/' . basename( FEEDZY_BASEFILE );
281281
$this->loader->add_filter( "plugin_action_links_$plugin_slug", self::$instance->admin, 'plugin_actions', 10, 2 );
282282

283-
( new Feedzy_Rss_Feeds_Task_Manager() )->register_actions();
283+
add_action(
284+
'feedzy_log',
285+
function ( $log_data ) {
286+
$level = isset( $log_data['level'] ) ? $log_data['level'] : 'debug';
287+
$message = isset( $log_data['message'] ) ? $log_data['message'] : '';
288+
$context = isset( $log_data['context'] ) ? $log_data['context'] : array();
289+
290+
if ( ! isset( Feedzy_Rss_Feeds_Log::PRIORITIES_MAPPING[ $level ] ) ) {
291+
return;
292+
}
293+
294+
Feedzy_Rss_Feeds_Log::log( Feedzy_Rss_Feeds_Log::PRIORITIES_MAPPING[ $level ], $message, $context );
295+
}
296+
);
297+
( new Feedzy_Rss_Feeds_Task_Manager() )->register_actions();
298+
299+
add_filter(
300+
'feedzy_disable_db_cache',
301+
function ( $a, $b ) {
302+
return true;
303+
},
304+
10,
305+
2
306+
);
284307

285308
if ( ! defined( 'TI_UNIT_TESTING' ) ) {
286309
add_action(

includes/layouts/feedzy-logs-viewer.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99
$logs_type = isset( $_REQUEST['logs_type'] ) ? strtoupper( sanitize_text_field( wp_unslash( $_REQUEST['logs_type'] ) ) ) : null; // phpcs:ignore WordPress.Security.NonceVerification
1010

1111
$log_types = array(
12-
'debug' => __( 'Debug', 'feedzy-rss-feeds' ),
13-
'info' => __( 'Info', 'feedzy-rss-feeds' ),
14-
'warning' => __( 'Warning', 'feedzy-rss-feeds' ),
15-
'error' => __( 'Error', 'feedzy-rss-feeds' ),
16-
'critical' => __( 'Critical', 'feedzy-rss-feeds' ),
12+
'debug' => __( 'Debug', 'feedzy-rss-feeds' ),
13+
'info' => __( 'Info', 'feedzy-rss-feeds' ),
14+
'warning' => __( 'Warning', 'feedzy-rss-feeds' ),
15+
'error' => __( 'Error', 'feedzy-rss-feeds' ),
1716
);
1817

1918
$file_size = Feedzy_Rss_Feeds_Log::get_instance()->get_log_file_size();

includes/layouts/settings.php

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,29 +68,49 @@
6868
<div class="fz-tabs-menu">
6969
<ul>
7070
<li>
71-
<a href="<?php echo esc_url( admin_url( 'admin.php?page=feedzy-settings&tab=general' ) ); ?>"
72-
class="<?php echo 'general' === $active_tab ? esc_attr( 'active' ) : ''; ?>"><?php esc_html_e( 'General', 'feedzy-rss-feeds' ); ?></a>
71+
<a
72+
href="<?php echo esc_url( admin_url( 'admin.php?page=feedzy-settings&tab=general' ) ); ?>"
73+
class="<?php echo 'general' === $active_tab ? esc_attr( 'active' ) : ''; ?>"
74+
>
75+
<?php esc_html_e( 'General', 'feedzy-rss-feeds' ); ?>
76+
</a>
7377
</li>
7478
<li>
75-
<a href="<?php echo esc_url( admin_url( 'admin.php?page=feedzy-settings&tab=headers' ) ); ?>"
76-
class="<?php echo 'headers' === $active_tab ? esc_attr( 'active' ) : ''; ?>"><?php esc_html_e( 'Headers', 'feedzy-rss-feeds' ); ?></a>
79+
<a
80+
href="<?php echo esc_url( admin_url( 'admin.php?page=feedzy-settings&tab=headers' ) ); ?>"
81+
class="<?php echo 'headers' === $active_tab ? esc_attr( 'active' ) : ''; ?>"
82+
>
83+
<?php esc_html_e( 'Headers', 'feedzy-rss-feeds' ); ?>
84+
</a>
7785
</li>
7886
<li>
79-
<a href="<?php echo esc_url( admin_url( 'admin.php?page=feedzy-settings&tab=proxy' ) ); ?>"
80-
class="<?php echo 'proxy' === $active_tab ? esc_attr( 'active' ) : ''; ?>"><?php esc_html_e( 'Proxy', 'feedzy-rss-feeds' ); ?></a>
87+
<a
88+
href="<?php echo esc_url( admin_url( 'admin.php?page=feedzy-settings&tab=proxy' ) ); ?>"
89+
class="<?php echo 'proxy' === $active_tab ? esc_attr( 'active' ) : ''; ?>"
90+
>
91+
<?php esc_html_e( 'Proxy', 'feedzy-rss-feeds' ); ?>
92+
</a>
8193
</li>
8294
<li>
83-
<a href="<?php echo esc_url( admin_url( 'admin.php?page=feedzy-settings&tab=logs' ) ); ?>"
84-
class="<?php echo 'proxy' === $active_tab ? esc_attr( 'active' ) : ''; ?>"><?php esc_html_e( 'Logs', 'feedzy-rss-feeds' ); ?></a>
95+
<a
96+
href="<?php echo esc_url( admin_url( 'admin.php?page=feedzy-settings&tab=logs' ) ); ?>"
97+
class="<?php echo 'logs' === $active_tab ? esc_attr( 'active' ) : ''; ?>"
98+
>
99+
<?php esc_html_e( 'Logs', 'feedzy-rss-feeds' ); ?>
100+
</a>
85101
</li>
86102
<?php
87103
$_tabs = apply_filters( 'feedzy_settings_tabs', array() );
88104
if ( $_tabs ) {
89105
foreach ( $_tabs as $_tab => $label ) {
90106
?>
91107
<li>
92-
<a href="<?php echo esc_url( admin_url( 'admin.php?page=feedzy-settings&tab=' . $_tab ) ); ?>"
93-
class="<?php echo $_tab === $active_tab ? esc_attr( 'active' ) : ''; ?>"><?php echo wp_kses_post( $label ); ?></a>
108+
<a
109+
href="<?php echo esc_url( admin_url( 'admin.php?page=feedzy-settings&tab=' . $_tab ) ); ?>"
110+
class="<?php echo $_tab === $active_tab ? esc_attr( 'active' ) : ''; ?>"
111+
>
112+
<?php echo wp_kses_post( $label ); ?>
113+
</a>
94114
</li>
95115
<?php
96116
}
@@ -273,6 +293,12 @@ class="btn btn-outline-primary<?php echo 0 === $index ? ' disabled' : ''; ?>" <?
273293
<?php esc_html_e( 'Logging Level', 'feedzy-rss-feeds' ); ?>
274294
</label>
275295
<select class="form-control fz-select-control" name="logs-logging-level">
296+
<option
297+
value="none"
298+
<?php selected( 'none', $logging_level ); ?>
299+
>
300+
<?php esc_html_e( 'None', 'feedzy-rss-feeds' ); ?>
301+
</option>
276302
<option
277303
value="error"
278304
<?php selected( 'error', $logging_level ); ?>

includes/views/js/import-metabox-edit.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,13 @@
957957
);
958958
},
959959
},
960+
{
961+
text: window.feedzy.i10n.goToLogsTab,
962+
class: 'button button-secondary',
963+
click: () => {
964+
window.location.href = window.feedzy.pages.logs;
965+
}
966+
},
960967
{
961968
text: feedzy.i10n.okButton,
962969
class: 'alignright',

phpstan-baseline.neon

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -925,21 +925,11 @@ parameters:
925925
count: 1
926926
path: includes/admin/feedzy-rss-feeds-import.php
927927

928-
-
929-
message: "#^Method Feedzy_Rss_Feeds_Import\\:\\:run_job\\(\\) should return int but empty return statement found\\.$#"
930-
count: 1
931-
path: includes/admin/feedzy-rss-feeds-import.php
932-
933928
-
934929
message: "#^Method Feedzy_Rss_Feeds_Import\\:\\:set_wpml_element_language_details\\(\\) has no return type specified\\.$#"
935930
count: 1
936931
path: includes/admin/feedzy-rss-feeds-import.php
937932

938-
-
939-
message: "#^Method Feedzy_Rss_Feeds_Import\\:\\:try_save_featured_image\\(\\) has parameter \\$import_errors with no value type specified in iterable type array\\.$#"
940-
count: 1
941-
path: includes/admin/feedzy-rss-feeds-import.php
942-
943933
-
944934
message: "#^Method Feedzy_Rss_Feeds_Import\\:\\:try_save_featured_image\\(\\) has parameter \\$import_info with no value type specified in iterable type array\\.$#"
945935
count: 1
@@ -1025,11 +1015,6 @@ parameters:
10251015
count: 1
10261016
path: includes/admin/feedzy-rss-feeds-import.php
10271017

1028-
-
1029-
message: "#^Result of && is always true\\.$#"
1030-
count: 1
1031-
path: includes/admin/feedzy-rss-feeds-import.php
1032-
10331018
-
10341019
message: "#^Unreachable statement \\- code above always terminates\\.$#"
10351020
count: 1

0 commit comments

Comments
 (0)