class-wpml-language-domain-validation.php
2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
class WPML_Language_Domain_Validation {
const VALIDATE_DOMAIN_KEY = '____icl_validate_domain';
/** @var WPML_WP_API $wp_api */
private $wp_api;
/** @var WP_Http $http */
private $http;
/** @var string $url */
private $url;
/** @var string $validation_url */
private $validation_url;
/**
* @param WPML_WP_API $wp_api
* @param WP_Http $http
*
* @throws \InvalidArgumentException
*/
public function __construct( WPML_WP_API $wp_api, WP_Http $http) {
$this->wp_api = $wp_api;
$this->http = $http;
}
/**
* @param string $url
*
* @return bool
*/
public function is_valid( $url ) {
$this->url = $url;
$this->validation_url = $this->get_validation_url();
if ( ! $this->has_scheme_and_host() ) {
return false;
}
if ( is_multisite() && defined( 'SUBDOMAIN_INSTALL' ) && SUBDOMAIN_INSTALL ) {
return true;
}
$response = $this->get_validation_response();
if ( $this->is_valid_response( $response ) ) {
return in_array( $response['body'], $this->get_accepted_responses( $this->validation_url ), true );
}
return false;
}
/**
* @return bool
*/
private function has_scheme_and_host() {
$url_parts = wpml_parse_url( $this->url );
return array_key_exists( 'scheme', $url_parts ) && array_key_exists( 'host', $url_parts );
}
/**
* @return string
*/
private function get_validation_url() {
return add_query_arg( array( self::VALIDATE_DOMAIN_KEY => 1 ), trailingslashit( $this->url ) );
}
/**
* @param string $url
*
* @return array
*/
private function get_accepted_responses( $url ) {
$accepted_responses = array(
'<!--' . untrailingslashit( $this->wp_api->get_home_url() ) . '-->',
'<!--' . untrailingslashit( $this->wp_api->get_site_url() ) . '-->',
);
if ( defined( 'SUNRISE' ) && SUNRISE === 'on' ) {
$accepted_responses[] = '<!--' . str_replace( '?' . self::VALIDATE_DOMAIN_KEY . '=1', '', $url ) . '-->';
return $accepted_responses;
}
return $accepted_responses;
}
/**
* @return array|WP_Error
*/
private function get_validation_response() {
return $this->http->request( $this->validation_url, 'timeout=15' );
}
/**
* @param array|WP_Error $response
*
* @return bool
*/
private function is_valid_response( $response ) {
return ! is_wp_error( $response ) && '200' === (string) $response['response']['code'];
}
}