PluginProbe
Polylang / 2.1
Polylang v2.1
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 +34 -61 3.02.1 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,27 +42,23 @@
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
59 + if ( false === strpos( $url, $this->home . '/' . $this->root . $slug ) ) {
60 + return str_replace( $this->home . '/' . $this->root, $this->home . '/' . $this->root . $slug, $url );
78 61 }
79 62 }
80 63 return $url;
81 64 }
@@ -88,11 +71,9 @@
88 71 *
89 72 * @param string $url url to modify
90 73 * @return string modified url
91 74 */
92 - public function remove_language_from_link( $url ) {
93 - $languages = array();
94 -
75 + function remove_language_from_link( $url ) {
95 76 foreach ( $this->model->get_languages_list() as $language ) {
96 77 if ( ! $this->options['hide_default'] || $this->options['default_lang'] != $language->slug ) {
97 78 $languages[] = $language->slug;
98 79 }
@@ -98,13 +79,11 @@
98 79 }
99 80 }
100 81
101 82 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 );
83 + $pattern = str_replace( '/', '\/', $this->home . '/' . $this->root );
84 + $pattern = '#' . $pattern . ( $this->options['rewrite'] ? '' : 'language\/' ) . '('.implode( '|', $languages ).')(\/|$)#';
85 + $url = preg_replace( $pattern, $this->home . '/' . $this->root, $url );
107 86 }
108 87 return $url;
109 88 }
110 89
@@ -119,27 +98,23 @@
119 98 * @return string language slug
120 99 */
121 100 public function get_language_from_url( $url = '' ) {
122 101 if ( empty( $url ) ) {
123 - $url = pll_get_requested_url();
102 + $url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
124 103 }
125 104
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
105 + $pattern = str_replace( '/', '\/', $this->home . '/' . $this->root . ( $this->options['rewrite'] ? '' : 'language/' ) );
106 + $pattern = '#' . $pattern . '('. implode( '|', $this->model->get_languages_list( array( 'fields' => 'slug' ) ) ) . ')(\/|$)#';
107 + return preg_match( $pattern, trailingslashit( $url ), $matches ) ? $matches[1] : ''; // $matches[1] is the slug of the requested language
133 108 }
134 109
135 110 /**
136 - * Returns the home url in a given language.
137 - * links_model interface.
111 + * Returns the home url
112 + * links_model interface
138 113 *
139 114 * @since 1.3.1
140 115 *
141 - * @param PLL_Language $lang PLL_Language object.
116 + * @param object $lang PLL_Language object
142 117 * @return string
143 118 */
144 119 public function home_url( $lang ) {
145 120 $base = $this->options['rewrite'] ? '' : 'language/';
@@ -147,15 +122,13 @@
147 122 return trailingslashit( $this->home . $slug );
148 123 }
149 124
150 125 /**
151 - * Optionally removes 'language' in permalinks so that we get http://www.myblog/en/ instead of http://www.myblog/language/en/
126 + * Optionaly removes 'language' in permalinks so that we get http://www.myblog/en/ instead of http://www.myblog/language/en/
152 127 *
153 128 * @since 1.2
154 - *
155 - * @return void
156 129 */
157 - public function add_permastruct() {
130 + function add_permastruct() {
158 131 // Language information always in front of the uri ( 'with_front' => false )
159 132 // The 3rd parameter structure has been modified in WP 3.4
160 133 // Leads to error 404 for pages when there is no language created yet
161 134 if ( $this->model->get_languages_list() ) {
@@ -163,14 +136,14 @@
163 136 }
164 137 }
165 138
166 139 /**
167 - * Prepares the rewrite rules filters.
140 + * Prepares rewrite rules filters
168 141 *
169 142 * @since 0.8.1
170 143 *
171 - * @param mixed $pre Not used as the filter is used as an action.
172 - * @return mixed
144 + * @param array $pre not used
145 + * @return unmodified $pre
173 146 */
174 147 public function prepare_rewrite_rules( $pre ) {
175 148 // Don't modify the rules if there is no languages created yet
176 149 // Make sure to add filter only once and if all custom post types and taxonomies have been registered
@@ -188,15 +161,15 @@
188 161 }
189 162
190 163 /**
191 164 * 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.
165 + * always make sure the default language is at the end in case the language information is hidden for default language
166 + * thanks to brbrbr http://wordpress.org/support/topic/plugin-polylang-rewrite-rules-not-correct
194 167 *
195 168 * @since 0.8.1
196 169 *
197 - * @param string[] $rules Rewrite rules.
198 - * @return string[] Modified rewrite rules.
170 + * @param array $rules rewrite rules
171 + * @return array modified rewrite rules
199 172 */
200 173 public function rewrite_rules( $rules ) {
201 174 $filter = str_replace( '_rewrite_rules', '', current_filter() );
202 175
@@ -229,10 +202,10 @@
229 202 * @param array $rule original rewrite rule
230 203 * @param string $filter current set of rules being modified
231 204 * @param string|bool $archive custom post post type archive name or false if it is not a cpt archive
232 205 */
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(
206 + if ( isset( $slug ) && apply_filters( 'pll_modify_rewrite_rule', true, array( $key => $rule ), $filter, false ) ) {
207 + $newrules[ $slug . str_replace( $wp_rewrite->root, '', $key ) ] = str_replace(
235 208 array( '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '[1]', '?' ),
236 209 array( '[9]', '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '?lang=$matches[1]&' ),
237 210 $rule
238 211 ); // Should be enough!
@@ -246,9 +219,9 @@
246 219
247 220 /** This filter is documented in include/links-directory.php */
248 221 if ( apply_filters( 'pll_modify_rewrite_rule', true, array( $key => $rule ), $filter, empty( $matches[1] ) ? false : $matches[1] ) ) {
249 222 if ( isset( $slug ) ) {
250 - $newrules[ $slug . str_replace( $wp_rewrite->root, '', ltrim( $key, '^' ) ) ] = str_replace(
223 + $newrules[ $slug . str_replace( $wp_rewrite->root, '', $key ) ] = str_replace(
251 224 array( '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '[1]', '?' ),
252 225 array( '[9]', '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '?lang=$matches[1]&' ),
253 226 $rule
254 227 ); // Should be enough!
@@ -256,9 +229,9 @@
256 229
257 230 if ( $this->options['hide_default'] ) {
258 231 $newrules[ $key ] = str_replace( '?', '?lang=' . $this->options['default_lang'] . '&', $rule );
259 232 }
260 - } else {
233 + } else {
261 234 $newrules[ $key ] = $rule;
262 235 }
263 236 }
264 237
@@ -269,9 +242,9 @@
269 242 }
270 243
271 244 // The home rewrite rule
272 245 if ( 'root' == $filter && isset( $slug ) ) {
273 - $newrules[ $slug . '?$' ] = $wp_rewrite->index . '?lang=$matches[1]';
246 + $newrules[ $slug . '?$' ] = $wp_rewrite->index.'?lang=$matches[1]';
274 247 }
275 248
276 249 return $newrules;
277 250 }