| @@ -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,15 +55,9 @@ | ||
| 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 | |
| 71 | - } | |
| 59 | + return str_replace( $this->home . '/' . $this->root, $this->home . '/' . $this->root . $slug, $url ); | |
| 72 | 60 | } |
| 73 | 61 | return $url; |
| 74 | 62 | } |
| 75 | 63 | |
| @@ -81,11 +69,9 @@ | ||
| 81 | 69 | * |
| 82 | 70 | * @param string $url url to modify |
| 83 | 71 | * @return string modified url |
| 84 | 72 | */ |
| 85 | - public function remove_language_from_link( $url ) { | |
| 86 | - $languages = array(); | |
| 87 | - | |
| 73 | + function remove_language_from_link( $url ) { | |
| 88 | 74 | foreach ( $this->model->get_languages_list() as $language ) { |
| 89 | 75 | if ( ! $this->options['hide_default'] || $this->options['default_lang'] != $language->slug ) { |
| 90 | 76 | $languages[] = $language->slug; |
| 91 | 77 | } |
| @@ -91,13 +77,11 @@ | ||
| 91 | 77 | } |
| 92 | 78 | } |
| 93 | 79 | |
| 94 | 80 | 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 ); | |
| 81 | + $pattern = str_replace( '/', '\/', $this->home . '/' . $this->root ); | |
| 82 | + $pattern = '#' . $pattern . ( $this->options['rewrite'] ? '' : 'language\/' ) . '('.implode( '|', $languages ).')(\/|$)#'; | |
| 83 | + $url = preg_replace( $pattern, $this->home . '/' . $this->root, $url ); | |
| 100 | 84 | } |
| 101 | 85 | return $url; |
| 102 | 86 | } |
| 103 | 87 | |
| @@ -112,18 +96,14 @@ | ||
| 112 | 96 | * @return string language slug |
| 113 | 97 | */ |
| 114 | 98 | public function get_language_from_url( $url = '' ) { |
| 115 | 99 | if ( empty( $url ) ) { |
| 116 | - $url = pll_get_requested_url(); | |
| 100 | + $url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; | |
| 117 | 101 | } |
| 118 | 102 | |
| 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 | |
| 103 | + $pattern = str_replace( '/', '\/', $this->home . '/' . $this->root . ( $this->options['rewrite'] ? '' : 'language/' ) ); | |
| 104 | + $pattern = '#' . $pattern . '('. implode( '|', $this->model->get_languages_list( array( 'fields' => 'slug' ) ) ) . ')(\/|$)#'; | |
| 105 | + return preg_match( $pattern, trailingslashit( $url ), $matches ) ? $matches[1] : ''; // $matches[1] is the slug of the requested language | |
| 126 | 106 | } |
| 127 | 107 | |
| 128 | 108 | /** |
| 129 | 109 | * Returns the home url |
| @@ -140,19 +120,16 @@ | ||
| 140 | 120 | return trailingslashit( $this->home . $slug ); |
| 141 | 121 | } |
| 142 | 122 | |
| 143 | 123 | /** |
| 144 | - * Optionally removes 'language' in permalinks so that we get http://www.myblog/en/ instead of http://www.myblog/language/en/ | |
| 124 | + * Optionaly removes 'language' in permalinks so that we get http://www.myblog/en/ instead of http://www.myblog/language/en/ | |
| 145 | 125 | * |
| 146 | 126 | * @since 1.2 |
| 147 | 127 | */ |
| 148 | - public function add_permastruct() { | |
| 128 | + function add_permastruct() { | |
| 149 | 129 | // Language information always in front of the uri ( 'with_front' => false ) |
| 150 | 130 | // 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 | - } | |
| 131 | + add_permastruct( 'language', $this->options['rewrite'] ? '%language%' : 'language/%language%', array( 'with_front' => false ) ); | |
| 155 | 132 | } |
| 156 | 133 | |
| 157 | 134 | /** |
| 158 | 135 | * Prepares rewrite rules filters |
| @@ -163,10 +140,9 @@ | ||
| 163 | 140 | * @return unmodified $pre |
| 164 | 141 | */ |
| 165 | 142 | public function prepare_rewrite_rules( $pre ) { |
| 166 | 143 | // 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' ) ) { | |
| 144 | + if ( $this->model->get_languages_list() && ! has_filter( 'language_rewrite_rules', '__return_empty_array' ) ) { | |
| 169 | 145 | // Suppress the rules created by WordPress for our taxonomy |
| 170 | 146 | add_filter( 'language_rewrite_rules', '__return_empty_array' ); |
| 171 | 147 | |
| 172 | 148 | foreach ( $this->get_rewrite_rules_filters() as $type ) { |
| @@ -220,10 +196,14 @@ | ||
| 220 | 196 | * @param array $rule original rewrite rule |
| 221 | 197 | * @param string $filter current set of rules being modified |
| 222 | 198 | * @param string|bool $archive custom post post type archive name or false if it is not a cpt archive |
| 223 | 199 | */ |
| 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( | |
| 200 | + if ( ! apply_filters( 'pll_modify_rewrite_rule', true, array( $key => $rule ), $filter, false ) ) { | |
| 201 | + continue; | |
| 202 | + } | |
| 203 | + | |
| 204 | + if ( isset( $slug ) ) { | |
| 205 | + $newrules[ $slug . str_replace( $wp_rewrite->root, '', $key ) ] = str_replace( | |
| 226 | 206 | array( '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '[1]', '?' ), |
| 227 | 207 | array( '[9]', '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '?lang=$matches[1]&' ), |
| 228 | 208 | $rule |
| 229 | 209 | ); // Should be enough! |
| @@ -228,9 +208,13 @@ | ||
| 228 | 208 | $rule |
| 229 | 209 | ); // Should be enough! |
| 230 | 210 | } |
| 231 | 211 | |
| 232 | - $newrules[ $key ] = $rule; | |
| 212 | + if ( $this->options['hide_default'] ) { | |
| 213 | + $newrules[ $key ] = $rules[ $key ]; | |
| 214 | + // Unset only if we hide the code for the default language as check_language_code_in_url will do its job in other cases | |
| 215 | + unset( $rules[ $key ] ); | |
| 216 | + } | |
| 233 | 217 | } |
| 234 | 218 | |
| 235 | 219 | // Rewrite rules filtered by language |
| 236 | 220 | 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'] ) ) { |
| @@ -235,35 +219,32 @@ | ||
| 235 | 219 | // Rewrite rules filtered by language |
| 236 | 220 | 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 | 221 | |
| 238 | 222 | /** This filter is documented in include/links-directory.php */ |
| 239 | - if ( apply_filters( 'pll_modify_rewrite_rule', true, array( $key => $rule ), $filter, empty( $matches[1] ) ? false : $matches[1] ) ) { | |
| 240 | - if ( isset( $slug ) ) { | |
| 241 | - $newrules[ $slug . str_replace( $wp_rewrite->root, '', ltrim( $key, '^' ) ) ] = str_replace( | |
| 242 | - array( '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '[1]', '?' ), | |
| 243 | - array( '[9]', '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '?lang=$matches[1]&' ), | |
| 244 | - $rule | |
| 245 | - ); // Should be enough! | |
| 246 | - } | |
| 223 | + if ( ! apply_filters( 'pll_modify_rewrite_rule', true, array( $key => $rule ), $filter, empty( $matches[1] ) ? false : $matches[1] ) ) { | |
| 224 | + continue; | |
| 225 | + } | |
| 247 | 226 | |
| 248 | - if ( $this->options['hide_default'] ) { | |
| 249 | - $newrules[ $key ] = str_replace( '?', '?lang=' . $this->options['default_lang'] . '&', $rule ); | |
| 250 | - } | |
| 251 | - } else { | |
| 252 | - $newrules[ $key ] = $rule; | |
| 227 | + if ( isset( $slug ) ) { | |
| 228 | + $newrules[ $slug . str_replace( $wp_rewrite->root, '', $key ) ] = str_replace( | |
| 229 | + array( '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '[1]', '?' ), | |
| 230 | + array( '[9]', '[8]', '[7]', '[6]', '[5]', '[4]', '[3]', '[2]', '?lang=$matches[1]&' ), | |
| 231 | + $rule | |
| 232 | + ); // Should be enough! | |
| 253 | 233 | } |
| 254 | - } | |
| 255 | 234 | |
| 256 | - // Unmodified rules | |
| 257 | - else { | |
| 258 | - $newrules[ $key ] = $rule; | |
| 235 | + if ( $this->options['hide_default'] ) { | |
| 236 | + $newrules[ $key ] = str_replace( '?', '?lang=' . $this->options['default_lang'] . '&', $rule ); | |
| 237 | + } | |
| 238 | + | |
| 239 | + unset( $rules[ $key ] ); // Now useless | |
| 259 | 240 | } |
| 260 | 241 | } |
| 261 | 242 | |
| 262 | 243 | // The home rewrite rule |
| 263 | 244 | if ( 'root' == $filter && isset( $slug ) ) { |
| 264 | - $newrules[ $slug . '?$' ] = $wp_rewrite->index . '?lang=$matches[1]'; | |
| 245 | + $newrules[ $slug . '?$' ] = $wp_rewrite->index.'?lang=$matches[1]'; | |
| 265 | 246 | } |
| 266 | 247 | |
| 267 | - return $newrules; | |
| 248 | + return $newrules + $rules; | |
| 268 | 249 | } |
| 269 | 250 | } |