Exemple #1
0
/**
 * 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());
}
/**
 * Possibly intercept the template being loaded
 *
 * Listens to the 'template_include' filter and waits for any Achievements specific
 * template condition to be met. If one is met and the template file exists it will be used. 
 *
 * @param string $template Optional.
 * @return string The path to the template file that is being used
 * @see dpa_template_include_theme_compat()
 * @since Achievements (3.0)
 */
function dpa_template_include_theme_supports($template = '')
{
    // Single achievement
    if (dpa_is_single_achievement() && ($new_template = dpa_get_single_achievement_template())) {
        // Achievement archive
    } elseif (dpa_is_achievement_archive() && ($new_template = dpa_get_achievement_archive_template())) {
        // User achievements page
    } elseif (dpa_is_single_user_achievements() && ($new_template = dpa_get_single_user_achievements_template())) {
    }
    // An Achievements template file was located, so override the WordPress template, and use it to switch off Achievements' theme compatibility.
    if (!empty($new_template)) {
        $template = dpa_set_template_included($new_template);
    }
    return apply_filters('dpa_template_include_theme_supports', $template);
}
Exemple #3
0
/**
 * Displays achievement notices
 *
 * @since Achievements (3.0)
 */
function dpa_achievement_notices()
{
    // Bail if not viewing an achievement
    if (!dpa_is_single_achievement()) {
        return;
    }
    // Filter notice text and bail if empty
    $notice_text = apply_filters('dpa_achievement_notices', '', dpa_get_achievement_id());
    if (empty($notice_text)) {
        return;
    }
    dpa_add_error('achievement_notice', $notice_text, 'message');
}
Exemple #4
0
/**
 * Custom page title for Achievements pages
 *
 * @param string $title Optional. The title (not used).
 * @param string $sep Optional, default is '»'. How to separate each part within the page title.
 * @param string $seplocation Optional. Direction to display title, 'right'.
 * @return string The title
 * @since Achievements (3.0)
 */
function dpa_title($title = '', $sep = '»', $seplocation = '')
{
    $new_title = array();
    // Achievement archive
    if (dpa_is_achievement_archive()) {
        $new_title['text'] = dpa_get_achievement_archive_title();
        // Single achievement page
    } elseif (dpa_is_single_achievement()) {
        $new_title['text'] = dpa_get_achievement_title();
        $new_title['format'] = esc_attr__('Achievement: %s', 'achievements');
    }
    $new_title = apply_filters('dpa_raw_title_array', $new_title);
    $new_title = dpa_parse_args($new_title, array('format' => '%s', 'text' => $title), 'title');
    // Get the formatted raw title
    $new_title = sprintf($new_title['format'], $new_title['text']);
    $new_title = apply_filters('dpa_raw_title', $new_title, $sep, $seplocation);
    // Compare new title with original title
    if ($new_title === $title) {
        return $title;
    }
    // Temporary separator for accurate flipping, if necessary
    $t_sep = '%WP_TITILE_SEP%';
    $prefix = '';
    if (!empty($new_title)) {
        $prefix = " {$sep} ";
    }
    // Separate on right, so reverse the order
    if ('right' === $seplocation) {
        $new_title_array = explode($t_sep, $new_title);
        $new_title_array = array_reverse($new_title_array);
        $new_title = implode(" {$sep} ", $new_title_array) . $prefix;
        // Separate on left, do not reverse
    } else {
        $new_title_array = explode($t_sep, $new_title);
        $new_title = $prefix . implode(" {$sep} ", $new_title_array);
    }
    // Filter and return
    return apply_filters('dpa_title', $new_title, $sep, $seplocation);
}
/**
 * Remove the canonical redirect to allow pretty pagination
 *
 * @global unknown $wp_rewrite
 * @param string $redirect_url Redirect url
 * @return bool|string False if it's an achievement archive or post, and on the first page of pagination, otherwise the redirect url.
 * @since Achievements (3.0)
 */
function dpa_redirect_canonical($redirect_url)
{
    global $wp_rewrite;
    // Canonical is for the beautiful
    if ($wp_rewrite->using_permalinks()) {
        // If viewing beyond page 1 of several
        if (1 < dpa_get_paged()) {
            // On a single achievement
            if (dpa_is_single_achievement()) {
                $redirect_url = false;
                // ...and any single anything else...
            } elseif (is_page() || is_singular()) {
                $redirect_url = false;
            }
        }
    }
    return $redirect_url;
}