function widget($args, $instance)
 {
     global $user_ID;
     extract($args);
     echo $before_widget;
     $title = apply_filters('widget_title', $instance['title']);
     if (!empty($title)) {
         echo $before_title . $title . $after_title;
     }
     //user must be logged in to view earned badges and points
     if (is_user_logged_in()) {
         //display user's points if widget option is enabled
         if ($instance['point_total'] == 'on') {
             echo '<p class="badgeos-total-points">' . sprintf(__('My Total Points: %s', 'badgeos'), '<strong>' . number_format(badgeos_get_users_points()) . '</strong>') . '</p>';
         }
         $achievements = badgeos_get_user_achievements();
         if (is_array($achievements) && !empty($achievements)) {
             $number_to_show = absint($instance['number']);
             $thecount = 0;
             wp_enqueue_script('badgeos-achievements');
             wp_enqueue_style('badgeos-widget');
             //load widget setting for achievement types to display
             $set_achievements = isset($instance['set_achievements']) ? $instance['set_achievements'] : '';
             //show most recently earned achievement first
             $achievements = array_reverse($achievements);
             echo '<ul class="widget-achievements-listing">';
             foreach ($achievements as $achievement) {
                 //verify achievement type is set to display in the widget settings
                 //if $set_achievements is not an array it means nothing is set so show all achievements
                 if (!is_array($set_achievements) || in_array($achievement->post_type, $set_achievements)) {
                     //exclude step CPT entries from displaying in the widget
                     if (get_post_type($achievement->ID) != 'step') {
                         $permalink = get_permalink($achievement->ID);
                         $title = get_the_title($achievement->ID);
                         $img = badgeos_get_achievement_post_thumbnail($achievement->ID, array(50, 50), 'wp-post-image');
                         $thumb = $img ? '<a style="margin-top: -25px;" class="badgeos-item-thumb" href="' . esc_url($permalink) . '">' . $img . '</a>' : '';
                         $class = 'widget-badgeos-item-title';
                         $item_class = $thumb ? ' has-thumb' : '';
                         // Setup credly data if giveable
                         $giveable = credly_is_achievement_giveable($achievement->ID, $user_ID);
                         $item_class .= $giveable ? ' share-credly addCredly' : '';
                         $credly_ID = $giveable ? 'data-credlyid="' . absint($achievement->ID) . '"' : '';
                         echo '<li id="widget-achievements-listing-item-' . absint($achievement->ID) . '" ' . $credly_ID . ' class="widget-achievements-listing-item' . esc_attr($item_class) . '">';
                         echo $thumb;
                         echo '<a class="widget-badgeos-item-title ' . esc_attr($class) . '" href="' . esc_url($permalink) . '">' . esc_html($title) . '</a>';
                         echo '</li>';
                         $thecount++;
                         if ($thecount == $number_to_show && $number_to_show != 0) {
                             break;
                         }
                     }
                 }
             }
             echo '</ul><!-- widget-achievements-listing -->';
         }
     } else {
         //user is not logged in so display a message
         _e('You must be logged in to view earned achievements', 'badgeos');
     }
     echo $after_widget;
 }
 /**
  * Post a users earned badge to Credly
  *
  * @since  1.0.0
  * @param  int  $user_id  The given users ID
  * @param  int  $badge_id The badge ID the user is earning
  * @return string         Results of the API call
  */
 public function post_credly_user_badge($user_id = 0, $badge_id = 0)
 {
     // Bail if the badge isn't in Credly
     if (!credly_is_achievement_giveable($badge_id, $user_id)) {
         return false;
     }
     if (empty($user_id)) {
         $user_id = $this->user_id;
     }
     // Generate our API URL endpoint
     $url = $this->api_url_with_token($this->api_url_user_badge());
     // Generate our args
     $body = $this->post_user_badge_args($user_id, $badge_id);
     // POST our data to the Credly API
     $response = $this->credly_api_post($url, $body);
     // Process our response
     $results = $this->process_api_response_user_badge($response);
     // If post was successful, trigger other actions
     if ($results) {
         do_action('post_credly_user_badge', $user_id, $badge_id, $results);
     }
     return $results;
 }
/**
 * Render an achievement
 *
 * @since  1.0.0
 * @param  integer $achievement The achievement's post ID
 * @return string               Concatenated markup
 */
