PluginProbe
Polylang / 2.6.2
Polylang v2.6.2
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.2, at frontend/frontend.php

218 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
102 /**
103 * Setups filters and nav menus once the language has been defined
104 *
105 * @since 1.2
106 */
107 public function pll_language_defined() {
108 // Filters
109 $this->filters_links = new PLL_Frontend_Filters_Links( $this );
110 $this->filters = new PLL_Frontend_Filters( $this );
111 $this->filters_search = new PLL_Frontend_Filters_Search( $this );
112 $this->posts = new PLL_CRUD_Posts( $this );
113 $this->terms = new PLL_CRUD_Terms( $this );
114
115 $this->sync = new PLL_Sync( $this );
116
117 // Auto translate for Ajax
118 if ( ( ! defined( 'PLL_AUTO_TRANSLATE' ) || PLL_AUTO_TRANSLATE ) && wp_doing_ajax() ) {
119 $this->auto_translate();
120 }
121 }
122
123 /**
124 * When querying multiple taxonomies, makes sure that the language is not the queried object
125 *
126 * @since 1.8
127 *
128 * @param object $query WP_Query object
129 */
130 public function parse_tax_query( $query ) {
131 $pll_query = new PLL_Query( $query, $this->model );
132 $queried_taxonomies = $pll_query->get_queried_taxonomies();
133
134 if ( ! empty( $queried_taxonomies ) && 'language' == reset( $queried_taxonomies ) ) {
135 $query->tax_query->queried_terms['language'] = array_shift( $query->tax_query->queried_terms );
136 }
137 }
138
139 /**
140 * Modifies some query vars to "hide" that the language is a taxonomy and avoid conflicts
141 *
142 * @since 1.2
143 *
144 * @param object $query WP_Query object
145 */
146 public function parse_query( $query ) {
147 $qv = $query->query_vars;
148 $pll_query = new PLL_Query( $query, $this->model );
149 $taxonomies = $pll_query->get_queried_taxonomies();
150
151 // Allow filtering recent posts and secondary queries by the current language
152 if ( ! empty( $this->curlang ) ) {
153 $pll_query->filter_query( $this->curlang );
154 }
155
156 // Modifies query vars when the language is queried
157 if ( ! empty( $qv['lang'] ) || ( ! empty( $taxonomies ) && array( 'language' ) == array_values( $taxonomies ) ) ) {
158 // Do we query a custom taxonomy?
159 $taxonomies = array_diff( $taxonomies, array( 'language', 'category', 'post_tag' ) );
160
161 // Remove pages query when the language is set unless we do a search
162 // Take care not to break the single page, attachment and taxonomies queries!
163 if ( empty( $qv['post_type'] ) && ! $query->is_search && ! $query->is_singular && empty( $taxonomies ) ) {
164 $query->set( 'post_type', 'post' );
165 }
166
167 // Unset the is_archive flag for language pages to prevent loading the archive template
168 // Keep archive flag for comment feed otherwise the language filter does not work
169 if ( empty( $taxonomies ) && ! $query->is_comment_feed && ! $query->is_post_type_archive && ! $query->is_date && ! $query->is_author && ! $query->is_category && ! $query->is_tag ) {
170 $query->is_archive = false;
171 }
172
173 // Unset the is_tax flag except if another custom tax is queried
174 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 ) ) {
175 $query->is_tax = false;
176 unset( $query->queried_object ); // FIXME useless?
177 }
178 }
179 }
180
181 /**
182 * Auto translate posts and terms ids
183 *
184 * @since 1.2
185 */
186 public function auto_translate() {
187 $this->auto_translate = new PLL_Frontend_Auto_Translate( $this );
188 }
189
190 /**
191 * Resets some variables when switching blog
192 * Overrides parent method
193 *
194 * @since 1.5.1
195 *
196 * @param int $new_blog
197 * @param int $old_blog
198 */
199 public function switch_blog( $new_blog, $old_blog ) {
200 // Need to check that some languages are defined when user is logged in, has several blogs, some without any languages
201 if ( parent::switch_blog( $new_blog, $old_blog ) && did_action( 'pll_language_defined' ) && $this->model->get_languages_list() ) {
202 static $restore_curlang;
203 if ( empty( $restore_curlang ) ) {
204 $restore_curlang = $this->curlang->slug; // To always remember the current language through blogs
205 }
206
207 $lang = $this->model->get_language( $restore_curlang );
208 $this->curlang = $lang ? $lang : $this->model->get_language( $this->options['default_lang'] );
209
210 if ( isset( $this->static_pages ) ) {
211 $this->static_pages->init();
212 }
213
214 $this->load_strings_translations();
215 }
216 }
217 }
218