3 class WPML_URL_Converter_Subdir_Strategy extends WPML_URL_Converter_Abstract_Strategy {
5 private $use_directory_for_default_lang;
7 /** @var array copy of $sitepress->get_settings( 'urls' ) */
8 private $urls_settings;
10 /** @var string|bool */
13 /** @var array map of wpml codes to custom codes*/
14 private $language_codes_map;
15 private $language_codes_reverse_map;
18 private $is_rest_request;
21 * @param bool $use_directory_for_default_lang
22 * @param string $default_language
23 * @param array $active_languages
24 * @param array $urls_settings
26 public function __construct(
27 $use_directory_for_default_lang,
32 parent::__construct( $default_language, $active_languages );
33 $this->use_directory_for_default_lang = (bool) $use_directory_for_default_lang;
34 $this->urls_settings = $urls_settings;
36 $this->language_codes_map = array_combine( $active_languages, $active_languages );
37 $this->language_codes_map = apply_filters( 'wpml_language_codes_map', $this->language_codes_map );
39 $this->language_codes_reverse_map = array_flip( $this->language_codes_map );
44 public function add_hooks() {
45 add_filter( 'rest_url', [ $this, 'convertRestUrl' ] );
53 public function convertRestUrl( $url ) {
57 $matchTrailingSlash = $url[ strlen( $url ) - 1 ] === '/' ? 'trailingslashit' : 'untrailingslashit';
59 return $matchTrailingSlash( $this->convert_url_string( $url, $sitepress->get_current_language() ) );
62 public function get_lang_from_url_string( $url ) {
63 $url_path = $this->get_url_path( wpml_strip_subdir_from_url( $url ) );
64 $lang = $this->extract_lang_from_url_path( $url_path );
66 if ( $lang && in_array( $lang, $this->active_languages, true ) ) {
70 return $this->use_directory_for_default_lang ? null : $this->default_language;
73 public function validate_language( $language, $url ) {
74 if ( ! ( null === $language && $this->use_directory_for_default_lang && ! $this->get_url_helper()->is_url_admin( $url ) ) ) {
75 $language = parent::validate_language( $language, $url );
81 public function convert_url_string( $source_url, $code ) {
82 if ( $this->is_root_url( $source_url ) || $this->skip_convert_url_string( $source_url, $code ) ) {
86 $source_url = $this->filter_source_url( $source_url );
88 $absolute_home_url = trailingslashit( preg_replace( '#^(http|https)://#', '', $this->get_url_helper()->get_abs_home() ) );
89 $absolute_home_url = strpos( $source_url, $absolute_home_url ) === false ? trailingslashit( get_option( 'home' ) ) : $absolute_home_url;
91 $current_language = $this->get_lang_from_url_string( $source_url );
92 $code = $this->get_language_of_current_dir( $code, '' );
93 $current_language = $this->get_language_of_current_dir( $current_language, '' );
95 $code = isset( $this->language_codes_map[ $code ] ) ? $this->language_codes_map[ $code ] : $code;
96 $current_language = isset( $this->language_codes_map[ $current_language ] ) ? $this->language_codes_map[ $current_language ] : $current_language;
98 $source_url = str_replace(
100 trailingslashit( $absolute_home_url . $current_language ),
104 $code ? ( $absolute_home_url . $code . '/' ) : trailingslashit( $absolute_home_url ),
110 return $this->slash_helper->maybe_user_trailingslashit( $source_url );
113 public function convert_admin_url_string( $source_url, $lang ) {
114 return $source_url; // Admin strings should not be converted with language in directories
119 * @param string $language
123 public function get_home_url_relative( $url, $language ) {
124 $language = $this->get_language_of_current_dir( $language, '' );
125 $language = isset( $this->language_codes_map[ $language ] ) ? $this->language_codes_map[ $language ] : $language;
128 $parts = parse_url( get_option( 'home' ) );
129 $path = isset( $parts['path'] ) ? $parts['path'] : '';
130 $url = preg_replace( '@^' . $path . '@', '', $url );
132 return rtrim( $path, '/' ) . '/' . $language . $url;
138 public function use_wp_login_url_converter() {
143 * Will return true if root URL or child of root URL
149 private function is_root_url( $url ) {
152 if ( isset( $this->urls_settings['root_page'], $this->urls_settings['show_on_root'] ) &&
153 'page' === $this->urls_settings['show_on_root'] &&
154 ! empty( $this->urls_settings['directory_for_default_language'] )
157 $root_url = $this->get_root_url();
159 $result = strpos( trailingslashit( $url ), $root_url ) === 0;
167 * @return string|bool
169 private function get_root_url() {
170 if ( null === $this->root_url ) {
171 $root_post = get_post( $this->urls_settings['root_page'] );
174 $this->root_url = trailingslashit( $this->get_url_helper()->get_abs_home() ) . $root_post->post_name;
175 $this->root_url = trailingslashit( $this->root_url );
177 $this->root_url = false;
181 return $this->root_url;
185 * @param string $source_url
189 private function filter_source_url( $source_url ) {
190 if ( false === strpos( $source_url, '?' ) ) {
191 $source_url = trailingslashit( $source_url );
192 } elseif ( false !== strpos( $source_url, '?' ) && false === strpos( $source_url, '/?' ) ) {
193 $source_url = str_replace( '?', '/?', $source_url );
204 private function get_url_path( $url ) {
205 if ( strpos( $url, 'http://' ) === 0 || strpos( $url, 'https://' ) === 0 ) {
206 $url_path = wpml_parse_url( $url, PHP_URL_PATH );
208 $pathparts = array_filter( explode( '/', $url ) );
209 if ( count( $pathparts ) > 1 ) {
210 unset( $pathparts[0] );
211 $url_path = implode( '/', $pathparts );
221 * @param string $url_path
225 private function extract_lang_from_url_path( $url_path ) {
226 $fragments = array_filter( (array) explode( '/', $url_path ) );
227 $lang = array_shift( $fragments );
229 $lang_get_parts = explode( '?', $lang );
230 $lang = $lang_get_parts[0];
232 return isset( $this->language_codes_reverse_map[ $lang ] ) ? $this->language_codes_reverse_map[ $lang ] : $lang;
236 * @param string $language_code
237 * @param null|string $value_if_default_language
239 * @return string|null
241 private function get_language_of_current_dir( $language_code, $value_if_default_language = null ) {
242 if ( ! $this->use_directory_for_default_lang && $language_code === $this->default_language ) {
243 return $value_if_default_language;
246 return $language_code;