| @@ -1,28 +1,23 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | - * Frontend controller | |
| 4 | + * frontend controller | |
| 8 | 5 | * accessible as $polylang global object |
| 9 | 6 | * |
| 10 | - * Properties: | |
| 11 | - * options => inherited, reference to Polylang options array | |
| 12 | - * model => inherited, reference to PLL_Model object | |
| 13 | - * links_model => inherited, reference to PLL_Links_Model object | |
| 14 | - * links => reference to PLL_Links object | |
| 15 | - * static_pages => reference to PLL_Frontend_Static_Pages object | |
| 16 | - * choose_lang => reference to PLL_Choose_Lang object | |
| 17 | - * curlang => current language | |
| 18 | - * filters => reference to PLL_Frontend_Filters object | |
| 19 | - * filters_links => reference to PLL_Frontend_Filters_Links object | |
| 20 | - * filters_search => reference to PLL_Frontend_Filters_Search object | |
| 21 | - * posts => reference to PLL_CRUD_Posts object | |
| 22 | - * terms => reference to PLL_CRUD_Terms object | |
| 23 | - * nav_menu => reference to PLL_Frontend_Nav_Menu object | |
| 24 | - * auto_translate => optional, reference to PLL_Auto_Translate object | |
| 7 | + * properties: | |
| 8 | + * options => inherited, reference to Polylang options array | |
| 9 | + * model => inherited, reference to PLL_Model object | |
| 10 | + * links_model => inherited, reference to PLL_Links_Model object | |
| 11 | + * links => reference to PLL_Links object | |
| 12 | + * static_pages => reference to PLL_Frontend_Static_Pages object | |
| 13 | + * filters_links => inherited, reference to PLL_Frontend_Filters_Links object | |
| 14 | + * choose_lang => reference to PLL_Choose_lang object | |
| 15 | + * curlang => current language | |
| 16 | + * filters => reference to PLL_Filters object | |
| 17 | + * filters_search => reference to PLL_Frontend_Filters_Search object | |
| 18 | + * nav_menu => reference to PLL_Frontend_Nav_Menu object | |
| 19 | + * auto_translate => optional, reference to PLL_Auto_Translate object | |
| 25 | 20 | * |
| 26 | 21 | * @since 1.2 |
| 27 | 22 | */ |
| 28 | 23 | class PLL_Frontend extends PLL_Base { |
| @@ -29,9 +24,9 @@ | ||
| 29 | 24 | public $curlang; |
| 30 | 25 | public $links, $choose_lang, $filters, $filters_search, $nav_menu, $auto_translate; |
| 31 | 26 | |
| 32 | 27 | /** |
| 33 | - * Constructor | |
| 28 | + * constructor | |
| 34 | 29 | * |
| 35 | 30 | * @since 1.2 |
| 36 | 31 | * |
| 37 | 32 | * @param object $links_model |
| @@ -40,15 +35,15 @@ | ||
| 40 | 35 | parent::__construct( $links_model ); |
| 41 | 36 | |
| 42 | 37 | add_action( 'pll_language_defined', array( $this, 'pll_language_defined' ), 1 ); |
| 43 | 38 | |
| 44 | - // Avoids the language being the queried object when querying multiple taxonomies | |
| 39 | + // avoids the language being the queried object when querying multiple taxonomies | |
| 45 | 40 | add_action( 'parse_tax_query', array( $this, 'parse_tax_query' ), 1 ); |
| 46 | 41 | |
| 47 | - // Filters posts by language | |
| 42 | + // filters posts by language | |
| 48 | 43 | add_action( 'parse_query', array( $this, 'parse_query' ), 6 ); |
| 49 | 44 | |
| 50 | - // Not before 'check_canonical_url' | |
| 45 | + // not before 'check_canonical_url' | |
| 51 | 46 | if ( ! defined( 'PLL_AUTO_TRANSLATE' ) || PLL_AUTO_TRANSLATE ) { |
| 52 | 47 | add_action( 'template_redirect', array( $this, 'auto_translate' ), 7 ); |
| 53 | 48 | } |
| 54 | 49 | } |
| @@ -53,51 +48,41 @@ | ||
| 53 | 48 | } |
| 54 | 49 | } |
| 55 | 50 | |
| 56 | 51 | /** |
| 57 | - * Setups the language chooser based on options | |
| 52 | + * setups the language chooser based on options | |
| 58 | 53 | * |
| 59 | 54 | * @since 1.2 |
| 60 | 55 | */ |
| 61 | 56 | public function init() { |
| 62 | - parent::init(); | |
| 63 | - | |
| 64 | 57 | $this->links = new PLL_Frontend_Links( $this ); |
| 58 | + $this->static_pages = new PLL_Frontend_Static_Pages( $this ); | |
| 65 | 59 | |
| 66 | - // Static front page and page for posts | |
| 67 | - if ( 'page' === get_option( 'show_on_front' ) ) { | |
| 68 | - $this->static_pages = new PLL_Frontend_Static_Pages( $this ); | |
| 69 | - } | |
| 70 | - | |
| 71 | - // Setup the language chooser | |
| 60 | + // setup the language chooser | |
| 72 | 61 | $c = array( 'Content', 'Url', 'Url', 'Domain' ); |
| 73 | 62 | $class = 'PLL_Choose_Lang_' . $c[ $this->options['force_lang'] ]; |
| 74 | 63 | $this->choose_lang = new $class( $this ); |
| 75 | 64 | $this->choose_lang->init(); |
| 76 | 65 | |
| 77 | - // Need to load nav menu class early to correctly define the locations in the customizer when the language is set from the content | |
| 66 | + // need to load nav menu class early to correctly define the locations in the customizer when the language is set from the content | |
| 78 | 67 | $this->nav_menu = new PLL_Frontend_Nav_Menu( $this ); |
| 79 | 68 | } |
| 80 | 69 | |
| 81 | 70 | /** |
| 82 | - * Setups filters and nav menus once the language has been defined | |
| 71 | + * setups filters and nav menus once the language has been defined | |
| 83 | 72 | * |
| 84 | 73 | * @since 1.2 |
| 85 | 74 | */ |
| 86 | 75 | public function pll_language_defined() { |
| 87 | - // Filters | |
| 76 | + // filters | |
| 88 | 77 | $this->filters_links = new PLL_Frontend_Filters_Links( $this ); |
| 89 | 78 | $this->filters = new PLL_Frontend_Filters( $this ); |
| 90 | 79 | $this->filters_search = new PLL_Frontend_Filters_Search( $this ); |
| 80 | + } | |
| 91 | 81 | |
| 92 | - // Auto translate for Ajax | |
| 93 | - if ( ( ! defined( 'PLL_AUTO_TRANSLATE' ) || PLL_AUTO_TRANSLATE ) && wp_doing_ajax() ) { | |
| 94 | - $this->auto_translate(); | |
| 95 | - } | |
| 96 | - } | |
| 97 | 82 | |
| 98 | 83 | /** |
| 99 | - * When querying multiple taxonomies, makes sure that the language is not the queried object | |
| 84 | + * when querying multiple taxonomies, makes sure that the language is not the queried object | |
| 100 | 85 | * |
| 101 | 86 | * @since 1.8 |
| 102 | 87 | * |
| 103 | 88 | * @param object $query WP_Query object |
| @@ -102,10 +87,9 @@ | ||
| 102 | 87 | * |
| 103 | 88 | * @param object $query WP_Query object |
| 104 | 89 | */ |
| 105 | 90 | public function parse_tax_query( $query ) { |
| 106 | - $pll_query = new PLL_Query( $query, $this->model ); | |
| 107 | - $queried_taxonomies = $pll_query->get_queried_taxonomies(); | |
| 91 | + $queried_taxonomies = $this->get_queried_taxonomies( $query ); | |
| 108 | 92 | |
| 109 | 93 | if ( ! empty( $queried_taxonomies ) && 'language' == reset( $queried_taxonomies ) ) { |
| 110 | 94 | $query->tax_query->queried_terms['language'] = array_shift( $query->tax_query->queried_terms ); |
| 111 | 95 | } |
| @@ -111,9 +95,9 @@ | ||
| 111 | 95 | } |
| 112 | 96 | } |
| 113 | 97 | |
| 114 | 98 | /** |
| 115 | - * Modifies some query vars to "hide" that the language is a taxonomy and avoid conflicts | |
| 99 | + * modifies some query vars to "hide" that the language is a taxonomy and avoid conflicts | |
| 116 | 100 | * |
| 117 | 101 | * @since 1.2 |
| 118 | 102 | * |
| 119 | 103 | * @param object $query WP_Query object |
| @@ -119,35 +103,50 @@ | ||
| 119 | 103 | * @param object $query WP_Query object |
| 120 | 104 | */ |
| 121 | 105 | public function parse_query( $query ) { |
| 122 | 106 | $qv = $query->query_vars; |
| 123 | - $pll_query = new PLL_Query( $query, $this->model ); | |
| 124 | - $taxonomies = $pll_query->get_queried_taxonomies(); | |
| 107 | + $taxonomies = $this->get_queried_taxonomies( $query ); | |
| 125 | 108 | |
| 126 | - // Allow filtering recent posts and secondary queries by the current language | |
| 127 | - if ( ! empty( $this->curlang ) ) { | |
| 128 | - $pll_query->filter_query( $this->curlang ); | |
| 109 | + // to avoid returning an empty result if the query includes a translated taxonomy in a different language | |
| 110 | + $has_tax = isset( $query->tax_query->queries ) && $this->model->have_translated_taxonomy( $query->tax_query->queries ); | |
| 111 | + | |
| 112 | + // allow filtering recent posts and secondary queries by the current language | |
| 113 | + // take care not to break queries for non visible post types such as nav_menu_items | |
| 114 | + // do not filter if lang is set to an empty value | |
| 115 | + // do not filter single page and translated taxonomies to avoid conflicts | |
| 116 | + if ( ! empty( $this->curlang ) && ! isset( $qv['lang'] ) && ! $has_tax && empty( $qv['page_id'] ) && empty( $qv['pagename'] ) ) { | |
| 117 | + if ( $taxonomies && ( empty( $qv['post_type'] ) || 'any' === $qv['post_type'] ) ) { | |
| 118 | + foreach ( $taxonomies as $taxonomy ) { | |
| 119 | + $tax_object = get_taxonomy( $taxonomy ); | |
| 120 | + if ( $this->model->is_translated_post_type( $tax_object->object_type ) ) { | |
| 121 | + $this->choose_lang->set_lang_query_var( $query, $this->curlang ); | |
| 122 | + break; | |
| 123 | + } | |
| 124 | + } | |
| 125 | + } elseif ( empty( $qv['post_type'] ) || $this->model->is_translated_post_type( $qv['post_type'] ) ) { | |
| 126 | + $this->choose_lang->set_lang_query_var( $query, $this->curlang ); | |
| 127 | + } | |
| 129 | 128 | } |
| 130 | 129 | |
| 131 | - // Modifies query vars when the language is queried | |
| 132 | - if ( ! empty( $qv['lang'] ) || ( ! empty( $taxonomies ) && array( 'language' ) == array_values( $taxonomies ) ) ) { | |
| 133 | - // Do we query a custom taxonomy? | |
| 134 | - $taxonomies = array_diff( $taxonomies, array( 'language', 'category', 'post_tag' ) ); | |
| 130 | + // modifies query vars when the language is queried | |
| 131 | + if ( ! empty( $qv['lang'] ) || ( ! empty( $taxonomies ) && array( 'language') == array_values( $taxonomies ) ) ) { | |
| 132 | + // do we query a custom taxonomy? | |
| 133 | + $taxonomies = array_diff( $taxonomies , array( 'language', 'category', 'post_tag' ) ); | |
| 135 | 134 | |
| 136 | - // Remove pages query when the language is set unless we do a search | |
| 137 | - // Take care not to break the single page, attachment and taxonomies queries! | |
| 138 | - if ( empty( $qv['post_type'] ) && ! $query->is_search && ! $query->is_singular && empty( $taxonomies ) && ! $query->is_category && ! $query->is_tag ) { | |
| 135 | + // remove pages query when the language is set unless we do a search | |
| 136 | + // take care not to break the single page, attachment and taxonomies queries! | |
| 137 | + if ( empty( $qv['post_type'] ) && ! $query->is_search && ! $query->is_page && ! $query->is_attachment && empty( $taxonomies ) ) { | |
| 139 | 138 | $query->set( 'post_type', 'post' ); |
| 140 | 139 | } |
| 141 | 140 | |
| 142 | - // Unset the is_archive flag for language pages to prevent loading the archive template | |
| 143 | - // Keep archive flag for comment feed otherwise the language filter does not work | |
| 141 | + // unset the is_archive flag for language pages to prevent loading the archive template | |
| 142 | + // keep archive flag for comment feed otherwise the language filter does not work | |
| 144 | 143 | if ( empty( $taxonomies ) && ! $query->is_comment_feed && ! $query->is_post_type_archive && ! $query->is_date && ! $query->is_author && ! $query->is_category && ! $query->is_tag ) { |
| 145 | 144 | $query->is_archive = false; |
| 146 | 145 | } |
| 147 | 146 | |
| 148 | - // Unset the is_tax flag except if another custom tax is queried | |
| 149 | - if ( empty( $taxonomies ) && ( $query->is_category || $query->is_tag || $query->is_author || $query->is_post_type_archive || $query->is_date || $query->is_search || $query->is_feed ) ) { | |
| 147 | + // unset the is_tax flag except if another custom tax is queried | |
| 148 | + if ( empty( $taxonomies ) && ($query->is_category || $query->is_tag || $query->is_author || $query->is_post_type_archive || $query->is_date || $query->is_search || $query->is_feed ) ) { | |
| 150 | 149 | $query->is_tax = false; |
| 151 | 150 | unset( $query->queried_object ); // FIXME useless? |
| 152 | 151 | } |
| 153 | 152 | } |
| @@ -153,9 +152,9 @@ | ||
| 153 | 152 | } |
| 154 | 153 | } |
| 155 | 154 | |
| 156 | 155 | /** |
| 157 | - * Auto translate posts and terms ids | |
| 156 | + * auto translate posts and terms ids | |
| 158 | 157 | * |
| 159 | 158 | * @since 1.2 |
| 160 | 159 | */ |
| 161 | 160 | public function auto_translate() { |
| @@ -162,31 +161,36 @@ | ||
| 162 | 161 | $this->auto_translate = new PLL_Frontend_Auto_Translate( $this ); |
| 163 | 162 | } |
| 164 | 163 | |
| 165 | 164 | /** |
| 166 | - * Resets some variables when switching blog | |
| 167 | - * Overrides parent method | |
| 165 | + * resets some variables when switching blog | |
| 166 | + * overrides parent method | |
| 168 | 167 | * |
| 169 | 168 | * @since 1.5.1 |
| 170 | - * | |
| 171 | - * @param int $new_blog | |
| 172 | - * @param int $old_blog | |
| 173 | 169 | */ |
| 174 | 170 | public function switch_blog( $new_blog, $old_blog ) { |
| 175 | - // Need to check that some languages are defined when user is logged in, has several blogs, some without any languages | |
| 171 | + // need to check that some languages are defined when user is logged in, has several blogs, some without any languages | |
| 176 | 172 | if ( parent::switch_blog( $new_blog, $old_blog ) && did_action( 'pll_language_defined' ) && $this->model->get_languages_list() ) { |
| 177 | 173 | static $restore_curlang; |
| 178 | 174 | if ( empty( $restore_curlang ) ) { |
| 179 | - $restore_curlang = $this->curlang->slug; // To always remember the current language through blogs | |
| 175 | + $restore_curlang = $this->curlang->slug; // to always remember the current language through blogs | |
| 180 | 176 | } |
| 181 | 177 | |
| 182 | 178 | $lang = $this->model->get_language( $restore_curlang ); |
| 183 | 179 | $this->curlang = $lang ? $lang : $this->model->get_language( $this->options['default_lang'] ); |
| 184 | - | |
| 185 | - if ( isset( $this->static_pages ) ) { | |
| 186 | - $this->static_pages->init(); | |
| 187 | - } | |
| 188 | - | |
| 180 | + $this->static_pages->init(); | |
| 189 | 181 | $this->load_strings_translations(); |
| 190 | 182 | } |
| 183 | + } | |
| 184 | + | |
| 185 | + /** | |
| 186 | + * get queried taxonomies | |
| 187 | + * | |
| 188 | + * @since 1.8 | |
| 189 | + * | |
| 190 | + * @param object $query WP_Query object | |
| 191 | + * @return array queried taxonomies | |
| 192 | + */ | |
| 193 | + protected function get_queried_taxonomies( $query ) { | |
| 194 | + return isset( $query->tax_query->queried_terms ) ? array_keys( wp_list_filter( $query->tax_query->queried_terms, array( 'operator' => 'NOT IN' ), 'NOT' ) ) : array(); | |
| 191 | 195 | } |
| 192 | 196 | } |