/** * Last unlocked achievement column * * @param WP_User $user A singular item (one full row) * @see WP_List_Table::single_row_columns() * @since Achievements (3.0) */ public function column_dpa_last_id(WP_User $user) { $output = true; // Get this user's most recent unlocked achievement $achievement_id = dpa_get_user_last_unlocked($user->ID); if (empty($achievement_id)) { $output = false; } // Check user ID is valid if ($output && !dpa_is_user_active($user->ID)) { $output = false; } // Check achievement is still valid if ($output) { $achievement = get_post($achievement_id); } if ($output && (empty($achievement) || 'publish' !== $achievement->post_status)) { $output = false; } if ($output) { printf('<a href="%1$s">%2$s</a>', esc_url(get_permalink($achievement->ID)), esc_html(apply_filters('dpa_get_achievement_title', $achievement->post_title, $achievement->ID))); } else { echo '—'; } }
/** * Output the ID of the last achievement this user unlocked * * @param int $user_id Optional. User ID to retrieve value for * @since Achievements (3.0) */ function dpa_user_last_unlocked($user_id = 0) { echo number_format_i18n(dpa_get_user_last_unlocked($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); }