/** * When an achievement is unlocked by a user, update various stats relating to the user. * * @param WP_Post $achievement_obj The Achievement object. * @param int $user_id ID of the user who unlocked the achievement. * @param int $progress_id The Progress object's ID. * @since Achievements (3.0) */ function dpa_update_user_stats(WP_Post $achievement_obj, $user_id, $progress_id) { // Let other plugins easily bypass updating user's stats if (!apply_filters('dpa_maybe_update_user_stats', true, $achievement_obj, $user_id, $progress_id)) { return; } // Increment the user's current unlocked count $new_unlock_count = dpa_get_user_unlocked_count($user_id) + 1; $new_unlock_count = apply_filters('dpa_update_user_stats_value', $new_unlock_count, $achievement_obj, $user_id, $progress_id); // Update user's unlocked count dpa_update_user_unlocked_count($user_id, $new_unlock_count); // Store the ID of the unlocked achievement for this user dpa_update_user_last_unlocked($user_id, $achievement_obj->ID); // Allow other things to happen after the user's stats have been updated do_action('dpa_update_user_stats', $achievement_obj, $user_id, $progress_id, $new_unlock_count); }
/** * Output the total of how many achievements that this user has unlocked * * @param int $user_id Optional. User ID to retrieve value for * @since Achievements (3.0) */ function dpa_user_unlocked_count($user_id = 0) { echo number_format_i18n(dpa_get_user_unlocked_count($user_id)); }
/** * Delete a user's progress for an achievement. Essentially, un-reward the achievement for this user. * * @param int $achievement_id Achievement ID * @param int $user_id User ID * @since Achievements (3.0) */ function dpa_delete_achievement_progress($achievement_id, $user_id) { $achievement_id = dpa_get_achievement_id($achievement_id); if (empty($achievement_id) || !dpa_is_achievement($achievement_id)) { return; } $progress_id = dpa_get_progress(array('author' => $user_id, 'fields' => 'ids', 'no_found_rows' => true, 'nopaging' => true, 'numberposts' => 1, 'post_parent' => $achievement_id)); if (empty($progress_id)) { return; } $progress_id = apply_filters('dpa_delete_achievement_progress', array_pop($progress_id), $achievement_id, $user_id); do_action('dpa_before_delete_achievement_progress', $progress_id, $achievement_id, $user_id); wp_delete_post($progress_id, true); // Check that the delete achievement isn't in the user's pending notifications $notifications = dpa_get_user_notifications($user_id); if (isset($notifications[$achievement_id])) { unset($notifications[$achievement_id]); } // Update the user's notifications in case we cleared any above dpa_update_user_notifications($notifications, $user_id); // Decrease user unlocked count dpa_update_user_unlocked_count($user_id, dpa_get_user_unlocked_count($user_id) - 1); /** * If the progress was linked to an achievement that is the same achievement that is stored in * this user's "last unlocked" meta, then clear the "last unlocked" meta, too. */ if ((int) dpa_get_user_last_unlocked($user_id) === $achievement_id) { dpa_update_user_last_unlocked($user_id, 0); } do_action('dpa_after_delete_achievement_progress', $progress_id, $achievement_id, $user_id); }