3 use \WPML\SuperGlobals\Server;
5 class WPML_URL_Converter_Domain_Strategy extends WPML_URL_Converter_Abstract_Strategy {
7 /** @var string[] $domains */
8 private $domains = array();
11 * @param array $domains
12 * @param string $default_language
13 * @param array $active_languages
15 public function __construct(
20 parent::__construct( $default_language, $active_languages );
22 $domains = array_map( 'untrailingslashit', $domains );
23 $this->domains = array_map( array( $this, 'strip_protocol' ), $domains );
25 if ( isset( $this->domains[ $default_language ] ) ) {
26 unset( $this->domains[ $default_language ] );
32 public function add_hooks() {
33 add_filter( 'rest_url', [ $this, 'convertRestUrlToCurrentDomain' ], 10, 4 );
37 * Filter REST url to avoid CORS error in Gutenberg.
38 * https://onthegosystems.myjetbrains.com/youtrack/issue/wpmlcore-7022
40 * @param string $url REST URL.
41 * @param string $path REST route.
42 * @param int $blog_id Blog ID.
43 * @param string $scheme Sanitization scheme.
47 public function convertRestUrlToCurrentDomain( $url, $path, $blog_id, $scheme ) {
48 $url_parts = $this->parse_domain_and_subdir( $url );
49 $url_parts['host'] = Server::getServerName();
50 $url = http_build_url( $url_parts );
55 public function get_lang_from_url_string( $url ) {
56 $url = $this->strip_protocol( $url );
58 if ( strpos( $url, '?' ) ) {
59 $parts = explode( '?', $url );
63 foreach ( $this->domains as $code => $domain ) {
64 if ( $domain && strpos( trailingslashit( $url ), trailingslashit( $domain ) ) === 0 ) {
72 public function convert_url_string( $source_url, $lang ) {
73 $original_source_url = untrailingslashit( $source_url );
74 if ( is_admin() && $this->get_url_helper()->is_url_admin( $original_source_url ) ) {
75 return $original_source_url;
78 return $this->convert_url( $source_url, $lang );
81 public function convert_admin_url_string( $source_url, $lang ) {
82 return $this->convert_url( $source_url, $lang );
85 private function convert_url( $source_url, $lang ) {
86 if ( $this->skip_convert_url_string( $source_url, $lang ) ) {
90 $base_url = isset( $this->domains[ $lang ] ) ? $this->domains[ $lang ] : $this->get_url_helper()->get_abs_home();
92 $base_url_parts = $this->parse_domain_and_subdir( $base_url );
93 $url_parts = $this->parse_domain_and_subdir( $source_url );
95 if ( isset( $base_url_parts['host'] ) ) {
96 $url_parts['host'] = $base_url_parts['host'];
99 $converted_url = http_build_url( $url_parts );
101 return $this->slash_helper->maybe_user_trailingslashit( $converted_url );
105 * @param string $base_url
109 private function parse_domain_and_subdir( $base_url ) {
110 $url_parts = wpml_parse_url( $base_url );
112 return is_array( $url_parts ) ?
113 $this->slash_helper->parse_missing_host_from_path( $url_parts ) :
119 * @param string $language
123 public function get_home_url_relative( $url, $language ) {
130 * @return array|string
132 private function strip_protocol( $url ) {
133 $url_parts = wpml_parse_url( $url );
134 if ( is_array( $url_parts ) ) {
135 $url_parts = $this->slash_helper->parse_missing_host_from_path( $url_parts );
136 unset( $url_parts['scheme'] );
138 return http_build_url( $url_parts );
140 return preg_replace( '/^https?:\/\//', '', $url );