Skip to content

Commit

Permalink
WIP: decouple badges & actions
Browse files Browse the repository at this point in the history
  • Loading branch information
ilicfilip committed Nov 14, 2024
1 parent ee8d6db commit 6f6e188
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 27 deletions.
13 changes: 2 additions & 11 deletions classes/actions/class-content.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,17 +280,8 @@ private function add_post_activity( $post, $type ) {
// Update the badges.
if ( 'publish' === $type ) {

// WIP: So the clearing is done internally by the badges class.
$group_badges = \progress_planner()->get_badges()->get_badges( 'content' );
foreach ( $group_badges as $badge ) {

// If the badge is already complete, skip it.
if ( 100 === $badge->progress_callback()['progress'] ) {
continue;
}
// Delete the badge value so it can be re-calculated.
$badge->clear_progress();
}
// WIP.
\do_action( 'prpl_wip_clear_content_progress_action', $activity->data_id );

// Check if there is a publish activity for this post.
$existing = \progress_planner()->get_query()->query_activities(
Expand Down
62 changes: 62 additions & 0 deletions classes/class-badges.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public function __construct() {
];

$this->monthly = \Progress_Planner\Badges\Monthly::get_instances();

\add_action( 'progress_planner_suggested_task_completed', [ $this, 'clear_monthly_progress' ] );
\add_action( 'prpl_wip_clear_content_progress_action', [ $this, 'clear_content_progress' ] );
}

/**
Expand Down Expand Up @@ -92,6 +95,65 @@ public function get_badge( $badge_id ) {
return null;
}

/**
* Clear the progress of all monthly badges.
*
* @param string $activity_id The activity ID.
*
* @return void
*/
public function clear_monthly_progress( $activity_id ) {

$activities = \progress_planner()->get_query()->query_activities(
[
'category' => 'suggested_task',
'type' => 'completed',
'data_id' => (string) $activity_id,
],
'ACTIVITIES'
);

if ( empty( $activities ) ) {
return;
}

// Clear monthly saved progress.
$badge_id = 'monthly-' . $activities[0]->date->format( 'Y' ) . '-m' . $activities[0]->date->format( 'm' );
$monthly_badge = $this->get_badge( $badge_id );

if ( $monthly_badge ) {
// Clear the progress.
$monthly_badge->clear_progress();

// Save the progress.
$monthly_badge->get_progress();
}
}


/**
* Clear the progress of all badges.
*
* @param string $activity_id The activity ID.
*
* @return void
*/
public function clear_content_progress( $activity_id ) {

// Clear content saved progress.
foreach ( $this->content as $badge ) {

// If the badge is already complete, skip it.
if ( 100 === $badge->progress_callback()['progress'] ) {
continue;
}

// Delete the badge value so it can be re-calculated.
$badge->clear_progress();
}
}


/**
* Get the latest completed badge.
*
Expand Down
1 change: 1 addition & 0 deletions classes/class-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function init() {
$this->cached['settings'] = new Settings();
$this->cached['settings_page'] = new \Progress_Planner\Admin\Page_Settings();
$this->cached['suggested_tasks'] = new Suggested_Tasks();
$this->cached['badges'] = new Badges();
}

/**
Expand Down
17 changes: 3 additions & 14 deletions classes/class-suggested-tasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,28 +77,17 @@ public function get_local() {
* @return void
*/
public function mark_task_as_completed( $task_id ) {
$activity_date = new \DateTime();

$activity = new \Progress_Planner\Activities\Suggested_Task();
$activity->type = 'completed';
$activity->data_id = (string) $task_id;
$activity->date = $activity_date;
$activity->date = new \DateTime();
$activity->user_id = \get_current_user_id();
$activity->save();

// Clear monthly saved progress.
$badge_id = 'monthly-' . $activity_date->format( 'Y' ) . '-m' . $activity_date->format( 'm' );
$monthly_badge = \progress_planner()->get_badges()->get_badge( $badge_id );

if ( $monthly_badge ) {
// Clear the progress.
$monthly_badge->clear_progress();

// Save the progress.
$monthly_badge->get_progress();
}

$this->mark_task_as_pending_celebration( $task_id );

do_action( 'progress_planner_suggested_task_completed', $task_id );
}

/**
Expand Down
4 changes: 2 additions & 2 deletions views/page-widgets/parts/monthly-badge-2024.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
exit;
}

$prpl_badge = \progress_planner()->get_badges()->get_badge( 'monthly-2024-m12' );
$prpl_badge_progress = $prpl_badge->get_progress();
$prpl_badge = \progress_planner()->get_badges()->get_badge( 'monthly-2024-m12' );
$prpl_badge_progress = $prpl_badge->get_progress();
$prpl_badge_completed = 100 === (int) $prpl_badge_progress['progress'];

if ( ! $prpl_badge_completed ) {
Expand Down

0 comments on commit 6f6e188

Please sign in to comment.