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
polylang / frontend / frontend.php

frontend.php in Polylang 1.5, at frontend/frontend.php

136 lines 4.4 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 * 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
18 *
19 * @since 1.2
20 */
21 class PLL_Frontend extends PLL_Base{
22 public $curlang;
23 public $links, $choose_lang, $filters, $filters_search, $auto_translate;
24
25 /*
26 * constructor
27 *
28 * @since 1.2
29 *
30 * @param object $links_model
31 */
32 public function __construct(&$links_model) {
33 parent::__construct($links_model);
34
35 add_action('pll_language_defined', array(&$this, 'pll_language_defined'), 1, 2);
36
37 // filters posts by language
38 add_filter('parse_query', array(&$this, 'parse_query'), 6);
39
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);
43 }
44
45 /*
46 * setups url modifications based on the links mode
47 * setups the language chooser based on options
48 *
49 * @since 1.2
50 *
51 * @param object $links_model
52 */
53 public function init() {
54 $this->links = new PLL_Frontend_Links($this);
55
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);
59 }
60
61 /*
62 * setups filters and nav menus once the language has been defined
63 *
64 * @since 1.2
65 *
66 * @param string $slug current language slug
67 * @param object $curlang current language object
68 */
69 public function pll_language_defined($slug, $curlang) {
70 $this->curlang = $curlang;
71
72 // filters
73 $this->filters = new PLL_Frontend_Filters($this);
74 $this->filters_search = new PLL_Frontend_Filters_Search($this);
75
76 // nav menu
77 $this->nav_menu = new PLL_Frontend_Nav_Menu($this);
78 }
79
80 /*
81 * modifies some query vars to "hide" that the language is a taxonomy and avoid conflicts
82 *
83 * @since 1.2
84 *
85 * @param object $query WP_Query object
86 */
87 public function parse_query($query) {
88 $qv = $query->query_vars;
89
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);
104 }
105
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');
111
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'))
115 $query->is_archive = false;
116
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) {
120 $query->is_tax = false;
121 unset($query->queried_object);
122 }
123 }
124 }
125
126 /*
127 * auto translate posts and terms ids
128 *
129 * @since 1.2
130 */
131 public function auto_translate() {
132 $this->auto_translate = new PLL_Frontend_Auto_Translate($this);
133 }
134 }
135
136