function badgeos_render_achievement($achievement = 0)
{
    global $user_ID;
    // If we were given an ID, get the post
    if (is_numeric($achievement)) {
        $achievement = get_post($achievement);
    }
    // make sure our JS and CSS is enqueued
    wp_enqueue_script('badgeos-achievements');
    wp_enqueue_style('badgeos-widget');
    // check if user has earned this Achievement, and add an 'earned' class
    $earned_status = badgeos_get_user_achievements(array('user_id' => $user_ID, 'achievement_id' => absint($achievement->ID))) ? 'user-has-earned' : 'user-has-not-earned';
    // Setup our credly classes
    $credly_class = '';
    $credly_ID = '';
    // If the achievement is earned and givable, override our credly classes
    if ('user-has-earned' == $earned_status && ($giveable = credly_is_achievement_giveable($achievement->ID, $user_ID))) {
        $credly_class = ' share-credly addCredly';
        $credly_ID = 'data-credlyid="' . absint($achievement->ID) . '"';
    }
    // Each Achievement
    $output = '';
    $output .= '<div id="badgeos-achievements-list-item-' . $achievement->ID . '" class="badgeos-achievements-list-item ' . $earned_status . $credly_class . '"' . $credly_ID . '>';
    // Achievement Image
    $output .= '<div class="badgeos-item-image">';
    $output .= '<a href="' . get_permalink($achievement->ID) . '">' . badgeos_get_achievement_post_thumbnail($achievement->ID) . '</a>';
    $output .= '</div><!-- .badgeos-item-image -->';
    // Achievement Content
    $output .= '<div class="badgeos-item-description">';
    // Achievement Title
    $output .= '<h2 class="badgeos-item-title"><a href="' . get_permalink($achievement->ID) . '">' . get_the_title($achievement->ID) . '</a></h2>';
    // Achievement Short Description
    $output .= '<div class="badgeos-item-excerpt">';
    $output .= badgeos_achievement_points_markup($achievement->ID);
    $excerpt = !empty($achievement->post_excerpt) ? $achievement->post_excerpt : $achievement->post_content;
    $output .= wpautop(apply_filters('get_the_excerpt', $excerpt));
    $output .= '</div><!-- .badgeos-item-excerpt -->';
    // Render our Steps
    if ($steps = badgeos_get_required_achievements_for_achievement($achievement->ID)) {
        $output .= '<div class="badgeos-item-attached">';
        $output .= '<div id="show-more-' . $achievement->ID . '" class="badgeos-open-close-switch"><a class="show-hide-open" data-badgeid="' . $achievement->ID . '" data-action="open" href="#">' . __('Show Details', 'badgeos') . '</a></div>';
        $output .= '<div id="badgeos_toggle_more_window_' . $achievement->ID . '" class="badgeos-extras-window">' . badgeos_get_required_achievements_for_achievement_list_markup($steps, $achievement->ID) . '</div><!-- .badgeos-extras-window -->';
        $output .= '</div><!-- .badgeos-item-attached -->';
    }
    $output .= '</div><!-- .badgeos-item-description -->';
    $output .= '</div><!-- .badgeos-achievements-list-item -->';
    // Return our filterable markup
    return apply_filters('badgeos_render_achievement', $output, $achievement->ID);
}
/**
 * Output badges on profile
 *
 */
function boss_edu_profile_achievements()
{
    global $user_ID;
    //user must be logged in to view earned badges and points
    if (is_user_logged_in() && function_exists('badgeos_get_user_achievements')) {
        $achievements = badgeos_get_user_achievements(array('user_id' => bp_displayed_user_id()));
        if (is_array($achievements) && !empty($achievements)) {
            $number_to_show = 5;
            $thecount = 0;
            wp_enqueue_script('badgeos-achievements');
            wp_enqueue_style('badgeos-widget');
            //load widget setting for achievement types to display
            $set_achievements = isset($instance['set_achievements']) ? $instance['set_achievements'] : '';
            //show most recently earned achievement first
            $achievements = array_reverse($achievements);
            echo '<ul class="profile-achievements-listing">';
            foreach ($achievements as $achievement) {
                //verify achievement type is set to display in the widget settings
                //if $set_achievements is not an array it means nothing is set so show all achievements
                if (!is_array($set_achievements) || in_array($achievement->post_type, $set_achievements)) {
                    //exclude step CPT entries from displaying in the widget
                    if (get_post_type($achievement->ID) != 'step') {
                        $permalink = get_permalink($achievement->ID);
                        $title = get_the_title($achievement->ID);
                        $img = badgeos_get_achievement_post_thumbnail($achievement->ID, array(50, 50), 'wp-post-image');
                        $thumb = $img ? '<a style="margin-top: -25px;" class="badgeos-item-thumb" href="' . esc_url($permalink) . '">' . $img . '</a>' : '';
                        $class = 'widget-badgeos-item-title';
                        $item_class = $thumb ? ' has-thumb' : '';
                        // Setup credly data if giveable
                        $giveable = credly_is_achievement_giveable($achievement->ID, $user_ID);
                        $item_class .= $giveable ? ' share-credly addCredly' : '';
                        $credly_ID = $giveable ? 'data-credlyid="' . absint($achievement->ID) . '"' : '';
                        echo '<li id="widget-achievements-listing-item-' . absint($achievement->ID) . '" ' . $credly_ID . ' class="widget-achievements-listing-item' . esc_attr($item_class) . '">';
                        echo $thumb;
                        echo '<a class="widget-badgeos-item-title ' . esc_attr($class) . '" href="' . esc_url($permalink) . '">' . esc_html($title) . '</a>';
                        echo '</li>';
                        $thecount++;
                        if ($thecount == $number_to_show && $number_to_show != 0) {
                            echo '<li id="widget-achievements-listing-item-more" class="widget-achievements-listing-item">';
                            echo '<a class="badgeos-item-thumb" href="' . bp_core_get_user_domain(get_current_user_id()) . '/achievements/"><span class="fa fa-ellipsis-h"></span></a>';
                            echo '<a class="widget-badgeos-item-title ' . esc_attr($class) . '" href="' . bp_core_get_user_domain(get_current_user_id()) . '/achievements/">' . __('See All', 'social-learner') . '</a>';
                            echo '</li>';
                            break;
                        }
                    }
                }
            }
            echo '</ul><!-- widget-achievements-listing -->';
        }
    }
}