| @@ -1,8 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | 4 | * A class to manipulate the language query var in WP_Query |
| 8 | 5 | * |
| @@ -8,8 +5,32 @@ | ||
| 8 | 5 | * |
| 9 | 6 | * @since 2.2 |
| 10 | 7 | */ |
| 11 | 8 | class PLL_Query { |
| 9 | + | |
| 10 | + static protected $excludes = array( | |
| 11 | + 'p', | |
| 12 | + 'post_parent', | |
| 13 | + 'attachment', | |
| 14 | + 'attachment_id', | |
| 15 | + 'name', | |
| 16 | + 'pagename', | |
| 17 | + 'page_id', | |
| 18 | + 'category_name', | |
| 19 | + 'tag', | |
| 20 | + 'cat', | |
| 21 | + 'tag_id', | |
| 22 | + 'category__in', | |
| 23 | + 'category__and', | |
| 24 | + 'post__in', | |
| 25 | + 'post_name__in', | |
| 26 | + 'tag__in', | |
| 27 | + 'tag__and', | |
| 28 | + 'tag_slug__in', | |
| 29 | + 'tag_slug__and', | |
| 30 | + 'post_parent__in', | |
| 31 | + ); | |
| 32 | + | |
| 12 | 33 | /** |
| 13 | 34 | * Constructor |
| 14 | 35 | * |
| 15 | 36 | * @since 2.2 |
| @@ -24,9 +45,8 @@ | ||
| 24 | 45 | |
| 25 | 46 | /** |
| 26 | 47 | * Check if translated taxonomy is queried |
| 27 | 48 | * Compatible with nested queries introduced in WP 4.1 |
| 28 | - * | |
| 29 | 49 | * @see https://wordpress.org/support/topic/tax_query-bug |
| 30 | 50 | * |
| 31 | 51 | * @since 1.7 |
| 32 | 52 | * |
| @@ -33,18 +53,16 @@ | ||
| 33 | 53 | * @param array $tax_queries |
| 34 | 54 | * @return bool |
| 35 | 55 | */ |
| 36 | 56 | protected function have_translated_taxonomy( $tax_queries ) { |
| 37 | - if ( is_array( $tax_queries ) ) { | |
| 38 | - foreach ( $tax_queries as $tax_query ) { | |
| 39 | - if ( isset( $tax_query['taxonomy'] ) && $this->model->is_translated_taxonomy( $tax_query['taxonomy'] ) && ! ( isset( $tax_query['operator'] ) && 'NOT IN' === $tax_query['operator'] ) ) { | |
| 40 | - return true; | |
| 41 | - } | |
| 57 | + foreach ( $tax_queries as $tax_query ) { | |
| 58 | + if ( isset( $tax_query['taxonomy'] ) && $this->model->is_translated_taxonomy( $tax_query['taxonomy'] ) && ! ( isset( $tax_query['operator'] ) && 'NOT IN' === $tax_query['operator'] ) ) { | |
| 59 | + return true; | |
| 60 | + } | |
| 42 | 61 | |
| 43 | - // Nested queries | |
| 44 | - elseif ( is_array( $tax_query ) && $this->have_translated_taxonomy( $tax_query ) ) { | |
| 45 | - return true; | |
| 46 | - } | |
| 62 | + // Nested queries | |
| 63 | + elseif ( is_array( $tax_query ) && $this->have_translated_taxonomy( $tax_query ) ) { | |
| 64 | + return true; | |
| 47 | 65 | } |
| 48 | 66 | } |
| 49 | 67 | |
| 50 | 68 | return false; |
| @@ -70,32 +88,14 @@ | ||
| 70 | 88 | * @param object $lang |
| 71 | 89 | */ |
| 72 | 90 | public function set_language( $lang ) { |
| 73 | 91 | // Defining directly the tax_query ( rather than setting 'lang' avoids transforming the query by WP ) |
| 74 | - $lang_query = array( | |
| 92 | + $this->query->query_vars['tax_query'][] = array( | |
| 75 | 93 | 'taxonomy' => 'language', |
| 76 | 94 | 'field' => 'term_taxonomy_id', // Since WP 3.5 |
| 77 | 95 | 'terms' => $lang->term_taxonomy_id, |
| 78 | 96 | 'operator' => 'IN', |
| 79 | 97 | ); |
| 80 | - | |
| 81 | - $tax_query = &$this->query->query_vars['tax_query']; | |
| 82 | - | |
| 83 | - if ( isset( $tax_query['relation'] ) && 'OR' === $tax_query['relation'] ) { | |
| 84 | - $tax_query = array( | |
| 85 | - $lang_query, | |
| 86 | - array( $tax_query ), | |
| 87 | - 'relation' => 'AND', | |
| 88 | - ); | |
| 89 | - } elseif ( is_array( $tax_query ) ) { | |
| 90 | - // The tax query is expected to be *always* an array, but it seems that 3rd parties fill it with a string | |
| 91 | - // Causing a fatal error if we don't check it. | |
| 92 | - // See https://wordpress.org/support/topic/fatal-error-2947/ | |
| 93 | - $tax_query[] = $lang_query; | |
| 94 | - } elseif ( empty( $tax_query ) ) { | |
| 95 | - // Supposing the tax query has been wrongly filled with an empty string | |
| 96 | - $tax_query = array( $lang_query ); | |
| 97 | - } | |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | /** |
| 101 | 101 | * Add the language in query after it has checked that it won't conflict with other query vars |
| @@ -106,9 +106,16 @@ | ||
| 106 | 106 | */ |
| 107 | 107 | public function filter_query( $lang ) { |
| 108 | 108 | $qvars = &$this->query->query_vars; |
| 109 | 109 | |
| 110 | + // Do not filter the query if the language is already specified in another way | |
| 110 | 111 | if ( ! isset( $qvars['lang'] ) ) { |
| 112 | + foreach ( self::$excludes as $k ) { | |
| 113 | + if ( ! empty( $qvars[ $k ] ) ) { | |
| 114 | + return; | |
| 115 | + } | |
| 116 | + } | |
| 117 | + | |
| 111 | 118 | $taxonomies = array_intersect( $this->model->get_translated_taxonomies(), get_taxonomies( array( '_builtin' => false ) ) ); |
| 112 | 119 | |
| 113 | 120 | foreach ( $taxonomies as $tax ) { |
| 114 | 121 | $tax = get_taxonomy( $tax ); |
| @@ -116,9 +123,9 @@ | ||
| 116 | 123 | return; |
| 117 | 124 | } |
| 118 | 125 | } |
| 119 | 126 | |
| 120 | - if ( ! empty( $qvars['tax_query'] ) && $this->have_translated_taxonomy( $qvars['tax_query'] ) ) { | |
| 127 | + if ( ! empty( $qvars['tax_query'] ) && is_array( $qvars['tax_query'] ) && $this->have_translated_taxonomy( $qvars['tax_query'] ) ) { | |
| 121 | 128 | return; |
| 122 | 129 | } |
| 123 | 130 | |
| 124 | 131 | // Filter queries according to the requested language |
| @@ -138,9 +145,9 @@ | ||
| 138 | 145 | } |
| 139 | 146 | } |
| 140 | 147 | } else { |
| 141 | 148 | // Do not filter untranslatable post types such as nav_menu_item |
| 142 | - if ( isset( $qvars['post_type'] ) && ! $this->model->is_translated_post_type( $qvars['post_type'] ) && ( empty( $qvars['tax_query'] ) || ! $this->have_translated_taxonomy( $qvars['tax_query'] ) ) ) { | |
| 149 | + if ( isset( $qvars['post_type'] ) && ! $this->model->is_translated_post_type( $qvars['post_type'] ) ) { | |
| 143 | 150 | unset( $qvars['lang'] ); |
| 144 | 151 | } |
| 145 | 152 | |
| 146 | 153 | // Unset 'all' query var (mainly for admin language filter). |