| 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\Model\Languages; |
| 10 |
use WP_Syntex\Polylang\Options\Options; |
| 11 |
use WP_Syntex\Polylang\Options\Primitive\Abstract_String; |
| 12 |
|
| 13 |
defined( 'ABSPATH' ) || exit; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class defining language slug string option. |
| 17 |
* |
| 18 |
* @since 3.7 |
| 19 |
*/ |
| 20 |
class Default_Lang extends Abstract_String { |
| 21 |
/** |
| 22 |
* Returns option key. |
| 23 |
* |
| 24 |
* @since 3.7 |
| 25 |
* |
| 26 |
* @return string |
| 27 |
* |
| 28 |
* @phpstan-return 'default_lang' |
| 29 |
*/ |
| 30 |
public static function key(): string { |
| 31 |
return 'default_lang'; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Returns the JSON schema part specific to this option. |
| 36 |
* |
| 37 |
* @since 3.7 |
| 38 |
* |
| 39 |
* @return array Partial schema. |
| 40 |
* |
| 41 |
* @phpstan-return array{type: 'string', pattern: Languages::SLUG_PATTERN} |
| 42 |
*/ |
| 43 |
protected function get_data_structure(): array { |
| 44 |
$string_schema = parent::get_data_structure(); |
| 45 |
$string_schema['pattern'] = Languages::SLUG_PATTERN; |
| 46 |
|
| 47 |
return $string_schema; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Returns the description used in the JSON schema. |
| 52 |
* |
| 53 |
* @since 3.7 |
| 54 |
* |
| 55 |
* @return string |
| 56 |
*/ |
| 57 |
protected function get_description(): string { |
| 58 |
return __( 'Slug of the default language.', 'polylang' ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Sanitizes option's value. |
| 63 |
* Can populate the `$errors` property with blocking and non-blocking errors: in case of non-blocking errors, |
| 64 |
* the value is sanitized and can be stored. |
| 65 |
* |
| 66 |
* @since 3.7 |
| 67 |
* |
| 68 |
* @param string $value Value to sanitize. |
| 69 |
* @param Options $options All options. |
| 70 |
* @return string|WP_Error The sanitized value. An instance of `WP_Error` in case of error. |
| 71 |
*/ |
| 72 |
protected function sanitize( $value, Options $options ) { |
| 73 |
$value = parent::sanitize( $value, $options ); |
| 74 |
|
| 75 |
if ( is_wp_error( $value ) ) { |
| 76 |
return $value; |
| 77 |
} |
| 78 |
|
| 79 |
/** @var string $value */ |
| 80 |
if ( ! get_term_by( 'slug', $value, 'language' ) ) { |
| 81 |
return new WP_Error( 'pll_invalid_language', sprintf( 'The language slug \'%s\' is not a valid language.', $value ) ); |
| 82 |
} |
| 83 |
|
| 84 |
return $value; |
| 85 |
} |
| 86 |
} |
| 87 |
|