PluginProbe
Polylang / 1.4
Polylang v1.4
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 / include / links-directory.php

links-directory.php in Polylang 1.4, at include/links-directory.php

230 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * links model for use when the language code is added in url as a directory
5 * for example mysite.com/en/something
6 * implements the "links_model interface"
7 *
8 * @since 1.2
9 */
10 class PLL_Links_Directory {
11 public $model, $options;
12 protected $home, $rewrite_rules = array();
13 protected $always_rewrite = array('date', 'root', 'comments', 'search', 'author', 'post_format', 'language');
14
15 /*
16 * constructor
17 *
18 * @since 1.2
19 *
20 * @param object $model PLL_Model instance
21 */
22 public function __construct($model) {
23 $this->model = &$model;
24 $this->options = &$model->options;
25 $this->home = get_option('home');
26
27 add_action ('setup_theme', array(&$this, 'add_permastruct'));
28
29 // refresh rewrite rules if the 'page_on_front' option is modified
30 add_action('update_option_page_on_front', 'flush_rewrite_rules');
31
32 // make sure to prepare rewrite rules when flushing
33 add_action ('pre_option_rewrite_rules', array(&$this, 'prepare_rewrite_rules'));
34 }
35
36 /*
37 * adds the language code in url
38 * links_model interface
39 *
40 * @since 1.2
41 *
42 * @param string $url url to modify
43 * @param object $lang language
44 * @return string modified url
45 */
46 public function add_language_to_link($url, $lang) {
47 if (!empty($lang)) {
48 global $wp_rewrite;
49
50 $base = $this->options['rewrite'] ? '' : 'language/';
51 $slug = $this->options['default_lang'] == $lang->slug && $this->options['hide_default'] ? '' : $base.$lang->slug.'/';
52 return str_replace($this->home.'/'.$wp_rewrite->root, $this->home.'/'.$wp_rewrite->root.$slug, $url);
53 }
54 return $url;
55 }
56
57 /*
58 * returns the url without language code
59 * links_model interface
60 *
61 * @since 1.2
62 *
63 * @param string $url url to modify
64 * @return string modified url
65 */
66 function remove_language_from_link($url) {
67 foreach ($this->model->get_languages_list() as $language)
68 if (!$this->options['hide_default'] || $this->options['default_lang'] != $language->slug)
69 $languages[] = $language->slug;
70
71 if (!empty($languages)) {
72 global $wp_rewrite;
73 $pattern = '#' . ($this->options['rewrite'] ? '' : '\/language') . '\/('.implode('|', $languages).')\/#';
74 $url = preg_replace($pattern, $wp_rewrite->root . '/', $url);
75 }
76 return $url;
77 }
78
79 /*
80 * returns the link to the first page
81 * links_model interface
82 *
83 * @since 1.2
84 *
85 * @param string $url url to modify
86 * @return string modified url
87 */
88 function remove_paged_from_link($url) {
89 return preg_replace('#\/page\/[0-9]+\/#', '/', $url);
90 }
91
92 /*
93 * returns the language based on language code in url
94 * links_model interface
95 *
96 * @since 1.2
97 *
98 * @return string language slug
99 */
100 public function get_language_from_url() {
101 $root = $this->options['rewrite'] ? '' : 'language/';
102 $pattern = '#\/'.$root.'('.implode('|', $this->model->get_languages_list(array('fields' => 'slug'))).')\/#';
103 return preg_match($pattern, trailingslashit($_SERVER['REQUEST_URI']), $matches) ? $matches[1] : ''; // $matches[1] is the slug of the requested language
104 }
105
106 /*
107 * returns the home url
108 * links_model interface
109 *
110 * @since 1.3.1
111 *
112 * @param object $lang PLL_Language object
113 * @return string
114 */
115 public function home_url($lang) {
116 return $this->options['hide_default'] && $lang->slug == $this->options['default_lang'] ? $this->home : get_term_link($lang, 'language');
117 }
118
119 /*
120 * optionaly removes 'language' in permalinks so that we get http://www.myblog/en/ instead of http://www.myblog/language/en/
121 *
122 * @since 1.2
123 */
124 function add_permastruct() {
125 // language information always in front of the uri ('with_front' => false)
126 // the 3rd parameter structure has been modified in WP 3.4
127 add_permastruct('language', $this->options['rewrite'] ? '%language%' : 'language/%language%', array('with_front' => false));
128 }
129
130 /*
131 * prepares rewrite rules filters
132 *
133 * @since 0.8.1
134 *
135 * @param array $pre not used
136 * @return unmodified $pre
137 */
138 public function prepare_rewrite_rules($pre) {
139 //clean filters which could have been set in a previous call (may occur when changing Polylang settings)
140 foreach ($this->rewrite_rules as $type)
141 remove_filter($type . '_rewrite_rules', array(&$this, 'rewrite_rules'));
142
143 // don't modify the rules if there is no languages created yet
144 if ($this->model->get_languages_list()) {
145 // make sure we have the right post types and taxonomies
146 $types = array_values(array_merge($this->model->get_translated_post_types(), $this->model->get_translated_taxonomies()));
147 $types = array_merge($this->always_rewrite, $types);
148 $this->rewrite_rules = apply_filters('pll_rewrite_rules', $types); // allow plugins to add rewrite rules to the language filter
149
150 foreach ($this->rewrite_rules as $type)
151 add_filter($type . '_rewrite_rules', array(&$this, 'rewrite_rules'));
152
153 add_filter('rewrite_rules_array', array(&$this, 'rewrite_rules')); // needed for post type archives
154 }
155 return $pre;
156 }
157
158 /*
159 * the rewrite rules !
160 * always make sure the default language is at the end in case the language information is hidden for default language
161 * thanks to brbrbr http://wordpress.org/support/topic/plugin-polylang-rewrite-rules-not-correct
162 *
163 * @since 0.8.1
164 *
165 * @param array $rules rewrite rules
166 * @return array modified rewrite rules
167 */
168 public function rewrite_rules($rules) {
169 $filter = str_replace('_rewrite_rules', '', current_filter());
170
171 // suppress the rules created by WordPress for our taxonomy
172 if ($filter == 'language')
173 return array();
174
175 global $wp_rewrite;
176 $newrules = array();
177
178 $languages = $this->model->get_languages_list(array('fields' => 'slug'));
179 if ($this->options['hide_default'])
180 $languages = array_diff($languages, array($this->options['default_lang']));
181
182 if (!empty($languages))
183 $slug = $wp_rewrite->root . ($this->options['rewrite'] ? '' : 'language/') . '('.implode('|', $languages).')/';
184
185 // for custom post type archives
186 $cpts = array_intersect($this->model->get_translated_post_types(), get_post_types(array('_builtin' => false)));
187 $cpts = $cpts ? '#post_type=('.implode('|', $cpts).')#' : '';
188
189 foreach ($rules as $key => $rule) {
190 // we don't need the lang parameter for post types and taxonomies
191 // moreover adding it would create issues for pages and taxonomies
192 if ($this->options['force_lang'] && in_array($filter, array_merge($this->model->get_translated_post_types(), $this->model->get_translated_taxonomies()))) {
193 if (isset($slug))
194 $newrules[$slug.str_replace($wp_rewrite->root, '', $key)] = str_replace(
195 array('[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '[1]'),
196 array('[9]', '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]'),
197 $rule
198 ); // hopefully it is sufficient!
199
200 if ($this->options['hide_default']) {
201 $newrules[$key] = $rules[$key];
202 // unset only if we hide the code for the default language as check_language_code_in_url will do its job in other cases
203 unset($rules[$key]);
204 }
205 }
206
207 // rewrite rules filtered by language
208 elseif (in_array($filter, $this->always_rewrite) || ($cpts && preg_match($cpts, $rule) && !strpos($rule, 'name=')) || ($filter != 'rewrite_rules_array' && $this->options['force_lang'])) {
209 if (isset($slug))
210 $newrules[$slug.str_replace($wp_rewrite->root, '', $key)] = str_replace(
211 array('[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '[1]', '?'),
212 array('[9]', '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '?lang=$matches[1]&'),
213 $rule
214 ); // should be enough!
215
216 if ($this->options['hide_default'])
217 $newrules[$key] = str_replace('?', '?lang='.$this->options['default_lang'].'&', $rule);
218
219 unset($rules[$key]); // now useless
220 }
221 }
222
223 // the home rewrite rule
224 if ($filter == 'root' && isset($slug))
225 $newrules[$slug.'?$'] = $wp_rewrite->index.'?lang=$matches[1]';
226
227 return $newrules + $rules;
228 }
229 }
230