/** * When an achievement is unlocked, give the points to the user. * * @param WP_Post $achievement_obj The Achievement object to send a notification for. * @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_send_points(WP_Post $achievement_obj, $user_id, $progress_id) { // Let other plugins easily bypass sending points. if (!apply_filters('dpa_maybe_send_points', true, $achievement_obj, $user_id, $progress_id)) { return; } // Get the user's current total points plus the point value for the unlocked achievement $points = dpa_get_user_points($user_id) + dpa_get_achievement_points($achievement_obj->ID); $points = apply_filters('dpa_send_points_value', $points, $achievement_obj, $user_id, $progress_id); // Give points to user dpa_update_user_points($points, $user_id); // Allow other things to happen after the user's points have been updated do_action('dpa_send_points', $achievement_obj, $user_id, $progress_id, $points); }
/** * Update the user's "User Points" meta information when the Edit User page has been saved, * and modify the user's current achievements as appropriate. * * The action that this function is hooked to is only executed on a succesful update, * which is behind a nonce and capability check (see wp-admin/user-edit.php). * * @param int $user_id * @since Achievements (3.0) */ public function save_profile_fields($user_id) { if (!isset($_POST['dpa_achievements']) || !is_super_admin()) { return; } if (!isset($_POST['dpa_user_achievements'])) { $_POST['dpa_user_achievements'] = array(); } // If multisite and running network-wide, switch_to_blog to the data store site if (is_multisite() && dpa_is_running_networkwide()) { switch_to_blog(DPA_DATA_STORE); } // Update user's points dpa_update_user_points((int) $_POST['dpa_achievements'], $user_id); // Get unlocked achievements $unlocked_achievements = dpa_get_progress(array('author' => $user_id, 'post_status' => dpa_get_unlocked_status_id())); $old_unlocked_achievements = wp_list_pluck($unlocked_achievements, 'post_parent'); $new_unlocked_achievements = array_filter(wp_parse_id_list($_POST['dpa_user_achievements'])); // Figure out which achievements to add or remove $achievements_to_add = array_diff($new_unlocked_achievements, $old_unlocked_achievements); $achievements_to_remove = array_diff($old_unlocked_achievements, $new_unlocked_achievements); // Remove achievements :( if (!empty($achievements_to_remove)) { foreach ($achievements_to_remove as $achievement_id) { dpa_delete_achievement_progress($achievement_id, $user_id); } } // Award achievements! :D if (!empty($achievements_to_add)) { // Get achievements to add $new_achievements = dpa_get_achievements(array('post__in' => $achievements_to_add, 'posts_per_page' => count($achievements_to_add))); // Get any still-locked progress for this user $existing_progress = dpa_get_progress(array('author' => $user_id, 'post_status' => dpa_get_locked_status_id())); foreach ($new_achievements as $achievement_obj) { $progress_obj = array(); // If we have existing progress, pass that to dpa_maybe_unlock_achievement(). foreach ($existing_progress as $progress) { if ($achievement_obj->ID === $progress->post_parent) { $progress_obj = $progress; break; } } dpa_maybe_unlock_achievement($user_id, 'skip_validation', $progress_obj, $achievement_obj); } } // If multisite and running network-wide, undo the switch_to_blog if (is_multisite() && dpa_is_running_networkwide()) { restore_current_blog(); } }