class-wpml-lang-domains-box.php
6.41 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
/**
* Class WPML_Lang_Domains_Box
*
* Displays the table holding the language domains on languages.php
*/
class WPML_Lang_Domains_Box extends WPML_SP_User {
public function render() {
$active_languages = $this->sitepress->get_active_languages();
$default_language = $this->sitepress->get_default_language();
$language_domains = $this->sitepress->get_setting( 'language_domains', array() );
$default_home = (string) $this->sitepress->convert_url( $this->sitepress->get_wp_api()->get_home_url(), $default_language );
$home_schema = wpml_parse_url( $default_home, PHP_URL_SCHEME ) . '://';
$home_path = wpml_parse_url( $default_home, PHP_URL_PATH );
$is_per_domain = WPML_LANGUAGE_NEGOTIATION_TYPE_DOMAIN === (int) $this->sitepress->get_setting( 'language_negotiation_type' );
$is_sso_enabled = (bool) $this->sitepress->get_setting( 'language_per_domain_sso_enabled', ! $is_per_domain );
ob_start();
?>
<table class="language_domains sub-section">
<?php
foreach ( $active_languages as $code => $lang ) {
$text_box_id = esc_attr( 'language_domain_' . $code );
?>
<tr>
<td>
<label
for="<?php echo $text_box_id; ?>">
<?php echo esc_html( $lang['display_name'] ); ?>
</label>
</td>
<?php
if ( $code === $default_language ) {
?>
<td id="icl_ln_home">
<code>
<?php echo esc_url( $default_home ); ?>
</code>
</td>
<td> </td>
<?php
} else {
?>
<td style="white-space: nowrap">
<code><?php echo esc_html( $home_schema ); ?></code>
<input
type="text"
id="<?php echo $text_box_id; ?>"
name="language_domains[<?php echo esc_attr( $code ); ?>]"
value="<?php echo $this->get_language_domain( $code, $default_home, $language_domains ); ?>"
data-language="<?php echo esc_attr( $code ); ?>"
size="30"/>
<?php if ( isset( $home_path[1] ) && is_string( $home_path[1] ) ) { ?>
<code><?php echo esc_html( $home_path ); ?></code>
<?php } ?>
</td>
<td>
<p style="white-space: nowrap"><input
class="validate_language_domain"
type="checkbox"
id="validate_language_domains_<?php echo esc_attr( $code ); ?>"
name="validate_language_domains[]"
value="<?php echo esc_attr( $code ); ?>"
checked="checked"/>
<label
for="validate_language_domains_<?php echo esc_attr( $code ); ?>">
<?php esc_html_e( 'Validate on save', 'sitepress' ); ?>
</label>
</p>
<p style="white-space: nowrap">
<span class="spinner spinner-<?php echo esc_attr( $code ); ?>"></span>
<span id="ajx_ld_<?php echo esc_attr( $code ); ?>"></span>
</p>
</td>
<?php
}
?>
</tr>
<?php
}
?>
<tr>
<td colspan="2">
<label for="sso_enabled">
<input type="checkbox" id="sso_enabled" name="sso_enabled"
value="1" <?php checked( $is_sso_enabled, true, true ); ?>>
<?php esc_html_e( 'Auto sign-in and sign-out users from all domains', 'sitepress' ); ?>
</label>
<span id="sso_information"><i class="otgs-ico-help"></i></span>
<div id="sso_enabled_notice" style="display: none;">
<?php esc_html_e( 'Please log-out and login again in order to be able to access the admin features in all language domains.', 'sitepress' ); ?>
</div>
</td>
</tr>
</table>
<div id="language_per_domain_sso_description" style="display:none;">
<p>
<?php
/* translators: this is the first of two sentences explaining the "Auto sign-in and sign-out users from all domains" feature */
echo esc_html_x( 'This feature allows the theme and plugins to work correctly when on sites that use languages in domains.', 'Tooltip: Auto sign-in and sign-out users from all domains', 'sitepress' );
?>
<br>
<?php
/* translators: this is the second of two sentences explaining the "Auto sign-in and sign-out users from all domains" feature */
echo esc_html_x( "It requires a call to each of the site's language domains on both log-in and log-out, so there's a small performance penalty to using it.", 'Tooltip: Auto sign-in and sign-out users from all domains', 'sitepress' );
?>
</p>
</div>
<?php
return ob_get_clean();
}
/**
* @param string $code
* @param string $default_home
* @param string[] $language_domains
*
* @return string
*/
private function get_language_domain( $code, $default_home, $language_domains ) {
$home_schema = wpml_parse_url( $default_home, PHP_URL_SCHEME ) . '://';
$home_path = wpml_parse_url( $default_home, PHP_URL_PATH );
if ( isset( $language_domains[ $code ] ) ) {
$pattern = [
'#^' . $home_schema . '#',
'#' . $home_path . '$#',
];
$language_domain_raw = preg_replace( $pattern, '', $language_domains[ $code ] );
} else {
$language_domain_raw = $this->render_suggested_url( $default_home, $code );
}
return filter_var( $language_domain_raw, FILTER_SANITIZE_URL );
}
private function render_suggested_url( $home, $lang ) {
$url_parts = parse_url( $home );
$exp = explode( '.', $url_parts['host'] );
$suggested_url = $lang . '.';
array_shift( $exp );
$suggested_url .= count( $exp ) < 2 ? $url_parts['host'] : implode( '.', $exp );
return $suggested_url;
}
}