Skip to content

Commit b8d51e0

Browse files
authored
Merge pull request #562 from ProgressPlanner/filip/v17/handle-unsnooze
Check tasks when snoozed period is over
2 parents 41aabdb + 18ff804 commit b8d51e0

13 files changed

Lines changed: 152 additions & 17 deletions

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/class-suggested-tasks.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,6 @@ public function get_tasks_in_rest_format( array $args = [] ) {
479479

480480
// Get the tasks for each category.
481481
foreach ( $max_items_per_category as $category_slug => $max_items ) {
482-
483482
// Skip excluded providers.
484483
if ( ! empty( $args['exclude_provider'] ) && \in_array( $category_slug, $args['exclude_provider'], true ) ) {
485484
continue;

classes/class-todo.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ public function maybe_change_first_item_points_on_monday() {
7676
* @return void
7777
*/
7878
public function handle_creating_user_task( $post, $request, $creating ) {
79-
8079
if ( ! $creating || ! \has_term( 'user', 'prpl_recommendations_provider', $post->ID ) ) {
8180
return;
8281
}

classes/rest/class-tasks.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ public function validate_token( $token ) {
7777
* @return \WP_REST_Response The REST response object containing the recommendations.
7878
*/
7979
public function get_tasks() {
80-
8180
// Collection of task objects.
8281
$tasks = \progress_planner()->get_suggested_tasks_db()->get_tasks_by( [ 'post_status' => [ 'publish', 'trash', 'draft', 'future', 'pending' ] ] );
8382
$tasks_to_return = [];

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

Lines changed: 36 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,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
}

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-email-sending.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ class Email_Sending extends Tasks {
109109
* @return void
110110
*/
111111
public function init() {
112-
113112
// Enqueue the scripts.
114113
\add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
115114

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.

classes/update/class-update-161.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ private function migrate_badges() {
3939
$badges = \progress_planner()->get_settings()->get( 'badges', [] );
4040

4141
foreach ( $badges as $badge_id => $badge ) {
42-
4342
// We are only migrating monthly badges.
4443
if ( 0 !== \strpos( $badge_id, 'monthly-' ) ) {
4544
continue;

classes/utils/class-playground.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,6 @@ public function inject_playground_js_patch() {
258258
?>
259259
<script>
260260
( function waitForWp() {
261-
262261
if ( ! window.wp || ! wp.api?.models?.Prpl_recommendations ) {
263262
return setTimeout(waitForWp, 50);
264263
}

0 commit comments

Comments
 (0)