PluginProbe
Polylang / 2.6.3
Polylang v2.6.3
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.6.3, at frontend/frontend.php

222 lines 7.5 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 * 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
23 *
24 * @since 1.2
25 */
26 class PLL_Frontend extends PLL_Base {
27 public $curlang;
28 public $links, $choose_lang, $filters, $filters_search, $nav_menu, $auto_translate;
29
30 /**
31 * Constructor
32 *
33 * @since 1.2
34 *
35 * @param object $links_model
36 */
37 public function __construct( &$links_model ) {
38 parent::__construct( $links_model );
39
40 add_action( 'pll_language_defined', array( $this, 'pll_language_defined' ), 1 );
41
42 // Avoids the language being the queried object when querying multiple taxonomies
43 add_action( 'parse_tax_query', array( $this, 'parse_tax_query' ), 1 );
44
45 // Filters posts by language
46 add_action( 'parse_query', array( $this, 'parse_query' ), 6 );
47
48 // Not before 'check_canonical_url'
49 if ( ! defined( 'PLL_AUTO_TRANSLATE' ) || PLL_AUTO_TRANSLATE ) {
50 add_action( 'template_redirect', array( $this, 'auto_translate' ), 7 );
51 }
52 }
53
54 /**
55 * Setups the language chooser based on options
56 *
57 * @since 1.2
58 */
59 public function init() {
60 parent::init();
61
62 $this->links = new PLL_Frontend_Links( $this );
63
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
70 $c = array( 'Content', 'Url', 'Url', 'Domain' );
71 $class = 'PLL_Choose_Lang_' . $c[ $this->options['force_lang'] ];
72 $this->choose_lang = new $class( $this );
73 $this->choose_lang->init();
74
75 // Need to load nav menu class early to correctly define the locations in the customizer when the language is set from the content
76 $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 = version_compare( $GLOBALS['wp_version'], '4.8', '<' ) ?
96 new PLL_Frontend_Share_Term_Slug( $this ) :
97 new PLL_Share_Term_Slug( $this );
98 }
99 }
100
101 if ( class_exists( 'PLL_Sync_Post' ) ) {
102 $this->sync_post = new PLL_Sync_Post( $this );
103 }
104 }
105
106 /**
107 * Setups filters and nav menus once the language has been defined
108 *
109 * @since 1.2
110 */
111 public function pll_language_defined() {
112 // Filters
113 $this->filters_links = new PLL_Frontend_Filters_Links( $this );
114 $this->filters = new PLL_Frontend_Filters( $this );
115 $this->filters_search = new PLL_Frontend_Filters_Search( $this );
116 $this->posts = new PLL_CRUD_Posts( $this );
117 $this->terms = new PLL_CRUD_Terms( $this );
118
119 $this->sync = new PLL_Sync( $this );
120
121 // Auto translate for Ajax
122 if ( ( ! defined( 'PLL_AUTO_TRANSLATE' ) || PLL_AUTO_TRANSLATE ) && wp_doing_ajax() ) {
123 $this->auto_translate();
124 }
125 }
126
127 /**
128 * When querying multiple taxonomies, makes sure that the language is not the queried object
129 *
130 * @since 1.8
131 *
132 * @param object $query WP_Query object
133 */
134 public function parse_tax_query( $query ) {
135 $pll_query = new PLL_Query( $query, $this->model );
136 $queried_taxonomies = $pll_query->get_queried_taxonomies();
137
138 if ( ! empty( $queried_taxonomies ) && 'language' == reset( $queried_taxonomies ) ) {
139 $query->tax_query->queried_terms['language'] = array_shift( $query->tax_query->queried_terms );
140 }
141 }
142
143 /**
144 * Modifies some query vars to "hide" that the language is a taxonomy and avoid conflicts
145 *
146 * @since 1.2
147 *
148 * @param object $query WP_Query object
149 */
150 public function parse_query( $query ) {
151 $qv = $query->query_vars;
152 $pll_query = new PLL_Query( $query, $this->model );
153 $taxonomies = $pll_query->get_queried_taxonomies();
154
155 // Allow filtering recent posts and secondary queries by the current language
156 if ( ! empty( $this->curlang ) ) {
157 $pll_query->filter_query( $this->curlang );
158 }
159
160 // Modifies query vars when the language is queried
161 if ( ! empty( $qv['lang'] ) || ( ! empty( $taxonomies ) && array( 'language' ) == array_values( $taxonomies ) ) ) {
162 // Do we query a custom taxonomy?
163 $taxonomies = array_diff( $taxonomies, array( 'language', 'category', 'post_tag' ) );
164
165 // Remove pages query when the language is set unless we do a search
166 // Take care not to break the single page, attachment and taxonomies queries!
167 if ( empty( $qv['post_type'] ) && ! $query->is_search && ! $query->is_singular && empty( $taxonomies ) ) {
168 $query->set( 'post_type', 'post' );
169 }
170
171 // Unset the is_archive flag for language pages to prevent loading the archive template
172 // Keep archive flag for comment feed otherwise the language filter does not work
173 if ( empty( $taxonomies ) && ! $query->is_comment_feed && ! $query->is_post_type_archive && ! $query->is_date && ! $query->is_author && ! $query->is_category && ! $query->is_tag ) {
174 $query->is_archive = false;
175 }
176
177 // Unset the is_tax flag except if another custom tax is queried
178 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 ) ) {
179 $query->is_tax = false;
180 unset( $query->queried_object ); // FIXME useless?
181 }
182 }
183 }
184
185 /**
186 * Auto translate posts and terms ids
187 *
188 * @since 1.2
189 */
190 public function auto_translate() {
191 $this->auto_translate = new PLL_Frontend_Auto_Translate( $this );
192 }
193
194 /**
195 * Resets some variables when switching blog
196 * Overrides parent method
197 *
198 * @since 1.5.1
199 *
200 * @param int $new_blog
201 * @param int $old_blog
202 */
203 public function switch_blog( $new_blog, $old_blog ) {
204 // Need to check that some languages are defined when user is logged in, has several blogs, some without any languages
205 if ( parent::switch_blog( $new_blog, $old_blog ) && did_action( 'pll_language_defined' ) && $this->model->get_languages_list() ) {
206 static $restore_curlang;
207 if ( empty( $restore_curlang ) ) {
208 $restore_curlang = $this->curlang->slug; // To always remember the current language through blogs
209 }
210
211 $lang = $this->model->get_language( $restore_curlang );
212 $this->curlang = $lang ? $lang : $this->model->get_language( $this->options['default_lang'] );
213
214 if ( isset( $this->static_pages ) ) {
215 $this->static_pages->init();
216 }
217
218 $this->load_strings_translations();
219 }
220 }
221 }
222