3 class WPML_URL_Converter_Subdir_Strategy extends WPML_URL_Converter_Abstract_Strategy {
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 * @param string $dir_default
19 * @param string $default_language
20 * @param array $active_languages
21 * @param array $urls_settings
23 public function __construct(
29 parent::__construct( $default_language, $active_languages );
30 $this->dir_default = $dir_default;
31 $this->urls_settings = $urls_settings;
33 $this->language_codes_map = array_combine( $active_languages, $active_languages );
34 $this->language_codes_map = apply_filters( 'wpml_language_codes_map', $this->language_codes_map );
36 $this->language_codes_reverse_map = array_flip( $this->language_codes_map );
39 public function get_lang_from_url_string( $url ) {
41 $url = wpml_strip_subdir_from_url( $url );
43 if ( strpos( $url, 'http://' ) === 0 || strpos( $url, 'https://' ) === 0 ) {
44 $url_path = wpml_parse_url( $url, PHP_URL_PATH );
46 $pathparts = array_filter( explode( '/', $url ) );
47 if ( count( $pathparts ) > 1 ) {
48 unset( $pathparts[0] );
49 $url_path = implode( '/', $pathparts );
55 $fragments = array_filter( (array) explode( '/', $url_path ) );
56 $lang = array_shift( $fragments );
58 $lang_get_parts = explode( '?', $lang );
59 $lang = $lang_get_parts[0];
61 $lang = isset( $this->language_codes_reverse_map[ $lang ] ) ? $this->language_codes_reverse_map[ $lang ] : $lang;
63 if ( $lang && in_array( $lang, $this->active_languages, true ) ) {
66 return $this->dir_default ? null : $this->default_language;
69 public function validate_language( $language, $url ) {
70 if ( ! ( null === $language && $this->dir_default && ! $this->get_url_helper()->is_url_admin( $url ) ) ) {
71 $language = parent::validate_language( $language, $url );
77 public function convert_url_string( $source_url, $code ) {
78 if ( ! $this->is_root_url( $source_url ) ) {
79 $source_url = $this->filter_source_url( $source_url );
81 $absolute_home_url = trailingslashit( preg_replace( '#^(http|https)://#', '', $this->get_url_helper()->get_abs_home() ) );
82 $absolute_home_url = strpos( $source_url, $absolute_home_url ) === false ? trailingslashit( get_option( 'home' ) ) : $absolute_home_url;
84 $code = ! $this->dir_default && $code === $this->default_language ? '' : $code;
85 $current_language = $this->get_lang_from_url_string( $source_url );
86 $current_language = ! $this->dir_default && $current_language === $this->default_language ? '' : $current_language;
88 $code = isset( $this->language_codes_map[ $code ] ) ? $this->language_codes_map[ $code ] : $code;
89 $current_language = isset( $this->language_codes_map[ $current_language ] ) ? $this->language_codes_map[ $current_language ] : $current_language;
91 $redirector = new WPML_WPSEO_Redirection();
93 if ( ! $redirector->is_redirection() ) {
94 $source_url = str_replace(
96 trailingslashit( $absolute_home_url . $current_language ),
100 $code ? ( $absolute_home_url . $code . '/' ) : trailingslashit( $absolute_home_url ),
108 return $this->slash_helper->maybe_user_trailingslashit( $source_url, 'untrailingslashit' );
113 * @param string $langauge
117 public function get_home_url_relative( $url, $language ) {
118 $language = ! $this->dir_default && $language === $this->default_language ? '' : $language;
119 $language = isset( $this->language_codes_map[ $language ] ) ? $this->language_codes_map[ $language ] : $language;
122 return '/' . $language . $url;
129 * Will return true if root URL or child of root URL
135 private function is_root_url( $url ) {
138 if ( isset( $this->urls_settings['root_page'], $this->urls_settings['show_on_root'] ) &&
139 'page' === $this->urls_settings['show_on_root'] &&
140 ! empty( $this->urls_settings['directory_for_default_language'] )
143 $root_url = $this->get_root_url();
145 $result = strpos( trailingslashit( $url ), $root_url ) === 0;
153 * @return string|bool
155 private function get_root_url() {
156 if ( null === $this->root_url ) {
157 $root_post = get_post( $this->urls_settings['root_page'] );
160 $this->root_url = trailingslashit( $this->get_url_helper()->get_abs_home() ) . $root_post->post_name;
161 $this->root_url = trailingslashit( $this->root_url );
163 $this->root_url = false;
167 return $this->root_url;
171 * @param string $source_url
175 private function filter_source_url( $source_url ) {
176 if ( false === strpos( $source_url, '?' ) ) {
177 $source_url = trailingslashit( $source_url );
178 } elseif ( false !== strpos( $source_url, '?' ) && false === strpos( $source_url, '/?' ) ) {
179 $source_url = str_replace( '?', '/?', $source_url );