PluginProbe
Polylang / 2.9.2
Polylang v2.9.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.9.2, at frontend/frontend.php

193 lines 6.5 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 * Frontend controller
8 * accessible as $polylang global object
9 *
10 * Properties:
11 * options => inherited, reference to Polylang options array
12 * model => inherited, reference to PLL_Model object
13 * links_model => inherited, reference to PLL_Links_Model object
14 * links => reference to PLL_Links object
15 * static_pages => reference to PLL_Frontend_Static_Pages object
16 * choose_lang => reference to PLL_Choose_Lang object
17 * curlang => current language
18 * filters => reference to PLL_Frontend_Filters object
19 * filters_links => reference to PLL_Frontend_Filters_Links object
20 * filters_search => reference to PLL_Frontend_Filters_Search object
21 * posts => reference to PLL_CRUD_Posts object
22 * terms => reference to PLL_CRUD_Terms object
23 * nav_menu => reference to PLL_Frontend_Nav_Menu object
24 * auto_translate => optional, reference to PLL_Auto_Translate object
25 *
26 * @since 1.2
27 */
28 class PLL_Frontend extends PLL_Base {
29 public $curlang;
30 public $links, $choose_lang, $filters, $filters_search, $nav_menu, $auto_translate;
31
32 /**
33 * Constructor
34 *
35 * @since 1.2
36 *
37 * @param object $links_model
38 */
39 public function __construct( &$links_model ) {
40 parent::__construct( $links_model );
41
42 add_action( 'pll_language_defined', array( $this, 'pll_language_defined' ), 1 );
43
44 // Avoids the language being the queried object when querying multiple taxonomies
45 add_action( 'parse_tax_query', array( $this, 'parse_tax_query' ), 1 );
46
47 // Filters posts by language
48 add_action( 'parse_query', array( $this, 'parse_query' ), 6 );
49
50 // Not before 'check_canonical_url'
51 if ( ! defined( 'PLL_AUTO_TRANSLATE' ) || PLL_AUTO_TRANSLATE ) {
52 add_action( 'template_redirect', array( $this, 'auto_translate' ), 7 );
53 }
54 }
55
56 /**
57 * Setups the language chooser based on options
58 *
59 * @since 1.2
60 */
61 public function init() {
62 parent::init();
63
64 $this->links = new PLL_Frontend_Links( $this );
65
66 // Static front page and page for posts
67 if ( 'page' === get_option( 'show_on_front' ) ) {
68 $this->static_pages = new PLL_Frontend_Static_Pages( $this );
69 }
70
71 // Setup the language chooser
72 $c = array( 'Content', 'Url', 'Url', 'Domain' );
73 $class = 'PLL_Choose_Lang_' . $c[ $this->options['force_lang'] ];
74 $this->choose_lang = new $class( $this );
75 $this->choose_lang->init();
76
77 // Need to load nav menu class early to correctly define the locations in the customizer when the language is set from the content
78 $this->nav_menu = new PLL_Frontend_Nav_Menu( $this );
79 }
80
81 /**
82 * Setups filters and nav menus once the language has been defined
83 *
84 * @since 1.2
85 */
86 public function pll_language_defined() {
87 // Filters
88 $this->filters_links = new PLL_Frontend_Filters_Links( $this );
89 $this->filters = new PLL_Frontend_Filters( $this );
90 $this->filters_search = new PLL_Frontend_Filters_Search( $this );
91
92 // Auto translate for Ajax
93 if ( ( ! defined( 'PLL_AUTO_TRANSLATE' ) || PLL_AUTO_TRANSLATE ) && wp_doing_ajax() ) {
94 $this->auto_translate();
95 }
96 }
97
98 /**
99 * When querying multiple taxonomies, makes sure that the language is not the queried object
100 *
101 * @since 1.8
102 *
103 * @param object $query WP_Query object
104 */
105 public function parse_tax_query( $query ) {
106 $pll_query = new PLL_Query( $query, $this->model );
107 $queried_taxonomies = $pll_query->get_queried_taxonomies();
108
109 if ( ! empty( $queried_taxonomies ) && 'language' == reset( $queried_taxonomies ) ) {
110 $query->tax_query->queried_terms['language'] = array_shift( $query->tax_query->queried_terms );
111 }
112 }
113
114 /**
115 * Modifies some query vars to "hide" that the language is a taxonomy and avoid conflicts
116 *
117 * @since 1.2
118 *
119 * @param object $query WP_Query object
120 */
121 public function parse_query( $query ) {
122 $qv = $query->query_vars;
123 $pll_query = new PLL_Query( $query, $this->model );
124 $taxonomies = $pll_query->get_queried_taxonomies();
125
126 // Allow filtering recent posts and secondary queries by the current language
127 if ( ! empty( $this->curlang ) ) {
128 $pll_query->filter_query( $this->curlang );
129 }
130
131 // Modifies query vars when the language is queried
132 if ( ! empty( $qv['lang'] ) || ( ! empty( $taxonomies ) && array( 'language' ) == array_values( $taxonomies ) ) ) {
133 // Do we query a custom taxonomy?
134 $taxonomies = array_diff( $taxonomies, array( 'language', 'category', 'post_tag' ) );
135
136 // Remove pages query when the language is set unless we do a search
137 // Take care not to break the single page, attachment and taxonomies queries!
138 if ( empty( $qv['post_type'] ) && ! $query->is_search && ! $query->is_singular && empty( $taxonomies ) && ! $query->is_category && ! $query->is_tag ) {
139 $query->set( 'post_type', 'post' );
140 }
141
142 // Unset the is_archive flag for language pages to prevent loading the archive template
143 // Keep archive flag for comment feed otherwise the language filter does not work
144 if ( empty( $taxonomies ) && ! $query->is_comment_feed && ! $query->is_post_type_archive && ! $query->is_date && ! $query->is_author && ! $query->is_category && ! $query->is_tag ) {
145 $query->is_archive = false;
146 }
147
148 // Unset the is_tax flag except if another custom tax is queried
149 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 ) ) {
150 $query->is_tax = false;
151 unset( $query->queried_object ); // FIXME useless?
152 }
153 }
154 }
155
156 /**
157 * Auto translate posts and terms ids
158 *
159 * @since 1.2
160 */
161 public function auto_translate() {
162 $this->auto_translate = new PLL_Frontend_Auto_Translate( $this );
163 }
164
165 /**
166 * Resets some variables when switching blog
167 * Overrides parent method
168 *
169 * @since 1.5.1
170 *
171 * @param int $new_blog
172 * @param int $old_blog
173 */
174 public function switch_blog( $new_blog, $old_blog ) {
175 // Need to check that some languages are defined when user is logged in, has several blogs, some without any languages
176 if ( parent::switch_blog( $new_blog, $old_blog ) && did_action( 'pll_language_defined' ) && $this->model->get_languages_list() ) {
177 static $restore_curlang;
178 if ( empty( $restore_curlang ) ) {
179 $restore_curlang = $this->curlang->slug; // To always remember the current language through blogs
180 }
181
182 $lang = $this->model->get_language( $restore_curlang );
183 $this->curlang = $lang ? $lang : $this->model->get_language( $this->options['default_lang'] );
184
185 if ( isset( $this->static_pages ) ) {
186 $this->static_pages->init();
187 }
188
189 $this->load_strings_translations();
190 }
191 }
192 }
193