Skip to content

Commit 549cc84

Browse files
committed
add "is_completed" callback
1 parent a21d327 commit 549cc84

11 files changed

Lines changed: 77 additions & 95 deletions

classes/activities/class-suggested-task.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,8 @@ public function get_points( $date ) {
6666
$points = 1;
6767
$tasks = \progress_planner()->get_suggested_tasks_db()->get_tasks_by( [ 'task_id' => $this->data_id ] );
6868

69-
if ( ! empty( $tasks ) && $tasks[0]->get_provider_id() ) {
70-
$task_provider = \progress_planner()->get_suggested_tasks()->get_tasks_manager()->get_task_provider( $tasks[0]->get_provider_id() );
71-
72-
if ( $task_provider ) {
73-
$points = $task_provider->get_points();
74-
}
69+
if ( ! empty( $tasks ) ) {
70+
$points = $tasks[0]->points;
7571
}
7672
$this->points[ $date_ymd ] = $points;
7773

classes/class-suggested-tasks-db.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ public function get_post( $id ) {
293293
*
294294
* @param array $params The parameters to filter by ([ 'provider' => 'provider_id' ] etc).
295295
*
296-
* @return array
296+
* @return array<\Progress_Planner\Suggested_Tasks\Task>
297297
*/
298298
public function get_tasks_by( $params ) {
299299
$args = [];
@@ -339,7 +339,7 @@ public function get_tasks_by( $params ) {
339339
*
340340
* @param array $args The arguments.
341341
*
342-
* @return array
342+
* @return array<\Progress_Planner\Suggested_Tasks\Task>
343343
*/
344344
public function get( $args = [] ) {
345345
$args = \wp_parse_args(

classes/class-suggested-tasks.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public function __construct() {
6565

6666
// Filter the REST API response.
6767
\add_filter( 'rest_prepare_prpl_recommendations', [ $this, 'rest_prepare_recommendation' ], 10, 2 );
68+
69+
\add_filter( 'wp_trash_post_days', [ $this, 'change_trashed_posts_lifetime' ], 10, 2 );
6870
}
6971

7072
/**
@@ -153,7 +155,7 @@ public function on_automatic_updates_complete(): void {
153155
\progress_planner()->get_suggested_tasks_db()->update_recommendation( $pending_tasks[0]->ID, [ 'post_status' => 'trash' ] );
154156

155157
// Insert an activity.
156-
$this->insert_activity( $pending_tasks[0]->ID );
158+
$this->insert_activity( $pending_tasks[0]->task_id );
157159
}
158160

159161
/**
@@ -339,6 +341,22 @@ public function register_post_type() {
339341
}
340342
}
341343

344+
/**
345+
* Custom trash lifetime by post type.
346+
*
347+
* @param int $days The number of days to keep in trash.
348+
* @param \WP_Post $post The post.
349+
*
350+
* @return int
351+
*/
352+
public function change_trashed_posts_lifetime( $days, $post ) {
353+
if ( 'prpl_recommendations' === $post->post_type ) {
354+
return 60;
355+
}
356+
357+
return $days;
358+
}
359+
342360
/**
343361
* Register a custom taxonomies for suggested tasks.
344362
*

classes/class-todo.php

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -25,52 +25,6 @@ public function __construct() {
2525
\add_action( 'rest_after_insert_prpl_recommendations', [ $this, 'handle_creating_user_task' ], 10, 3 );
2626
}
2727

28-
/**
29-
* Get the points for a new task.
30-
*
31-
* @return int
32-
*/
33-
public function calc_points_for_new_task() {
34-
$items = \progress_planner()->get_suggested_tasks_db()->get_tasks_by( [ 'provider_id' => 'user' ] );
35-
36-
// If this is the first user task ever, return 1.
37-
if ( ! count( $items ) ) {
38-
return 1;
39-
}
40-
41-
// Get the task IDs from the todos.
42-
$task_ids = array_column( $items, 'task_id' );
43-
44-
// Get the completed activities for this week that are in the todos.
45-
$activities = array_filter(
46-
\progress_planner()->get_activities__query()->query_activities(
47-
[
48-
'start_date' => new \DateTime( 'monday this week' ),
49-
'end_date' => new \DateTime( 'sunday this week' ),
50-
'category' => 'suggested_task',
51-
'type' => 'completed',
52-
]
53-
),
54-
function ( $activity ) use ( $task_ids ) {
55-
return in_array( $activity->data_id, $task_ids, true );
56-
}
57-
);
58-
59-
// If there are completed todos this week, we already have set the golden task and it was completed.
60-
if ( count( $activities ) ) {
61-
return 0;
62-
}
63-
64-
// Check if there are already published user tasks with a points value other than 0.
65-
foreach ( $items as $item ) {
66-
if ( 'publish' === $item['post_status'] && isset( $item['points'] ) && $item['points'] !== 0 ) {
67-
return 0;
68-
}
69-
}
70-
71-
return 1;
72-
}
73-
7428
/**
7529
* Maybe change the points of the first item in the todo list on Monday.
7630
*

classes/suggested-tasks/class-task.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* @property int|null $target_term_id The target term ID for the task
3030
* @property string|null $target_taxonomy The target taxonomy for the task
3131
* @property string|null $target_term_name The target term name for the task
32+
* @property string|null $date The task date in YW format (year-week)
3233
*/
3334
class Task {
3435
/**

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

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,30 @@ public function should_add_task() {
4848
* @return array
4949
*/
5050
public function get_tasks_to_inject() {
51+
// We don't need to inject tasks for this provider.
52+
return [];
53+
}
5154

52-
$tasks = [];
53-
$saved_tasks = \progress_planner()->get_settings()->get( 'tasks', [] );
54-
foreach ( $saved_tasks as $task_data ) {
55-
if ( isset( $task_data['provider_id'] ) && self::PROVIDER_ID === $task_data['provider_id'] ) {
56-
$tasks[] = [
57-
'task_id' => $task_data['task_id'],
58-
'provider_id' => $this->get_provider_id(),
59-
'category' => $this->get_provider_category(),
60-
'points' => 0,
61-
];
62-
}
55+
/**
56+
* Check if a specific task is completed.
57+
* Child classes can override this method to handle specific task IDs.
58+
*
59+
* @param string $task_id The task ID to check.
60+
* @return bool
61+
*/
62+
protected function is_specific_task_completed( $task_id ) {
63+
$tasks = \progress_planner()->get_suggested_tasks_db()->get_tasks_by( [ 'task_id' => $task_id ] );
64+
if ( empty( $tasks ) ) {
65+
return false;
66+
}
67+
68+
$task_data = $tasks[0]->get_data();
69+
70+
if ( isset( $task_data['is_completed_callback'] ) && is_callable( $task_data['is_completed_callback'] ) ) {
71+
return call_user_func( $task_data['is_completed_callback'], $task_id );
6372
}
6473

65-
return $tasks;
74+
return false;
6675
}
6776

6877
/**

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,8 @@ public function evaluate_task( $task_id ) {
402402
}
403403

404404
if ( ! $this->is_repetitive() ) {
405-
if ( ! $task->task_id || 0 !== strpos( $task->task_id, $this->get_task_id() ) ) {
405+
// TODO: Collaborator tasks have custom task_ids.
406+
if ( ! $task->task_id || ( 0 !== strpos( $task->task_id, $this->get_task_id() ) && 'collaborator' !== $this->get_provider_id() ) ) {
406407
return false;
407408
}
408409
return $this->is_task_completed( $task->task_id ) ? $task : false;

classes/suggested-tasks/providers/integrations/yoast/class-cornerstone-workout.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function maybe_update_workout_status( $old_value, $value, $option ) {
9393
}
9494

9595
// For this type of task only the provider ID is needed, but just in case.
96-
if ( $this->is_task_dismissed( $tasks[0] ) ) {
96+
if ( $this->is_task_dismissed( $tasks[0]->get_data() ) ) {
9797
return;
9898
}
9999

classes/suggested-tasks/providers/integrations/yoast/class-orphaned-content-workout.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function maybe_update_workout_status( $old_value, $value, $option ) {
9696
}
9797

9898
// For this type of task only the provider ID is needed, but just in case.
99-
if ( $this->is_task_dismissed( $tasks[0] ) ) {
99+
if ( $this->is_task_dismissed( $tasks[0]->get_data() ) ) {
100100
return;
101101
}
102102

classes/utils/class-debug-tools.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ protected function add_suggested_tasks_submenu_item( $admin_bar ) {
238238
foreach ( $suggested_tasks as $task ) {
239239
$title = $task->post_title;
240240
if ( $task->post_status && 'future' === $task->post_status && $task->post_date ) {
241-
$until = is_float( $task->post_date ) ? '(forever)' : '(until ' . $task->post_date . ')';
241+
$until = '(until ' . $task->post_date . ')';
242242
$title .= ' ' . $until;
243243
}
244244

0 commit comments

Comments
 (0)