Skip to content

Commit 17214f9

Browse files
feat: add errors report email frequency (#1142)
1 parent 2804edc commit 17214f9

File tree

8 files changed

+175
-31
lines changed

8 files changed

+175
-31
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1411,8 +1411,8 @@ private function save_settings() {
14111411
return;
14121412
}
14131413
$post_tab = isset( $_POST['tab'] ) ? sanitize_text_field( wp_unslash( $_POST['tab'] ) ) : '';
1414-
14151414
$settings = apply_filters( 'feedzy_get_settings', array() );
1415+
14161416
switch ( $post_tab ) {
14171417
case 'general':
14181418
$auto_categories_raw = array();

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3248,14 +3248,15 @@ public function integration_tabs( $tabs ) {
32483248
public function save_tab_settings( $settings, $tab ) {
32493249
if (
32503250
! isset( $_POST['nonce'] ) ||
3251-
! wp_verify_nonce( sanitize_text_field( wp_unslash( 'nonce' ) ), $tab )
3251+
! wp_verify_nonce( sanitize_text_field( $_POST['nonce'] ), $tab )
32523252
) {
32533253
return array();
32543254
}
32553255

32563256
if ( 'misc' === $tab ) {
3257-
$settings['canonical'] = isset( $_POST['canonical'] ) ? absint( $_POST['canonical'] ) : 0;
3258-
$settings['general']['rss-feeds'] = isset( $_POST['rss-feeds'] ) ? absint( $_POST['rss-feeds'] ) : '';
3257+
$settings['canonical'] = isset( $_POST['canonical'] ) ? absint( $_POST['canonical'] ) : 0;
3258+
$settings['general']['rss-feeds'] = isset( $_POST['rss-feeds'] ) ? absint( $_POST['rss-feeds'] ) : '';
3259+
$settings['logs']['email_frequency'] = isset( $_POST['logs-email-frequency'] ) ? sanitize_text_field( wp_unslash( $_POST['logs-email-frequency'] ) ) : '';
32593260
}
32603261

32613262
return $settings;

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

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,76 +16,87 @@ class Feedzy_Rss_Feeds_Log {
1616
/**
1717
* Option key for storing log statistics.
1818
*
19+
* @since 5.1.0
1920
* @var string Option key for storing log statistics.
2021
*/
2122
const STATS_OPTION_KEY = 'feedzy_log_stats';
2223

2324
/**
2425
* Debug level.
2526
*
27+
* @since 5.1.0
2628
* @var int Debug level.
2729
*/
2830
const DEBUG = 100;
2931

3032
/**
3133
* Info level.
3234
*
35+
* @since 5.1.0
3336
* @var int Info level.
3437
*/
3538
const INFO = 200;
3639

3740
/**
3841
* Warning level.
3942
*
43+
* @since 5.1.0
4044
* @var int Warning level.
4145
*/
4246
const WARNING = 300;
4347

4448
/**
4549
* Error level.
4650
*
51+
* @since 5.1.0
4752
* @var int Error level.
4853
*/
4954
const ERROR = 400;
5055

5156
/**
5257
* Ignore level.
5358
*
59+
* @since 5.1.0
5460
* @var int Ignore level.
5561
*/
5662
const NONE = 500;
5763

5864
/**
5965
* Log file name.
6066
*
67+
* @since 5.1.0
6168
* @var string Default log name.
6269
*/
6370
const FILE_NAME = 'feedzy';
6471

6572
/**
6673
* Log file extension.
6774
*
75+
* @since 5.1.0
6876
* @var string Log file extension.
6977
*/
7078
const FILE_EXT = '.jsonl';
7179

7280
/**
7381
* Default max file size.
7482
*
83+
* @since 5.1.0
7584
* @var int Default max file size in bytes (50MB).
7685
*/
7786
const DEFAULT_MAX_FILE_SIZE = 50 * 1024 * 1024;
7887

7988
/**
8089
* Default max files.
8190
*
91+
* @since 5.1.0
8292
* @var int Default max number of log files.
8393
*/
8494
const DEFAULT_MAX_FILES = 3;
8595

8696
/**
8797
* Log levels.
8898
*
99+
* @since 5.1.0
89100
* @var array<int, string> Log levels.
90101
*/
91102
private static $levels = array(
@@ -95,6 +106,12 @@ class Feedzy_Rss_Feeds_Log {
95106
self::ERROR => 'error',
96107
);
97108

109+
/**
110+
* Priorities mapping.
111+
*
112+
* @since 5.1.0
113+
* @var array<string, int> Priorities mapping.
114+
*/
98115
const PRIORITIES_MAPPING = array(
99116
'debug' => self::DEBUG,
100117
'info' => self::INFO,
@@ -106,62 +123,79 @@ class Feedzy_Rss_Feeds_Log {
106123
/**
107124
* The single instance of the class.
108125
*
126+
* @since 5.1.0
109127
* @var ?self The single instance of the class.
110128
*/
111129
private static $instance = null;
112130

113131
/**
114132
* The path to the log file.
115133
*
134+
* @since 5.1.0
116135
* @var string The path to the log file.
117136
*/
118137
private $filepath;
119138

120139
/**
121140
* The WordPress filesystem instance.
122141
*
142+
* @since 5.1.0
123143
* @var \WP_Filesystem_Base|null The WordPress filesystem instance.
124144
*/
125145
private $filesystem;
126146

127147
/**
128148
* The context for the logger.
129149
*
150+
* @since 5.1.0
130151
* @var array<string, mixed> The context for the logger.
131152
*/
132153
private $context = array();
133154

134155
/**
135156
* The minimum log level threshold for logging messages.
136157
*
158+
* @since 5.1.0
137159
* @var int The minimum log level threshold.
138160
*/
139161
public $level_threshold = self::ERROR;
140162

141163
/**
142164
* Whether to retain error messages for import run errors meta.
143165
*
166+
* @since 5.1.0
144167
* @var string[]
145168
*/
146169
private $error_messages_accumulator = array();
147170

148171
/**
149172
* Whether to retain error messages for import run errors meta.
150173
*
174+
* @since 5.1.0
151175
* @var bool Whether to retain error messages.
152176
*/
153177
private $retain_error_messages = false;
154178

155179
/**
156180
* Whether email reports can be sent.
157181
*
182+
* @since 5.1.0
158183
* @var bool Whether email reports can be sent.
159184
*/
160185
public $can_send_email = false;
161186

187+
/**
188+
* The email frequency for sending reports.
189+
*
190+
* @since 5.1.0
191+
* @var string
192+
*/
193+
public $email_frequency = 'weekly';
194+
162195
/**
163196
* The email address to send reports to.
164197
*
198+
* @since 5.1.0
165199
* @var string The email address to send reports to.
166200
*/
167201
public $to_email = '';
@@ -233,7 +267,7 @@ private function setup_log_directory() {
233267
* @return void
234268
*/
235269
private function init_saved_settings() {
236-
$feedzy_settings = get_option( 'feedzy-settings', array() );
270+
$feedzy_settings = apply_filters( 'feedzy_get_settings', array() );
237271
if ( ! isset( $feedzy_settings['logs'] ) ) {
238272
return;
239273
}
@@ -242,8 +276,9 @@ private function init_saved_settings() {
242276
$this->level_threshold = self::PRIORITIES_MAPPING[ $feedzy_settings['logs']['level'] ];
243277
}
244278

245-
$this->can_send_email = isset( $feedzy_settings['logs']['send_email_report'] ) && $feedzy_settings['logs']['send_email_report'];
246-
$this->to_email = isset( $feedzy_settings['logs']['email'] ) ? sanitize_email( $feedzy_settings['logs']['email'] ) : '';
279+
$this->can_send_email = isset( $feedzy_settings['logs']['send_email_report'] ) && $feedzy_settings['logs']['send_email_report'];
280+
$this->to_email = isset( $feedzy_settings['logs']['email'] ) ? sanitize_email( $feedzy_settings['logs']['email'] ) : '';
281+
$this->email_frequency = isset( $feedzy_settings['logs']['email_frequency'] ) ? sanitize_text_field( $feedzy_settings['logs']['email_frequency'] ) : 'weekly';
247282
}
248283

249284
/**

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

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
* Register and manage scheduled tasks for Feedzy RSS Feeds.
44
*
55
* @package Feedzy_Rss_Feeds_Task_Manager
6-
* @version 5.1.0
76
*/
87

98
/**
109
* Class Feedzy_Rss_Feeds_Task_Manager.
10+
*
11+
* @since 5.1.0
1112
*/
1213
class Feedzy_Rss_Feeds_Task_Manager {
1314

@@ -22,19 +23,23 @@ public function register_actions() {
2223
'task_feedzy_send_error_report',
2324
array( $this, 'send_error_report' )
2425
);
26+
27+
add_action(
28+
'update_option_feedzy-settings',
29+
array( $this, 'maybe_reschedule_email_report' ),
30+
10,
31+
2
32+
);
2533

2634
add_action(
2735
'task_feedzy_cleanup_logs',
28-
function () {
29-
Feedzy_Rss_Feeds_Log::get_instance()->should_clean_logs();
30-
}
36+
array( $this, 'check_and_clean_logs' )
3137
);
3238

3339
add_action(
3440
'init',
3541
function () {
36-
$this->schedule_weekly_tasks();
37-
$this->schedule_hourly_tasks();
42+
$this->schedule_tasks();
3843
}
3944
);
4045
}
@@ -45,7 +50,22 @@ function () {
4550
* @since 5.1.0
4651
* @return void
4752
*/
48-
public function schedule_weekly_tasks() {
53+
public function schedule_tasks() {
54+
if (
55+
false === Feedzy_Rss_Feeds_Util_Scheduler::is_scheduled( 'task_feedzy_cleanup_logs' )
56+
) {
57+
Feedzy_Rss_Feeds_Util_Scheduler::schedule_event( time(), 'hourly', 'task_feedzy_cleanup_logs' );
58+
}
59+
$this->schedule_email_report();
60+
}
61+
62+
/**
63+
* Schedule daily tasks.
64+
*
65+
* @since 5.1.0
66+
* @return void
67+
*/
68+
public function schedule_email_report() {
4969
$log_instance = Feedzy_Rss_Feeds_Log::get_instance();
5070

5171
if (
@@ -54,24 +74,53 @@ public function schedule_weekly_tasks() {
5474
) {
5575
return;
5676
}
57-
58-
Feedzy_Rss_Feeds_Util_Scheduler::schedule_event( time(), 'weekly', 'task_feedzy_send_error_report' );
77+
78+
$frequency = $log_instance->email_frequency;
79+
if ( ! in_array( $frequency, array( 'daily', 'weekly' ), true ) ) {
80+
$frequency = 'weekly';
81+
}
82+
83+
Feedzy_Rss_Feeds_Util_Scheduler::schedule_event( time(), $frequency, 'task_feedzy_send_error_report' );
5984
}
6085

6186
/**
62-
* Schedule daily tasks.
87+
* When feedzy settings are updated, ensure the error report schedule matches the new frequency.
6388
*
6489
* @since 5.1.0
90+
* @param array<string, mixed> $old_value Previous option value.
91+
* @param array<string, mixed> $value New option value.
6592
* @return void
6693
*/
67-
public function schedule_hourly_tasks() {
68-
if (
69-
false !== Feedzy_Rss_Feeds_Util_Scheduler::is_scheduled( 'task_feedzy_cleanup_logs' )
70-
) {
94+
public function maybe_reschedule_email_report( $old_value, $value ) {
95+
$old_freq = isset( $old_value['logs']['email_frequency'] ) ? sanitize_text_field( $old_value['logs']['email_frequency'] ) : '';
96+
$new_freq = isset( $value['logs']['email_frequency'] ) ? sanitize_text_field( $value['logs']['email_frequency'] ) : '';
97+
98+
if ( $old_freq === $new_freq ) {
7199
return;
72100
}
101+
102+
Feedzy_Rss_Feeds_Util_Scheduler::clear_scheduled_hook( 'task_feedzy_send_error_report' );
103+
104+
if ( ! in_array( $new_freq, array( 'daily', 'weekly' ), true ) ) {
105+
$new_freq = 'weekly';
106+
}
107+
108+
$send_reports = ! empty( $value['logs']['send_email_report'] );
109+
$to_email = isset( $value['logs']['email'] ) ? sanitize_email( $value['logs']['email'] ) : '';
110+
111+
if ( $send_reports && ! empty( $to_email ) ) {
112+
Feedzy_Rss_Feeds_Util_Scheduler::schedule_event( time(), $new_freq, 'task_feedzy_send_error_report' );
113+
}
114+
}
73115

74-
Feedzy_Rss_Feeds_Util_Scheduler::schedule_event( time(), 'hourly', 'task_feedzy_cleanup_logs' );
116+
/**
117+
* Check and clean logs if necessary.
118+
*
119+
* @since 5.1.0
120+
* @return void
121+
*/
122+
public function check_and_clean_logs() {
123+
Feedzy_Rss_Feeds_Log::get_instance()->should_clean_logs();
75124
}
76125

77126
/**

0 commit comments

Comments
 (0)