PluginProbe
Polylang / 2.6.2
Polylang v2.6.2
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 +24 -38 3.02.6.2 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,21 +7,16 @@
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 11 protected $home_relative;
20 12
21 13 /**
22 - * Constructor.
14 + * Constructor
23 15 *
24 16 * @since 1.2
25 17 *
26 - * @param PLL_Model $model PLL_Model instance.
18 + * @param object $model PLL_Model instance
27 19 */
28 20 public function __construct( &$model ) {
29 21 parent::__construct( $model );
30 22
@@ -40,10 +32,8 @@
40 32 /**
41 33 * Called only at first object creation to avoid duplicating filters when switching blog
42 34 *
43 35 * @since 1.6
44 - *
45 - * @return void
46 36 */
47 37 public function init() {
48 38 if ( did_action( 'setup_theme' ) ) {
49 39 $this->add_permastruct();
@@ -55,25 +45,25 @@
55 45 add_action( 'pre_option_rewrite_rules', array( $this, 'prepare_rewrite_rules' ) );
56 46 }
57 47
58 48 /**
59 - * Adds the language code in a url.
60 - * links_model interface.
49 + * Adds the language code in url
50 + * links_model interface
61 51 *
62 52 * @since 1.2
63 53 *
64 - * @param string $url The url to modify.
65 - * @param PLL_Language $lang The language object.
66 - * @return string Modified url.
54 + * @param string $url url to modify
55 + * @param object $lang language
56 + * @return string modified url
67 57 */
68 58 public function add_language_to_link( $url, $lang ) {
69 59 if ( ! empty( $lang ) ) {
70 60 $base = $this->options['rewrite'] ? '' : 'language/';
71 61 $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 );
62 + $root = ( false === strpos( $url, '://' ) ) ? $this->home_relative . $this->root : preg_replace( '/^https?:\/\//', '://', $this->home . '/' . $this->root );
73 63
74 64 if ( false === strpos( $url, $new = $root . $slug ) ) {
75 - $pattern = preg_quote( $root, '#' );
65 + $pattern = str_replace( '/', '\/', $root );
76 66 $pattern = '#' . $pattern . '#';
77 67 return preg_replace( $pattern, $new, $url, 1 ); // Only once
78 68 }
79 69 }
@@ -89,10 +79,8 @@
89 79 * @param string $url url to modify
90 80 * @return string modified url
91 81 */
92 82 public function remove_language_from_link( $url ) {
93 - $languages = array();
94 -
95 83 foreach ( $this->model->get_languages_list() as $language ) {
96 84 if ( ! $this->options['hide_default'] || $this->options['default_lang'] != $language->slug ) {
97 85 $languages[] = $language->slug;
98 86 }
@@ -98,12 +86,12 @@
98 86 }
99 87 }
100 88
101 89 if ( ! empty( $languages ) ) {
102 - $root = ( false === strpos( $url, '://' ) ) ? $this->home_relative . $this->root : preg_replace( '#^https?://#', '://', $this->home . '/' . $this->root );
90 + $root = ( false === strpos( $url, '://' ) ) ? $this->home_relative . $this->root : preg_replace( '/^https?:\/\//', '://', $this->home . '/' . $this->root );
103 91
104 - $pattern = preg_quote( $root, '#' );
105 - $pattern = '#' . $pattern . ( $this->options['rewrite'] ? '' : 'language/' ) . '(' . implode( '|', $languages ) . ')(/|$)#';
92 + $pattern = str_replace( '/', '\/', $root );
93 + $pattern = '#' . $pattern . ( $this->options['rewrite'] ? '' : 'language\/' ) . '(' . implode( '|', $languages ) . ')(\/|$)#';
106 94 $url = preg_replace( $pattern, $root, $url );
107 95 }
108 96 return $url;
109 97 }
@@ -126,20 +114,20 @@
126 114 $path = wp_parse_url( $url, PHP_URL_PATH );
127 115 $root = ( false === strpos( $url, '://' ) ) ? $this->home_relative . $this->root : $this->home . '/' . $this->root;
128 116
129 117 $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' ) ) ) . ')(/|$)#';
118 + $pattern = str_replace( '/', '\/', $pattern );
119 + $pattern = '#' . $pattern . '(' . implode( '|', $this->model->get_languages_list( array( 'fields' => 'slug' ) ) ) . ')(\/|$)#';
132 120 return preg_match( $pattern, trailingslashit( $path ), $matches ) ? $matches[1] : ''; // $matches[1] is the slug of the requested language
133 121 }
134 122
135 123 /**
136 - * Returns the home url in a given language.
137 - * links_model interface.
124 + * Returns the home url
125 + * links_model interface
138 126 *
139 127 * @since 1.3.1
140 128 *
141 - * @param PLL_Language $lang PLL_Language object.
129 + * @param object $lang PLL_Language object
142 130 * @return string
143 131 */
144 132 public function home_url( $lang ) {
145 133 $base = $this->options['rewrite'] ? '' : 'language/';
@@ -150,10 +138,8 @@
150 138 /**
151 139 * Optionally removes 'language' in permalinks so that we get http://www.myblog/en/ instead of http://www.myblog/language/en/
152 140 *
153 141 * @since 1.2
154 - *
155 - * @return void
156 142 */
157 143 public function add_permastruct() {
158 144 // Language information always in front of the uri ( 'with_front' => false )
159 145 // The 3rd parameter structure has been modified in WP 3.4
@@ -163,14 +149,14 @@
163 149 }
164 150 }
165 151
166 152 /**
167 - * Prepares the rewrite rules filters.
153 + * Prepares rewrite rules filters
168 154 *
169 155 * @since 0.8.1
170 156 *
171 - * @param mixed $pre Not used as the filter is used as an action.
172 - * @return mixed
157 + * @param array $pre not used
158 + * @return unmodified $pre
173 159 */
174 160 public function prepare_rewrite_rules( $pre ) {
175 161 // Don't modify the rules if there is no languages created yet
176 162 // Make sure to add filter only once and if all custom post types and taxonomies have been registered
@@ -188,15 +174,15 @@
188 174 }
189 175
190 176 /**
191 177 * 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.
178 + * always make sure the default language is at the end in case the language information is hidden for default language
179 + * thanks to brbrbr http://wordpress.org/support/topic/plugin-polylang-rewrite-rules-not-correct
194 180 *
195 181 * @since 0.8.1
196 182 *
197 - * @param string[] $rules Rewrite rules.
198 - * @return string[] Modified rewrite rules.
183 + * @param array $rules rewrite rules
184 + * @return array modified rewrite rules
199 185 */
200 186 public function rewrite_rules( $rules ) {
201 187 $filter = str_replace( '_rewrite_rules', '', current_filter() );
202 188