예제 #1
0
/**
 * Get an achievement's children posts
 *
 * @since  1.0.0
 * @param  integer $achievement_id The given achievment's post ID
 * @return array                   An array of our achievment's children (empty if none)
 */
function badgeos_get_children_of_achievement($achievement_id = 0)
{
    // Grab the current post ID if no achievement_id was specified
    if (!$achievement_id) {
        global $post;
        $achievement_id = $post->ID;
    }
    // Grab and return our achievement's children
    return badgeos_get_achievements(array('children_of' => $achievement_id, 'achievement_relationship' => 'required'));
}
/**
 * Check if a user has unlocked all achievements of a given type
 *
 * Triggers hook badgeos_unlock_all_{$post_type}
 *
 * @since  1.2.0
 * @param  integer $user_id        The given user's ID
 * @param  integer $achievement_id The given achievement's post ID
 * @return void
 */
function badgeos_maybe_trigger_unlock_all($user_id = 0, $achievement_id = 0)
{
    // Grab our user's (presumably updated) earned achievements
    $earned_achievements = badgeos_get_user_achievements(array('user_id' => $user_id));
    // Get the post type of the earned achievement
    $post_type = get_post_type($achievement_id);
    // Hook for unlocking all achievements of this achievement type
    if ($all_achievements_of_type = badgeos_get_achievements(array('post_type' => $post_type))) {
        // Assume we can award the user for unlocking all achievements of this type
        $all_per_type = true;
        // Loop through each of our achievements of this type
        foreach ($all_achievements_of_type as $achievement) {
            // Assume the user hasn't earned this achievement
            $found_achievement = false;
            // Loop through each eacrned achivement and see if we've earned it
            foreach ($earned_achievements as $earned_achievement) {
                if ($earned_achievement->ID == $achievement->ID) {
                    $found_achievement = true;
                    break;
                }
            }
            // If we haven't earned this single achievement, we haven't earned them all
            if (!$found_achievement) {
                $all_per_type = false;
                break;
            }
        }
        // If we've earned all achievements of this type, trigger our hook
        if ($all_per_type) {
            do_action('badgeos_unlock_all_' . $post_type, $user_id, $achievement_id);
        }
    }
}