PluginProbe
Polylang / 3.7.7
Polylang v3.7.7
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 +124 -98 2.93.7.7 View file →
@@ -3,72 +3,71 @@
3 3 * @package Polylang
4 4 */
5 5
6 6 /**
7 - * Links model for use when the language code is added in url as a directory
8 - * for example mysite.com/en/something
9 - * implements the "links_model interface"
7 + * Links model for use when the language code is added in the url as a directory
8 + * for example mysite.com/en/something.
10 9 *
11 10 * @since 1.2
12 11 */
13 12 class PLL_Links_Directory extends PLL_Links_Permalinks {
13 + /**
14 + * Relative path to the home url.
15 + *
16 + * @var string
17 + */
14 18 protected $home_relative;
15 19
16 20 /**
17 - * Constructor
21 + * Constructor.
18 22 *
19 23 * @since 1.2
20 24 *
21 - * @param object $model PLL_Model instance
25 + * @param PLL_Model $model PLL_Model instance.
22 26 */
23 27 public function __construct( &$model ) {
24 28 parent::__construct( $model );
25 29
26 30 $this->home_relative = home_url( '/', 'relative' );
27 -
28 - if ( did_action( 'pll_init' ) ) {
29 - $this->init();
30 - } else {
31 - add_action( 'pll_init', array( $this, 'init' ) );
32 - }
33 31 }
34 32
35 33 /**
36 - * Called only at first object creation to avoid duplicating filters when switching blog
34 + * Adds hooks for rewrite rules.
37 35 *
38 36 * @since 1.6
37 + *
38 + * @return void
39 39 */
40 40 public function init() {
41 - if ( did_action( 'setup_theme' ) ) {
42 - $this->add_permastruct();
43 - } else {
44 - add_action( 'setup_theme', array( $this, 'add_permastruct' ), 2 );
45 - }
41 + add_action( 'pll_prepare_rewrite_rules', array( $this, 'prepare_rewrite_rules' ) ); // Ensure it's hooked before `self::do_prepare_rewrite_rules()` is called.
46 42
47 - // Make sure to prepare rewrite rules when flushing
48 - add_action( 'pre_option_rewrite_rules', array( $this, 'prepare_rewrite_rules' ) );
43 + parent::init();
49 44 }
50 45
51 46 /**
52 - * Adds the language code in url
53 - * links_model interface
47 + * Adds the language code in a url.
54 48 *
55 49 * @since 1.2
50 + * @since 3.4 Accepts now a language slug.
56 51 *
57 - * @param string $url url to modify
58 - * @param object $lang language
59 - * @return string modified url
52 + * @param string $url The url to modify.
53 + * @param PLL_Language|string|false $language Language object or slug.
54 + * @return string The modified url.
60 55 */
61 - public function add_language_to_link( $url, $lang ) {
62 - if ( ! empty( $lang ) ) {
56 + public function add_language_to_link( $url, $language ) {
57 + if ( $language instanceof PLL_Language ) {
58 + $language = $language->slug;
59 + }
60 +
61 + if ( ! empty( $language ) ) {
63 62 $base = $this->options['rewrite'] ? '' : 'language/';
64 - $slug = $this->options['default_lang'] == $lang->slug && $this->options['hide_default'] ? '' : $base . $lang->slug . '/';
63 + $slug = $this->options['default_lang'] === $language && $this->options['hide_default'] ? '' : $base . $language . '/';
65 64 $root = ( false === strpos( $url, '://' ) ) ? $this->home_relative . $this->root : preg_replace( '#^https?://#', '://', $this->home . '/' . $this->root );
66 65
67 66 if ( false === strpos( $url, $new = $root . $slug ) ) {
68 67 $pattern = preg_quote( $root, '#' );
69 68 $pattern = '#' . $pattern . '#';
70 - return preg_replace( $pattern, $new, $url, 1 ); // Only once
69 + return preg_replace( $pattern, $new, $url, 1 ); // Only once.
71 70 }
72 71 }
73 72 return $url;
74 73 }
@@ -73,44 +72,41 @@
73 72 return $url;
74 73 }
75 74
76 75 /**
77 - * Returns the url without language code
78 - * links_model interface
76 + * Returns the url without the language code.
79 77 *
80 78 * @since 1.2
81 79 *
82 - * @param string $url url to modify
83 - * @return string modified url
80 + * @param string $url The url to modify.
81 + * @return string The modified url.
84 82 */
85 83 public function remove_language_from_link( $url ) {
86 - $languages = array();
84 + $languages = $this->model->get_languages_list(
85 + array(
86 + 'hide_default' => $this->options['hide_default'],
87 + 'fields' => 'slug',
88 + )
89 + );
87 90
88 - foreach ( $this->model->get_languages_list() as $language ) {
89 - if ( ! $this->options['hide_default'] || $this->options['default_lang'] != $language->slug ) {
90 - $languages[] = $language->slug;
91 - }
92 - }
93 -
94 91 if ( ! empty( $languages ) ) {
95 92 $root = ( false === strpos( $url, '://' ) ) ? $this->home_relative . $this->root : preg_replace( '#^https?://#', '://', $this->home . '/' . $this->root );
96 93
97 - $pattern = preg_quote( $root, '#' );
98 - $pattern = '#' . $pattern . ( $this->options['rewrite'] ? '' : 'language/' ) . '(' . implode( '|', $languages ) . ')(/|$)#';
99 - $url = preg_replace( $pattern, $root, $url );
94 + $pattern = preg_quote( $root, '@' );
95 + $pattern = '@' . $pattern . ( $this->options['rewrite'] ? '' : 'language/' ) . '(' . implode( '|', $languages ) . ')(([?#])|(/|$))@';
96 + $url = preg_replace( $pattern, $root . '$3', $url );
100 97 }
101 98 return $url;
102 99 }
103 100
104 101 /**
105 - * Returns the language based on language code in url
106 - * links_model interface
102 + * Returns the language based on the language code in the url.
107 103 *
108 104 * @since 1.2
109 - * @since 2.0 add $url argument
105 + * @since 2.0 Add the $url argument.
110 106 *
111 - * @param string $url optional, defaults to current url
112 - * @return string language slug
107 + * @param string $url Optional, defaults to the current url.
108 + * @return string The language slug.
113 109 */
114 110 public function get_language_from_url( $url = '' ) {
115 111 if ( empty( $url ) ) {
116 112 $url = pll_get_requested_url();
@@ -115,79 +111,69 @@
115 111 if ( empty( $url ) ) {
116 112 $url = pll_get_requested_url();
117 113 }
118 114
119 - $path = wp_parse_url( $url, PHP_URL_PATH );
115 + $path = (string) wp_parse_url( $url, PHP_URL_PATH );
120 116 $root = ( false === strpos( $url, '://' ) ) ? $this->home_relative . $this->root : $this->home . '/' . $this->root;
121 117
122 - $pattern = wp_parse_url( $root . ( $this->options['rewrite'] ? '' : 'language/' ), PHP_URL_PATH );
118 + $pattern = (string) wp_parse_url( $root . ( $this->options['rewrite'] ? '' : 'language/' ), PHP_URL_PATH );
123 119 $pattern = preg_quote( $pattern, '#' );
124 120 $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
121 + return preg_match( $pattern, trailingslashit( $path ), $matches ) ? $matches[1] : ''; // $matches[1] is the slug of the requested language.
126 122 }
127 123
128 124 /**
129 - * Returns the home url
130 - * links_model interface
125 + * Returns the home url in a given language.
131 126 *
132 127 * @since 1.3.1
128 + * @since 3.4 Accepts now a language slug.
133 129 *
134 - * @param object $lang PLL_Language object
130 + * @param PLL_Language|string $language Language object or slug.
135 131 * @return string
136 132 */
137 - public function home_url( $lang ) {
133 + public function home_url( $language ) {
134 + if ( $language instanceof PLL_Language ) {
135 + $language = $language->slug;
136 + }
137 +
138 138 $base = $this->options['rewrite'] ? '' : 'language/';
139 - $slug = $this->options['default_lang'] == $lang->slug && $this->options['hide_default'] ? '' : '/' . $this->root . $base . $lang->slug;
139 + $slug = $this->options['default_lang'] === $language && $this->options['hide_default'] ? '' : '/' . $this->root . $base . $language;
140 140 return trailingslashit( $this->home . $slug );
141 141 }
142 142
143 143 /**
144 - * Optionally removes 'language' in permalinks so that we get http://www.myblog/en/ instead of http://www.myblog/language/en/
144 + * Prepares the rewrite rules filters.
145 145 *
146 - * @since 1.2
147 - */
148 - public function add_permastruct() {
149 - // Language information always in front of the uri ( 'with_front' => false )
150 - // The 3rd parameter structure has been modified in WP 3.4
151 - // Leads to error 404 for pages when there is no language created yet
152 - if ( $this->model->get_languages_list() ) {
153 - add_permastruct( 'language', $this->options['rewrite'] ? '%language%' : 'language/%language%', array( 'with_front' => false ) );
154 - }
155 - }
156 -
157 - /**
158 - * Prepares rewrite rules filters
159 - *
160 146 * @since 0.8.1
147 + * @since 3.5 Hooked to `pll_prepare_rewrite_rules` and remove `$pre` parameter.
161 148 *
162 - * @param array $pre not used
163 - * @return unmodified $pre
149 + * @return void
164 150 */
165 - public function prepare_rewrite_rules( $pre ) {
166 - // Don't modify the rules if there is no languages created yet
167 - // Make sure to add filter only once and if all custom post types and taxonomies have been registered
168 - if ( $this->model->get_languages_list() && did_action( 'wp_loaded' ) && ! has_filter( 'language_rewrite_rules', '__return_empty_array' ) ) {
169 - // Suppress the rules created by WordPress for our taxonomy
170 - add_filter( 'language_rewrite_rules', '__return_empty_array' );
151 + public function prepare_rewrite_rules() {
152 + /*
153 + * Don't modify the rules if there is no languages created yet and make sure
154 + * to add the filters only once and if all custom post types and taxonomies
155 + * have been registered.
156 + */
157 + if ( ! $this->model->has_languages() || ! did_action( 'wp_loaded' ) || ! self::$can_filter_rewrite_rules ) {
158 + return;
159 + }
171 160
172 - foreach ( $this->get_rewrite_rules_filters() as $type ) {
173 - add_filter( $type . '_rewrite_rules', array( $this, 'rewrite_rules' ) );
174 - }
175 -
176 - add_filter( 'rewrite_rules_array', array( $this, 'rewrite_rules' ) ); // needed for post type archives
161 + foreach ( $this->get_rewrite_rules_filters_with_callbacks() as $rule => $callback ) {
162 + add_filter( $rule, $callback );
177 163 }
178 - return $pre;
179 164 }
180 165
181 166 /**
182 167 * The rewrite rules !
183 - * always make sure the default language is at the end in case the language information is hidden for default language
184 - * thanks to brbrbr http://wordpress.org/support/topic/plugin-polylang-rewrite-rules-not-correct
185 168 *
169 + * Always make sure that the default language is at the end in case the language information is hidden for default language.
170 + * Thanks to brbrbr http://wordpress.org/support/topic/plugin-polylang-rewrite-rules-not-correct.
171 + *
186 172 * @since 0.8.1
187 173 *
188 - * @param array $rules rewrite rules
189 - * @return array modified rewrite rules
174 + * @param string[] $rules Rewrite rules.
175 + * @return string[] Modified rewrite rules.
190 176 */
191 177 public function rewrite_rules( $rules ) {
192 178 $filter = str_replace( '_rewrite_rules', '', current_filter() );
193 179
@@ -202,25 +188,30 @@
202 188 if ( ! empty( $languages ) ) {
203 189 $slug = $wp_rewrite->root . ( $this->options['rewrite'] ? '' : 'language/' ) . '(' . implode( '|', $languages ) . ')/';
204 190 }
205 191
206 - // For custom post type archives
192 + // For custom post type archives.
207 193 $cpts = array_intersect( $this->model->get_translated_post_types(), get_post_types( array( '_builtin' => false ) ) );
208 194 $cpts = $cpts ? '#post_type=(' . implode( '|', $cpts ) . ')#' : '';
209 195
210 196 foreach ( $rules as $key => $rule ) {
211 - // Special case for translated post types and taxonomies to allow canonical redirection
197 + if ( ! is_string( $rule ) || ! is_string( $key ) ) {
198 + // Protection against a bug in Sendinblue for WooCommerce. See: https://wordpress.org/support/topic/bug-introduced-in-rewrite-rules/
199 + continue;
200 + }
201 +
202 + // Special case for translated post types and taxonomies to allow canonical redirection.
212 203 if ( $this->options['force_lang'] && in_array( $filter, array_merge( $this->model->get_translated_post_types(), $this->model->get_translated_taxonomies() ) ) ) {
213 204
214 205 /**
215 - * Filters the rewrite rules to modify
206 + * Filters the rewrite rules to modify.
216 207 *
217 208 * @since 1.9.1
218 209 *
219 - * @param bool $modify whether to modify or not the rule, defaults to true
220 - * @param array $rule original rewrite rule
221 - * @param string $filter current set of rules being modified
222 - * @param string|bool $archive custom post post type archive name or false if it is not a cpt archive
210 + * @param bool $modify Whether to modify or not the rule, defaults to true.
211 + * @param array $rule Original rewrite rule.
212 + * @param string $filter Current set of rules being modified.
213 + * @param string|bool $archive Custom post post type archive name or false if it is not a cpt archive.
223 214 */
224 215 if ( isset( $slug ) && apply_filters( 'pll_modify_rewrite_rule', true, array( $key => $rule ), $filter, false ) ) {
225 216 $newrules[ $slug . str_replace( $wp_rewrite->root, '', ltrim( $key, '^' ) ) ] = str_replace(
226 217 array( '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '[1]', '?' ),
@@ -231,9 +222,9 @@
231 222
232 223 $newrules[ $key ] = $rule;
233 224 }
234 225
235 - // Rewrite rules filtered by language
226 + // Rewrite rules filtered by language.
236 227 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 228
238 229 /** This filter is documented in include/links-directory.php */
239 230 if ( apply_filters( 'pll_modify_rewrite_rule', true, array( $key => $rule ), $filter, empty( $matches[1] ) ? false : $matches[1] ) ) {
@@ -252,18 +243,53 @@
252 243 $newrules[ $key ] = $rule;
253 244 }
254 245 }
255 246
256 - // Unmodified rules
247 + // Unmodified rules.
257 248 else {
258 249 $newrules[ $key ] = $rule;
259 250 }
260 251 }
261 252
262 - // The home rewrite rule
253 + // The home rewrite rule.
263 254 if ( 'root' == $filter && isset( $slug ) ) {
264 255 $newrules[ $slug . '?$' ] = $wp_rewrite->index . '?lang=$matches[1]';
265 256 }
266 257
267 258 return $newrules;
259 + }
260 +
261 + /**
262 + * Removes hooks to filter rewrite rules, called when switching blog @see {PLL_Base::switch_blog()}.
263 + * See `self::prepare_rewrite_rules()` for added hooks.
264 + *
265 + * @since 3.5
266 + *
267 + * @return void
268 + */
269 + public function remove_filters() {
270 + parent::remove_filters();
271 +
272 + foreach ( $this->get_rewrite_rules_filters_with_callbacks() as $rule => $callback ) {
273 + remove_filter( $rule, $callback );
274 + }
275 + }
276 +
277 + /**
278 + * Returns *all* rewrite rules filters with their associated callbacks.
279 + *
280 + * @since 3.5
281 + *
282 + * @return callable[] Array of hook names as key and callbacks as values.
283 + */
284 + protected function get_rewrite_rules_filters_with_callbacks() {
285 + $filters = array(
286 + 'rewrite_rules_array' => array( $this, 'rewrite_rules' ), // Needed for post type archives.
287 + );
288 +
289 + foreach ( $this->get_rewrite_rules_filters() as $type ) {
290 + $filters[ $type . '_rewrite_rules' ] = array( $this, 'rewrite_rules' );
291 + }
292 +
293 + return $filters;
268 294 }
269 295 }