PluginProbe
Polylang / 1.5
Polylang v1.5
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
← All changes | frontend/frontend.php +79 -167 2.71.5 View file →
@@ -1,223 +1,135 @@
1 1 <?php
2 2
3 -/**
4 - * Frontend controller
3 +/*
4 + * frontend controller
5 5 * accessible as $polylang global object
6 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
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 + * choose_lang => reference to PLL_Choose_lang object
13 + * curlang => current language
14 + * filters => reference to PLL_Filters object
15 + * filters_search => reference to PLL_Frontend_Filters_Search object
16 + * nav_menu => reference to PLL_Frontend_Nav_Menu object
17 + * auto_translate => optional, reference to PLL_Auto_Translate object
23 18 *
24 19 * @since 1.2
25 20 */
26 -class PLL_Frontend extends PLL_Base {
21 +class PLL_Frontend extends PLL_Base{
27 22 public $curlang;
28 - public $links, $choose_lang, $filters, $filters_search, $nav_menu, $auto_translate;
23 + public $links, $choose_lang, $filters, $filters_search, $auto_translate;
29 24
30 - /**
31 - * Constructor
25 + /*
26 + * constructor
32 27 *
33 28 * @since 1.2
34 29 *
35 30 * @param object $links_model
36 31 */
37 - public function __construct( &$links_model ) {
38 - parent::__construct( $links_model );
32 + public function __construct(&$links_model) {
33 + parent::__construct($links_model);
39 34
40 - add_action( 'pll_language_defined', array( $this, 'pll_language_defined' ), 1 );
35 + add_action('pll_language_defined', array(&$this, 'pll_language_defined'), 1, 2);
41 36
42 - // Avoids the language being the queried object when querying multiple taxonomies
43 - add_action( 'parse_tax_query', array( $this, 'parse_tax_query' ), 1 );
37 + // filters posts by language
38 + add_filter('parse_query', array(&$this, 'parse_query'), 6);
44 39
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 - }
40 + // not before 'check_language_code_in_url'
41 + if (!defined('PLL_AUTO_TRANSLATE') || PLL_AUTO_TRANSLATE)
42 + add_action('wp', array(&$this, 'auto_translate'), 20);
52 43 }
53 44
54 - /**
55 - * Setups the language chooser based on options
45 + /*
46 + * setups url modifications based on the links mode
47 + * setups the language chooser based on options
56 48 *
57 49 * @since 1.2
50 + *
51 + * @param object $links_model
58 52 */
59 53 public function init() {
60 - parent::init();
54 + $this->links = new PLL_Frontend_Links($this);
61 55
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 = new PLL_Share_Term_Slug( $this );
96 - }
97 - }
98 -
99 - if ( class_exists( 'PLL_Sync_Post_Model' ) ) {
100 - $this->sync_post_model = new PLL_Sync_Post_Model( $this );
101 - }
102 -
103 - if ( class_exists( 'PLL_Sync_Post' ) ) {
104 - $this->sync_post = new PLL_Sync_Post( $this );
105 - }
56 + $c = array('Content', 'Url', 'Url', 'Domain');
57 + $class = 'PLL_Choose_Lang_' . $c[$this->options['force_lang'] * (get_option('permalink_structure') ? 1 : 0 )];
58 + $this->choose_lang = new $class($this);
106 59 }
107 60
108 - /**
109 - * Setups filters and nav menus once the language has been defined
61 + /*
62 + * setups filters and nav menus once the language has been defined
110 63 *
111 64 * @since 1.2
65 + *
66 + * @param string $slug current language slug
67 + * @param object $curlang current language object
112 68 */
113 - public function pll_language_defined() {
114 - // Filters
115 - $this->filters_links = new PLL_Frontend_Filters_Links( $this );
116 - $this->filters = new PLL_Frontend_Filters( $this );
117 - $this->filters_search = new PLL_Frontend_Filters_Search( $this );
118 - $this->posts = new PLL_CRUD_Posts( $this );
119 - $this->terms = new PLL_CRUD_Terms( $this );
69 + public function pll_language_defined($slug, $curlang) {
70 + $this->curlang = $curlang;
120 71
121 - $this->sync = new PLL_Sync( $this );
72 + // filters
73 + $this->filters = new PLL_Frontend_Filters($this);
74 + $this->filters_search = new PLL_Frontend_Filters_Search($this);
122 75
123 - // Auto translate for Ajax
124 - if ( ( ! defined( 'PLL_AUTO_TRANSLATE' ) || PLL_AUTO_TRANSLATE ) && wp_doing_ajax() ) {
125 - $this->auto_translate();
126 - }
76 + // nav menu
77 + $this->nav_menu = new PLL_Frontend_Nav_Menu($this);
127 78 }
128 79
129 - /**
130 - * When querying multiple taxonomies, makes sure that the language is not the queried object
80 + /*
81 + * modifies some query vars to "hide" that the language is a taxonomy and avoid conflicts
131 82 *
132 - * @since 1.8
133 - *
134 - * @param object $query WP_Query object
135 - */
136 - public function parse_tax_query( $query ) {
137 - $pll_query = new PLL_Query( $query, $this->model );
138 - $queried_taxonomies = $pll_query->get_queried_taxonomies();
139 -
140 - if ( ! empty( $queried_taxonomies ) && 'language' == reset( $queried_taxonomies ) ) {
141 - $query->tax_query->queried_terms['language'] = array_shift( $query->tax_query->queried_terms );
142 - }
143 - }
144 -
145 - /**
146 - * Modifies some query vars to "hide" that the language is a taxonomy and avoid conflicts
147 - *
148 83 * @since 1.2
149 84 *
150 85 * @param object $query WP_Query object
151 86 */
152 - public function parse_query( $query ) {
87 + public function parse_query($query) {
153 88 $qv = $query->query_vars;
154 - $pll_query = new PLL_Query( $query, $this->model );
155 - $taxonomies = $pll_query->get_queried_taxonomies();
156 89
157 - // Allow filtering recent posts and secondary queries by the current language
158 - if ( ! empty( $this->curlang ) ) {
159 - $pll_query->filter_query( $this->curlang );
90 + // to avoid conflict beetwen taxonomies
91 + // FIXME generalize post format like taxonomies (untranslated but filtered)
92 + $has_tax = false;
93 + if (isset($query->tax_query->queries))
94 + foreach ($query->tax_query->queries as $tax)
95 + if ('post_format' != $tax['taxonomy'])
96 + $has_tax = true;
97 +
98 + // allow filtering recent posts and secondary queries by the current language
99 + // take care not to break queries for non visible post types such as nav_menu_items
100 + // do not filter if lang is set to an empty value
101 + // do not filter single page and translated taxonomies to avoid conflicts
102 + if (!empty($this->curlang) && !isset($qv['lang']) && !$has_tax && empty($qv['page_id']) && empty($qv['pagename']) && (empty($qv['post_type']) || $this->model->is_translated_post_type($qv['post_type']))) {
103 + $this->choose_lang->set_lang_query_var($query, $this->curlang);
160 104 }
161 105
162 - // Modifies query vars when the language is queried
163 - if ( ! empty( $qv['lang'] ) || ( ! empty( $taxonomies ) && array( 'language' ) == array_values( $taxonomies ) ) ) {
164 - // Do we query a custom taxonomy?
165 - $taxonomies = array_diff( $taxonomies, array( 'language', 'category', 'post_tag' ) );
106 + // modifies query vars when the language is queried
107 + if (!empty($qv['lang'])) {
108 + // remove pages query when the language is set unless we do a search
109 + if (empty($qv['post_type']) && !$query->is_search)
110 + $query->set('post_type', 'post');
166 111
167 - // Remove pages query when the language is set unless we do a search
168 - // Take care not to break the single page, attachment and taxonomies queries!
169 - if ( empty( $qv['post_type'] ) && ! $query->is_search && ! $query->is_singular && empty( $taxonomies ) && ! $query->is_category && ! $query->is_tag ) {
170 - $query->set( 'post_type', 'post' );
171 - }
172 -
173 - // Unset the is_archive flag for language pages to prevent loading the archive template
174 - // Keep archive flag for comment feed otherwise the language filter does not work
175 - if ( empty( $taxonomies ) && ! $query->is_comment_feed && ! $query->is_post_type_archive && ! $query->is_date && ! $query->is_author && ! $query->is_category && ! $query->is_tag ) {
112 + // unset the is_archive flag for language pages to prevent loading the archive template
113 + // keep archive flag for comment feed otherwise the language filter does not work
114 + if (!$query->is_comment_feed && !$query->is_post_type_archive && !$query->is_date && !$query->is_author && !$query->is_category && !$query->is_tag && !$query->is_tax('post_format'))
176 115 $query->is_archive = false;
177 - }
178 116
179 - // Unset the is_tax flag except if another custom tax is queried
180 - 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 ) ) {
117 + // unset the is_tax flag for authors pages and post types archives
118 + // FIXME Should I do this for other cases?
119 + if ($query->is_author || $query->is_post_type_archive || $query->is_date || $query->is_search) {
181 120 $query->is_tax = false;
182 - unset( $query->queried_object ); // FIXME useless?
121 + unset($query->queried_object);
183 122 }
184 123 }
185 124 }
186 125
187 - /**
188 - * Auto translate posts and terms ids
126 + /*
127 + * auto translate posts and terms ids
189 128 *
190 129 * @since 1.2
191 130 */
192 131 public function auto_translate() {
193 - $this->auto_translate = new PLL_Frontend_Auto_Translate( $this );
132 + $this->auto_translate = new PLL_Frontend_Auto_Translate($this);
194 133 }
134 +}
195 135
196 - /**
197 - * Resets some variables when switching blog
198 - * Overrides parent method
199 - *
200 - * @since 1.5.1
201 - *
202 - * @param int $new_blog
203 - * @param int $old_blog
204 - */
205 - public function switch_blog( $new_blog, $old_blog ) {
206 - // Need to check that some languages are defined when user is logged in, has several blogs, some without any languages
207 - if ( parent::switch_blog( $new_blog, $old_blog ) && did_action( 'pll_language_defined' ) && $this->model->get_languages_list() ) {
208 - static $restore_curlang;
209 - if ( empty( $restore_curlang ) ) {
210 - $restore_curlang = $this->curlang->slug; // To always remember the current language through blogs
211 - }
212 -
213 - $lang = $this->model->get_language( $restore_curlang );
214 - $this->curlang = $lang ? $lang : $this->model->get_language( $this->options['default_lang'] );
215 -
216 - if ( isset( $this->static_pages ) ) {
217 - $this->static_pages->init();
218 - }
219 -
220 - $this->load_strings_translations();
221 - }
222 - }
223 -}