PluginProbe
Polylang / 2.1
Polylang v2.1
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
← All changes | frontend/frontend.php +85 -119 3.02.1 View file →
@@ -1,70 +1,36 @@
1 1 <?php
2 -/**
3 - * @package Polylang
4 - */
5 2
6 3 /**
7 - * Main Polylang class when on frontend, accessible from @see PLL().
4 + * frontend controller
5 + * accessible as $polylang global object
8 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 + *
9 21 * @since 1.2
10 22 */
11 23 class PLL_Frontend extends PLL_Base {
12 - /**
13 - * Current language.
14 - *
15 - * @var PLL_Language
16 - */
17 24 public $curlang;
25 + public $links, $choose_lang, $filters, $filters_search, $nav_menu, $auto_translate;
18 26
19 27 /**
20 - * @var PLL_Frontend_Auto_Translate
21 - */
22 - public $auto_translate;
23 -
24 - /**
25 - * The class selecting the current language.
28 + * constructor
26 29 *
27 - * @var PLL_Choose_Lang
28 - */
29 - public $choose_lang;
30 -
31 - /**
32 - * @var PLL_Frontend_Filters
33 - */
34 - public $filters;
35 -
36 - /**
37 - * @var PLL_Frontend_Filters_Links
38 - */
39 - public $filters_links;
40 -
41 - /**
42 - * @var PLL_Frontend_Filters_Search
43 - */
44 - public $filters_search;
45 -
46 - /**
47 - * @var PLL_Frontend_Links
48 - */
49 - public $links;
50 -
51 - /**
52 - * @var PLL_Frontend_Nav_Menu
53 - */
54 - public $nav_menu;
55 -
56 - /**
57 - * @var PLL_Frontend_Static_Pages
58 - */
59 - public $static_pages;
60 -
61 - /**
62 - * Constructor.
63 - *
64 30 * @since 1.2
65 31 *
66 - * @param PLL_Links_Model $links_model Reference to the links model.
32 + * @param object $links_model
67 33 */
68 34 public function __construct( &$links_model ) {
69 35 parent::__construct( $links_model );
70 36
@@ -69,15 +35,15 @@
69 35 parent::__construct( $links_model );
70 36
71 37 add_action( 'pll_language_defined', array( $this, 'pll_language_defined' ), 1 );
72 38
73 - // Avoids the language being the queried object when querying multiple taxonomies
39 + // avoids the language being the queried object when querying multiple taxonomies
74 40 add_action( 'parse_tax_query', array( $this, 'parse_tax_query' ), 1 );
75 41
76 - // Filters posts by language
42 + // filters posts by language
77 43 add_action( 'parse_query', array( $this, 'parse_query' ), 6 );
78 44
79 - // Not before 'check_canonical_url'
45 + // not before 'check_canonical_url'
80 46 if ( ! defined( 'PLL_AUTO_TRANSLATE' ) || PLL_AUTO_TRANSLATE ) {
81 47 add_action( 'template_redirect', array( $this, 'auto_translate' ), 7 );
82 48 }
83 49 }
@@ -82,62 +48,48 @@
82 48 }
83 49 }
84 50
85 51 /**
86 - * Setups the language chooser based on options
52 + * setups the language chooser based on options
87 53 *
88 54 * @since 1.2
89 55 */
90 56 public function init() {
91 - parent::init();
92 -
93 57 $this->links = new PLL_Frontend_Links( $this );
58 + $this->static_pages = new PLL_Frontend_Static_Pages( $this );
94 59
95 - // Static front page and page for posts
96 - if ( 'page' === get_option( 'show_on_front' ) ) {
97 - $this->static_pages = new PLL_Frontend_Static_Pages( $this );
98 - }
99 -
100 - // Setup the language chooser
60 + // setup the language chooser
101 61 $c = array( 'Content', 'Url', 'Url', 'Domain' );
102 62 $class = 'PLL_Choose_Lang_' . $c[ $this->options['force_lang'] ];
103 63 $this->choose_lang = new $class( $this );
104 64 $this->choose_lang->init();
105 65
106 - // 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
107 67 $this->nav_menu = new PLL_Frontend_Nav_Menu( $this );
108 68 }
109 69
110 70 /**
111 - * Setups filters and nav menus once the language has been defined
71 + * setups filters and nav menus once the language has been defined
112 72 *
113 73 * @since 1.2
114 - *
115 - * @return void
116 74 */
117 75 public function pll_language_defined() {
118 - // Filters
76 + // filters
119 77 $this->filters_links = new PLL_Frontend_Filters_Links( $this );
120 78 $this->filters = new PLL_Frontend_Filters( $this );
121 79 $this->filters_search = new PLL_Frontend_Filters_Search( $this );
80 + }
122 81
123 - // Auto translate for Ajax
124 - if ( ( ! defined( 'PLL_AUTO_TRANSLATE' ) || PLL_AUTO_TRANSLATE ) && wp_doing_ajax() ) {
125 - $this->auto_translate();
126 - }
127 - }
128 82
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 - * @param WP_Query $query WP_Query object.
135 - * @return void
88 + * @param object $query WP_Query object
136 89 */
137 90 public function parse_tax_query( $query ) {
138 - $pll_query = new PLL_Query( $query, $this->model );
139 - $queried_taxonomies = $pll_query->get_queried_taxonomies();
91 + $queried_taxonomies = $this->get_queried_taxonomies( $query );
140 92
141 93 if ( ! empty( $queried_taxonomies ) && 'language' == reset( $queried_taxonomies ) ) {
142 94 $query->tax_query->queried_terms['language'] = array_shift( $query->tax_query->queried_terms );
143 95 }
@@ -143,44 +95,58 @@
143 95 }
144 96 }
145 97
146 98 /**
147 - * 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
148 100 *
149 101 * @since 1.2
150 102 *
151 - * @param WP_Query $query WP_Query object.
152 - * @return void
103 + * @param object $query WP_Query object
153 104 */
154 105 public function parse_query( $query ) {
155 106 $qv = $query->query_vars;
156 - $pll_query = new PLL_Query( $query, $this->model );
157 - $taxonomies = $pll_query->get_queried_taxonomies();
107 + $taxonomies = $this->get_queried_taxonomies( $query );
158 108
159 - // Allow filtering recent posts and secondary queries by the current language
160 - if ( ! empty( $this->curlang ) ) {
161 - $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 + }
162 128 }
163 129
164 - // Modifies query vars when the language is queried
165 - if ( ! empty( $qv['lang'] ) || ( ! empty( $taxonomies ) && array( 'language' ) == array_values( $taxonomies ) ) ) {
166 - // Do we query a custom taxonomy?
167 - $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' ) );
168 134
169 - // Remove pages query when the language is set unless we do a search
170 - // Take care not to break the single page, attachment and taxonomies queries!
171 - 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 ) ) {
172 138 $query->set( 'post_type', 'post' );
173 139 }
174 140
175 - // Unset the is_archive flag for language pages to prevent loading the archive template
176 - // 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
177 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 ) {
178 144 $query->is_archive = false;
179 145 }
180 146
181 - // Unset the is_tax flag except if another custom tax is queried
182 - 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 ) ) {
183 149 $query->is_tax = false;
184 150 unset( $query->queried_object ); // FIXME useless?
185 151 }
186 152 }
@@ -186,13 +152,11 @@
186 152 }
187 153 }
188 154
189 155 /**
190 - * Auto translate posts and terms ids
156 + * auto translate posts and terms ids
191 157 *
192 158 * @since 1.2
193 - *
194 - * @return void
195 159 */
196 160 public function auto_translate() {
197 161 $this->auto_translate = new PLL_Frontend_Auto_Translate( $this );
198 162 }
@@ -197,34 +161,36 @@
197 161 $this->auto_translate = new PLL_Frontend_Auto_Translate( $this );
198 162 }
199 163
200 164 /**
201 - * Resets some variables when the blog is switched.
202 - * Overrides the parent method.
165 + * resets some variables when switching blog
166 + * overrides parent method
203 167 *
204 168 * @since 1.5.1
205 - *
206 - * @param int $new_blog_id New blog ID.
207 - * @param int $prev_blog_id Previous blog ID.
208 - * @return void
209 169 */
210 - public function switch_blog( $new_blog_id, $prev_blog_id ) {
211 - parent::switch_blog( $new_blog_id, $prev_blog_id );
212 -
213 - // Need to check that some languages are defined when user is logged in, has several blogs, some without any languages.
214 - if ( $this->is_active_on_new_blog( $new_blog_id, $prev_blog_id ) && did_action( 'pll_language_defined' ) && $this->model->get_languages_list() ) {
170 + public function switch_blog( $new_blog, $old_blog ) {
171 + // need to check that some languages are defined when user is logged in, has several blogs, some without any languages
172 + if ( parent::switch_blog( $new_blog, $old_blog ) && did_action( 'pll_language_defined' ) && $this->model->get_languages_list() ) {
215 173 static $restore_curlang;
216 174 if ( empty( $restore_curlang ) ) {
217 - $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
218 176 }
219 177
220 178 $lang = $this->model->get_language( $restore_curlang );
221 179 $this->curlang = $lang ? $lang : $this->model->get_language( $this->options['default_lang'] );
222 -
223 - if ( isset( $this->static_pages ) ) {
224 - $this->static_pages->init();
225 - }
226 -
180 + $this->static_pages->init();
227 181 $this->load_strings_translations();
228 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();
229 195 }
230 196 }