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

198 lines 7.2 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 * 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 *
21 * @since 1.2
22 */
23 class PLL_Frontend extends PLL_Base {
24 public $curlang;
25 public $links, $choose_lang, $filters, $filters_search, $nav_menu, $auto_translate;
26
27 /**
28 * constructor
29 *
30 * @since 1.2
31 *
32 * @param object $links_model
33 */
34 public function __construct( &$links_model ) {
35 parent::__construct( $links_model );
36
37 add_action( 'pll_language_defined', array( $this, 'pll_language_defined' ), 1 );
38
39 // avoids the language being the queried object when querying multiple taxonomies
40 add_action( 'parse_tax_query', array( $this, 'parse_tax_query' ), 1 );
41
42 // filters posts by language
43 add_action( 'parse_query', array( $this, 'parse_query' ), 6 );
44
45 // not before 'check_canonical_url'
46 if ( ! defined( 'PLL_AUTO_TRANSLATE' ) || PLL_AUTO_TRANSLATE ) {
47 add_action( 'template_redirect', array( $this, 'auto_translate' ), 7 );
48 }
49 }
50
51 /**
52 * setups the language chooser based on options
53 *
54 * @since 1.2
55 */
56 public function init() {
57 $this->links = new PLL_Frontend_Links( $this );
58 $this->static_pages = new PLL_Frontend_Static_Pages( $this );
59
60 // setup the language chooser
61 $c = array( 'Content', 'Url', 'Url', 'Domain' );
62 $class = 'PLL_Choose_Lang_' . $c[ $this->options['force_lang'] ];
63 $this->choose_lang = new $class( $this );
64 $this->choose_lang->init();
65
66 // need to load nav menu class early to correctly define the locations in the customizer when the language is set from the content
67 $this->nav_menu = new PLL_Frontend_Nav_Menu( $this );
68 }
69
70 /**
71 * setups filters and nav menus once the language has been defined
72 *
73 * @since 1.2
74 */
75 public function pll_language_defined() {
76 // filters
77 $this->filters_links = new PLL_Frontend_Filters_Links( $this );
78 $this->filters = new PLL_Frontend_Filters( $this );
79 $this->filters_search = new PLL_Frontend_Filters_Search( $this );
80 }
81
82
83 /**
84 * when querying multiple taxonomies, makes sure that the language is not the queried object
85 *
86 * @since 1.8
87 *
88 * @param object $query WP_Query object
89 */
90 public function parse_tax_query( $query ) {
91 $queried_taxonomies = $this->get_queried_taxonomies( $query );
92
93 if ( ! empty( $queried_taxonomies ) && 'language' == reset( $queried_taxonomies ) ) {
94 $query->tax_query->queried_terms['language'] = array_shift( $query->tax_query->queried_terms );
95 }
96 }
97
98 /**
99 * modifies some query vars to "hide" that the language is a taxonomy and avoid conflicts
100 *
101 * @since 1.2
102 *
103 * @param object $query WP_Query object
104 */
105 public function parse_query( $query ) {
106 $qv = $query->query_vars;
107
108 // to avoid returning an empty result if the query includes a translated taxonomy in a different language
109 $has_tax = isset( $query->tax_query->queries ) && $this->model->have_translated_taxonomy( $query->tax_query->queries );
110
111 // allow filtering recent posts and secondary queries by the current language
112 // take care not to break queries for non visible post types such as nav_menu_items
113 // do not filter if lang is set to an empty value
114 // do not filter single page and translated taxonomies to avoid conflicts
115 if ( ! empty( $this->curlang ) && ! isset( $qv['lang'] ) && ! $has_tax && empty( $qv['page_id'] ) && empty( $qv['pagename'] ) ) {
116 $taxonomies = $this->get_queried_taxonomies( $query );
117
118 if ( $taxonomies && ( empty( $qv['post_type'] ) || 'any' === $qv['post_type'] ) ) {
119 foreach ( $taxonomies as $taxonomy ) {
120 $tax_object = get_taxonomy( $taxonomy );
121 if ( $this->model->is_translated_post_type( $tax_object->object_type ) ) {
122 $this->choose_lang->set_lang_query_var( $query, $this->curlang );
123 break;
124 }
125 }
126 } elseif ( empty( $qv['post_type'] ) || $this->model->is_translated_post_type( $qv['post_type'] ) ) {
127 $this->choose_lang->set_lang_query_var( $query, $this->curlang );
128 }
129 }
130
131 // modifies query vars when the language is queried
132 if ( ! empty( $qv['lang'] ) ) {
133 // do we query a custom taxonomy?
134 $taxonomies = array_diff( $this->get_queried_taxonomies( $query ) , 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_page && ! $query->is_attachment && empty( $taxonomies ) ) {
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 public function switch_blog( $new_blog, $old_blog ) {
172 // need to check that some languages are defined when user is logged in, has several blogs, some without any languages
173 if ( parent::switch_blog( $new_blog, $old_blog ) && did_action( 'pll_language_defined' ) && $this->model->get_languages_list() ) {
174 static $restore_curlang;
175 if ( empty( $restore_curlang ) ) {
176 $restore_curlang = $this->curlang->slug; // to always remember the current language through blogs
177 }
178
179 $lang = $this->model->get_language( $restore_curlang );
180 $this->curlang = $lang ? $lang : $this->model->get_language( $this->options['default_lang'] );
181 $this->static_pages->init();
182 $this->load_strings_translations();
183 }
184 }
185
186 /**
187 * get queried taxonomies
188 *
189 * @since 1.8
190 *
191 * @param object $query WP_Query object
192 * @return array queried taxonomies
193 */
194 protected function get_queried_taxonomies( $query ) {
195 return isset( $query->tax_query->queried_terms ) ? array_keys( wp_list_filter( $query->tax_query->queried_terms, array( 'operator' => 'NOT IN' ), 'NOT' ) ) : array();
196 }
197 }
198