@@ -66,6 +66,46 @@ class Cornerstone_Workout extends Tasks {
6666 */
6767 public function init () {
6868 $ this ->init_dismissable_task ();
69+
70+ // Hook into update_option.
71+ \add_action ( 'update_option_wpseo_premium ' , [ $ this , 'maybe_update_workout_status ' ], 10 , 3 );
72+ }
73+
74+ /**
75+ * Maybe update the workout status.
76+ *
77+ * @param mixed $old_value The old value.
78+ * @param mixed $value The new value.
79+ * @param string $option The option name.
80+ *
81+ * @return void
82+ */
83+ public function maybe_update_workout_status ( $ old_value , $ value , $ option ) {
84+ if ( 'wpseo_premium ' !== $ option || ! isset ( $ value ['workouts ' ]['cornerstone ' ] ) || ! isset ( $ old_value ['workouts ' ]['cornerstone ' ] ) ) {
85+ return ;
86+ }
87+
88+ // Check if there is pending task.
89+ $ tasks = \progress_planner ()->get_suggested_tasks ()->get_tasks_by ( 'task_id ' , $ this ->get_task_id () );
90+
91+ // If there is no pending task, return.
92+ if ( empty ( $ tasks ) || 'pending ' !== $ tasks [0 ]['status ' ] ) {
93+ return ;
94+ }
95+
96+ // For this type of task only the provider ID is needed, but just in case.
97+ if ( $ this ->is_task_dismissed ( $ tasks [0 ] ) ) {
98+ return ;
99+ }
100+
101+ // There should be 3 steps in the workout.
102+ $ workout_was_completed = 3 === count ( $ old_value ['workouts ' ]['cornerstone ' ]['finishedSteps ' ] );
103+ $ workout_completed = 3 === count ( $ value ['workouts ' ]['cornerstone ' ]['finishedSteps ' ] );
104+
105+ // Dismiss the task if workout wasn't completed before and now is.
106+ if ( ! $ workout_was_completed && $ workout_completed ) {
107+ $ this ->handle_task_dismissal ( $ this ->get_task_id () );
108+ }
69109 }
70110
71111 /**
0 commit comments