/** * Single Achievement Shortcode. * * @since 1.0.0 * * @param array $atts Shortcode attributes. * @return string HTML markup. */ function badgeos_achievement_shortcode($atts = array()) { // get the post id $atts = shortcode_atts(array('id' => get_the_ID()), $atts, 'badgeos_achievement'); // return if post id not specified if (empty($atts['id'])) { return; } wp_enqueue_style('badgeos-front'); wp_enqueue_script('badgeos-achievements'); // get the post content and format the badge display $achievement = get_post($atts['id']); $output = ''; // If we're dealing with an achievement post if (badgeos_is_achievement($achievement)) { $output .= '<div id="badgeos-single-achievement-container" class="badgeos-single-achievement">'; // necessary for the jquery click handler to be called $output .= badgeos_render_achievement($achievement); $output .= '</div>'; } // Return our rendered achievement return $output; }
public function achievements() { global $blog_id, $json_api; $type = badgeos_get_achievement_types_slugs(); // Drop steps from our list of "all" achievements $step_key = array_search('step', $type); if ($step_key) { unset($type[$step_key]); } $type[] = 'submission'; //$user_id = get_current_user_id(); // Get the current user if one wasn't specified if (!$user_id) { if ($json_api->query->user_id) { $user_id = $json_api->query->user_id; } else { return array("message" => "No user_id"); } } // Get submissions $args = array('post_type' => 'submission', 'posts_per_page' => -1, 'author' => $user_id, 'post_status' => 'publish', 'fields' => 'ids'); $sub_arg = $args; $submissions = get_posts($args); $hidden = badgeos_get_hidden_achievement_ids($type); // Initialize our output and counters $achievements = array(); $achievement_count = 0; // Grab our earned badges (used to filter the query) $earned_ids = badgeos_get_user_earned_achievement_ids($user_id, $type); $earned_ids = array_map('intval', $earned_ids); // Query Achievements $args = array('post_type' => $type, 'posts_per_page' => -1, 'post_status' => 'publish'); $args['post__in'] = array_merge(array(0), $earned_ids); $exclude = array(); // exclude badges which are submissions if (!empty($submissions)) { foreach ($submissions as $sub_id) { $exclude[] = absint(get_post_meta($sub_id, '_badgeos_submission_achievement_id', true)); $args['post__in'][] = $sub_id; } $args['post__in'] = array_diff($args['post__in'], $exclude); } // Loop Achievements $achievement_posts = new WP_Query($args); $query_count += $achievement_posts->found_posts; $base_url = site_url() . '/' . get_option('json_api_base', 'api') . '/badge/assertion/?uid='; $pushed_badges = ($pushed_items = get_user_meta(absint($user_id), '_badgeos_backpack_pushed')) ? (array) $pushed_items : array(); while ($achievement_posts->have_posts()) { $achievement_posts->the_post(); $achievement_id = get_the_ID(); if (!in_array($achievement_id, $hidden)) { $uid = $achievement_id . "-" . get_post_time('U', true) . "-" . $user_id; $button_text = !in_array($base_url . $uid, $pushed_badges) ? __('Send to Mozilla Backpack', 'badgeos_obi_issuer') : __('Resend to Mozilla Backpack', 'badgeos_obi_issuer'); $badge_html = get_post_type() === 'submission' ? badgeos_render_achievement(get_post_meta(get_the_ID(), '_badgeos_submission_achievement_id', true)) : badgeos_render_achievement($achievement_id); $badge_html .= '<div class="badgeos_backpack_action">'; $badge_html .= '<a href="" class="badgeos_backpack button" data-uid="' . $base_url . $uid . '">' . $button_text . '</a> '; $badge_html .= '<input type="checkbox" value="' . $base_url . $uid . '" name="badgeos_backpack_issues[]" />'; $badge_html .= '</div>'; $achievements[] = array("uid" => $base_url . $uid, "type" => get_post_type($achievement_id), "data" => $badge_html); $achievement_count++; } } return array("achievements" => $achievements, "count" => $achievement_count); }
/** * AJAX Helper for returning achievements * * @since 1.0.0 * @return void */ function badgeos_ajax_get_achievements() { global $user_ID, $blog_id; // Setup our AJAX query vars $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : false; $limit = isset($_REQUEST['limit']) ? $_REQUEST['limit'] : false; $offset = isset($_REQUEST['offset']) ? $_REQUEST['offset'] : false; $count = isset($_REQUEST['count']) ? $_REQUEST['count'] : false; $filter = isset($_REQUEST['filter']) ? $_REQUEST['filter'] : false; $search = isset($_REQUEST['search']) ? $_REQUEST['search'] : false; $user_id = isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : false; $orderby = isset($_REQUEST['orderby']) ? $_REQUEST['orderby'] : false; $order = isset($_REQUEST['order']) ? $_REQUEST['order'] : false; $wpms = isset($_REQUEST['wpms']) ? $_REQUEST['wpms'] : false; $include = isset($_REQUEST['include']) ? $_REQUEST['include'] : array(); $exclude = isset($_REQUEST['exclude']) ? $_REQUEST['exclude'] : array(); $meta_key = isset($_REQUEST['meta_key']) ? $_REQUEST['meta_key'] : ''; $meta_value = isset($_REQUEST['meta_value']) ? $_REQUEST['meta_value'] : ''; // Convert $type to properly support multiple achievement types if ('all' == $type) { $type = badgeos_get_achievement_types_slugs(); // Drop steps from our list of "all" achievements $step_key = array_search('step', $type); if ($step_key) { unset($type[$step_key]); } } else { $type = explode(',', $type); } // Get the current user if one wasn't specified if (!$user_id) { $user_id = $user_ID; } // Build $include array if (!is_array($include)) { $include = explode(',', $include); } // Build $exclude array if (!is_array($exclude)) { $exclude = explode(',', $exclude); } // Initialize our output and counters $achievements = ''; $achievement_count = 0; $query_count = 0; // Grab our hidden badges (used to filter the query) $hidden = badgeos_get_hidden_achievement_ids($type); // If we're polling all sites, grab an array of site IDs if ($wpms && $wpms != 'false') { $sites = badgeos_get_network_site_ids(); } else { $sites = array($blog_id); } // Loop through each site (default is current site only) foreach ($sites as $site_blog_id) { // If we're not polling the current site, switch to the site we're polling if ($blog_id != $site_blog_id) { switch_to_blog($site_blog_id); } // Grab our earned badges (used to filter the query) $earned_ids = badgeos_get_user_earned_achievement_ids($user_id, $type); // Query Achievements $args = array('post_type' => $type, 'orderby' => $orderby, 'order' => $order, 'posts_per_page' => $limit, 'offset' => $offset, 'post_status' => 'publish', 'post__not_in' => array_diff($hidden, $earned_ids)); // Filter - query completed or non completed achievements if ($filter == 'completed') { $args['post__in'] = array_merge(array(0), $earned_ids); } elseif ($filter == 'not-completed') { $args['post__not_in'] = array_merge($hidden, $earned_ids); } if ('' !== $meta_key && '' !== $meta_value) { $args['meta_key'] = $meta_key; $args['meta_value'] = $meta_value; } // Include certain achievements if (!empty($include)) { $args['post__not_in'] = array_diff($args['post__not_in'], $include); $args['post__in'] = array_merge(array(0), array_diff($include, $args['post__in'])); } // Exclude certain achievements if (!empty($exclude)) { $args['post__not_in'] = array_merge($args['post__not_in'], $exclude); } // Search if ($search) { $args['s'] = $search; } // Loop Achievements $achievement_posts = new WP_Query($args); $query_count += $achievement_posts->found_posts; while ($achievement_posts->have_posts()) { $achievement_posts->the_post(); $achievements .= badgeos_render_achievement(get_the_ID()); $achievement_count++; } // Sanity helper: if we're filtering for complete and we have no // earned achievements, $achievement_posts should definitely be false /*if ( 'completed' == $filter && empty( $earned_ids ) ) $achievements = '';*/ // Display a message for no results if (empty($achievements)) { // If we have exactly one achivement type, get its plural name, otherwise use "achievements" $post_type_plural = 1 == count($type) ? get_post_type_object(current($type))->labels->name : __('achievements', 'badgeos'); // Setup our completion message $achievements .= '<div class="badgeos-no-results">'; if ('completed' == $filter) { $achievements .= '<p>' . sprintf(__('No completed %s to display at this time.', 'badgeos'), strtolower($post_type_plural)) . '</p>'; } else { $achievements .= '<p>' . sprintf(__('No %s to display at this time.', 'badgeos'), strtolower($post_type_plural)) . '</p>'; } $achievements .= '</div><!-- .badgeos-no-results -->'; } if ($blog_id != $site_blog_id) { // Come back to current blog restore_current_blog(); } } // Send back our successful response wp_send_json_success(array('message' => $achievements, 'offset' => $offset + $limit, 'query_count' => $query_count, 'badge_count' => $achievement_count, 'type' => $type)); }