PluginProbe
Polylang / 3.1.4
Polylang v3.1.4
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 3.1.4, at frontend/frontend.php

237 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Polylang
4 */
5
6 /**
7 * Main Polylang class when on frontend, accessible from @see PLL().
8 *
9 * @since 1.2
10 */
11 class PLL_Frontend extends PLL_Base {
12 /**
13 * Current language.
14 *
15 * @var PLL_Language
16 */
17 public $curlang;
18
19 /**
20 * @var PLL_Frontend_Auto_Translate
21 */
22 public $auto_translate;
23
24 /**
25 * The class selecting the current language.
26 *
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 * @var PLL_Frontend_Filters_Widgets
63 */
64 public $filters_widgets;
65
66 /**
67 * Constructor.
68 *
69 * @since 1.2
70 *
71 * @param PLL_Links_Model $links_model Reference to the links model.
72 */
73 public function __construct( &$links_model ) {
74 parent::__construct( $links_model );
75
76 add_action( 'pll_language_defined', array( $this, 'pll_language_defined' ), 1 );
77
78 // Avoids the language being the queried object when querying multiple taxonomies
79 add_action( 'parse_tax_query', array( $this, 'parse_tax_query' ), 1 );
80
81 // Filters posts by language
82 add_action( 'parse_query', array( $this, 'parse_query' ), 6 );
83
84 // Not before 'check_canonical_url'
85 if ( ! defined( 'PLL_AUTO_TRANSLATE' ) || PLL_AUTO_TRANSLATE ) {
86 add_action( 'template_redirect', array( $this, 'auto_translate' ), 7 );
87 }
88 }
89
90 /**
91 * Setups the language chooser based on options
92 *
93 * @since 1.2
94 */
95 public function init() {
96 parent::init();
97
98 $this->links = new PLL_Frontend_Links( $this );
99
100 // Static front page and page for posts
101 if ( 'page' === get_option( 'show_on_front' ) ) {
102 $this->static_pages = new PLL_Frontend_Static_Pages( $this );
103 }
104
105 // Setup the language chooser
106 $c = array( 'Content', 'Url', 'Url', 'Domain' );
107 $class = 'PLL_Choose_Lang_' . $c[ $this->options['force_lang'] ];
108 $this->choose_lang = new $class( $this );
109 $this->choose_lang->init();
110
111 // Need to load nav menu class early to correctly define the locations in the customizer when the language is set from the content
112 $this->nav_menu = new PLL_Frontend_Nav_Menu( $this );
113 }
114
115 /**
116 * Setups filters and nav menus once the language has been defined
117 *
118 * @since 1.2
119 *
120 * @return void
121 */
122 public function pll_language_defined() {
123 // Filters
124 $this->filters_links = new PLL_Frontend_Filters_Links( $this );
125 $this->filters = new PLL_Frontend_Filters( $this );
126 $this->filters_search = new PLL_Frontend_Filters_Search( $this );
127 $this->filters_widgets = new PLL_Frontend_Filters_Widgets( $this );
128
129 // Auto translate for Ajax
130 if ( ( ! defined( 'PLL_AUTO_TRANSLATE' ) || PLL_AUTO_TRANSLATE ) && wp_doing_ajax() ) {
131 $this->auto_translate();
132 }
133 }
134
135 /**
136 * When querying multiple taxonomies, makes sure that the language is not the queried object.
137 *
138 * @since 1.8
139 *
140 * @param WP_Query $query WP_Query object.
141 * @return void
142 */
143 public function parse_tax_query( $query ) {
144 $pll_query = new PLL_Query( $query, $this->model );
145 $queried_taxonomies = $pll_query->get_queried_taxonomies();
146
147 if ( ! empty( $queried_taxonomies ) && 'language' == reset( $queried_taxonomies ) ) {
148 $query->tax_query->queried_terms['language'] = array_shift( $query->tax_query->queried_terms );
149 }
150 }
151
152 /**
153 * Modifies some query vars to "hide" that the language is a taxonomy and avoid conflicts.
154 *
155 * @since 1.2
156 *
157 * @param WP_Query $query WP_Query object.
158 * @return void
159 */
160 public function parse_query( $query ) {
161 $qv = $query->query_vars;
162 $pll_query = new PLL_Query( $query, $this->model );
163 $taxonomies = $pll_query->get_queried_taxonomies();
164
165 // Allow filtering recent posts and secondary queries by the current language
166 if ( ! empty( $this->curlang ) ) {
167 $pll_query->filter_query( $this->curlang );
168 }
169
170 // Modifies query vars when the language is queried
171 if ( ! empty( $qv['lang'] ) || ( ! empty( $taxonomies ) && array( 'language' ) == array_values( $taxonomies ) ) ) {
172 // Do we query a custom taxonomy?
173 $taxonomies = array_diff( $taxonomies, array( 'language', 'category', 'post_tag' ) );
174
175 // Remove pages query when the language is set unless we do a search
176 // Take care not to break the single page, attachment and taxonomies queries!
177 if ( empty( $qv['post_type'] ) && ! $query->is_search && ! $query->is_singular && empty( $taxonomies ) && ! $query->is_category && ! $query->is_tag ) {
178 $query->set( 'post_type', 'post' );
179 }
180
181 // Unset the is_archive flag for language pages to prevent loading the archive template
182 // Keep archive flag for comment feed otherwise the language filter does not work
183 if ( empty( $taxonomies ) && ! $query->is_comment_feed && ! $query->is_post_type_archive && ! $query->is_date && ! $query->is_author && ! $query->is_category && ! $query->is_tag ) {
184 $query->is_archive = false;
185 }
186
187 // Unset the is_tax flag except if another custom tax is queried
188 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 ) ) {
189 $query->is_tax = false;
190 unset( $query->queried_object ); // FIXME useless?
191 }
192 }
193 }
194
195 /**
196 * Auto translate posts and terms ids
197 *
198 * @since 1.2
199 *
200 * @return void
201 */
202 public function auto_translate() {
203 $this->auto_translate = new PLL_Frontend_Auto_Translate( $this );
204 }
205
206 /**
207 * Resets some variables when the blog is switched.
208 * Overrides the parent method.
209 *
210 * @since 1.5.1
211 *
212 * @param int $new_blog_id New blog ID.
213 * @param int $prev_blog_id Previous blog ID.
214 * @return void
215 */
216 public function switch_blog( $new_blog_id, $prev_blog_id ) {
217 parent::switch_blog( $new_blog_id, $prev_blog_id );
218
219 // Need to check that some languages are defined when user is logged in, has several blogs, some without any languages.
220 if ( $this->is_active_on_new_blog( $new_blog_id, $prev_blog_id ) && did_action( 'pll_language_defined' ) && $this->model->get_languages_list() ) {
221 static $restore_curlang;
222 if ( empty( $restore_curlang ) ) {
223 $restore_curlang = $this->curlang->slug; // To always remember the current language through blogs.
224 }
225
226 $lang = $this->model->get_language( $restore_curlang );
227 $this->curlang = $lang ? $lang : $this->model->get_language( $this->options['default_lang'] );
228
229 if ( isset( $this->static_pages ) ) {
230 $this->static_pages->init();
231 }
232
233 $this->load_strings_translations();
234 }
235 }
236 }
237