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 */
1213class 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