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 | include/links-directory.php +111 -160 2.71.5 View file →
@@ -1,78 +1,62 @@
1 1 <?php
2 2
3 -/**
4 - * Links model for use when the language code is added in url as a directory
3 +/*
4 + * links model for use when the language code is added in url as a directory
5 5 * for example mysite.com/en/something
6 6 * implements the "links_model interface"
7 7 *
8 8 * @since 1.2
9 9 */
10 -class PLL_Links_Directory extends PLL_Links_Permalinks {
11 - protected $home_relative;
10 +class PLL_Links_Directory extends PLL_Links_Model {
11 + protected $index = 'index.php'; // need this before $wp_rewrite is created, also harcoded in wp-includes/rewrite.php
12 + protected $root;
13 + protected $rewrite_rules = array();
14 + protected $always_rewrite = array('date', 'root', 'comments', 'search', 'author', 'post_format', 'language');
12 15
13 - /**
14 - * Constructor
16 + /*
17 + * constructor
15 18 *
16 19 * @since 1.2
17 20 *
18 21 * @param object $model PLL_Model instance
19 22 */
20 - public function __construct( &$model ) {
21 - parent::__construct( $model );
23 + public function __construct(&$model) {
24 + parent::__construct($model);
22 25
23 - $this->home_relative = home_url( '/', 'relative' );
26 + // inspired by wp-includes/rewrite.php
27 + $this->root = preg_match('#^/*' . $this->index . '#', get_option('permalink_structure')) ? $this->index . '/' : '';
24 28
25 - if ( did_action( 'pll_init' ) ) {
26 - $this->init();
27 - } else {
28 - add_action( 'pll_init', array( $this, 'init' ) );
29 - }
30 - }
29 + add_action ('setup_theme', array(&$this, 'add_permastruct'));
31 30
32 - /**
33 - * Called only at first object creation to avoid duplicating filters when switching blog
34 - *
35 - * @since 1.6
36 - */
37 - public function init() {
38 - if ( did_action( 'setup_theme' ) ) {
39 - $this->add_permastruct();
40 - } else {
41 - add_action( 'setup_theme', array( $this, 'add_permastruct' ), 2 );
42 - }
31 + // refresh rewrite rules if the 'page_on_front' option is modified
32 + add_action('update_option_page_on_front', 'flush_rewrite_rules');
43 33
44 - // Make sure to prepare rewrite rules when flushing
45 - add_action( 'pre_option_rewrite_rules', array( $this, 'prepare_rewrite_rules' ) );
34 + // make sure to prepare rewrite rules when flushing
35 + add_action ('pre_option_rewrite_rules', array(&$this, 'prepare_rewrite_rules'));
46 36 }
47 37
48 - /**
49 - * Adds the language code in url
38 + /*
39 + * adds the language code in url
50 40 * links_model interface
51 41 *
52 42 * @since 1.2
53 43 *
54 - * @param string $url url to modify
44 + * @param string $url url to modify
55 45 * @param object $lang language
56 46 * @return string modified url
57 47 */
58 - public function add_language_to_link( $url, $lang ) {
59 - if ( ! empty( $lang ) ) {
48 + public function add_language_to_link($url, $lang) {
49 + if (!empty($lang)) {
60 50 $base = $this->options['rewrite'] ? '' : 'language/';
61 51 $slug = $this->options['default_lang'] == $lang->slug && $this->options['hide_default'] ? '' : $base . $lang->slug . '/';
62 - $root = ( false === strpos( $url, '://' ) ) ? $this->home_relative . $this->root : preg_replace( '#^https?://#', '://', $this->home . '/' . $this->root );
63 -
64 - if ( false === strpos( $url, $new = $root . $slug ) ) {
65 - $pattern = preg_quote( $root, '#' );
66 - $pattern = '#' . $pattern . '#';
67 - return preg_replace( $pattern, $new, $url, 1 ); // Only once
68 - }
52 + return str_replace($this->home . '/' . $this->root, $this->home . '/' . $this->root . $slug, $url);
69 53 }
70 54 return $url;
71 55 }
72 56
73 - /**
74 - * Returns the url without language code
57 + /*
58 + * returns the url without language code
75 59 * links_model interface
76 60 *
77 61 * @since 1.2
78 62 *
@@ -78,53 +62,38 @@
78 62 *
79 63 * @param string $url url to modify
80 64 * @return string modified url
81 65 */
82 - public function remove_language_from_link( $url ) {
83 - $languages = array();
84 -
85 - foreach ( $this->model->get_languages_list() as $language ) {
86 - if ( ! $this->options['hide_default'] || $this->options['default_lang'] != $language->slug ) {
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)
87 69 $languages[] = $language->slug;
88 - }
89 - }
90 70
91 - if ( ! empty( $languages ) ) {
92 - $root = ( false === strpos( $url, '://' ) ) ? $this->home_relative . $this->root : preg_replace( '#^https?://#', '://', $this->home . '/' . $this->root );
93 -
94 - $pattern = preg_quote( $root, '#' );
95 - $pattern = '#' . $pattern . ( $this->options['rewrite'] ? '' : 'language/' ) . '(' . implode( '|', $languages ) . ')(/|$)#';
96 - $url = preg_replace( $pattern, $root, $url );
71 + if (!empty($languages)) {
72 + $pattern = str_replace('/', '\/', $this->home . '/' . $this->root);
73 + $pattern = '#' . $pattern . ($this->options['rewrite'] ? '' : 'language') . '('.implode('|', $languages).')(\/|$)#';
74 + $url = preg_replace($pattern, $this->home . '/' . $this->root, $url);
97 75 }
98 76 return $url;
99 77 }
100 78
101 - /**
102 - * Returns the language based on language code in url
79 + /*
80 + * returns the language based on language code in url
103 81 * links_model interface
104 82 *
105 83 * @since 1.2
106 - * @since 2.0 add $url argument
107 84 *
108 - * @param string $url optional, defaults to current url
109 85 * @return string language slug
110 86 */
111 - public function get_language_from_url( $url = '' ) {
112 - if ( empty( $url ) ) {
113 - $url = pll_get_requested_url();
114 - }
115 -
116 - $path = wp_parse_url( $url, PHP_URL_PATH );
117 - $root = ( false === strpos( $url, '://' ) ) ? $this->home_relative . $this->root : $this->home . '/' . $this->root;
118 -
119 - $pattern = wp_parse_url( $root . ( $this->options['rewrite'] ? '' : 'language/' ), PHP_URL_PATH );
120 - $pattern = preg_quote( $pattern, '#' );
121 - $pattern = '#^' . $pattern . '(' . implode( '|', $this->model->get_languages_list( array( 'fields' => 'slug' ) ) ) . ')(/|$)#';
122 - return preg_match( $pattern, trailingslashit( $path ), $matches ) ? $matches[1] : ''; // $matches[1] is the slug of the requested language
87 + public function get_language_from_url() {
88 + $requested_url = (is_ssl() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
89 + $pattern = str_replace('/', '\/', $this->home . '/' . $this->root . ($this->options['rewrite'] ? '' : 'language/'));
90 + $pattern = '#' . $pattern . '('. implode('|', $this->model->get_languages_list(array('fields' => 'slug'))) . ')(\/|$)#';
91 + return preg_match($pattern, trailingslashit($requested_url), $matches) ? $matches[1] : ''; // $matches[1] is the slug of the requested language
123 92 }
124 93
125 - /**
126 - * Returns the home url
94 + /*
95 + * returns the home url
127 96 * links_model interface
128 97 *
129 98 * @since 1.3.1
130 99 *
@@ -130,30 +99,25 @@
130 99 *
131 100 * @param object $lang PLL_Language object
132 101 * @return string
133 102 */
134 - public function home_url( $lang ) {
135 - $base = $this->options['rewrite'] ? '' : 'language/';
136 - $slug = $this->options['default_lang'] == $lang->slug && $this->options['hide_default'] ? '' : '/' . $this->root . $base . $lang->slug;
137 - return trailingslashit( $this->home . $slug );
103 + public function home_url($lang) {
104 + return $this->options['hide_default'] && $lang->slug == $this->options['default_lang'] ? $this->home : get_term_link($lang, 'language');
138 105 }
139 106
140 - /**
141 - * Optionally removes 'language' in permalinks so that we get http://www.myblog/en/ instead of http://www.myblog/language/en/
107 + /*
108 + * optionaly removes 'language' in permalinks so that we get http://www.myblog/en/ instead of http://www.myblog/language/en/
142 109 *
143 110 * @since 1.2
144 111 */
145 - public function add_permastruct() {
146 - // Language information always in front of the uri ( 'with_front' => false )
147 - // The 3rd parameter structure has been modified in WP 3.4
148 - // Leads to error 404 for pages when there is no language created yet
149 - if ( $this->model->get_languages_list() ) {
150 - add_permastruct( 'language', $this->options['rewrite'] ? '%language%' : 'language/%language%', array( 'with_front' => false ) );
151 - }
112 + function add_permastruct() {
113 + // language information always in front of the uri ('with_front' => false)
114 + // the 3rd parameter structure has been modified in WP 3.4
115 + add_permastruct('language', $this->options['rewrite'] ? '%language%' : 'language/%language%', array('with_front' => false));
152 116 }
153 117
154 - /**
155 - * Prepares rewrite rules filters
118 + /*
119 + * prepares rewrite rules filters
156 120 *
157 121 * @since 0.8.1
158 122 *
159 123 * @param array $pre not used
@@ -158,26 +122,30 @@
158 122 *
159 123 * @param array $pre not used
160 124 * @return unmodified $pre
161 125 */
162 - public function prepare_rewrite_rules( $pre ) {
163 - // Don't modify the rules if there is no languages created yet
164 - // Make sure to add filter only once and if all custom post types and taxonomies have been registered
165 - if ( $this->model->get_languages_list() && did_action( 'wp_loaded' ) && ! has_filter( 'language_rewrite_rules', '__return_empty_array' ) ) {
166 - // Suppress the rules created by WordPress for our taxonomy
167 - add_filter( 'language_rewrite_rules', '__return_empty_array' );
126 + public function prepare_rewrite_rules($pre) {
127 + //clean filters which could have been set in a previous call (may occur when changing Polylang settings)
128 + foreach ($this->rewrite_rules as $type)
129 + remove_filter($type . '_rewrite_rules', array(&$this, 'rewrite_rules'));
168 130
169 - foreach ( $this->get_rewrite_rules_filters() as $type ) {
170 - add_filter( $type . '_rewrite_rules', array( $this, 'rewrite_rules' ) );
171 - }
131 + // don't modify the rules if there is no languages created yet
132 + if ($this->model->get_languages_list()) {
133 + // make sure we have the right post types and taxonomies
134 + $types = array_values(array_merge($this->model->get_translated_post_types(), $this->model->get_translated_taxonomies()));
135 + $types = array_merge($this->always_rewrite, $types);
136 + $this->rewrite_rules = apply_filters('pll_rewrite_rules', $types); // allow plugins to add rewrite rules to the language filter
172 137
173 - add_filter( 'rewrite_rules_array', array( $this, 'rewrite_rules' ) ); // needed for post type archives
138 + foreach ($this->rewrite_rules as $type)
139 + add_filter($type . '_rewrite_rules', array(&$this, 'rewrite_rules'));
140 +
141 + add_filter('rewrite_rules_array', array(&$this, 'rewrite_rules')); // needed for post type archives
174 142 }
175 143 return $pre;
176 144 }
177 145
178 - /**
179 - * The rewrite rules !
146 + /*
147 + * the rewrite rules !
180 148 * always make sure the default language is at the end in case the language information is hidden for default language
181 149 * thanks to brbrbr http://wordpress.org/support/topic/plugin-polylang-rewrite-rules-not-correct
182 150 *
183 151 * @since 0.8.1
@@ -184,83 +152,66 @@
184 152 *
185 153 * @param array $rules rewrite rules
186 154 * @return array modified rewrite rules
187 155 */
188 - public function rewrite_rules( $rules ) {
189 - $filter = str_replace( '_rewrite_rules', '', current_filter() );
156 + public function rewrite_rules($rules) {
157 + $filter = str_replace('_rewrite_rules', '', current_filter());
190 158
159 + // suppress the rules created by WordPress for our taxonomy
160 + if ($filter == 'language')
161 + return array();
162 +
191 163 global $wp_rewrite;
192 164 $newrules = array();
193 165
194 - $languages = $this->model->get_languages_list( array( 'fields' => 'slug' ) );
195 - if ( $this->options['hide_default'] ) {
196 - $languages = array_diff( $languages, array( $this->options['default_lang'] ) );
197 - }
166 + $languages = $this->model->get_languages_list(array('fields' => 'slug'));
167 + if ($this->options['hide_default'])
168 + $languages = array_diff($languages, array($this->options['default_lang']));
198 169
199 - if ( ! empty( $languages ) ) {
200 - $slug = $wp_rewrite->root . ( $this->options['rewrite'] ? '' : 'language/' ) . '(' . implode( '|', $languages ) . ')/';
201 - }
170 + if (!empty($languages))
171 + $slug = $wp_rewrite->root . ($this->options['rewrite'] ? '' : 'language/') . '('.implode('|', $languages).')/';
202 172
203 - // For custom post type archives
204 - $cpts = array_intersect( $this->model->get_translated_post_types(), get_post_types( array( '_builtin' => false ) ) );
205 - $cpts = $cpts ? '#post_type=(' . implode( '|', $cpts ) . ')#' : '';
173 + // for custom post type archives
174 + $cpts = array_intersect($this->model->get_translated_post_types(), get_post_types(array('_builtin' => false)));
175 + $cpts = $cpts ? '#post_type=('.implode('|', $cpts).')#' : '';
206 176
207 - foreach ( $rules as $key => $rule ) {
208 - // Special case for translated post types and taxonomies to allow canonical redirection
209 - if ( $this->options['force_lang'] && in_array( $filter, array_merge( $this->model->get_translated_post_types(), $this->model->get_translated_taxonomies() ) ) ) {
177 + foreach ($rules as $key => $rule) {
178 + // we don't need the lang parameter for post types and taxonomies
179 + // moreover adding it would create issues for pages and taxonomies
180 + if ($this->options['force_lang'] && in_array($filter, array_merge($this->model->get_translated_post_types(), $this->model->get_translated_taxonomies()))) {
181 + if (isset($slug))
182 + $newrules[$slug.str_replace($wp_rewrite->root, '', $key)] = str_replace(
183 + array('[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '[1]'),
184 + array('[9]', '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]'),
185 + $rule
186 + ); // hopefully it is sufficient!
210 187
211 - /**
212 - * Filters the rewrite rules to modify
213 - *
214 - * @since 1.9.1
215 - *
216 - * @param bool $modify whether to modify or not the rule, defaults to true
217 - * @param array $rule original rewrite rule
218 - * @param string $filter current set of rules being modified
219 - * @param string|bool $archive custom post post type archive name or false if it is not a cpt archive
220 - */
221 - if ( isset( $slug ) && apply_filters( 'pll_modify_rewrite_rule', true, array( $key => $rule ), $filter, false ) ) {
222 - $newrules[ $slug . str_replace( $wp_rewrite->root, '', ltrim( $key, '^' ) ) ] = str_replace(
223 - array( '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '[1]', '?' ),
224 - array( '[9]', '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '?lang=$matches[1]&' ),
225 - $rule
226 - ); // Should be enough!
188 + if ($this->options['hide_default']) {
189 + $newrules[$key] = $rules[$key];
190 + // unset only if we hide the code for the default language as check_language_code_in_url will do its job in other cases
191 + unset($rules[$key]);
227 192 }
228 -
229 - $newrules[ $key ] = $rule;
230 193 }
231 194
232 - // Rewrite rules filtered by language
233 - elseif ( in_array( $filter, $this->always_rewrite ) || in_array( $filter, $this->model->get_filtered_taxonomies() ) || ( $cpts && preg_match( $cpts, $rule, $matches ) && ! strpos( $rule, 'name=' ) ) || ( 'rewrite_rules_array' != $filter && $this->options['force_lang'] ) ) {
195 + // rewrite rules filtered by language
196 + elseif (in_array($filter, $this->always_rewrite) || ($cpts && preg_match($cpts, $rule) && !strpos($rule, 'name=')) || ($filter != 'rewrite_rules_array' && $this->options['force_lang'])) {
197 + if (isset($slug))
198 + $newrules[$slug.str_replace($wp_rewrite->root, '', $key)] = str_replace(
199 + array('[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '[1]', '?'),
200 + array('[9]', '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '?lang=$matches[1]&'),
201 + $rule
202 + ); // should be enough!
234 203
235 - /** This filter is documented in include/links-directory.php */
236 - if ( apply_filters( 'pll_modify_rewrite_rule', true, array( $key => $rule ), $filter, empty( $matches[1] ) ? false : $matches[1] ) ) {
237 - if ( isset( $slug ) ) {
238 - $newrules[ $slug . str_replace( $wp_rewrite->root, '', ltrim( $key, '^' ) ) ] = str_replace(
239 - array( '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '[1]', '?' ),
240 - array( '[9]', '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '?lang=$matches[1]&' ),
241 - $rule
242 - ); // Should be enough!
243 - }
204 + if ($this->options['hide_default'])
205 + $newrules[$key] = str_replace('?', '?lang='.$this->options['default_lang'].'&', $rule);
244 206
245 - if ( $this->options['hide_default'] ) {
246 - $newrules[ $key ] = str_replace( '?', '?lang=' . $this->options['default_lang'] . '&', $rule );
247 - }
248 - } else {
249 - $newrules[ $key ] = $rule;
250 - }
207 + unset($rules[$key]); // now useless
251 208 }
252 -
253 - // Unmodified rules
254 - else {
255 - $newrules[ $key ] = $rule;
256 - }
257 209 }
258 210
259 - // The home rewrite rule
260 - if ( 'root' == $filter && isset( $slug ) ) {
261 - $newrules[ $slug . '?$' ] = $wp_rewrite->index . '?lang=$matches[1]';
262 - }
211 + // the home rewrite rule
212 + if ($filter == 'root' && isset($slug))
213 + $newrules[$slug.'?$'] = $wp_rewrite->index.'?lang=$matches[1]';
263 214
264 - return $newrules;
215 + return $newrules + $rules;
265 216 }
266 217 }