/** * Attempts to guess the correct URL based on query vars * * @since 3.2.3 * * @param string $name * @param WP_Query $query * * @return array containing most likely name, type and whether or not a match was found */ public function guess_cpt_by_name($name, $query) { $type = $query->get('post_type'); $ret = array($name, $type, false); $types = (bool) $type === false ? $this->sitepress->get_wp_api()->get_post_types(array('public' => true)) : (array) $type; if ((bool) $types === true) { $where = $this->wpdb->prepare("post_name = %s ", $name); $where .= " AND post_type IN ('" . implode("', '", $types) . "')"; $date_snippet = $this->by_date_snippet($query); $where .= $date_snippet; $res = $this->wpdb->get_row("\n\t\t\t\t\t\t\t\t\t SELECT post_type, post_name\n\t\t\t\t\t\t\t\t\t FROM {$this->wpdb->posts} p\n\t\t\t\t\t\t\t\t\t LEFT JOIN {$this->wpdb->prefix}icl_translations t\n\t\t\t\t\t\t\t\t\t\tON t.element_id = p.ID\n\t\t\t\t\t\t\t\t\t\t \tAND CONCAT('post_', p.post_type) = t.element_type\n\t\t\t\t\t\t\t\t\t \t\tAND " . $this->query_filter->in_translated_types_snippet(false, 'p') . "\n\t\t\t\t\t\t\t\t\t WHERE {$where}\n\t\t\t\t\t\t\t\t\t \tAND ( post_status = 'publish'\n\t\t\t\t\t\t\t\t\t \t OR ( post_type = 'attachment'\n\t\t\t\t\t\t\t\t\t \t AND post_status = 'inherit' ) )\n\t\t\t\t\t\t\t\t\t \t" . $this->order_by_language_snippet((bool) $date_snippet) . "\n\t\t\t\t\t\t\t\t LIMIT 1"); if ((bool) $res === true) { $ret = array($res->post_name, $res->post_type, true); } } return $ret; }
/** * Tries to transform certain queries from "by name" querying to "by ID" to overcome WordPress Core functionality * for resolving names not being filtered by language * * @param WP_Query $q * * @return WP_Query */ private function maybe_adjust_name_var($q) { if (((bool) ($name_in_q = $q->get('name')) === true || (bool) ($name_in_q = $q->get('pagename')) === true) && (bool) $q->get('page_id') === false || (bool) ($post_type = $q->get('post_type')) === true && is_scalar($post_type) && (bool) ($name_in_q = $q->get($post_type)) === true) { list($name_found, $type, $altered) = $this->query_filter->get_404_util()->guess_cpt_by_name($name_in_q, $q); if ($altered === true) { $name_before = $q->get('name'); $q->set('name', $name_found); } $type = $type ? $type : 'page'; $type = is_scalar($type) ? $type : (count($type) === 1 ? end($type) : false); $q = $type ? $this->query_filter->get_page_name_filter($type)->filter_page_name($q) : $q; if (isset($name_before)) { $q->set('name', $name_before); } } return $q; }