function recipe_hero_tc_template_chooser($template) { global $post; if (!empty($post)) { $post_id = $post->ID; } else { return $template; } if (get_option('recipe_hero_recipes_page_id')) { $rh_home_id = get_option('recipe_hero_recipes_page_id'); if (isset($rh_home_id)) { if ($post_id == $rh_home_id) { return recipe_hero_tc_get_template_hierarchy('archive'); } } } if (get_post_type($post_id) == 'recipe') { // Else use custom template if (is_single()) { return recipe_hero_tc_get_template_hierarchy('single'); } elseif (is_archive() || is_page(rh_get_page_id('recipes'))) { return recipe_hero_tc_get_template_hierarchy('archive'); } elseif (is_search()) { return recipe_hero_tc_get_template_hierarchy('search-results'); } else { return $template; } } return $template; }
/** * Handle redirects before content is output - hooked into template_redirect so is_page works. * * @return void */ function rh_template_redirect() { // When default permalinks are enabled, redirect recipes page to post type archive url if (!empty($_GET['page_id']) && get_option('permalink_structure') == "" && $_GET['page_id'] == rh_get_page_id('recipes')) { wp_safe_redirect(get_post_type_archive_link('recipe')); exit; } }
/** * Get the page name/id for a WC page * @param string $wc_page * @return array */ private static function get_page_uris($rh_page) { $rh_page_uris = array(); if (($page_id = rh_get_page_id($rh_page)) && $page_id > 0 && ($page = get_post($page_id))) { $rh_page_uris[] = 'p=' . $page_id; $rh_page_uris[] = '/' . $page->post_name; } return $rh_page_uris; }
/** * Register 'Recipe' Post Type. * * @package Recipe Hero * @author Captain Theme <*****@*****.**> * @since 1.0.8 */ public static function register_post_types() { if (post_type_exists('recipe')) { return; } $permalinks = get_option('recipe_hero_permalinks'); $recipe_permalink = empty($permalinks['recipe_base']) ? _x('recipe', 'slug', 'recipe-hero') : $permalinks['recipe_base']; $labels = array('name' => _x('Recipes', 'post type general name'), 'singular_name' => _x('Recipe', 'post type singular name'), 'all_items' => __('All Recipes'), 'add_new' => __('Add New'), 'add_new_item' => __('Add New Recipe'), 'edit_item' => __('Edit Recipe'), 'new_item' => __('New Recipe'), 'view_item' => __('View Recipe'), 'search_items' => __('Search Recipes'), 'not_found' => __('No Recipes found'), 'not_found_in_trash' => __('No Recipes found in the trash'), 'parent_item_colon' => '', 'menu_name' => __('Recipe Hero')); $taxonomies = array(); $supports = array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'custom-fields', 'publicize', 'page-attributes'); $post_type_args = apply_filters('recipe_hero_recipe_post_type_args', array('labels' => $labels, 'singular_label' => __('Recipe'), 'public' => true, 'show_ui' => true, 'publicly_queryable' => true, 'query_var' => true, 'exclude_from_search' => false, 'show_in_nav_menus' => true, 'capability_type' => 'post', 'has_archive' => ($recipes_page_id = rh_get_page_id('recipes')) && get_post($recipes_page_id) ? get_page_uri($recipes_page_id) : 'recipes', 'hierarchical' => false, 'rewrite' => $recipe_permalink ? array('slug' => untrailingslashit($recipe_permalink), 'with_front' => false, 'feeds' => true) : false, 'supports' => $supports, 'menu_position' => 35, 'menu_icon' => 'dashicons-shield', 'taxonomies' => $taxonomies)); register_post_type('recipe', $post_type_args); }
function recipe_hero_recipes_archive_description() { if (is_post_type_archive('recipe') && get_query_var('paged') == 0) { $recipe_page = get_post(rh_get_page_id('recipes')); if ($recipe_page) { $description = wpautop(do_shortcode($recipe_page->post_content)); if ($description) { echo '<div class="page-description">' . $description . '</div>'; } } } }
/** * Save the settings */ public function settings_save() { if (!is_admin()) { return; } // We need to save the options ourselves; settings api does not trigger save for the permalinks page if (isset($_POST['permalink_structure']) || isset($_POST['cuisine_base']) || isset($_POST['course_base']) && isset($_POST['recipe_permalink'])) { // Cat and tag bases $recipe_hero_recipe_cuisine_slug = rh_clean($_POST['recipe_hero_recipe_cuisine_slug']); $recipe_hero_recipe_course_slug = rh_clean($_POST['recipe_hero_recipe_course_slug']); $permalinks = get_option('recipe_hero_permalinks'); if (!$permalinks) { $permalinks = array(); } $permalinks['cuisine_base'] = untrailingslashit($recipe_hero_recipe_cuisine_slug); $permalinks['course_base'] = untrailingslashit($recipe_hero_recipe_course_slug); // Recipe base $recipe_permalink = rh_clean($_POST['recipe_permalink']); if ($recipe_permalink == 'custom') { // Get permalink without slashes $recipe_permalink = trim(rh_clean($_POST['recipe_permalink_structure']), '/'); // Prepending slash $recipe_permalink = '/' . $recipe_permalink; } elseif (empty($recipe_permalink)) { $recipe_permalink = false; } $permalinks['recipe_base'] = untrailingslashit($recipe_permalink); // Shop base may require verbose page rules if nesting pages $recipe_page_id = rh_get_page_id('recipes'); $recipes_permalink = $recipe_page_id > 0 && get_post($recipe_page_id) ? get_page_uri($recipe_page_id) : _x('shop', 'default-slug', 'recipe-hero'); if ($recipe_page_id && trim($permalinks['recipe_base'], '/') === $recipes_permalink) { $permalinks['use_verbose_page_rules'] = true; } update_option('recipe_hero_permalinks', $permalinks); } }
/** * Hook into pre_get_posts to do the main recipe query * * @access public * @param mixed $q query object * @return void */ public function pre_get_posts($q) { // We only want to affect the main query if (!$q->is_main_query()) { return; } if (get_option('recipe_hero_recipes_page_id')) { $rh_home_id = get_option('recipe_hero_recipes_page_id'); if (isset($rh_home_id)) { $recipe_page = $rh_home_id; } else { $recipe_page = ''; } } else { return; } if (is_array($q)) { $current_id = array_key_exists('queried_object', $q) && array_key_exists('ID', $q->queried_object) ? $q->queried_object->ID : ''; } else { $current_id = ''; } // Fix for verbose page rules if ($current_id == $recipe_page) { $q->set('post_type', 'recipe'); $q->set('page', ''); $q->set('pagename', ''); // Fix conditional Functions $q->is_archive = true; $q->is_post_type_archive = true; $q->is_singular = false; $q->is_page = false; } // Fix for endpoints on the homepage if ($q->is_home() && 'page' == get_option('show_on_front') && get_option('page_on_front') != $q->get('page_id')) { $_query = wp_parse_args($q->query); if (!empty($_query) && array_intersect(array_keys($_query), array_keys($this->query_vars))) { $q->is_page = true; $q->is_home = false; $q->is_singular = true; $q->set('page_id', get_option('page_on_front')); } } // When orderby is set, WordPress shows posts. Get around that here. if ($q->is_home() && 'page' == get_option('show_on_front') && get_option('page_on_front') == rh_get_page_id('recipes')) { $_query = wp_parse_args($q->query); if (empty($_query) || !array_diff(array_keys($_query), array('preview', 'page', 'paged', 'cpage', 'orderby'))) { $q->is_page = true; $q->is_home = false; $q->set('page_id', get_option('page_on_front')); $q->set('post_type', 'recipe'); } } // Special check for sites with the recipe archive on front if ($q->is_page() && 'page' == get_option('show_on_front') && $q->get('page_id') == $recipe_page) { // This is a front-page shop $q->set('post_type', 'recipe'); $q->set('page_id', ''); if (isset($q->query['paged'])) { $q->set('paged', $q->query['paged']); } // Define a variable so we know this is the front page shop later on define('RECIPE_IS_ON_FRONT', true); // Get the actual WP page to avoid errors and let us use is_front_page() // This is hacky but works. Awaiting http://core.trac.wordpress.org/ticket/21096 global $wp_post_types; $recipe_page_object = get_post($recipe_page); $wp_post_types['recipe']->ID = $recipe_page_object->ID; $wp_post_types['recipe']->post_title = $recipe_page_object->post_title; $wp_post_types['recipe']->post_name = $recipe_page_object->post_name; $wp_post_types['recipe']->post_type = $recipe_page_object->post_type; $wp_post_types['recipe']->ancestors = get_ancestors($recipe_page_object->ID, $recipe_page_object->post_type); // Fix conditional Functions like is_front_page $q->is_singular = false; $q->is_post_type_archive = true; $q->is_archive = true; $q->is_page = true; // Only apply to recipe categories, the recipe post archive, the recipe page and recipe taxonomies } elseif (!$q->is_post_type_archive('recipe') && !$q->is_tax(get_object_taxonomies('recipe'))) { return; } $this->recipe_query($q); // We're on a recipe archive page so queue the get_recipes_in_view method add_action('wp', array($this, 'get_recipes_in_view'), 2); // And remove the pre_get_posts hook $this->remove_recipe_query(); }