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 +38 -53 2.92.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,9 +7,8 @@
10 7 *
11 8 * @since 1.2
12 9 */
13 10 class PLL_Links_Directory extends PLL_Links_Permalinks {
14 - protected $home_relative;
15 11
16 12 /**
17 13 * Constructor
18 14 *
@@ -22,10 +18,8 @@
22 18 */
23 19 public function __construct( &$model ) {
24 20 parent::__construct( $model );
25 21
26 - $this->home_relative = home_url( '/', 'relative' );
27 -
28 22 if ( did_action( 'pll_init' ) ) {
29 23 $this->init();
30 24 } else {
31 25 add_action( 'pll_init', array( $this, 'init' ) );
@@ -61,15 +55,9 @@
61 55 public function add_language_to_link( $url, $lang ) {
62 56 if ( ! empty( $lang ) ) {
63 57 $base = $this->options['rewrite'] ? '' : 'language/';
64 58 $slug = $this->options['default_lang'] == $lang->slug && $this->options['hide_default'] ? '' : $base . $lang->slug . '/';
65 - $root = ( false === strpos( $url, '://' ) ) ? $this->home_relative . $this->root : preg_replace( '#^https?://#', '://', $this->home . '/' . $this->root );
66 -
67 - if ( false === strpos( $url, $new = $root . $slug ) ) {
68 - $pattern = preg_quote( $root, '#' );
69 - $pattern = '#' . $pattern . '#';
70 - return preg_replace( $pattern, $new, $url, 1 ); // Only once
71 - }
59 + return str_replace( $this->home . '/' . $this->root, $this->home . '/' . $this->root . $slug, $url );
72 60 }
73 61 return $url;
74 62 }
75 63
@@ -81,11 +69,9 @@
81 69 *
82 70 * @param string $url url to modify
83 71 * @return string modified url
84 72 */
85 - public function remove_language_from_link( $url ) {
86 - $languages = array();
87 -
73 + function remove_language_from_link( $url ) {
88 74 foreach ( $this->model->get_languages_list() as $language ) {
89 75 if ( ! $this->options['hide_default'] || $this->options['default_lang'] != $language->slug ) {
90 76 $languages[] = $language->slug;
91 77 }
@@ -91,13 +77,11 @@
91 77 }
92 78 }
93 79
94 80 if ( ! empty( $languages ) ) {
95 - $root = ( false === strpos( $url, '://' ) ) ? $this->home_relative . $this->root : preg_replace( '#^https?://#', '://', $this->home . '/' . $this->root );
96 -
97 - $pattern = preg_quote( $root, '#' );
98 - $pattern = '#' . $pattern . ( $this->options['rewrite'] ? '' : 'language/' ) . '(' . implode( '|', $languages ) . ')(/|$)#';
99 - $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 );
100 84 }
101 85 return $url;
102 86 }
103 87
@@ -112,18 +96,14 @@
112 96 * @return string language slug
113 97 */
114 98 public function get_language_from_url( $url = '' ) {
115 99 if ( empty( $url ) ) {
116 - $url = pll_get_requested_url();
100 + $url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
117 101 }
118 102
119 - $path = wp_parse_url( $url, PHP_URL_PATH );
120 - $root = ( false === strpos( $url, '://' ) ) ? $this->home_relative . $this->root : $this->home . '/' . $this->root;
121 -
122 - $pattern = wp_parse_url( $root . ( $this->options['rewrite'] ? '' : 'language/' ), PHP_URL_PATH );
123 - $pattern = preg_quote( $pattern, '#' );
124 - $pattern = '#^' . $pattern . '(' . implode( '|', $this->model->get_languages_list( array( 'fields' => 'slug' ) ) ) . ')(/|$)#';
125 - 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
126 106 }
127 107
128 108 /**
129 109 * Returns the home url
@@ -140,13 +120,13 @@
140 120 return trailingslashit( $this->home . $slug );
141 121 }
142 122
143 123 /**
144 - * 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/
145 125 *
146 126 * @since 1.2
147 127 */
148 - public function add_permastruct() {
128 + function add_permastruct() {
149 129 // Language information always in front of the uri ( 'with_front' => false )
150 130 // The 3rd parameter structure has been modified in WP 3.4
151 131 // Leads to error 404 for pages when there is no language created yet
152 132 if ( $this->model->get_languages_list() ) {
@@ -220,10 +200,14 @@
220 200 * @param array $rule original rewrite rule
221 201 * @param string $filter current set of rules being modified
222 202 * @param string|bool $archive custom post post type archive name or false if it is not a cpt archive
223 203 */
224 - if ( isset( $slug ) && apply_filters( 'pll_modify_rewrite_rule', true, array( $key => $rule ), $filter, false ) ) {
225 - $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(
226 210 array( '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '[1]', '?' ),
227 211 array( '[9]', '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '?lang=$matches[1]&' ),
228 212 $rule
229 213 ); // Should be enough!
@@ -228,9 +212,13 @@
228 212 $rule
229 213 ); // Should be enough!
230 214 }
231 215
232 - $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 + }
233 221 }
234 222
235 223 // Rewrite rules filtered by language
236 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'] ) ) {
@@ -235,35 +223,32 @@
235 223 // Rewrite rules filtered by language
236 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'] ) ) {
237 225
238 226 /** This filter is documented in include/links-directory.php */
239 - if ( apply_filters( 'pll_modify_rewrite_rule', true, array( $key => $rule ), $filter, empty( $matches[1] ) ? false : $matches[1] ) ) {
240 - if ( isset( $slug ) ) {
241 - $newrules[ $slug . str_replace( $wp_rewrite->root, '', ltrim( $key, '^' ) ) ] = str_replace(
242 - array( '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '[1]', '?' ),
243 - array( '[9]', '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '?lang=$matches[1]&' ),
244 - $rule
245 - ); // Should be enough!
246 - }
227 + if ( ! apply_filters( 'pll_modify_rewrite_rule', true, array( $key => $rule ), $filter, empty( $matches[1] ) ? false : $matches[1] ) ) {
228 + continue;
229 + }
247 230
248 - if ( $this->options['hide_default'] ) {
249 - $newrules[ $key ] = str_replace( '?', '?lang=' . $this->options['default_lang'] . '&', $rule );
250 - }
251 - } else {
252 - $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!
253 237 }
254 - }
255 238
256 - // Unmodified rules
257 - else {
258 - $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
259 244 }
260 245 }
261 246
262 247 // The home rewrite rule
263 248 if ( 'root' == $filter && isset( $slug ) ) {
264 - $newrules[ $slug . '?$' ] = $wp_rewrite->index . '?lang=$matches[1]';
249 + $newrules[ $slug . '?$' ] = $wp_rewrite->index.'?lang=$matches[1]';
265 250 }
266 251
267 - return $newrules;
252 + return $newrules + $rules;
268 253 }
269 254 }