/** * Returns array of achievement types a user has earned across a multisite network * * @since 1.2.0 * @param integer $user_id The user's ID * @return array An array of post types */ function badgeos_get_network_achievement_types_for_user($user_id) { global $blog_id; // Store a copy of the original ID for later $cached_id = $blog_id; // Assume we have no achievement types $all_achievement_types = array(); // Loop through all active sites $sites = badgeos_get_network_site_ids(); foreach ($sites as $site_blog_id) { // If we're polling a different blog, switch to it if ($blog_id != $site_blog_id) { switch_to_blog($site_blog_id); } // Merge earned achievements to our achievement type array $achievement_types = badgeos_get_user_earned_achievement_types($user_id); if (is_array($achievement_types)) { $all_achievement_types = array_merge($achievement_types, $all_achievement_types); } } if (is_multisite()) { // Restore the original blog so the sky doesn't fall switch_to_blog($cached_id); } // Pare down achievement type list so we return no duplicates $achievement_types = array_unique($all_achievement_types); // Return all found achievements return $achievement_types; }
/** * 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)); }