/**
 * Get archives for content type
 *
 * Specify content type to get taxonomy and month archives.
 * This can be useful for creating taxonomy page templates, dropdown navigation, etc.
 *
 * @since 1.7.1
 * @global object $wp_locale
 * @param array $args Arguments, all optional (see defaults and comments below)
 * @return array Archive data
 */
function ctfw_content_type_archives($args = array())
{
    global $wp_locale;
    // Default arguments
    $args = wp_parse_args($args, array('content_type' => ctfw_current_content_type(), 'specific_archive' => false, 'all_books' => false));
    extract($args);
    // make available as variables
    // Start array
    $archives = array();
    // Blog
    if ('blog' == $content_type) {
        // Categories (alphabetical)
        $taxonomy = 'category';
        if (!$specific_archive || $taxonomy == $specific_archive) {
            $archives[$taxonomy]['items'] = get_terms($taxonomy, array('pad_counts' => true));
        }
        // Tag (biggest first)
        $taxonomy = 'post_tag';
        if (!$specific_archive || $taxonomy == $specific_archive) {
            $archives[$taxonomy]['items'] = get_terms($taxonomy, array('orderby' => 'count', 'order' => 'DESC', 'pad_counts' => true));
        }
        // Months
        if (!$specific_archive || 'months' == $specific_archive) {
            $archives['months']['items'] = ctfw_get_month_archives('post');
        }
    }
    // Sermon
    if ('sermon' == $content_type) {
        // Topics (alphabetical)
        $taxonomy = 'ctc_sermon_topic';
        if (ctfw_ctc_taxonomy_supported($taxonomy) && (!$specific_archive || $taxonomy == $specific_archive)) {
            $archives[$taxonomy]['items'] = get_terms($taxonomy, array('pad_counts' => true));
        }
        // Series (newest first)
        $taxonomy = 'ctc_sermon_series';
        if (ctfw_ctc_taxonomy_supported($taxonomy) && (!$specific_archive || $taxonomy == $specific_archive)) {
            // Cache with transient because getting all series / sermons can be intensive
            // It's possible this function is called more than once during a page load
            // so let's cache it for a few seconds so the queries and loops are not repeated
            $transient_name = 'ctfw_content_type_archives_sermon_series';
            // 45 char max
            $transient = get_transient($transient_name);
            if ($transient) {
                // we have it; let's use it
                $series = $transient;
            } else {
                // Get series terms
                $series_terms = get_terms($taxonomy, array('orderby' => 'id', 'order' => 'DESC', 'pad_counts' => true));
                // Loop series
                $series_ids = array();
                $series = array();
                foreach ($series_terms as $series_term) {
                    // Get series IDs
                    $series_ids[] = $series_term->term_id;
                    // Add term to series array with ID as key
                    $series[$series_term->term_id] = $series_term;
                }
                // Get sermons having a series
                $series_sermons = get_posts(array('posts_per_page' => -1, 'post_type' => 'ctc_sermon', 'tax_query' => array(array('taxonomy' => 'ctc_sermon_series', 'field' => 'id', 'terms' => $series_ids))));
                // Loop sermons
                foreach ($series_sermons as $sermon) {
                    // Get series having this sermon
                    $series_with_sermon = wp_get_post_terms($sermon->ID, 'ctc_sermon_series', array('fields' => 'ids'));
                    // Loop series to add sermon
                    foreach ($series_with_sermon as $series_id) {
                        if (isset($series[$series_id])) {
                            if (!isset($series[$series_id]->sermons)) {
                                $series[$series_id]->sermons = array();
                            }
                            $series[$series_id]->sermons[$sermon->ID] = $sermon;
                        }
                    }
                }
                // Loop series to record latest and earliest sermon dates
                foreach ($series as $series_id => $series_data) {
                    $sermons = $series_data->sermons;
                    if ($sermons) {
                        // Latest sermon
                        $values = array_values($sermons);
                        $latest_sermon = array_shift($values);
                        $series[$series_id]->sermon_latest_date = strtotime($latest_sermon->post_date);
                        // Earliest sermon
                        $values = array_values($sermons);
                        $earliest_sermon = end($values);
                        $series[$series_id]->sermon_earliest_date = strtotime($earliest_sermon->post_date);
                    }
                }
                // Re-order series by latest sermon date
                usort($series, 'ctfw_sort_by_latest_sermon');
                // Cache with transient
                set_transient($transient_name, $series, 15);
                // 15 seconds is more than enough for a regular pageload
            }
            // Add to archives array
            $archives[$taxonomy]['items'] = $series;
        }
        // Book (in order of books in Bible)
        $taxonomy = 'ctc_sermon_book';
        if (ctfw_ctc_taxonomy_supported($taxonomy) && (!$specific_archive || $taxonomy == $specific_archive)) {
            $archives[$taxonomy]['items'] = get_terms($taxonomy, array('pad_counts' => true));
            // Re-order according to books in Bible
            if ($archives[$taxonomy]['items']) {
                $reordered_books = array();
                $unmatched_books = array();
                $bible_books = ctfw_bible_books();
                // Loop books in Bible
                foreach ($bible_books['all'] as $bible_book_key => $bible_book) {
                    // Include this book if found in terms
                    $found_book_term = false;
                    foreach ($archives[$taxonomy]['items'] as $book_term) {
                        if (trim(strtolower($book_term->name)) == strtolower($bible_book['name'])) {
                            // Add book data (testament, alternate names)
                            $book_term->book_data = $bible_book;
                            // Add it
                            $reordered_books[] = $book_term;
                            // Stop looking
                            $found_book_term = true;
                            break;
                        }
                    }
                    // Add book if no term was found and argument for empty books is set
                    if (!$found_book_term && $all_books) {
                        // Add name and count
                        $book_term = new stdClass();
                        $book_term->term_id = '';
                        $book_term->name = $bible_book['name'];
                        $book_term->count = 0;
                        // Add book data (testament, alternate names)
                        $book_term->book_data = $bible_book;
                        // Add it
                        $reordered_books[] = $book_term;
                    }
                }
                // Add those not found to end
                foreach ($archives[$taxonomy]['items'] as $book_term) {
                    // Not added to new array?
                    foreach ($bible_books['all'] as $bible_book_key => $bible_book) {
                        $found = false;
                        // Found it?
                        if ($bible_book['name'] == $book_term->name) {
                            $found = true;
                            break;
                        }
                    }
                    // Not found, append to end
                    if (!$found) {
                        $reordered_books[] = $book_term;
                    }
                }
                // Replace books with reordered array
                $archives[$taxonomy]['items'] = $reordered_books;
            }
        }
        // Speakers -- (by count)
        $taxonomy = 'ctc_sermon_speaker';
        if (ctfw_ctc_taxonomy_supported($taxonomy) && (!$specific_archive || $taxonomy == $specific_archive)) {
            $archives[$taxonomy]['items'] = get_terms($taxonomy, array('pad_counts' => true));
        }
        // Months
        if (!$specific_archive || 'months' == $specific_archive) {
            $archives['months']['items'] = ctfw_get_month_archives('ctc_sermon');
        }
    }
    // Event
    if ('event' == $content_type) {
        // Category (alphabetical)
        $taxonomy = 'ctc_event_category';
        if (ctfw_ctc_taxonomy_supported($taxonomy) && (!$specific_archive || $taxonomy == $specific_archive)) {
            $archives[$taxonomy]['items'] = get_terms($taxonomy, array('pad_counts' => false));
        }
        // Months
        // Each theme decides how to handle event archives, if any (such as on a monthly calendar via page template)
        // Therefore, ctfw_content_types must be filtered by theme to add correct URL format for month archives
        if (!$specific_archive || 'months' == $specific_archive) {
            $url_format = ctfw_content_type_data('event', 'month_archive_url_format');
            if (ctfw_is_url($url_format)) {
                // valid URL (e.g. page for a page template exists)
                // Date info
                $month_limit = apply_filters('ctfw_content_type_archives_event_month_limit', 12);
                // show up to X months into the future
                $year_month = date_i18n('Y-m');
                // start with current
                $DateTime = new DateTime($year_month);
                // Loop next X months
                $months_looped = 0;
                while ($months_looped < $month_limit) {
                    // Add month to archives array if has events
                    $count = ctfw_month_events_count($year_month);
                    // get number of event occurences in month
                    if ($count) {
                        // Date
                        $month_ts = strtotime($year_month);
                        $month = date_i18n('n', $month_ts);
                        // e.g. 1
                        $year = date_i18n('Y', $month_ts);
                        // e.g.  2015
                        // Name
                        // 'name' that is automatically localized (key matches taxonomy term object)
                        /* translators: 1: month name, 2: 4-digit year */
                        $name = sprintf(_x('%1$s %2$d', 'month archive', 'church-theme-framework'), $wp_locale->get_month($month), $year);
                        // URL
                        $url = ctfw_events_month_archive_url($year_month);
                        // Add data
                        // Use same format as ctfw_get_month_archives()
                        $archives['months']['items'][$months_looped] = new stdClass();
                        $archives['months']['items'][$months_looped]->year = $year;
                        $archives['months']['items'][$months_looped]->month = $month;
                        $archives['months']['items'][$months_looped]->count = $count;
                        $archives['months']['items'][$months_looped]->post = $count;
                        $archives['months']['items'][$months_looped]->name = $name;
                        $archives['months']['items'][$months_looped]->url = $url;
                    }
                    // Next month
                    $DateTime->modify('+1 month');
                    $year_month = $DateTime->format('Y-m');
                    // PHP 5.2 cannot chain methods
                    $months_looped++;
                }
            }
        }
    }
    // People
    if ('people' == $content_type) {
        // Groups (alphabetical)
        $taxonomy = 'ctc_person_group';
        if (ctfw_ctc_taxonomy_supported($taxonomy) && (!$specific_archive || $taxonomy == $specific_archive)) {
            $archives[$taxonomy]['items'] = get_terms($taxonomy, array('pad_counts' => true));
        }
    }
    // Loop archives
    // Remove those with no items
    // Add archive name and URLs to terms
    foreach ($archives as $archive_key => $archive) {
        // No items, remove archive
        if (empty($archives[$archive_key]['items'])) {
            unset($archives[$archive_key]);
        } else {
            // Month archive
            if ($archive_key == 'months') {
                // Type
                $archives[$archive_key]['type'] = 'months';
                // Name
                $archives[$archive_key]['name'] = _x('Months', 'content type archives', 'church-theme-framework');
            } else {
                // Type
                $archives[$archive_key]['type'] = 'taxonomy';
                // Name
                $taxonomy_data = get_taxonomy($archive_key);
                if (!empty($taxonomy_data->labels->menu_name)) {
                    $archives[$archive_key]['name'] = $taxonomy_data->labels->menu_name;
                    // e.g. "Topics" instead of "Sermon Topics"
                } else {
                    // should never happen, but just in case
                    $archives[$archive_key]['name'] = isset($taxonomy_data->labels->name) ? $taxonomy_data->labels->name : ctfw_make_friendly($archive_key);
                }
                // Loop items
                $archive_items = $archive['items'];
                foreach ($archive['items'] as $archive_item_key => $archive_item) {
                    $archives[$archive_key]['items'][$archive_item_key]->url = !empty($archive_item->term_id) ? get_term_link($archive_item) : '';
                }
            }
            // Move items to end of array so name, etc. is first
            $items = $archives[$archive_key]['items'];
            unset($archives[$archive_key]['items']);
            $archives[$archive_key]['items'] = $items;
        }
    }
    // Specific Archive
    if ($specific_archive) {
        $archives = isset($archives[$specific_archive]) ? $archives[$specific_archive] : array();
    }
    // Make filterable
    $archives = apply_filters('ctfw_content_type_archives', $archives, $args);
    $archives = apply_filters('ctfw_content_type_archives-' . $content_type, $archives, $args);
    return $archives;
}
Exemplo n.º 2
0
/**
 * Get content template
 *
 * Loads content-*.php according to post type, post format and whether or not is singular (full) or not (short).
 * Templates will be loaded from partials directory first (if exist); otherwise from root
 *
 * The order in which templates existence is checked is from most specific to least specific.
 * For example, content-post-short.php is checked for before content-post.php and content-post-audio-short.php before content-post-audio.php.
 *
 * These examples are NOT listed in order of priority (read above):
 *
 * content-post.php 				Standard blog post - is_singular or not
 * content-post-full.php 			Standard blog post - is_singular
 * content-post-short.php 			Standard blog post - not is_singular
 * content-post-audio.php			Blog post using audio post format - is_singular or not
 * content-post-audio-full.php		Blog post using audio post format - is_singular
 * content-post-audio-short.php		Blog post using audio post format - not is_singular
 * content-audio.php 				Same as above (but be careful, an 'audio' post type may use this) - is_singular or not
 * content-audio-full.php 			Same as above (but be careful, an 'audio' post type may use this) - is_singular
 * content-audio-short.php 			Same as above (but be careful, an 'audio' post type may use this) - not is_singular
 * content-post-type.php 			Custom post type 'ctc_post_type' made friendly - is_singular or not
 * content-post-type-full.php 		Custom post type 'ctc_post_type' made friendly - is_singular
 * content-post-type-short.php 		Custom post type 'ctc_post_type' made friendly - not is_singular
 * content-ctc_post_type.php  		Same as above but using actual name - is_singular or not
 * content-ctc_post_type-full.php  Same as above but using actual name - is_singular
 * content-ctc_post_type-short.php  Same as above but using actual name - not is_singular
 * content-attachment.php 			Media attachment
 *
 * Now here's an example of how prioerity loading works. *-single and *-full are available depending on
 * whether or not is_singular is or is not true. This example is for when is_singular is not true. If it
 * were true, -full would be checked for instead -short.
 *
 * partials/content-sermon-short.php (or *-full if is_singular)
 * partials/content-sermon.php
 * partials/content-ctc_sermon-short.php
 * partials/content-ctc_sermon.php
 * partials/content-short.php
 * partials/content.php
 * content-sermon-short.php
 * content-sermon.php
 * content-ctc_sermon-short.php
 * content-ctc_sermon.php
 * content-short.php
 * content.php
 *
 * One strategy is to use content-sermon.php for both single (full) and archive (short)
 * by checking is_singular( get_post_type() ) in the partial, which may save on redundant code.
 * The pro of separate partials is better organization and easier child theme overrides.
 *
 * This is based on Justin Tadlock's Hybrid Base 0.1 hybrid_base_get_content_template()
 * function: https://github.com/justintadlock/hybrid-base/blob/0.1/functions.php#L154
 *
 * @since 0.9
 * @return string Template file name if template loaded
 */
