| @@ -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,31 @@ | ||
| 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 | + 'tag_id', | |
| 21 | + 'category__in', | |
| 22 | + 'category__and', | |
| 23 | + 'post__in', | |
| 24 | + 'post_name__in', | |
| 25 | + 'tag__in', | |
| 26 | + 'tag__and', | |
| 27 | + 'tag_slug__in', | |
| 28 | + 'tag_slug__and', | |
| 29 | + 'post_parent__in', | |
| 30 | + ); | |
| 31 | + | |
| 12 | 32 | /** |
| 13 | 33 | * Constructor |
| 14 | 34 | * |
| 15 | 35 | * @since 2.2 |
| @@ -24,9 +44,8 @@ | ||
| 24 | 44 | |
| 25 | 45 | /** |
| 26 | 46 | * Check if translated taxonomy is queried |
| 27 | 47 | * Compatible with nested queries introduced in WP 4.1 |
| 28 | - * | |
| 29 | 48 | * @see https://wordpress.org/support/topic/tax_query-bug |
| 30 | 49 | * |
| 31 | 50 | * @since 1.7 |
| 32 | 51 | * |
| @@ -70,32 +89,14 @@ | ||
| 70 | 89 | * @param object $lang |
| 71 | 90 | */ |
| 72 | 91 | public function set_language( $lang ) { |
| 73 | 92 | // Defining directly the tax_query ( rather than setting 'lang' avoids transforming the query by WP ) |
| 74 | - $lang_query = array( | |
| 93 | + $this->query->query_vars['tax_query'][] = array( | |
| 75 | 94 | 'taxonomy' => 'language', |
| 76 | 95 | 'field' => 'term_taxonomy_id', // Since WP 3.5 |
| 77 | 96 | 'terms' => $lang->term_taxonomy_id, |
| 78 | 97 | 'operator' => 'IN', |
| 79 | 98 | ); |
| 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 | 99 | } |
| 99 | 100 | |
| 100 | 101 | /** |
| 101 | 102 | * Add the language in query after it has checked that it won't conflict with other query vars |
| @@ -107,8 +108,24 @@ | ||
| 107 | 108 | public function filter_query( $lang ) { |
| 108 | 109 | $qvars = &$this->query->query_vars; |
| 109 | 110 | |
| 110 | 111 | if ( ! isset( $qvars['lang'] ) ) { |
| 112 | + // Do not filter the query if the language is already specified in another way | |
| 113 | + foreach ( self::$excludes as $k ) { | |
| 114 | + if ( ! empty( $qvars[ $k ] ) ) { | |
| 115 | + return; | |
| 116 | + } | |
| 117 | + } | |
| 118 | + | |
| 119 | + // Specific case for 'cat' as it can contain negative values | |
| 120 | + if ( ! empty( $qvars['cat'] ) ) { | |
| 121 | + foreach ( explode( ',', $qvars['cat'] ) as $cat ) { | |
| 122 | + if ( $cat > 0 ) { | |
| 123 | + return; | |
| 124 | + } | |
| 125 | + } | |
| 126 | + } | |
| 127 | + | |
| 111 | 128 | $taxonomies = array_intersect( $this->model->get_translated_taxonomies(), get_taxonomies( array( '_builtin' => false ) ) ); |
| 112 | 129 | |
| 113 | 130 | foreach ( $taxonomies as $tax ) { |
| 114 | 131 | $tax = get_taxonomy( $tax ); |