@@ -84,6 +84,9 @@ public function __construct() {
8484
8585 // Add the cleanup action.
8686 \add_action ( 'admin_init ' , [ $ this , 'cleanup_pending_tasks ' ] );
87+
88+ // Handle tasks when snoozed period is over.
89+ \add_action ( 'transition_post_status ' , [ $ this , 'handle_task_unsnooze ' ], 10 , 3 );
8790 }
8891
8992 /**
@@ -287,4 +290,37 @@ public function cleanup_pending_tasks() {
287290
288291 \progress_planner ()->get_utils__cache ()->set ( 'cleanup_pending_tasks ' , true , DAY_IN_SECONDS );
289292 }
293+
294+ /**
295+ * Handle task unsnooze.
296+ *
297+ * @param string $new_status The new status.
298+ * @param string $old_status The old status.
299+ * @param \WP_Post $post The post.
300+ *
301+ * @return void
302+ */
303+ public function handle_task_unsnooze ( $ new_status , $ old_status , $ post ) {
304+ // Early exit if it's not task for which snooze period is over.
305+ if ( 'future ' !== $ old_status || 'publish ' !== $ new_status || 'prpl_recommendations ' !== get_post_type ( $ post ) ) {
306+ return ;
307+ }
308+
309+ $ task = \progress_planner ()->get_suggested_tasks_db ()->get_post ( $ post ->ID );
310+ if ( ! $ task ) {
311+ return ;
312+ }
313+
314+ $ task_provider = $ this ->get_task_provider ( $ task ->get_provider_id () );
315+
316+ // Delete tasks which don't have a task provider or repetitive tasks which were created in the previous week.
317+ if ( ! $ task_provider || ( $ task_provider ->is_repetitive () && ( ! $ task ->date || \gmdate ( 'YW ' ) !== (string ) $ task ->date ) ) ) {
318+ \progress_planner ()->get_suggested_tasks_db ()->delete_recommendation ( $ task ->ID );
319+ }
320+
321+ // Delete the task if it is not relevant anymore.
322+ if ( $ task_provider instanceof \Progress_Planner \Suggested_Tasks \Providers \Tasks && ! $ task_provider ->should_add_task () ) {
323+ \progress_planner ()->get_suggested_tasks_db ()->delete_recommendation ( $ task ->ID );
324+ }
325+ }
290326}
0 commit comments