/** * The Progress post type loop. * * @param array|string $args All the arguments supported by {@link WP_Query}, and some more. * @return bool Returns true if the query has any results to loop over * @since Achievements (3.0) */ function dpa_has_progress($args = array()) { // If multisite and running network-wide, switch_to_blog to the data store site if (is_multisite() && dpa_is_running_networkwide()) { switch_to_blog(DPA_DATA_STORE); } $defaults = array('ignore_sticky_posts' => true, 'max_num_pages' => false, 'order' => 'DESC', 'orderby' => 'date', 'paged' => dpa_get_paged(), 'post_status' => dpa_get_unlocked_status_id(), 'post_type' => dpa_get_progress_post_type(), 's' => '', 'author' => dpa_is_single_user_achievements() ? dpa_get_displayed_user_id() : null, 'post_parent' => dpa_is_single_achievement() ? dpa_get_achievement_id() : null, 'posts_per_page' => -1, 'ach_populate_achievements' => dpa_is_single_user_achievements() && is_a(achievements()->achievement_query, 'WP_Query') && empty(achievements()->achievement_query->request)); $args = dpa_parse_args($args, $defaults, 'has_progress'); // Run the query achievements()->progress_query = new WP_Query($args); // If no limit to posts per page, set it to the current post_count if (-1 === (int) $args['posts_per_page']) { $args['posts_per_page'] = achievements()->progress_query->post_count; } // Add pagination values to query object achievements()->progress_query->posts_per_page = $args['posts_per_page']; achievements()->progress_query->paged = $args['paged']; // Only add pagination if query returned results if (((int) achievements()->progress_query->post_count || (int) achievements()->progress_query->found_posts) && (int) achievements()->progress_query->posts_per_page) { // Limit the number of achievements shown based on maximum allowed pages if (!empty($args['max_num_pages']) && achievements()->progress_query->found_posts > achievements()->progress_query->max_num_pages * achievements()->progress_query->post_count) { achievements()->progress_query->found_posts = achievements()->progress_query->max_num_pages * achievements()->progress_query->post_count; } // If pretty permalinks are enabled, make our pagination pretty if ($GLOBALS['wp_rewrite']->using_permalinks()) { // Page or single post if (is_page() || is_single()) { $base = get_permalink(); } elseif (dpa_is_single_user_achievements()) { $base = dpa_get_user_avatar_link(array('type' => 'url', 'user_id' => dpa_get_displayed_user_id())); } else { $base = get_permalink($args['post_parent']); } // Use pagination base $base = trailingslashit($base) . user_trailingslashit($GLOBALS['wp_rewrite']->pagination_base . '/%#%/'); // Unpretty pagination } else { $base = add_query_arg('paged', '%#%'); } // Pagination settings with filter $progress_pagination = apply_filters('dpa_progress_pagination', array('base' => $base, 'current' => (int) achievements()->progress_query->paged, 'format' => '', 'mid_size' => 1, 'next_text' => is_rtl() ? '←' : '→', 'prev_text' => is_rtl() ? '→' : '←', 'total' => $args['posts_per_page'] == achievements()->progress_query->found_posts ? 1 : ceil((int) achievements()->progress_query->found_posts / (int) $args['posts_per_page']))); // Add pagination to query object achievements()->progress_query->pagination_links = paginate_links($progress_pagination); // Remove first page from pagination achievements()->progress_query->pagination_links = str_replace($GLOBALS['wp_rewrite']->pagination_base . "/1/'", "'", achievements()->progress_query->pagination_links); } // If on a user's achievements page, we need to fetch the achievements if ($args['ach_populate_achievements'] && achievements()->progress_query->have_posts()) { $achievement_ids = wp_list_pluck((array) achievements()->progress_query->posts, 'post_parent'); $achievement_args = array('order' => $args['order'], 'orderby' => $args['orderby'], 'post__in' => $achievement_ids, 'posts_per_page' => -1); // Run the query dpa_has_achievements($achievement_args); } // If multisite and running network-wide, undo the switch_to_blog if (is_multisite() && dpa_is_running_networkwide()) { restore_current_blog(); } return apply_filters('dpa_has_progress', achievements()->progress_query->have_posts()); }
/** * Output the avatar link of a user * * @param array $args See dpa_get_user_avatar_link() documentation. * @since Achievements (3.0) */ function dpa_user_avatar_link($args = array()) { echo dpa_get_user_avatar_link($args); }
/** * Return a fancy description of the achievements on the site, including the * number of public and hidden achievements, and the username and avatar of * the most recent person who unlocked the achievement. * * @param mixed $args This function supports these arguments: * - before: Before the text * - after: After the text * - size: Size of the avatar * @return string Fancy description * @since Achievements (3.0) */ function dpa_get_achievements_index_description($args = '') { $defaults = array('after' => '</p></div>', 'before' => '<div class="dpa-template-notice info"><p class="dpa-achievements-description">', 'size' => 14); $r = dpa_parse_args($args, $defaults, 'get_achievements_index_description'); extract($r); // Get count of total achievements $achievement_count = dpa_get_total_achievement_count(); $achievement_text = sprintf(_n('%s achievement', '%s achievements', $achievement_count, 'achievements'), number_format_i18n($achievement_count)); // Get data on the most recent unlocked achievement $recent_achievement_id = dpa_stats_get_last_achievement_id(); $recent_user_id = dpa_stats_get_last_achievement_user_id(); if (!empty($recent_user_id) && !empty($recent_achievement_id)) { // Check user ID is still valid $user = get_userdata($recent_user_id); if (!empty($user) && dpa_is_user_active($user)) { // Check achievement ID is valid $achievement = get_post($recent_achievement_id); if (!empty($achievement) && 'publish' === $achievement->post_status) { // Combine all the things to build the output text $retstr = sprintf(__('This site has %1$s, and the last unlocked was <a href="%2$s">%3$s</a> by %4$s.', 'achievements'), $achievement_text, esc_url(get_permalink($achievement->ID)), esc_html(apply_filters('dpa_get_achievement_title', $achievement->post_title, $achievement->ID)), dpa_get_user_avatar_link(array('size' => $size, 'user_id' => $user->ID))); } } } // If we haven't set a more specific description, fall back to the default. if (!isset($retstr)) { $retstr = sprintf(__('This site has %1$s.', 'achievements'), $achievement_text); } $retstr = $before . $retstr . $after; return apply_filters('dpa_get_achievements_index_description', $retstr, $args); }
/** * Add an "achievements" item to the Toolbar "profile" submenu (which is added by BuddyPress). * * @param string|array $wp_admin_menus Optional (not used in this function). * @since Achievements (3.2) */ public function setup_admin_bar($wp_admin_nav = '') { $wp_admin_nav = array(); // Add "achievements" menu under "profile" if (is_user_logged_in()) { $wp_admin_nav[] = array('href' => dpa_get_user_avatar_link(array('type' => 'url')), 'id' => 'my-account-' . $this->id . '-edit', 'parent' => 'my-account-xprofile', 'title' => __('Achievements', 'achievements')); } parent::setup_admin_bar($wp_admin_nav); }
/** * Username column * * @param WP_User $user A singular item (one full row) * @see WP_List_Table::single_row_columns() * @since Achievements (3.0) */ public function column_username(WP_User $user) { $avatar = get_avatar($user->ID, 32); $url = dpa_get_user_avatar_link(array('type' => 'url', 'user_id' => $user->ID)); printf('%1$s <strong><a href="%2$s">%3$s</a></strong>', $avatar, esc_url($url), $user->user_login); }
/** * Register custom menu items * * @global WP_Admin_Bar $wp_admin_bar * @since Achievements (3.0) */ public function admin_bar_menu() { global $wp_admin_bar; if (!dpa_is_user_active()) { return; } $wp_admin_bar->add_node(array('href' => dpa_get_user_avatar_link('type=url'), 'id' => 'dpa_my_achievements', 'parent' => 'user-actions', 'title' => _x('My Achievements', 'Menu item in the toolbar', 'achievements'))); }
<ul aria-live="polite" id="dpa-toaster" role="status" style="display: none"> <h1><?php _e('Achievement unlocked!', 'achievements'); ?> </h1> </ul> </script> <script type="text/html" id="tmpl-achievements-item"> <li class="dpa-toast" id="dpa-toast-id-{{ data.ID }}"> <# if (data.image_url) { #> <a href="{{ data.permalink }}"><img class="attachment-medium dpa-toast-image" src="{{ data.image_url }}" style="width: {{ data.image_width }}px" /></a> <# } #> <h2>{{ data.title }}</h2> <p> <?php // translators: "{{ data.title }}" will be replaced with the name of the achievement; leave this bit exactly as is. _e("Hey, you’ve unlocked the “{{ data.title }}” achievement. Congratulations!", 'achievements'); ?> </p> <p><a class="dpa-toast-cta" href="<?php echo esc_url(dpa_get_user_avatar_link('type=url&user_id=' . get_current_user_id())); ?> "><?php _e('See your other achievements', 'achievements'); ?> </a></p> </li> </script>