| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace WP_Syntex\Polylang\Switcher\Settings; |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class that holds the language switcher's settings for a menu. |
| 12 |
* |
| 13 |
* @since 3.9 |
| 14 |
*/ |
| 15 |
class Menu extends Settings { |
| 16 |
/** |
| 17 |
* @var string |
| 18 |
* |
| 19 |
* @phpstan-var 'horizontal'|'dropdown' |
| 20 |
*/ |
| 21 |
public string $layout = 'horizontal'; |
| 22 |
|
| 23 |
/** |
| 24 |
* Validates the settings (value and type). |
| 25 |
* This removes additional keys. |
| 26 |
* |
| 27 |
* @since 3.9 |
| 28 |
* |
| 29 |
* @param array $settings Switcher settings. |
| 30 |
* @return array |
| 31 |
*/ |
| 32 |
protected function validate( array $settings ): array { |
| 33 |
if ( ! isset( $settings['layout'] ) || in_array( $settings['layout'], array( 'horizontal', 'dropdown' ), true ) ) { |
| 34 |
return parent::validate( $settings ); |
| 35 |
} |
| 36 |
|
| 37 |
if ( 'select' === $settings['layout'] ) { |
| 38 |
$settings['layout'] = 'dropdown'; |
| 39 |
} else { |
| 40 |
$settings['layout'] = 'horizontal'; |
| 41 |
} |
| 42 |
|
| 43 |
return parent::validate( $settings ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Converts the legacy structure to the new one. |
| 48 |
* This preserves the legacy structure's keys. |
| 49 |
* |
| 50 |
* @since 3.9 |
| 51 |
* |
| 52 |
* @param array $settings The settings. |
| 53 |
* @return array |
| 54 |
*/ |
| 55 |
protected function convert_from_legacy( array $settings ): array { |
| 56 |
$settings = parent::convert_from_legacy( $settings ); |
| 57 |
|
| 58 |
$settings['layout'] = ! empty( $settings['dropdown'] ) ? 'dropdown' : 'horizontal'; |
| 59 |
|
| 60 |
return $settings; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Converts the new structure to the legacy one. |
| 65 |
* This preserves the new structure's keys. |
| 66 |
* |
| 67 |
* @since 3.9 |
| 68 |
* |
| 69 |
* @param array $settings Settings in new structure. |
| 70 |
* @return array |
| 71 |
*/ |
| 72 |
protected function convert_to_legacy( array $settings ): array { |
| 73 |
$args = parent::convert_to_legacy( $settings ); |
| 74 |
|
| 75 |
if ( isset( $settings['layout'] ) && 'dropdown' === $settings['layout'] ) { |
| 76 |
$args['dropdown'] = 1; |
| 77 |
} else { |
| 78 |
$args['dropdown'] = 0; |
| 79 |
} |
| 80 |
|
| 81 |
return $args; |
| 82 |
} |
| 83 |
} |
| 84 |
|