| @@ -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,17 +98,16 @@ | ||
| 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 | + $path = $_SERVER['REQUEST_URI']; | |
| 103 | + } else { | |
| 104 | + $path = parse_url( $url, PHP_URL_PATH ); | |
| 117 | 105 | } |
| 118 | 106 | |
| 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' ) ) ) . ')(/|$)#'; | |
| 107 | + $pattern = parse_url( $this->home . '/' . $this->root . ( $this->options['rewrite'] ? '' : 'language/' ), PHP_URL_PATH ); | |
| 108 | + $pattern = str_replace( '/', '\/', $pattern ); | |
| 109 | + $pattern = '#' . $pattern . '(' . implode( '|', $this->model->get_languages_list( array( 'fields' => 'slug' ) ) ) . ')(\/|$)#'; | |
| 125 | 110 | return preg_match( $pattern, trailingslashit( $path ), $matches ) ? $matches[1] : ''; // $matches[1] is the slug of the requested language |
| 126 | 111 | } |
| 127 | 112 | |
| 128 | 113 | /** |
| @@ -144,9 +129,9 @@ | ||
| 144 | 129 | * Optionally removes 'language' in permalinks so that we get http://www.myblog/en/ instead of http://www.myblog/language/en/ |
| 145 | 130 | * |
| 146 | 131 | * @since 1.2 |
| 147 | 132 | */ |
| 148 | - public function add_permastruct() { | |
| 133 | + function add_permastruct() { | |
| 149 | 134 | // Language information always in front of the uri ( 'with_front' => false ) |
| 150 | 135 | // The 3rd parameter structure has been modified in WP 3.4 |
| 151 | 136 | // Leads to error 404 for pages when there is no language created yet |
| 152 | 137 | if ( $this->model->get_languages_list() ) { |