| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace WP_Syntex\Polylang\Switcher\Settings; |
| 7 |
|
| 8 |
use WP_Syntex\Polylang\Switcher\Layout; |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Class that holds all the language switcher's settings. |
| 14 |
* |
| 15 |
* @since 3.9 |
| 16 |
*/ |
| 17 |
class Settings extends Abstract_Settings_Legacy { |
| 18 |
/** |
| 19 |
* @var string |
| 20 |
* |
| 21 |
* @phpstan-var 'horizontal'|'vertical'|'dropdown'|'select' |
| 22 |
*/ |
| 23 |
public string $layout = 'vertical'; |
| 24 |
|
| 25 |
/** |
| 26 |
* No default value here because it depends on `is_rtl()`. |
| 27 |
* |
| 28 |
* @var string |
| 29 |
* |
| 30 |
* @phpstan-var 'left'|'center'|'right'|'stretched'|'inherit' |
| 31 |
*/ |
| 32 |
public string $alignment; |
| 33 |
|
| 34 |
/** |
| 35 |
* @var bool |
| 36 |
*/ |
| 37 |
public bool $show_flags = false; |
| 38 |
|
| 39 |
/** |
| 40 |
* @var string |
| 41 |
* |
| 42 |
* @phpstan-var '3:2'|'1:1' |
| 43 |
*/ |
| 44 |
public string $flag_aspect_ratio = '3:2'; |
| 45 |
|
| 46 |
/** |
| 47 |
* @var string |
| 48 |
* |
| 49 |
* @phpstan-var ''|'names'|'codes' |
| 50 |
*/ |
| 51 |
public string $show_labels = 'names'; |
| 52 |
|
| 53 |
/** |
| 54 |
* @var bool |
| 55 |
*/ |
| 56 |
public bool $hide_current = false; |
| 57 |
|
| 58 |
/** |
| 59 |
* @var bool |
| 60 |
*/ |
| 61 |
public bool $hide_if_empty = true; |
| 62 |
|
| 63 |
/** |
| 64 |
* @var bool |
| 65 |
*/ |
| 66 |
public bool $hide_if_no_translation = false; |
| 67 |
|
| 68 |
/** |
| 69 |
* @var bool |
| 70 |
*/ |
| 71 |
public bool $force_home = false; |
| 72 |
|
| 73 |
/** |
| 74 |
* @var int |
| 75 |
*/ |
| 76 |
public int $post_id = 0; |
| 77 |
|
| 78 |
/** |
| 79 |
* @var bool |
| 80 |
*/ |
| 81 |
public bool $preserve_spacing = true; |
| 82 |
|
| 83 |
/** |
| 84 |
* @var bool |
| 85 |
*/ |
| 86 |
public bool $show_wrapper = true; |
| 87 |
|
| 88 |
/** |
| 89 |
* @var array |
| 90 |
* |
| 91 |
* @phpstan-var non-empty-string[] |
| 92 |
*/ |
| 93 |
public array $wrapper_classes = array(); |
| 94 |
|
| 95 |
/** |
| 96 |
* @var array |
| 97 |
* |
| 98 |
* @phpstan-var non-empty-string[] |
| 99 |
*/ |
| 100 |
public array $item_classes = array(); |
| 101 |
|
| 102 |
/** |
| 103 |
* @var array |
| 104 |
* |
| 105 |
* @phpstan-var non-empty-string[] |
| 106 |
*/ |
| 107 |
public array $link_classes = array(); |
| 108 |
|
| 109 |
/** |
| 110 |
* @var string |
| 111 |
*/ |
| 112 |
public string $unique_id = ''; |
| 113 |
|
| 114 |
/** |
| 115 |
* @var int |
| 116 |
*/ |
| 117 |
private static int $increment = 0; |
| 118 |
|
| 119 |
/** |
| 120 |
* Constructor. |
| 121 |
* |
| 122 |
* @since 3.9 |
| 123 |
* |
| 124 |
* @param array $settings { |
| 125 |
* Optional switcher settings. |
| 126 |
* |
| 127 |
* @type string $layout Layout of the switcher. Possible values are `horizontal`, `vertical`, |
| 128 |
* `dropdown`, and `select`. Default is `vertical`. |
| 129 |
* @type string $alignment Alignment of the items. Possible values are `left`, `center`, `right`, |
| 130 |
* `stretched`, and `inherit`. Default is `left` or `right`, depending on |
| 131 |
* `is_rtl()`. |
| 132 |
* @type bool $show_wrapper Display the wrapper or not. Default is `true`. |
| 133 |
* @type bool $show_flags Display the flags or not. Default is `false`. |
| 134 |
* @type string $flag_aspect_ratio Flags aspect ratio. Possible values are `3:2` and `1:1`. Default is `3:2`. |
| 135 |
* @type string $show_labels Display the labels. Possible values are an empty string (no labels), |
| 136 |
* `names` (language names), `codes` (languages codes). Default is `names`. |
| 137 |
* @type bool $hide_if_empty Hide languages that don't have any posts. Default is `true`. |
| 138 |
* @type bool $hide_if_no_translation Hide languages that don't have a translation. Default is `false`. |
| 139 |
* @type bool $hide_current Hide the current language. Default is `false`. |
| 140 |
* @type bool $force_home Force elements to link to the home pages instead of the translations. |
| 141 |
* Default is `false`. |
| 142 |
* @type int $post_id Build the links according to the translations of the given post ID. |
| 143 |
* Default is `0`. |
| 144 |
* @type bool $preserve_spacing Preserve or discard white space characters between tags. |
| 145 |
* Default is `true` (preserve). |
| 146 |
* @type string[] $wrapper_classes HTML classes to add to the wrapper. Default is an empty array. |
| 147 |
* @type string[] $item_classes HTML classes to add to each item. Default is an empty array. |
| 148 |
* @type string[] $link_classes HTML classes to add to each link. Default is an empty array. |
| 149 |
* @type string $unique_id A unique identifier. Default is an empty string. |
| 150 |
* } |
| 151 |
*/ |
| 152 |
public function __construct( array $settings ) { |
| 153 |
$settings = $this->maybe_filter_legacy( $settings ); |
| 154 |
|
| 155 |
/** |
| 156 |
* Filter the language switcher settings. |
| 157 |
* |
| 158 |
* @since 3.9 |
| 159 |
* |
| 160 |
* @param array $settings Settings. |
| 161 |
*/ |
| 162 |
$settings = apply_filters( 'pll_language_switcher_settings', $settings ); |
| 163 |
|
| 164 |
foreach ( $this->validate( $settings ) as $name => $value ) { |
| 165 |
$this->$name = $value; |
| 166 |
} |
| 167 |
|
| 168 |
if ( '' === $this->unique_id ) { |
| 169 |
++self::$increment; |
| 170 |
$this->unique_id = 'pll-switcher-' . self::$increment; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Returns an instance of the switcher. |
| 176 |
* |
| 177 |
* @since 3.9 |
| 178 |
* |
| 179 |
* @return string |
| 180 |
* |
| 181 |
* @phpstan-return class-string<Layout\Abstract_Layout>|'' |
| 182 |
*/ |
| 183 |
public function get_switcher_class(): string { |
| 184 |
switch ( $this->layout ) { |
| 185 |
case 'horizontal': |
| 186 |
case 'vertical': |
| 187 |
return Layout\Nav::class; |
| 188 |
|
| 189 |
case 'dropdown': |
| 190 |
return Layout\Dropdown::class; |
| 191 |
|
| 192 |
case 'select': |
| 193 |
return Layout\Select::class; |
| 194 |
|
| 195 |
default: |
| 196 |
return ''; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Validates the settings (value and type). |
| 202 |
* This removes additional keys. |
| 203 |
* |
| 204 |
* @since 3.9 |
| 205 |
* |
| 206 |
* @param array $settings Switcher settings. |
| 207 |
* @return array |
| 208 |
*/ |
| 209 |
protected function validate( array $settings ): array { |
| 210 |
$choices = array( |
| 211 |
'layout' => array( 'horizontal', 'vertical', 'dropdown', 'select' ), |
| 212 |
'alignment' => array( 'left', 'center', 'right', 'stretched', 'inherit' ), |
| 213 |
'flag_aspect_ratio' => array( '3:2', '1:1' ), |
| 214 |
'show_labels' => array( '', 'names', 'codes' ), |
| 215 |
); |
| 216 |
$validated = array_diff_key( get_class_vars( self::class ), array( 'increment' => 0 ) ); |
| 217 |
|
| 218 |
$validated['alignment'] = is_rtl() ? 'right' : 'left'; |
| 219 |
|
| 220 |
foreach ( $choices as $key => $setting_choices ) { |
| 221 |
if ( isset( $settings[ $key ] ) && in_array( $settings[ $key ], $setting_choices, true ) ) { |
| 222 |
$validated[ $key ] = $settings[ $key ]; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
foreach ( array( 'show_flags', 'hide_current', 'hide_if_empty', 'hide_if_no_translation', 'force_home', 'preserve_spacing', 'show_wrapper' ) as $key ) { |
| 227 |
if ( isset( $settings[ $key ] ) ) { |
| 228 |
$validated[ $key ] = ! empty( $settings[ $key ] ); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
if ( ! empty( $settings['post_id'] ) && is_numeric( $settings['post_id'] ) ) { |
| 233 |
$validated['post_id'] = absint( $settings['post_id'] ); |
| 234 |
} |
| 235 |
|
| 236 |
foreach ( array( 'wrapper_classes', 'item_classes', 'link_classes' ) as $key ) { |
| 237 |
if ( isset( $settings[ $key ] ) && is_array( $settings[ $key ] ) ) { |
| 238 |
$validated[ $key ] = array_filter( |
| 239 |
$settings[ $key ], |
| 240 |
static function ( $class ) { |
| 241 |
return is_string( $class ) && ! empty( trim( $class ) ); |
| 242 |
} |
| 243 |
); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
if ( ! empty( $settings['unique_id'] ) && is_string( $settings['unique_id'] ) ) { |
| 248 |
$validated['unique_id'] = sanitize_key( $settings['unique_id'] ); |
| 249 |
} |
| 250 |
|
| 251 |
// Mandatory settings for the `select` layout. |
| 252 |
if ( 'select' === $validated['layout'] ) { |
| 253 |
$validated['show_flags'] = false; |
| 254 |
$validated['show_labels'] = 'names'; |
| 255 |
} |
| 256 |
|
| 257 |
// Make sure something is displayed. |
| 258 |
if ( ! $validated['show_flags'] && empty( $validated['show_labels'] ) ) { |
| 259 |
$validated['show_labels'] = 'names'; |
| 260 |
} |
| 261 |
|
| 262 |
return $validated; |
| 263 |
} |
| 264 |
} |
| 265 |
|