PluginProbe
Polylang / 2.5
Polylang v2.5
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 +28 -41 3.02.5 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 : $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 : $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 }
@@ -119,27 +107,28 @@
119 107 * @return string language slug
120 108 */
121 109 public function get_language_from_url( $url = '' ) {
122 110 if ( empty( $url ) ) {
123 - $url = pll_get_requested_url();
111 + $path = $_SERVER['REQUEST_URI'];
112 + } else {
113 + $path = parse_url( $url, PHP_URL_PATH );
124 114 }
125 115
126 - $path = wp_parse_url( $url, PHP_URL_PATH );
127 116 $root = ( false === strpos( $url, '://' ) ) ? $this->home_relative . $this->root : $this->home . '/' . $this->root;
128 117
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' ) ) ) . ')(/|$)#';
118 + $pattern = parse_url( $root . ( $this->options['rewrite'] ? '' : 'language/' ), PHP_URL_PATH );
119 + $pattern = str_replace( '/', '\/', $pattern );
120 + $pattern = '#' . $pattern . '(' . implode( '|', $this->model->get_languages_list( array( 'fields' => 'slug' ) ) ) . ')(\/|$)#';
132 121 return preg_match( $pattern, trailingslashit( $path ), $matches ) ? $matches[1] : ''; // $matches[1] is the slug of the requested language
133 122 }
134 123
135 124 /**
136 - * Returns the home url in a given language.
137 - * links_model interface.
125 + * Returns the home url
126 + * links_model interface
138 127 *
139 128 * @since 1.3.1
140 129 *
141 - * @param PLL_Language $lang PLL_Language object.
130 + * @param object $lang PLL_Language object
142 131 * @return string
143 132 */
144 133 public function home_url( $lang ) {
145 134 $base = $this->options['rewrite'] ? '' : 'language/';
@@ -150,10 +139,8 @@
150 139 /**
151 140 * Optionally removes 'language' in permalinks so that we get http://www.myblog/en/ instead of http://www.myblog/language/en/
152 141 *
153 142 * @since 1.2
154 - *
155 - * @return void
156 143 */
157 144 public function add_permastruct() {
158 145 // Language information always in front of the uri ( 'with_front' => false )
159 146 // The 3rd parameter structure has been modified in WP 3.4
@@ -163,14 +150,14 @@
163 150 }
164 151 }
165 152
166 153 /**
167 - * Prepares the rewrite rules filters.
154 + * Prepares rewrite rules filters
168 155 *
169 156 * @since 0.8.1
170 157 *
171 - * @param mixed $pre Not used as the filter is used as an action.
172 - * @return mixed
158 + * @param array $pre not used
159 + * @return unmodified $pre
173 160 */
174 161 public function prepare_rewrite_rules( $pre ) {
175 162 // Don't modify the rules if there is no languages created yet
176 163 // Make sure to add filter only once and if all custom post types and taxonomies have been registered
@@ -188,15 +175,15 @@
188 175 }
189 176
190 177 /**
191 178 * 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.
179 + * always make sure the default language is at the end in case the language information is hidden for default language
180 + * thanks to brbrbr http://wordpress.org/support/topic/plugin-polylang-rewrite-rules-not-correct
194 181 *
195 182 * @since 0.8.1
196 183 *
197 - * @param string[] $rules Rewrite rules.
198 - * @return string[] Modified rewrite rules.
184 + * @param array $rules rewrite rules
185 + * @return array modified rewrite rules
199 186 */
200 187 public function rewrite_rules( $rules ) {
201 188 $filter = str_replace( '_rewrite_rules', '', current_filter() );
202 189