/** * Get the fork's parent post, set up a query, and load correct template. * * Duplicates the functionality of /wp-includes/template-loader.php and includes * a lot of copypasta, but that's only to ensure that it follows the same logic. * */ function choose_template() { $p = get_queried_object_id(); if (get_post_type($p) !== 'fork') { return; } $pp = get_post($p)->post_parent; $parent = get_post($pp); if ($parent->post_type == 'page') { $query = array('page_id' => $pp); } else { $query = array('p' => $pp); } $t = new WP_Query($query); $template = false; if ($t->is_404() && ($template = get_404_template())) { } elseif ($t->is_search() && ($template = get_search_template())) { } elseif ($t->is_tax() && ($template = get_taxonomy_template())) { } elseif ($t->is_front_page() && ($template = get_front_page_template())) { } elseif ($t->is_home() && ($template = get_home_template())) { } elseif ($t->is_attachment() && ($template = get_attachment_template())) { remove_filter('the_content', 'prepend_attachment'); } elseif ($t->is_single() && ($template = get_single_template())) { } elseif ($t->is_page && ($template = get_page_template())) { } elseif ($t->is_category() && ($template = get_category_template())) { } elseif ($t->is_tag() && ($template = get_tag_template())) { } elseif ($t->is_author() && ($template = get_author_template())) { } elseif ($t->is_date() && ($template = get_date_template())) { } elseif ($t->is_archive() && ($template = get_archive_template())) { } elseif ($t->is_comments_popup() && ($template = get_comments_popup_template())) { } elseif ($t->is_paged() && ($template = get_paged_template())) { } else { $template = get_index_template(); } if ($template = apply_filters('template_include', $template)) { include $template; } return; }
/** * Checks to see if we're in the main query and stores result as isMainQuery property * * @param WP_Query $query Instance of WP_Query to check * @return mixed $query * * @since 1.0 */ function check_for_main_query($query) { if ($this->force_run || !is_admin() && $query->is_main_query()) { if (!isset($_GET['swpjumpstart'])) { do_action('searchwp_log', 'check_for_main_query(): It is the main query'); } $this->isMainQuery = true; // plugin compat if ($query->is_search()) { do_action('searchwp_log', 'It is a search'); remove_filter('pre_get_posts', 'CPTO_pre_get_posts'); // Post Types Order } } return $query; }
/** * Filter `pre_get_posts`. * * @param WP_Query $wp_the_query * @return WP_Query */ public function preGetPosts($wp_the_query) { /** @var WP_Query $wp_the_query */ if ($wp_the_query->is_search() && !$wp_the_query->is_admin && $wp_the_query->is_main_query()) { $this->keyword = $wp_the_query->query['s']; $this->limit = isset($wp_the_query->query_vars['posts_per_page']) ? $wp_the_query->query_vars['posts_per_page'] : 10; if (isset($wp_the_query->query_vars['paged'])) { $this->paged = intval($wp_the_query->query_vars['paged']); $this->paged = $this->paged > 0 ? $this->paged : 1; } $offset = ($this->paged - 1) * $this->limit; try { $ret = $this->getOpenSearchClient()->search("default:'{$this->keyword}' AND post_status:'publish'", $offset, $this->limit); } catch (AliyunOpenSearchException $e) { wp_die(sprintf('搜索文章时发生错误:%s,请检查您的配置是否有误。<a href="%s" target="_blank">查看错误码说明</a>', $e->getMessage(), AliyunOpenSearch::getErrorCodeReferencesUrl())); } $this->posts = $ret['posts']; $this->postCount = $ret['total']; $this->pageCount = ceil($this->postCount / $this->limit); foreach ($this->posts as $post) { $this->ids[] = $post->ID; } $wp_the_query->query = array(); $wp_the_query->query_vars['post_in'] = $this->ids; $wp_the_query->query_vars['post_type'] = null; $wp_the_query->query_vars['s'] = null; $wp_the_query->query_vars['paged'] = null; // set_query_var('post__in', $this->ids); // set_query_var('post_type', null); // set_query_var('s', null); // set_query_var('paged', null); } return $wp_the_query; }
/** * Translate args to ElasticPress compat format. This is the meat of what the module does * * @param WP_Query $query * @since 2.1 */ function ep_wc_translate_args($query) { // Lets make sure this doesn't interfere with the CLI if (defined('WP_CLI') && WP_CLI) { return; } $product_name = $query->get('product', false); $post_parent = $query->get('post_parent', false); /** * Do nothing for single product queries */ if (!empty($product_name)) { return; } /** * ElasticPress does not yet support post_parent queries */ if (!empty($post_parent)) { return; } /** * Cant hook into WC API yet */ if (defined('WC_API_REQUEST') && WC_API_REQUEST) { return; } // Flag to check and make sure we are in a WooCommerce specific query $integrate = false; /** * Force ElasticPress if we are querying WC taxonomy */ $tax_query = $query->get('tax_query', array()); $supported_taxonomies = array('product_cat', 'pa_brand', 'product_tag', 'pa_sort-by'); if (!empty($tax_query)) { /** * First check if already set taxonomies are supported WC taxes */ foreach ($tax_query as $taxonomy_array) { if (isset($taxonomy_array['taxonomy']) && in_array($taxonomy_array['taxonomy'], $supported_taxonomies)) { $integrate = true; } } } /** * Next check if any taxonomies are in the root of query vars (shorthand form) */ foreach ($supported_taxonomies as $taxonomy) { $term = $query->get($taxonomy, false); if (!empty($term)) { $integrate = true; $terms = array($term); // to add child terms to the tax query if (is_taxonomy_hierarchical($taxonomy)) { $term_object = get_term_by('slug', $term, $taxonomy); $children = get_term_children($term_object->term_id, $taxonomy); if ($children) { foreach ($children as $child) { $child_object = get_term($child, $taxonomy); $terms[] = $child_object->slug; } } } $tax_query[] = array('taxonomy' => $taxonomy, 'field' => 'slug', 'terms' => $terms); } } $post_type = $query->get('post_type', false); if (!empty($tax_query)) { $query->set('tax_query', $tax_query); if (empty($post_type)) { $post_type = 'product'; } elseif (is_array($post_type)) { $post_type[] = 'product'; } else { $post_type = array($post_type, 'product'); } $query->set('post_type', $post_type); } /** * Force ElasticPress if product post type query */ $supported_post_types = array('product', 'shop_order', 'shop_order_refund', 'product_variation'); // For orders it queries an array of shop_order and shop_order_refund post types, hence an array_diff if (!empty($post_type) && (in_array($post_type, $supported_post_types) || is_array($post_type) && !array_diff($post_type, $supported_post_types))) { $integrate = true; } /** * If we have a WooCommerce specific query, lets hook it to ElasticPress and make the query ElasticSearch friendly */ if ($integrate || $query->is_search()) { // Handles the WC Top Rated Widget if (has_filter('posts_clauses', array(WC()->query, 'order_by_rating_post_clauses'))) { remove_filter('posts_clauses', array(WC()->query, 'order_by_rating_post_clauses')); $query->set('orderby', 'meta_value_num'); $query->set('meta_key', '_wc_average_rating'); } /** * We can't support any special fields parameters */ $fields = $query->get('fields', false); if ('ids' === $fields || 'id=>parent' === $fields) { $query->set('fields', 'default'); } /** * Handle meta queries */ $meta_query = $query->get('meta_query', array()); $meta_key = $query->get('meta_key', false); $meta_value = $query->get('meta_value', false); if (!empty($meta_key) && !empty($meta_value)) { $meta_query[] = array('key' => $meta_key, 'value' => $meta_value); $query->set('meta_query', $meta_query); } /** * Make sure filters are suppressed */ $query->query['suppress_filters'] = false; $query->set('suppress_filters', false); $orderby = $query->get('orderby'); if (!empty($orderby) && 'rand' === $orderby) { $query->set('orderby', false); // Just order by relevance. } $s = $query->get('s'); if (empty($s)) { $query->query_vars['ep_integrate'] = true; $query->query['ep_integrate'] = true; } else { $query->set('orderby', false); // Just order by relevance. // Search query if ('shop_order' === $post_type) { $search_fields = $query->get('search_fields', array('post_title', 'post_content', 'post_excerpt')); $search_fields['meta'] = array_map('wc_clean', apply_filters('shop_order_search_fields', array('_order_key', '_billing_company', '_billing_address_1', '_billing_address_2', '_billing_city', '_billing_postcode', '_billing_country', '_billing_state', '_billing_email', '_billing_phone', '_shipping_address_1', '_shipping_address_2', '_shipping_city', '_shipping_postcode', '_shipping_country', '_shipping_state', '_billing_last_name', '_billing_first_name', '_shipping_first_name', '_shipping_last_name'))); $query->set('search_fields', $search_fields); } elseif (empty($post_type) || 'product' === $post_type) { $search_fields = $query->get('search_fields', array('post_title', 'post_content', 'post_excerpt')); // Make sure we search skus on the front end $search_fields['meta'] = array('_sku'); // Search by proper taxonomies on the front end $search_fields['taxonomies'] = array('category', 'post_tag', 'product_tag', 'product_cat'); $query->set('search_fields', $search_fields); } } /** * Set orderby from GET param * Also make sure the orderby param affects only the main query */ if (!empty($_GET['orderby']) && $query->is_main_query()) { switch ($_GET['orderby']) { case 'popularity': $query->set('orderby', ep_wc_get_orderby_meta_mapping('total_sales')); break; case 'price': case 'price-desc': $query->set('orderby', ep_wc_get_orderby_meta_mapping('_price')); break; case 'rating': $query->set('orderby', ep_wc_get_orderby_meta_mapping('_wc_average_rating')); break; case 'date': $query->set('orderby', ep_wc_get_orderby_meta_mapping('date')); break; case 'ID': $query->set('orderby', ep_wc_get_orderby_meta_mapping('ID')); break; default: $query->set('orderby', ep_wc_get_orderby_meta_mapping('menu_order')); // Order by menu and title. } } } }
/** * The rule of this ordering is: from the most specific to the least. * Most of the default WP Template Hierarchy is the same, but not all is followed. * * For the full example of our lookup order plesase follow to: * * For the default WP hierarchy follow to: * http://codex.wordpress.org/Template_Hierarchy * * @param WP_Query $wp_query * @return array */ protected function defineLookupOrder(\WP_Query $wp_query) { $result = []; if (!$wp_query) { return $result; } // prepare vars $post = !empty($wp_query->posts) ? $wp_query->posts[0] : false; $post_type = $post ? $post->post_type : false; $post_slug = $post ? $post->post_name : false; $query_post_type = $wp_query->query_vars['post_type']; if (is_array($query_post_type)) { // it's not usual to have multiple post types on a rewrite rule // but even if there is, it's extremely inconsistent to rely on // a template name with multiple post types // if that's the case, the user will have to alter the template // order manually $query_post_type = false; } // start the template hierarchy build up if ($wp_query->is_404()) { // 404-[post-type] // 404 if ($query_post_type) { $result[] = '404-' . $query_post_type; } $result[] = '404'; } elseif ($wp_query->is_search()) { // search // archive $result[] = 'search'; $result[] = 'archive'; } elseif ($wp_query->is_front_page()) { // if is page on front: // front-page // page // singular // if is posts on front: // front-page // home // archive-[post-type] // [post-type] // archive $result[] = 'front-page'; if ($post_type) { if ($post_type !== 'page') { $result[] = 'home'; $result[] = 'archive-' . $post_type; $result[] = $post_type; $result[] = 'archive'; } else { $result[] = 'page'; $result[] = 'singular'; } } } elseif ($wp_query->is_home()) { // home // archive-[post-type] // [post-type] // archive $result[] = 'home'; if ($post_type) { $result[] = 'archive-' . $post_type; $result[] = $post_type; $result[] = 'archive'; } // for now this is not needed, test more // } elseif ($wp_query->is_post_type_archive()) { // $result[] = 'archive-'.$query_post_type; // $result[] = $query_post_type; // $result[] = 'archive'; } elseif ($wp_query->is_author()) { // author-[user-login] // author-[user-nicename] // author // archive if ($author = get_userdata($post->post_author)) { $result[] = 'author-' . $author->data->user_login; if ($author->data->user_login !== $author->data->user_nicename) { $result[] = 'author-' . $author->data->user_nicename; } } $result[] = 'author'; $result[] = 'archive'; } elseif ($wp_query->is_tax() || $wp_query->is_tag() || $wp_query->is_category()) { // taxonomy-[taxonomy]-[term-slug] // taxonomy-[taxonomy] // taxonomy-[post-type] // taxonomy // archive-[post-type] // [post-type] // archive $term = get_queried_object(); if (!empty($term->slug)) { $result[] = 'taxonomy-' . $term->taxonomy . '-' . $term->slug; $result[] = 'taxonomy-' . $term->taxonomy; } if ($query_post_type) { $result[] = 'taxonomy-' . $query_post_type; } $result[] = 'taxonomy'; if ($query_post_type) { $result[] = 'archive-' . $query_post_type; $result[] = $query_post_type; } $result[] = 'archive'; } elseif ($wp_query->is_date()) { // date-[post-type] // date // archive-[post-type] // [post-type] // archive if ($query_post_type) { $result[] = 'date-' . $query_post_type; } $result[] = 'date'; if ($query_post_type) { $result[] = 'archive-' . $query_post_type; $result[] = $query_post_type; } $result[] = 'archive'; } elseif ($wp_query->is_archive()) { // archive-[post-type] // [post-type] // archive if ($query_post_type) { $result[] = 'archive-' . $query_post_type; $result[] = $query_post_type; } $result[] = 'archive'; } elseif ($wp_query->is_page()) { // page-[parent-slug]-[post-slug] // page-[post-slug] // [page-template-name] // page // singular if ($post->post_parent) { if ($parent_slug = get_slug($post->post_parent)) { $result[] = 'page-' . $parent_slug . '-' . $post_slug; } } $result[] = 'page-' . $post_slug; // page templates can have their unique names, let's add them before the fallback if ($page_template_name = get_page_template_name($post->ID)) { $result[] = $page_template_name; } $result[] = 'page'; $result[] = 'singular'; } elseif ($wp_query->is_attachment()) { // single-attachment-[slugfied-long-mime-type] // single-attachment-[slugfied-short-mime-type] // single-attachment // attachment // single // singular // slugfied-long-mime-type = image-jpeg // slugfied-short-mime-type = jpeg if (!empty($post->post_mime_type)) { $result[] = 'single-attachment-' . \Bond\to_slug($post->post_mime_type); $mime = explode('/', $post->post_mime_type); if (count($mime) > 1) { $result[] = 'single-attachment-' . \Bond\to_slug($mime[1]); } $result[] = 'single-attachment-' . $mime[0]; } $result[] = 'single-attachment'; $result[] = 'attachment'; $result[] = 'single'; $result[] = 'singular'; } elseif ($wp_query->is_single()) { // single-[post-type]-[post-slug] // single-[post-type] // [post-type] // single // singular $result[] = 'single-' . $post_type . '-' . $post_slug; $result[] = 'single-' . $post_type; $result[] = $post_type; $result[] = 'single'; $result[] = 'singular'; } // everything is handled, allow a filter and go $result = apply_filters($this->hooks_prefix . '/lookup_order', $result); return $result; }
/** * Make sure we search all relevant post types * * @param string $post_type * @param WP_Query $query * @since 2.1 * @return bool|string */ function ep_use_searchable_post_types_on_any($post_type, $query) { if ($query->is_search() && 'any' === $post_type) { /* * This is a search query * To follow WordPress conventions, * make sure we only search 'searchable' post types */ $searchable_post_types = ep_get_searchable_post_types(); // If we have no searchable post types, there's no point going any further if (empty($searchable_post_types)) { // Have to return something or it improperly calculates the found_posts return false; } // Conform the post types array to an acceptable format for ES $post_types = array(); foreach ($searchable_post_types as $type) { $post_types[] = $type; } // These are now the only post types we will search $post_type = $post_types; } return $post_type; }
/** * Parse which page we are on using URL */ public function getPageObject($pageUrl) { global $wp_rewrite; // If post type, we are using url_to_postid function $postId = url_to_postid($pageUrl); if ($postId) { $postType = get_post_type_object(get_post($postId)->post_type); return array('value' => $postId, 'title' => get_the_title($postId), 'type' => get_post($postId)->post_type, 'label' => is_array($postType->labels) ? $postType->labels['name'] : $postType->labels->name); } $path = str_replace(get_site_url(), '', $pageUrl); $path = trim($path, '/'); // If path is empty, then it is front page if (empty($path)) { return array('value' => get_option('page_on_front') ? get_option('page_on_front') : '', 'title' => '', 'type' => 'front_page', 'label' => __('Home Page')); } // Otherwise, we will try to match through rewrite or by query $rewrite = $wp_rewrite->wp_rewrite_rules(); if (is_array($rewrite) && count($rewrite) > 0) { foreach ($rewrite as $match => $query) { if (preg_match("#^{$match}#", $path, $matches) || preg_match("#^{$match}#", urldecode($path), $matches)) { $query = preg_replace("!^.*\\?!", '', $query); $query = addslashes(WP_MatchesMapRegex::apply($query, $matches)); parse_str($query, $query_vars); break; } } } else { $query = preg_replace("!^.*\\?!", '', $path); parse_str($query, $query_vars); } // Workaround for fail pagename rewrite match if (isset($query_vars['pagename']) && strpos($query_vars['pagename'], '?') !== false) { $query = preg_replace("!^.*\\?!", '', $query_vars['pagename']); parse_str($query, $query_vars); } $querypost = new WP_Query($query_vars); if ($querypost->is_date()) { if ($querypost->query_vars['m']) { $date = $querypost->query_vars['m']; } else { if ($querypost->is_day()) { $date = $querypost->query_vars['year'] . zeroise($querypost->query_vars['monthnum'], 2) . zeroise($querypost->query_vars['day'], 2); } else { if ($querypost->is_month()) { $date = $querypost->query_vars['year'] . zeroise($querypost->query_vars['monthnum'], 2); } else { if ($querypost->is_year()) { $date = $querypost->query_vars['year']; } } } } return array('value' => $date, 'title' => '', 'type' => 'archive', 'label' => __("Archive")); } else { if ($querypost->is_category() || $querypost->is_tag() || $querypost->is_tax()) { $tax_query = $querypost->tax_query->queries; $taxonomy = get_taxonomy($tax_query[0]['taxonomy']); if ($tax_query[0]['field'] == 'term_id') { $term_id = $tax_query[0]['terms'][0]; } else { if ($tax_query[0]['field'] == 'slug') { $term_id = get_term_by('slug', $tax_query[0]['terms'][0], $taxonomy->name)->term_id; } } return array('value' => $term_id, 'title' => get_term($term_id, $taxonomy->name)->name, 'type' => $taxonomy->name, 'label' => is_array($taxonomy->labels->name) ? $taxonomy->labels['name'] : $taxonomy->labels->name); } else { if ($querypost->is_search()) { return array('value' => $querypost->query_vars['s'], 'title' => '', 'type' => 'search', 'label' => __("Search")); } else { if ($querypost->is_home()) { return array('value' => '', 'title' => '', 'type' => 'home', 'label' => __("Blog Home Page")); } } } } }