| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace WP_Syntex\Polylang\Options\Business; |
| 7 |
|
| 8 |
use WP_Error; |
| 9 |
use WP_Syntex\Polylang\Options\Options; |
| 10 |
use WP_Syntex\Polylang\Model\Languages; |
| 11 |
use WP_Syntex\Polylang\Options\Primitive\Abstract_Map; |
| 12 |
|
| 13 |
defined( 'ABSPATH' ) || exit; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class defining single associative array of domain as value and language slug as key option. |
| 17 |
* /!\ Sanitization depends on `force_lang`: this option must be set AFTER `force_lang`. |
| 18 |
* |
| 19 |
* @since 3.7 |
| 20 |
* |
| 21 |
* @phpstan-type DomainsValue array<non-falsy-string, string> |
| 22 |
*/ |
| 23 |
class Domains extends Abstract_Map { |
| 24 |
/** |
| 25 |
* Returns option key. |
| 26 |
* |
| 27 |
* @since 3.7 |
| 28 |
* |
| 29 |
* @return string |
| 30 |
* |
| 31 |
* @phpstan-return 'domains' |
| 32 |
*/ |
| 33 |
public static function key(): string { |
| 34 |
return 'domains'; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Returns the default value. |
| 39 |
* |
| 40 |
* @since 3.7 |
| 41 |
* |
| 42 |
* @return array |
| 43 |
*/ |
| 44 |
protected function get_default() { |
| 45 |
return array(); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Returns the JSON schema part specific to the inner structure of this option. |
| 50 |
* |
| 51 |
* @since 3.8 |
| 52 |
* |
| 53 |
* @return array Inner structure. |
| 54 |
*/ |
| 55 |
protected function get_inner_structure(): array { |
| 56 |
return array( |
| 57 |
'patternProperties' => array( |
| 58 |
Languages::SLUG_PATTERN => array( // Language slug as key. |
| 59 |
'type' => 'string', |
| 60 |
'format' => 'uri', |
| 61 |
), |
| 62 |
), |
| 63 |
'additionalProperties' => false, |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Sanitizes option's value. |
| 69 |
* Can populate the `$errors` property with blocking and non-blocking errors: in case of non-blocking errors, |
| 70 |
* the value is sanitized and can be stored. |
| 71 |
* |
| 72 |
* @since 3.7 |
| 73 |
* |
| 74 |
* @param array $value Value to sanitize. |
| 75 |
* @param Options $options All options. |
| 76 |
* @return array|WP_Error The sanitized value. An instance of `WP_Error` in case of blocking error. |
| 77 |
* |
| 78 |
* @phpstan-return DomainsValue|WP_Error |
| 79 |
*/ |
| 80 |
protected function sanitize( $value, Options $options ) { |
| 81 |
// Sanitize new URLs. |
| 82 |
$value = parent::sanitize( $value, $options ); |
| 83 |
|
| 84 |
if ( is_wp_error( $value ) ) { |
| 85 |
// Blocking error. |
| 86 |
return $value; |
| 87 |
} |
| 88 |
|
| 89 |
/** @phpstan-var DomainsValue */ |
| 90 |
$current_value = $this->get(); |
| 91 |
/** @phpstan-var DomainsValue $value */ |
| 92 |
$all_values = array(); // Previous and new values. |
| 93 |
$missing_langs = array(); // Lang names corresponding to the empty values. |
| 94 |
$language_terms = $this->get_language_terms(); |
| 95 |
|
| 96 |
// Detect empty values, fill missing keys with previous values. |
| 97 |
foreach ( $language_terms as $lang ) { |
| 98 |
if ( array_key_exists( $lang->slug, $value ) ) { |
| 99 |
// Use the new value. |
| 100 |
$all_values[ $lang->slug ] = $value[ $lang->slug ]; |
| 101 |
unset( $value[ $lang->slug ] ); |
| 102 |
} else { |
| 103 |
// Use previous value. |
| 104 |
$all_values[ $lang->slug ] = $current_value[ $lang->slug ] ?? ''; |
| 105 |
} |
| 106 |
|
| 107 |
if ( empty( $all_values[ $lang->slug ] ) ) { |
| 108 |
// The value is empty. |
| 109 |
$missing_langs[] = $lang->name; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
// Detect invalid language slugs. |
| 114 |
if ( ! empty( $value ) ) { |
| 115 |
// Non-blocking error. |
| 116 |
$this->add_unknown_languages_warning( array_keys( $value ) ); |
| 117 |
} |
| 118 |
|
| 119 |
if ( 3 === $options->get( 'force_lang' ) && ! empty( $missing_langs ) ) { |
| 120 |
// Non-blocking error. |
| 121 |
if ( 1 === count( $missing_langs ) ) { |
| 122 |
/* translators: %s is a native language name. */ |
| 123 |
$message = __( 'Please enter a valid URL for %s.', 'polylang' ); |
| 124 |
} else { |
| 125 |
/* translators: %s is a list of native language names. */ |
| 126 |
$message = __( 'Please enter valid URLs for %s.', 'polylang' ); |
| 127 |
} |
| 128 |
|
| 129 |
$this->errors->add( |
| 130 |
'pll_empty_domains', |
| 131 |
sprintf( $message, wp_sprintf_l( '%l', $missing_langs ) ), |
| 132 |
'warning' |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
// Ping all URLs to make sure they are valid. |
| 137 |
if ( $options->get( 'force_lang' ) > 1 ) { |
| 138 |
$failed_urls = array(); |
| 139 |
|
| 140 |
foreach ( array_filter( $all_values ) as $url ) { |
| 141 |
$url = add_query_arg( 'deactivate-polylang', 1, $url ); |
| 142 |
// Don't redefine vip_safe_wp_remote_get() as it has not the same signature as wp_remote_get(). |
| 143 |
$response = function_exists( 'vip_safe_wp_remote_get' ) ? vip_safe_wp_remote_get( $url ) : wp_remote_get( $url ); |
| 144 |
|
| 145 |
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 146 |
$failed_urls[] = $url; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
if ( ! empty( $failed_urls ) ) { |
| 151 |
// Non-blocking error. |
| 152 |
if ( 1 === count( $failed_urls ) ) { |
| 153 |
/* translators: %s is a URL. */ |
| 154 |
$message = __( 'Polylang was unable to access the %s URL. Please check that the URL is valid.', 'polylang' ); |
| 155 |
} else { |
| 156 |
/* translators: %s is a list of URLs. */ |
| 157 |
$message = __( 'Polylang was unable to access the %s URLs. Please check that the URLs are valid.', 'polylang' ); |
| 158 |
} |
| 159 |
$this->errors->add( |
| 160 |
'pll_invalid_domains', |
| 161 |
sprintf( $message, wp_sprintf_l( '%l', $failed_urls ) ), |
| 162 |
'warning' |
| 163 |
); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
/** @phpstan-var DomainsValue */ |
| 168 |
return $all_values; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Returns the description used in the JSON schema. |
| 173 |
* |
| 174 |
* @since 3.7 |
| 175 |
* |
| 176 |
* @return string |
| 177 |
*/ |
| 178 |
protected function get_description(): string { |
| 179 |
return __( 'Domains used when the language is set from different domains.', 'polylang' ); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Adds information to the site health info array. |
| 184 |
* |
| 185 |
* @since 3.8 |
| 186 |
* |
| 187 |
* @param Options $options An instance of the Options class providing additional configuration. |
| 188 |
* |
| 189 |
* @return array The updated site health information. |
| 190 |
*/ |
| 191 |
public function get_site_health_info( Options $options ): array { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 192 |
if ( 3 === $options->get( 'force_lang' ) ) { |
| 193 |
return $this->format_single_value_for_site_health_info( $this->get() ); |
| 194 |
} |
| 195 |
|
| 196 |
return parent::get_site_health_info( $options ); |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Returns the reset value for a key. |
| 201 |
* |
| 202 |
* @since 3.8 |
| 203 |
* |
| 204 |
* @param string $key The key to reset. Unused. |
| 205 |
* @return mixed The reset value. |
| 206 |
*/ |
| 207 |
protected function reset_value( string $key ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 208 |
return ''; |
| 209 |
} |
| 210 |
} |
| 211 |
|