Skip to content

Commit

Permalink
display monthly badge among latest badges
Browse files Browse the repository at this point in the history
  • Loading branch information
ilicfilip committed Nov 14, 2024
1 parent 4e60462 commit 3ab817c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 25 deletions.
11 changes: 6 additions & 5 deletions classes/actions/class-content.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,16 +279,17 @@ private function add_post_activity( $post, $type ) {

// Update the badges.
if ( 'publish' === $type ) {
$badge_ids = [ 'wonderful-writer', 'bold-blogger', 'awesome-author' ];
foreach ( $badge_ids as $badge_id ) {

// 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 === \progress_planner()->get_settings()->get( [ 'badges', $badge_id, 'progress' ], 0 ) ) {
if ( 100 === $badge->progress_callback()['progress'] ) {
continue;
}

// Delete the badge value so it can be re-calculated.
\progress_planner()->get_settings()->set( [ 'badges', $badge_id ], [] );
$badge->clear_progress();
}

// Check if there is a publish activity for this post.
Expand Down
9 changes: 9 additions & 0 deletions classes/badges/class-badge.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ protected function save_progress( $progress ) {
\progress_planner()->get_settings()->set( [ 'badges', $this->id ], $progress );
}

/**
* Clear the saved progress.
*
* @return void
*/
public function clear_progress() {
\progress_planner()->get_settings()->set( [ 'badges', $this->id ], [] );
}

/**
* Get the icon URL.
*
Expand Down
23 changes: 17 additions & 6 deletions classes/badges/class-monthly.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static function get_instances() {
}

foreach ( array_keys( self::get_months() ) as $month ) {
$id = 'monthly-' . gmdate( 'Y' ) . '-' . str_replace( '-', '', $month );
$id = 'monthly-' . gmdate( 'Y' ) . '-' . $month;
self::$instances[] = new self( $id );
}

Expand Down Expand Up @@ -132,6 +132,13 @@ public function get_month() {
* @return array
*/
public function progress_callback() {
$saved_progress = $this->get_saved();

// If we have a saved value, return it.
if ( isset( $saved_progress['progress'] ) && isset( $saved_progress['remaining'] ) ) {
return $saved_progress;
}

$month = self::get_months()[ 'm' . $this->get_month() ];
$year = $this->get_year();
$month_num = (int) $this->get_month();
Expand All @@ -154,16 +161,20 @@ public function progress_callback() {
}

if ( $points > self::TARGET_POINTS ) {
return [
$return_progress = [
'progress' => 100,
'remaining' => 0,
];
} else {
$return_progress = [
'progress' => (int) max( 0, min( 100, floor( 100 * $points / self::TARGET_POINTS ) ) ),
'remaining' => self::TARGET_POINTS - $points,
];
}

return [
'progress' => (int) max( 0, min( 100, floor( 100 * $points / self::TARGET_POINTS ) ) ),
'remaining' => self::TARGET_POINTS - $points,
];
$this->save_progress( $return_progress );

return $return_progress;
}

/**
Expand Down
16 changes: 5 additions & 11 deletions classes/class-badges.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,13 @@ public function get_latest_completed_badge() {

$latest_date = null;

$flat_badges = array_merge(
$this->content,
$this->maintenance,
$this->monthly,
);

foreach ( [ 'content', 'maintenance', 'monthly' ] as $context ) {
foreach ( $this->$context as $badge ) {
// Skip if the badge has no date.
if ( ! isset( $settings[ $badge->get_id() ]['date'] ) ) {
continue;
}

$badge_progress = $badge->get_progress();

// Continue if the badge is not completed.
Expand All @@ -131,11 +130,6 @@ public function get_latest_completed_badge() {
continue;
}

// Skip if the badge has no date.
if ( ! isset( $settings[ $badge->get_id() ]['date'] ) ) {
continue;
}

// Compare dates.
if ( \DateTime::createFromFormat( 'Y-m-d H:i:s', $settings[ $badge->get_id() ]['date'] )->format( 'U' ) >= \DateTime::createFromFormat( 'Y-m-d H:i:s', $latest_date )->format( 'U' ) ) {
$latest_date = $settings[ $badge->get_id() ]['date'];
Expand Down
24 changes: 21 additions & 3 deletions classes/class-suggested-tasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,31 @@ 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 = new \DateTime();
$activity->date = $activity_date;
$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' );

foreach ( \progress_planner()->get_badges()->get_badges( 'monthly' ) as $badge ) {

if ( $badge_id === $badge->get_id() ) {

// Clear the progress.
$badge->clear_progress();

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

$this->mark_task_as_pending_celebration( $task_id );
}

Expand Down Expand Up @@ -257,13 +275,13 @@ public function suggested_task_action() {

switch ( $action ) {
case 'complete':
\progress_planner()->get_suggested_tasks()->mark_task_as_completed( $task_id );
$this->mark_task_as_completed( $task_id );
$updated = true;
break;

case 'snooze':
$duration = isset( $_POST['duration'] ) ? \sanitize_text_field( \wp_unslash( $_POST['duration'] ) ) : '';
$updated = \progress_planner()->get_suggested_tasks()->mark_task_as_snoozed( $task_id, $duration );
$updated = $this->mark_task_as_snoozed( $task_id, $duration );
break;

default:
Expand Down

0 comments on commit 3ab817c

Please sign in to comment.