| @@ -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,9 +7,8 @@ | ||
| 10 | 7 | * |
| 11 | 8 | * @since 1.2 |
| 12 | 9 | */ |
| 13 | 10 | class PLL_Links_Directory extends PLL_Links_Permalinks { |
| 14 | - protected $home_relative; | |
| 15 | 11 | |
| 16 | 12 | /** |
| 17 | 13 | * Constructor |
| 18 | 14 | * |
| @@ -22,10 +18,8 @@ | ||
| 22 | 18 | */ |
| 23 | 19 | public function __construct( &$model ) { |
| 24 | 20 | parent::__construct( $model ); |
| 25 | 21 | |
| 26 | - $this->home_relative = home_url( '/', 'relative' ); | |
| 27 | - | |
| 28 | 22 | if ( did_action( 'pll_init' ) ) { |
| 29 | 23 | $this->init(); |
| 30 | 24 | } else { |
| 31 | 25 | add_action( 'pll_init', array( $this, 'init' ) ); |
| @@ -61,14 +55,10 @@ | ||
| 61 | 55 | public function add_language_to_link( $url, $lang ) { |
| 62 | 56 | if ( ! empty( $lang ) ) { |
| 63 | 57 | $base = $this->options['rewrite'] ? '' : 'language/'; |
| 64 | 58 | $slug = $this->options['default_lang'] == $lang->slug && $this->options['hide_default'] ? '' : $base . $lang->slug . '/'; |
| 65 | - $root = ( false === strpos( $url, '://' ) ) ? $this->home_relative . $this->root : preg_replace( '#^https?://#', '://', $this->home . '/' . $this->root ); | |
| 66 | - | |
| 67 | - if ( false === strpos( $url, $new = $root . $slug ) ) { | |
| 68 | - $pattern = preg_quote( $root, '#' ); | |
| 69 | - $pattern = '#' . $pattern . '#'; | |
| 70 | - 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 ); | |
| 71 | 61 | } |
| 72 | 62 | } |
| 73 | 63 | return $url; |
| 74 | 64 | } |
| @@ -81,11 +71,9 @@ | ||
| 81 | 71 | * |
| 82 | 72 | * @param string $url url to modify |
| 83 | 73 | * @return string modified url |
| 84 | 74 | */ |
| 85 | - public function remove_language_from_link( $url ) { | |
| 86 | - $languages = array(); | |
| 87 | - | |
| 75 | + function remove_language_from_link( $url ) { | |
| 88 | 76 | foreach ( $this->model->get_languages_list() as $language ) { |
| 89 | 77 | if ( ! $this->options['hide_default'] || $this->options['default_lang'] != $language->slug ) { |
| 90 | 78 | $languages[] = $language->slug; |
| 91 | 79 | } |
| @@ -91,13 +79,11 @@ | ||
| 91 | 79 | } |
| 92 | 80 | } |
| 93 | 81 | |
| 94 | 82 | if ( ! empty( $languages ) ) { |
| 95 | - $root = ( false === strpos( $url, '://' ) ) ? $this->home_relative . $this->root : preg_replace( '#^https?://#', '://', $this->home . '/' . $this->root ); | |
| 96 | - | |
| 97 | - $pattern = preg_quote( $root, '#' ); | |
| 98 | - $pattern = '#' . $pattern . ( $this->options['rewrite'] ? '' : 'language/' ) . '(' . implode( '|', $languages ) . ')(/|$)#'; | |
| 99 | - $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 ); | |
| 100 | 86 | } |
| 101 | 87 | return $url; |
| 102 | 88 | } |
| 103 | 89 | |
| @@ -112,18 +98,14 @@ | ||
| 112 | 98 | * @return string language slug |
| 113 | 99 | */ |
| 114 | 100 | public function get_language_from_url( $url = '' ) { |
| 115 | 101 | if ( empty( $url ) ) { |
| 116 | - $url = pll_get_requested_url(); | |
| 102 | + $url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; | |
| 117 | 103 | } |
| 118 | 104 | |
| 119 | - $path = wp_parse_url( $url, PHP_URL_PATH ); | |
| 120 | - $root = ( false === strpos( $url, '://' ) ) ? $this->home_relative . $this->root : $this->home . '/' . $this->root; | |
| 121 | - | |
| 122 | - $pattern = wp_parse_url( $root . ( $this->options['rewrite'] ? '' : 'language/' ), PHP_URL_PATH ); | |
| 123 | - $pattern = preg_quote( $pattern, '#' ); | |
| 124 | - $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 | |
| 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 | |
| 126 | 108 | } |
| 127 | 109 | |
| 128 | 110 | /** |
| 129 | 111 | * Returns the home url |
| @@ -140,13 +122,13 @@ | ||
| 140 | 122 | return trailingslashit( $this->home . $slug ); |
| 141 | 123 | } |
| 142 | 124 | |
| 143 | 125 | /** |
| 144 | - * 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/ | |
| 145 | 127 | * |
| 146 | 128 | * @since 1.2 |
| 147 | 129 | */ |
| 148 | - public function add_permastruct() { | |
| 130 | + function add_permastruct() { | |
| 149 | 131 | // Language information always in front of the uri ( 'with_front' => false ) |
| 150 | 132 | // The 3rd parameter structure has been modified in WP 3.4 |
| 151 | 133 | // Leads to error 404 for pages when there is no language created yet |
| 152 | 134 | if ( $this->model->get_languages_list() ) { |
| @@ -220,10 +202,10 @@ | ||
| 220 | 202 | * @param array $rule original rewrite rule |
| 221 | 203 | * @param string $filter current set of rules being modified |
| 222 | 204 | * @param string|bool $archive custom post post type archive name or false if it is not a cpt archive |
| 223 | 205 | */ |
| 224 | - if ( isset( $slug ) && apply_filters( 'pll_modify_rewrite_rule', true, array( $key => $rule ), $filter, false ) ) { | |
| 225 | - $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( | |
| 226 | 208 | array( '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '[1]', '?' ), |
| 227 | 209 | array( '[9]', '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '?lang=$matches[1]&' ), |
| 228 | 210 | $rule |
| 229 | 211 | ); // Should be enough! |
| @@ -237,9 +219,9 @@ | ||
| 237 | 219 | |
| 238 | 220 | /** This filter is documented in include/links-directory.php */ |
| 239 | 221 | if ( apply_filters( 'pll_modify_rewrite_rule', true, array( $key => $rule ), $filter, empty( $matches[1] ) ? false : $matches[1] ) ) { |
| 240 | 222 | if ( isset( $slug ) ) { |
| 241 | - $newrules[ $slug . str_replace( $wp_rewrite->root, '', ltrim( $key, '^' ) ) ] = str_replace( | |
| 223 | + $newrules[ $slug . str_replace( $wp_rewrite->root, '', $key ) ] = str_replace( | |
| 242 | 224 | array( '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '[1]', '?' ), |
| 243 | 225 | array( '[9]', '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '?lang=$matches[1]&' ), |
| 244 | 226 | $rule |
| 245 | 227 | ); // Should be enough! |
| @@ -247,9 +229,9 @@ | ||
| 247 | 229 | |
| 248 | 230 | if ( $this->options['hide_default'] ) { |
| 249 | 231 | $newrules[ $key ] = str_replace( '?', '?lang=' . $this->options['default_lang'] . '&', $rule ); |
| 250 | 232 | } |
| 251 | - } else { | |
| 233 | + } else { | |
| 252 | 234 | $newrules[ $key ] = $rule; |
| 253 | 235 | } |
| 254 | 236 | } |
| 255 | 237 | |
| @@ -260,9 +242,9 @@ | ||
| 260 | 242 | } |
| 261 | 243 | |
| 262 | 244 | // The home rewrite rule |
| 263 | 245 | if ( 'root' == $filter && isset( $slug ) ) { |
| 264 | - $newrules[ $slug . '?$' ] = $wp_rewrite->index . '?lang=$matches[1]'; | |
| 246 | + $newrules[ $slug . '?$' ] = $wp_rewrite->index.'?lang=$matches[1]'; | |
| 265 | 247 | } |
| 266 | 248 | |
| 267 | 249 | return $newrules; |
| 268 | 250 | } |