Skip to content

Commit 3291102

Browse files
committed
handle tasks when snooze period is over
1 parent 5fce6d0 commit 3291102

4 files changed

Lines changed: 45 additions & 8 deletions

File tree

classes/class-page-types.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,14 +383,14 @@ public function update_option_page_on_front( $page_id ) {
383383
*
384384
* Runs on post_updated hook.
385385
*
386-
* @param int $post_id The post ID.
387-
* @param \WP_Post $post The post object.
386+
* @param int $post_id The post ID.
387+
* @param \WP_Post|null $post The post object.
388388
*
389389
* @return void
390390
*/
391391
public function post_updated( $post_id, $post ) {
392392
// Check if the post is set as a homepage.
393-
if ( 'page' !== $post->post_type || 'publish' !== $post->post_status ) {
393+
if ( ! $post || 'page' !== $post->post_type || 'publish' !== $post->post_status ) {
394394
return;
395395
}
396396

classes/suggested-tasks/class-tasks-manager.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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,38 @@ 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+
305+
// Early exit if it's not task for which snooze period is over.
306+
if ( 'future' !== $old_status || 'publish' !== $new_status || 'prpl_recommendations' !== get_post_type( $post ) ) {
307+
return;
308+
}
309+
310+
$task = \progress_planner()->get_suggested_tasks_db()->get_post( $post->ID );
311+
if ( ! $task ) {
312+
return;
313+
}
314+
315+
$task_provider = $this->get_task_provider( $task->get_provider_id() );
316+
317+
// Delete tasks which don't have a task provider or repetitive tasks which were created in the previous week.
318+
if ( ! $task_provider || ( $task_provider->is_repetitive() && ( ! $task->date || \gmdate( 'YW' ) !== (string) $task->date ) ) ) {
319+
\progress_planner()->get_suggested_tasks_db()->delete_recommendation( $task->ID );
320+
}
321+
322+
// Delete the task if it is not relevant anymore.
323+
if ( $task_provider instanceof \Progress_Planner\Suggested_Tasks\Providers\Tasks && ! $task_provider->should_add_task() ) {
324+
\progress_planner()->get_suggested_tasks_db()->delete_recommendation( $task->ID );
325+
}
326+
}
290327
}

classes/suggested-tasks/data-collector/class-post-author.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ public function init() {
3434
/**
3535
* Update the cache when the post author changes.
3636
*
37-
* @param int $post_id The post ID.
38-
* @param \WP_Post $post_after The post object following the update.
39-
* @param \WP_Post $post_before The post object before the update.
37+
* @param int $post_id The post ID.
38+
* @param \WP_Post|null $post_after The post object following the update.
39+
* @param \WP_Post $post_before The post object before the update.
4040
*
4141
* @return void
4242
*/
4343
public function update_post_author_on_change( $post_id, $post_after, $post_before ) {
44-
if ( $post_after->post_author !== $post_before->post_author ) {
44+
if ( $post_after && $post_after->post_author !== $post_before->post_author ) {
4545
$this->update_cache();
4646
}
4747
}

classes/suggested-tasks/providers/class-tasks.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ public function evaluate_task( $task_id ) {
443443
*
444444
* @return bool true means that the task condition is satisfied, meaning that we don't need to add the task or task was completed.
445445
*/
446-
abstract protected function should_add_task();
446+
abstract public function should_add_task();
447447

448448
/**
449449
* Alias for should_add_task(), for better readability when using in the evaluate_task() method.

0 commit comments

Comments
 (0)