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