PluginProbe
Polylang / 2.0
Polylang v2.0
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / frontend / frontend.php

frontend.php in Polylang 2.0, at frontend/frontend.php

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