function ctfw_get_content_template()
{
    // Templates will be attempted to be loaded in the order they are added to this array
    $templates = array();
    // Get post type
    $post_type = get_post_type();
    $post_type_friendly = ctfw_make_friendly($post_type);
    // "ctc_post_type" is made into "post-type" for friendlier template naming
    // Singular post?
    $singular = is_singular($post_type) ? true : false;
    // Does post type support post formats?
    if (post_type_supports($post_type, 'post-formats')) {
        // Get post format
        $post_format = get_post_format();
        // Has post format
        if ($post_format) {
            // First check for something like content-post-audio.php (blog post using audio post format)
            if ($singular) {
                $templates[] = "content-{$post_type}-{$post_format}-full";
            }
            if (!$singular) {
                $templates[] = "content-{$post_type}-{$post_format}-short";
            }
            $templates[] = "content-{$post_type}-{$post_format}";
            // If that doesn't exist, check simply for content-audio.php (shorter but may conflict with post type name)
            if ($singular) {
                $templates[] = "content-{$post_format}-full";
            }
            if (!$singular) {
                $templates[] = "content-{$post_format}-short";
            }
            $templates[] = "content-{$post_format}";
        }
    }
    // If no post format, load content-post-type.php, where "post-type" is a friendly version of "ctc_post_type"
    if ($post_type_friendly != $post_type) {
        if ($singular) {
            $templates[] = "content-{$post_type_friendly}-full";
        }
        if (!$singular) {
            $templates[] = "content-{$post_type_friendly}-short";
        }
        $templates[] = "content-{$post_type_friendly}";
    }
    // If no friendly post type template, load content-ctc_post_type.php, using the actual post type name
    if ($singular) {
        $templates[] = "content-{$post_type}-full";
    }
    if (!$singular) {
        $templates[] = "content-{$post_type}-short";
    }
    $templates[] = "content-{$post_type}";
    // If all else fails, use the plain vanilla template
    if ($singular) {
        $templates[] = 'content-full';
    }
    if (!$singular) {
        $templates[] = 'content-short';
    }
    $templates[] = 'content';
    // Append .php extension
    foreach ($templates as $template_key => $template) {
        $templates[$template_key] = $template . '.php';
    }
    // Check in partials directory first
    $templates_partials = array();
    foreach ($templates as $template) {
        $templates_partials[] = CTFW_THEME_PARTIAL_DIR . '/' . $template;
    }
    $templates = array_merge($templates_partials, $templates);
    // Load template and return filename if succeeded
    return locate_template($templates, true, false);
}