/**
 * Check if user may access/earn achievement.
 *
 * @since  1.0.0
 * @param  integer $user_id        The given user's ID
 * @param  integer $achievement_id The given achievement's post ID
 * @param  string $this_trigger    The trigger
 * @param  integer $site_id        The triggered site id
 * @param  array $args        The triggered args
 * @return bool                    True if user has access, false otherwise
 */
function badgeos_user_has_access_to_achievement($user_id = 0, $achievement_id = 0, $this_trigger = '', $site_id = 0, $args = array())
{
    // Set to current site id
    if (!$site_id) {
        $site_id = get_current_blog_id();
    }
    // Assume we have access
    $return = true;
    // If the achievement is not published, we do not have access
    if ('publish' != get_post_status($achievement_id)) {
        $return = false;
    }
    // If we've exceeded the max earnings, we do not have acces
    if ($return && badgeos_achievement_user_exceeded_max_earnings($user_id, $achievement_id)) {
        $return = false;
    }
    // If we have access, and the achievement has a parent...
    if ($return && ($parent_achievement = badgeos_get_parent_of_achievement($achievement_id))) {
        // If we don't have access to the parent, we do not have access to this
        if (!badgeos_user_has_access_to_achievement($user_id, $parent_achievement->ID, $this_trigger, $site_id, $args)) {
            $return = false;
        }
        // If the parent requires sequential steps, confirm we've earned all previous steps
        if ($return && badgeos_is_achievement_sequential($parent_achievement->ID)) {
            foreach (badgeos_get_children_of_achievement($parent_achievement->ID) as $sibling) {
                // If this is the current step, we're good to go
                if ($sibling->ID == $achievement_id) {
                    break;
                }
                // If we haven't earned any previous step, we can't earn this one
                if (!badgeos_get_user_achievements(array('user_id' => absint($user_id), 'achievement_id' => absint($sibling->ID)))) {
                    $return = false;
                    break;
                }
            }
        }
    }
    // Available filter for custom overrides
    return apply_filters('user_has_access_to_achievement', $return, $user_id, $achievement_id, $this_trigger, $site_id, $args);
}
/**
 * Check if a user has access to the submission form for an achievement.
 *
 * @since  1.3.2
 *
 * @param  integer $user_id        User ID.
 * @param  integer $achievement_id Achievement post ID.
 * @return bool                    True if user has access, otherwise false.
 */
function badgeos_user_has_access_to_submission_form($user_id = 0, $achievement_id = 0)
{
    // Assume the user has access
    $has_access = true;
    // If user is not logged in, they have no access
    if (!absint($user_id)) {
        $has_access = false;
    }
    // If user cannot access achievement, they cannot submit anything
    if ($has_access && badgeos_achievement_user_exceeded_max_earnings($user_id, $achievement_id)) {
        $has_access = false;
    }
    // If the user has access, look for pending submissions
    if ($has_access) {
        $pending_submissions = get_posts(array('post_type' => 'submission', 'author' => absint($user_id), 'post_status' => 'publish', 'meta_query' => array('relation' => 'AND', array('key' => '_badgeos_submission_achievement_id', 'value' => absint($achievement_id)), array('key' => '_badgeos_submission_status', 'value' => 'pending'))));
        // If user has any pending submissions, they do not have access
        if (!empty($pending_submissions)) {
            $has_access = false;
        }
    }
    return apply_filters('badgeos_user_has_access_to_submission_form', $has_access, $user_id, $achievement_id);
}