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