| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace WP_Syntex\Polylang\Switcher\Fields; |
| 7 |
|
| 8 |
use WP_Syntex\Polylang\Switcher\Settings\Settings; |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Abstract class to use to manage setting field data. |
| 14 |
* |
| 15 |
* @since 3.9 |
| 16 |
* |
| 17 |
* @phpstan-type FieldsData array< |
| 18 |
* non-falsy-string, |
| 19 |
* array{ |
| 20 |
* label: string, |
| 21 |
* choices?: array<string>, |
| 22 |
* hide_if?: array<string, string|bool>, |
| 23 |
* section?: string |
| 24 |
* } |
| 25 |
* > |
| 26 |
*/ |
| 27 |
abstract class Abstract_Fields { |
| 28 |
/** |
| 29 |
* Returns setting field data available for the language switcher. |
| 30 |
* |
| 31 |
* @since 3.9 |
| 32 |
* |
| 33 |
* @return array[] |
| 34 |
* |
| 35 |
* @phpstan-return FieldsData |
| 36 |
*/ |
| 37 |
abstract public static function get(): array; |
| 38 |
|
| 39 |
/** |
| 40 |
* Removes the legacy keys that were stored in the database alongside the new ones in case of plugin rollback. |
| 41 |
* |
| 42 |
* This method is part of the plan to support rollbacks to versions < 3.9. |
| 43 |
* Backward compatibility with Polylang < 3.9. |
| 44 |
* |
| 45 |
* @since 3.9 |
| 46 |
* |
| 47 |
* @param array $raw_settings Raw settings. |
| 48 |
* @return array |
| 49 |
*/ |
| 50 |
public static function remove_legacy_settings( array $raw_settings ): array { |
| 51 |
if ( isset( $raw_settings['layout'] ) ) { |
| 52 |
unset( $raw_settings['dropdown'], $raw_settings['show_names'] ); |
| 53 |
} |
| 54 |
return $raw_settings; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Adds some legacy keys that we want to keep in the database alongside the new ones in case of plugin rollback. |
| 59 |
* Must not be called before `Abstract_Fields::filter()`. |
| 60 |
* |
| 61 |
* This would be useful in case a user rollbacks to a version < 3.9. |
| 62 |
* Backward compatibility with Polylang < 3.9. |
| 63 |
* |
| 64 |
* @since 3.9 |
| 65 |
* |
| 66 |
* @param array $settings Switcher settings. |
| 67 |
* @return array |
| 68 |
*/ |
| 69 |
abstract public static function add_legacy_settings( array $settings ): array; |
| 70 |
|
| 71 |
/** |
| 72 |
* Returns an array containing ONLY the values corresponding to the setting fields. |
| 73 |
* |
| 74 |
* @since 3.9 |
| 75 |
* |
| 76 |
* @param Settings $settings Switcher settings. |
| 77 |
* @return array |
| 78 |
*/ |
| 79 |
public static function filter( Settings $settings ): array { |
| 80 |
return array_intersect_key( get_object_vars( $settings ), static::get() ); |
| 81 |
} |
| 82 |
} |
| 83 |
|