PluginProbe
Polylang / 2.0.3
Polylang v2.0.3
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 +40 -56 2.7.4 → 2.0.3 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,19 +120,16 @@
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 - // 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 - }
131 + add_permastruct( 'language', $this->options['rewrite'] ? '%language%' : 'language/%language%', array( 'with_front' => false ) );
152 132 }
153 133
154 134 /**
155 135 * Prepares rewrite rules filters
@@ -160,10 +140,9 @@
160 140 * @return unmodified $pre
161 141 */
162 142 public function prepare_rewrite_rules( $pre ) {
163 143 // 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' ) ) {
144 + if ( $this->model->get_languages_list() && ! has_filter( 'language_rewrite_rules', '__return_empty_array' ) ) {
166 145 // Suppress the rules created by WordPress for our taxonomy
167 146 add_filter( 'language_rewrite_rules', '__return_empty_array' );
168 147
169 148 foreach ( $this->get_rewrite_rules_filters() as $type ) {
@@ -217,10 +196,14 @@
217 196 * @param array $rule original rewrite rule
218 197 * @param string $filter current set of rules being modified
219 198 * @param string|bool $archive custom post post type archive name or false if it is not a cpt archive
220 199 */
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(
200 + if ( ! apply_filters( 'pll_modify_rewrite_rule', true, array( $key => $rule ), $filter, false ) ) {
201 + continue;
202 + }
203 +
204 + if ( isset( $slug ) ) {
205 + $newrules[ $slug . str_replace( $wp_rewrite->root, '', $key ) ] = str_replace(
223 206 array( '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '[1]', '?' ),
224 207 array( '[9]', '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '?lang=$matches[1]&' ),
225 208 $rule
226 209 ); // Should be enough!
@@ -225,9 +208,13 @@
225 208 $rule
226 209 ); // Should be enough!
227 210 }
228 211
229 - $newrules[ $key ] = $rule;
212 + if ( $this->options['hide_default'] ) {
213 + $newrules[ $key ] = $rules[ $key ];
214 + // Unset only if we hide the code for the default language as check_language_code_in_url will do its job in other cases
215 + unset( $rules[ $key ] );
216 + }
230 217 }
231 218
232 219 // Rewrite rules filtered by language
233 220 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 +219,32 @@
232 219 // Rewrite rules filtered by language
233 220 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 221
235 222 /** 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 - }
223 + if ( ! apply_filters( 'pll_modify_rewrite_rule', true, array( $key => $rule ), $filter, empty( $matches[1] ) ? false : $matches[1] ) ) {
224 + continue;
225 + }
244 226
245 - if ( $this->options['hide_default'] ) {
246 - $newrules[ $key ] = str_replace( '?', '?lang=' . $this->options['default_lang'] . '&', $rule );
247 - }
248 - } else {
249 - $newrules[ $key ] = $rule;
227 + if ( isset( $slug ) ) {
228 + $newrules[ $slug . str_replace( $wp_rewrite->root, '', $key ) ] = str_replace(
229 + array( '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '[1]', '?' ),
230 + array( '[9]', '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '?lang=$matches[1]&' ),
231 + $rule
232 + ); // Should be enough!
250 233 }
251 - }
252 234
253 - // Unmodified rules
254 - else {
255 - $newrules[ $key ] = $rule;
235 + if ( $this->options['hide_default'] ) {
236 + $newrules[ $key ] = str_replace( '?', '?lang=' . $this->options['default_lang'] . '&', $rule );
237 + }
238 +
239 + unset( $rules[ $key ] ); // Now useless
256 240 }
257 241 }
258 242
259 243 // The home rewrite rule
260 244 if ( 'root' == $filter && isset( $slug ) ) {
261 - $newrules[ $slug . '?$' ] = $wp_rewrite->index . '?lang=$matches[1]';
245 + $newrules[ $slug . '?$' ] = $wp_rewrite->index.'?lang=$matches[1]';
262 246 }
263 247
264 - return $newrules;
248 + return $newrules + $rules;
265 249 }
266 250 }