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
← All changes | include/links-directory.php +55 -79 3.02.0.12 View file →
@@ -1,8 +1,5 @@
1 1 <?php
2 -/**
3 - * @package Polylang
4 - */
5 2
6 3 /**
7 4 * Links model for use when the language code is added in url as a directory
8 5 * for example mysite.com/en/something
@@ -10,27 +7,19 @@
10 7 *
11 8 * @since 1.2
12 9 */
13 10 class PLL_Links_Directory extends PLL_Links_Permalinks {
14 - /**
15 - * Relative path to the home url.
16 - *
17 - * @var string
18 - */
19 - protected $home_relative;
20 11
21 12 /**
22 - * Constructor.
13 + * Constructor
23 14 *
24 15 * @since 1.2
25 16 *
26 - * @param PLL_Model $model PLL_Model instance.
17 + * @param object $model PLL_Model instance
27 18 */
28 19 public function __construct( &$model ) {
29 20 parent::__construct( $model );
30 21
31 - $this->home_relative = home_url( '/', 'relative' );
32 -
33 22 if ( did_action( 'pll_init' ) ) {
34 23 $this->init();
35 24 } else {
36 25 add_action( 'pll_init', array( $this, 'init' ) );
@@ -40,10 +29,8 @@
40 29 /**
41 30 * Called only at first object creation to avoid duplicating filters when switching blog
42 31 *
43 32 * @since 1.6
44 - *
45 - * @return void
46 33 */
47 34 public function init() {
48 35 if ( did_action( 'setup_theme' ) ) {
49 36 $this->add_permastruct();
@@ -55,28 +42,22 @@
55 42 add_action( 'pre_option_rewrite_rules', array( $this, 'prepare_rewrite_rules' ) );
56 43 }
57 44
58 45 /**
59 - * Adds the language code in a url.
60 - * links_model interface.
46 + * Adds the language code in url
47 + * links_model interface
61 48 *
62 49 * @since 1.2
63 50 *
64 - * @param string $url The url to modify.
65 - * @param PLL_Language $lang The language object.
66 - * @return string Modified url.
51 + * @param string $url url to modify
52 + * @param object $lang language
53 + * @return string modified url
67 54 */
68 55 public function add_language_to_link( $url, $lang ) {
69 56 if ( ! empty( $lang ) ) {
70 57 $base = $this->options['rewrite'] ? '' : 'language/';
71 58 $slug = $this->options['default_lang'] == $lang->slug && $this->options['hide_default'] ? '' : $base . $lang->slug . '/';
72 - $root = ( false === strpos( $url, '://' ) ) ? $this->home_relative . $this->root : preg_replace( '#^https?://#', '://', $this->home . '/' . $this->root );
73 -
74 - if ( false === strpos( $url, $new = $root . $slug ) ) {
75 - $pattern = preg_quote( $root, '#' );
76 - $pattern = '#' . $pattern . '#';
77 - return preg_replace( $pattern, $new, $url, 1 ); // Only once
78 - }
59 + return str_replace( $this->home . '/' . $this->root, $this->home . '/' . $this->root . $slug, $url );
79 60 }
80 61 return $url;
81 62 }
82 63
@@ -88,11 +69,9 @@
88 69 *
89 70 * @param string $url url to modify
90 71 * @return string modified url
91 72 */
92 - public function remove_language_from_link( $url ) {
93 - $languages = array();
94 -
73 + function remove_language_from_link( $url ) {
95 74 foreach ( $this->model->get_languages_list() as $language ) {
96 75 if ( ! $this->options['hide_default'] || $this->options['default_lang'] != $language->slug ) {
97 76 $languages[] = $language->slug;
98 77 }
@@ -98,13 +77,11 @@
98 77 }
99 78 }
100 79
101 80 if ( ! empty( $languages ) ) {
102 - $root = ( false === strpos( $url, '://' ) ) ? $this->home_relative . $this->root : preg_replace( '#^https?://#', '://', $this->home . '/' . $this->root );
103 -
104 - $pattern = preg_quote( $root, '#' );
105 - $pattern = '#' . $pattern . ( $this->options['rewrite'] ? '' : 'language/' ) . '(' . implode( '|', $languages ) . ')(/|$)#';
106 - $url = preg_replace( $pattern, $root, $url );
81 + $pattern = str_replace( '/', '\/', $this->home . '/' . $this->root );
82 + $pattern = '#' . $pattern . ( $this->options['rewrite'] ? '' : 'language\/' ) . '('.implode( '|', $languages ).')(\/|$)#';
83 + $url = preg_replace( $pattern, $this->home . '/' . $this->root, $url );
107 84 }
108 85 return $url;
109 86 }
110 87
@@ -119,27 +96,23 @@
119 96 * @return string language slug
120 97 */
121 98 public function get_language_from_url( $url = '' ) {
122 99 if ( empty( $url ) ) {
123 - $url = pll_get_requested_url();
100 + $url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
124 101 }
125 102
126 - $path = wp_parse_url( $url, PHP_URL_PATH );
127 - $root = ( false === strpos( $url, '://' ) ) ? $this->home_relative . $this->root : $this->home . '/' . $this->root;
128 -
129 - $pattern = wp_parse_url( $root . ( $this->options['rewrite'] ? '' : 'language/' ), PHP_URL_PATH );
130 - $pattern = preg_quote( $pattern, '#' );
131 - $pattern = '#^' . $pattern . '(' . implode( '|', $this->model->get_languages_list( array( 'fields' => 'slug' ) ) ) . ')(/|$)#';
132 - return preg_match( $pattern, trailingslashit( $path ), $matches ) ? $matches[1] : ''; // $matches[1] is the slug of the requested language
103 + $pattern = str_replace( '/', '\/', $this->home . '/' . $this->root . ( $this->options['rewrite'] ? '' : 'language/' ) );
104 + $pattern = '#' . $pattern . '('. implode( '|', $this->model->get_languages_list( array( 'fields' => 'slug' ) ) ) . ')(\/|$)#';
105 + return preg_match( $pattern, trailingslashit( $url ), $matches ) ? $matches[1] : ''; // $matches[1] is the slug of the requested language
133 106 }
134 107
135 108 /**
136 - * Returns the home url in a given language.
137 - * links_model interface.
109 + * Returns the home url
110 + * links_model interface
138 111 *
139 112 * @since 1.3.1
140 113 *
141 - * @param PLL_Language $lang PLL_Language object.
114 + * @param object $lang PLL_Language object
142 115 * @return string
143 116 */
144 117 public function home_url( $lang ) {
145 118 $base = $this->options['rewrite'] ? '' : 'language/';
@@ -147,15 +120,13 @@
147 120 return trailingslashit( $this->home . $slug );
148 121 }
149 122
150 123 /**
151 - * Optionally removes 'language' in permalinks so that we get http://www.myblog/en/ instead of http://www.myblog/language/en/
124 + * Optionaly removes 'language' in permalinks so that we get http://www.myblog/en/ instead of http://www.myblog/language/en/
152 125 *
153 126 * @since 1.2
154 - *
155 - * @return void
156 127 */
157 - public function add_permastruct() {
128 + function add_permastruct() {
158 129 // Language information always in front of the uri ( 'with_front' => false )
159 130 // The 3rd parameter structure has been modified in WP 3.4
160 131 // Leads to error 404 for pages when there is no language created yet
161 132 if ( $this->model->get_languages_list() ) {
@@ -163,14 +134,14 @@
163 134 }
164 135 }
165 136
166 137 /**
167 - * Prepares the rewrite rules filters.
138 + * Prepares rewrite rules filters
168 139 *
169 140 * @since 0.8.1
170 141 *
171 - * @param mixed $pre Not used as the filter is used as an action.
172 - * @return mixed
142 + * @param array $pre not used
143 + * @return unmodified $pre
173 144 */
174 145 public function prepare_rewrite_rules( $pre ) {
175 146 // Don't modify the rules if there is no languages created yet
176 147 // Make sure to add filter only once and if all custom post types and taxonomies have been registered
@@ -188,15 +159,15 @@
188 159 }
189 160
190 161 /**
191 162 * The rewrite rules !
192 - * Always make sure that the default language is at the end in case the language information is hidden for default language.
193 - * Thanks to brbrbr http://wordpress.org/support/topic/plugin-polylang-rewrite-rules-not-correct.
163 + * always make sure the default language is at the end in case the language information is hidden for default language
164 + * thanks to brbrbr http://wordpress.org/support/topic/plugin-polylang-rewrite-rules-not-correct
194 165 *
195 166 * @since 0.8.1
196 167 *
197 - * @param string[] $rules Rewrite rules.
198 - * @return string[] Modified rewrite rules.
168 + * @param array $rules rewrite rules
169 + * @return array modified rewrite rules
199 170 */
200 171 public function rewrite_rules( $rules ) {
201 172 $filter = str_replace( '_rewrite_rules', '', current_filter() );
202 173
@@ -229,10 +200,14 @@
229 200 * @param array $rule original rewrite rule
230 201 * @param string $filter current set of rules being modified
231 202 * @param string|bool $archive custom post post type archive name or false if it is not a cpt archive
232 203 */
233 - if ( isset( $slug ) && apply_filters( 'pll_modify_rewrite_rule', true, array( $key => $rule ), $filter, false ) ) {
234 - $newrules[ $slug . str_replace( $wp_rewrite->root, '', ltrim( $key, '^' ) ) ] = str_replace(
204 + if ( ! apply_filters( 'pll_modify_rewrite_rule', true, array( $key => $rule ), $filter, false ) ) {
205 + continue;
206 + }
207 +
208 + if ( isset( $slug ) ) {
209 + $newrules[ $slug . str_replace( $wp_rewrite->root, '', $key ) ] = str_replace(
235 210 array( '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '[1]', '?' ),
236 211 array( '[9]', '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '?lang=$matches[1]&' ),
237 212 $rule
238 213 ); // Should be enough!
@@ -237,9 +212,13 @@
237 212 $rule
238 213 ); // Should be enough!
239 214 }
240 215
241 - $newrules[ $key ] = $rule;
216 + if ( $this->options['hide_default'] ) {
217 + $newrules[ $key ] = $rules[ $key ];
218 + // Unset only if we hide the code for the default language as check_language_code_in_url will do its job in other cases
219 + unset( $rules[ $key ] );
220 + }
242 221 }
243 222
244 223 // Rewrite rules filtered by language
245 224 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'] ) ) {
@@ -244,35 +223,32 @@
244 223 // Rewrite rules filtered by language
245 224 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'] ) ) {
246 225
247 226 /** This filter is documented in include/links-directory.php */
248 - if ( apply_filters( 'pll_modify_rewrite_rule', true, array( $key => $rule ), $filter, empty( $matches[1] ) ? false : $matches[1] ) ) {
249 - if ( isset( $slug ) ) {
250 - $newrules[ $slug . str_replace( $wp_rewrite->root, '', ltrim( $key, '^' ) ) ] = str_replace(
251 - array( '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '[1]', '?' ),
252 - array( '[9]', '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '?lang=$matches[1]&' ),
253 - $rule
254 - ); // Should be enough!
255 - }
227 + if ( ! apply_filters( 'pll_modify_rewrite_rule', true, array( $key => $rule ), $filter, empty( $matches[1] ) ? false : $matches[1] ) ) {
228 + continue;
229 + }
256 230
257 - if ( $this->options['hide_default'] ) {
258 - $newrules[ $key ] = str_replace( '?', '?lang=' . $this->options['default_lang'] . '&', $rule );
259 - }
260 - } else {
261 - $newrules[ $key ] = $rule;
231 + if ( isset( $slug ) ) {
232 + $newrules[ $slug . str_replace( $wp_rewrite->root, '', $key ) ] = str_replace(
233 + array( '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '[1]', '?' ),
234 + array( '[9]', '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '?lang=$matches[1]&' ),
235 + $rule
236 + ); // Should be enough!
262 237 }
263 - }
264 238
265 - // Unmodified rules
266 - else {
267 - $newrules[ $key ] = $rule;
239 + if ( $this->options['hide_default'] ) {
240 + $newrules[ $key ] = str_replace( '?', '?lang=' . $this->options['default_lang'] . '&', $rule );
241 + }
242 +
243 + unset( $rules[ $key ] ); // Now useless
268 244 }
269 245 }
270 246
271 247 // The home rewrite rule
272 248 if ( 'root' == $filter && isset( $slug ) ) {
273 - $newrules[ $slug . '?$' ] = $wp_rewrite->index . '?lang=$matches[1]';
249 + $newrules[ $slug . '?$' ] = $wp_rewrite->index.'?lang=$matches[1]';
274 250 }
275 251
276 - return $newrules;
252 + return $newrules + $rules;
277 253 }
278 254 }