/**
 * Get data for a specific content type
 *
 * Specify a key, such as "page_templates"; otherwise, all data is retrieved.
 *
 * @since 0.9
 * @param string $content_type Content type to get data for
 * @param string $key Optionally specify a key to get data for
 * @return mixed All data (array) for content type or data for specific key
 */
function ctfw_content_type_data($content_type, $key = false)
{
    $data = false;
    if (!empty($content_type)) {
        $type_data = ctfw_content_types();
        if (!empty($type_data[$content_type])) {
            if (!empty($key)) {
                if (!empty($type_data[$content_type][$key])) {
                    // check for data
                    $data = $type_data[$content_type][$key];
                }
            } else {
                // no key given, return all
                $data = $type_data[$content_type];
            }
        }
    }
    return apply_filters('ctfw_content_type_data', $data, $content_type, $key);
}
/**
 * Redirect post type archives to pages
 *
 * Use add_theme_support( 'ctfw-archive-redirection' ) to redirect post type archives to pages using specific page templates.
 * Post types and page templates from ctfw_content_types() are used to automate this (theme must filter page templates in).
 *
 * Page template should output same loop but with with title, featured image, etc. for nicer presentation and to avoid duplicate content.
 * This is done only for non-date archive. Feeds are unaffected.
 *
 * @since 0.9
 */
function ctfw_redirect_archives_to_pages()
{
    // Theme supports this?
    if (!current_theme_supports('ctfw-archive-redirection')) {
        return false;
    }
    // Run only on post type archive, but not date archive or feed
    if (!is_post_type_archive() || is_year() || is_month() || is_day() || is_feed()) {
        return false;
    }
    // Get content types
    $content_types = ctfw_content_types();
    // Loop content types
    foreach ($content_types as $content_type => $content_type_data) {
        // Get templates for content type
        // The first will be used if a page exists; otherwise the second, etc.
        $page_templates = !empty($content_type_data['page_templates']) ? $content_type_data['page_templates'] : array();
        // Have at least one page template
        if ($page_templates) {
            // Get post type(s) for content type (probably just one)
            $post_types = $content_type_data['post_types'];
            // Have post types
            if (!empty($post_types)) {
                // Loop post types
                foreach ($post_types as $post_type) {
                    // Have post type
                    if (!empty($post_type)) {
                        // Only if archive is for specific post type
                        if (is_post_type_archive($post_type)) {
                            // Loop each template in order of priority and redirect to first one that has page
                            foreach ($page_templates as $page_template) {
                                // Check if a page is the template
                                if ($redirect_page = ctfw_get_page_by_template($page_template)) {
                                    // Found a page?
                                    if (!empty($redirect_page->ID)) {
                                        // Get page data
                                        $post_type_obj = get_post_type_object($post_type);
                                        // Don't redirect if URL is the same (post type and page have same slug); prevent infinite loop
                                        if ($redirect_page->post_name != $post_type_obj->rewrite['slug']) {
                                            // Get URL
                                            $page_url = get_permalink($redirect_page->ID);
                                            // Go!
                                            wp_redirect($page_url, 301);
                                            exit;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}