Skip to content

Commit d6b7faf

Browse files
authored
Merge pull request #457 from ProgressPlanner/filip/detect-yoast-workouts
Detect when Yoast SEO Cornerstone & Orphaned content workouts are completed by the user
2 parents 7d03de6 + f227e46 commit d6b7faf

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
/**

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

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

70110
/**

0 commit comments

Comments
 (0)