| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace WP_Syntex\Polylang\Switcher\Settings; |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
/** |
| 11 |
* Compatibility layer between new and legacy switcher settings. |
| 12 |
* |
| 13 |
* @since 3.9 |
| 14 |
*/ |
| 15 |
abstract class Abstract_Settings_Legacy { |
| 16 |
/** |
| 17 |
* Legacy settings that don't exist anymore. |
| 18 |
*/ |
| 19 |
protected const REMOVED_ENTRIES = array( |
| 20 |
'dropdown' => 1, |
| 21 |
'show_names' => 1, |
| 22 |
'display_names_as' => 1, |
| 23 |
'item_spacing' => 1, |
| 24 |
'admin_render' => 1, |
| 25 |
'admin_current_lang' => 1, |
| 26 |
'classes' => 1, |
| 27 |
); |
| 28 |
|
| 29 |
/** |
| 30 |
* Legacy default settings. |
| 31 |
*/ |
| 32 |
protected const DEFAULTS = array( |
| 33 |
'dropdown' => 0, // Display as list and not as dropdown. |
| 34 |
'hide_if_empty' => 1, // Hides languages with no posts (or pages). |
| 35 |
'show_flags' => 0, // Don't show flags. |
| 36 |
'show_names' => 1, // Show language names. |
| 37 |
'display_names_as' => 'name', // Display the language name. |
| 38 |
'force_home' => 0, // Tries to find a translation. |
| 39 |
'hide_if_no_translation' => 0, // Don't hide the link if there is no translation. |
| 40 |
'hide_current' => 0, // Don't hide the current language. |
| 41 |
'post_id' => null, // Link to the translations of the current page. |
| 42 |
'item_spacing' => 'preserve', // Preserve whitespace between list items. |
| 43 |
'admin_render' => 0, // Make the switcher in a frontend context. |
| 44 |
'admin_current_lang' => null, // Use the global current language. |
| 45 |
); |
| 46 |
|
| 47 |
/** |
| 48 |
* Tells if the given settings list contain legacy settings. |
| 49 |
* |
| 50 |
* @since 3.9 |
| 51 |
* |
| 52 |
* @param array $settings Settings. |
| 53 |
* @return bool |
| 54 |
*/ |
| 55 |
public static function is_legacy( array $settings ): bool { |
| 56 |
return ! empty( array_intersect_key( $settings, self::REMOVED_ENTRIES ) ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Returns the values as an array after converting them to the legacy format. |
| 61 |
* |
| 62 |
* @since 3.9 |
| 63 |
* |
| 64 |
* @return array |
| 65 |
*/ |
| 66 |
public function get_legacy(): array { |
| 67 |
return $this->convert_to_legacy( get_object_vars( $this ) ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Converts new settings structure to the legacy one, then applies the deprecated filter `pll_the_languages_args`, |
| 72 |
* then converts it back to new settings structure. |
| 73 |
* This removes legacy settings. |
| 74 |
* |
| 75 |
* @since 3.9 |
| 76 |
* |
| 77 |
* @param array $settings Settings in new structure. |
| 78 |
* @return array |
| 79 |
*/ |
| 80 |
protected function maybe_filter_legacy( array $settings ): array { |
| 81 |
if ( ! has_filter( 'pll_the_languages_args' ) ) { |
| 82 |
if ( ! self::is_legacy( $settings ) ) { |
| 83 |
return $settings; |
| 84 |
} |
| 85 |
|
| 86 |
return array_diff_key( $this->convert_from_legacy( $settings ), self::REMOVED_ENTRIES ); |
| 87 |
} |
| 88 |
|
| 89 |
if ( ! self::is_legacy( $settings ) ) { |
| 90 |
$settings = $this->convert_to_legacy( $settings ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Filter the arguments of the 'pll_the_languages' template tag. |
| 95 |
* |
| 96 |
* @since 1.5 |
| 97 |
* @since 3.9 Deprecated. |
| 98 |
* @deprecated |
| 99 |
* |
| 100 |
* @param array $args |
| 101 |
*/ |
| 102 |
$settings = apply_filters_deprecated( |
| 103 |
'pll_the_languages_args', |
| 104 |
array( $settings ), |
| 105 |
'3.9.0', |
| 106 |
'pll_language_switcher_settings' |
| 107 |
); |
| 108 |
|
| 109 |
return array_diff_key( $this->convert_from_legacy( $settings ), self::REMOVED_ENTRIES ); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Converts the legacy structure to the new one. |
| 114 |
* This preserves the legacy structure's keys. |
| 115 |
* |
| 116 |
* @since 3.9 |
| 117 |
* |
| 118 |
* @param array $settings The settings. |
| 119 |
* @return array |
| 120 |
*/ |
| 121 |
protected function convert_from_legacy( array $settings ): array { |
| 122 |
if ( ! isset( $settings['show_wrapper'] ) ) { |
| 123 |
// Legacy list and dropdown had no outer wrapper (div/nav). The `<select>` |
| 124 |
// tag itself is always output for the select layout. |
| 125 |
$settings['show_wrapper'] = false; |
| 126 |
} |
| 127 |
|
| 128 |
if ( isset( $settings['layout'], $settings['dropdown'] ) ) { |
| 129 |
// Set a new value to `layout` only if the value of `layout` and `dropdown` don't match, so we don't lose `layout`'s real value. |
| 130 |
if ( ! empty( $settings['dropdown'] ) && 'select' !== $settings['layout'] ) { |
| 131 |
$settings['layout'] = 'select'; |
| 132 |
} elseif ( empty( $settings['dropdown'] ) && 'select' === $settings['layout'] ) { |
| 133 |
$settings['layout'] = 'vertical'; |
| 134 |
} |
| 135 |
} elseif ( ! isset( $settings['layout'] ) ) { |
| 136 |
$settings['layout'] = ! empty( $settings['dropdown'] ) ? 'select' : 'vertical'; |
| 137 |
} |
| 138 |
|
| 139 |
if ( isset( $settings['show_names'] ) && empty( $settings['show_names'] ) ) { |
| 140 |
$settings['show_labels'] = ''; |
| 141 |
} elseif ( isset( $settings['display_names_as'] ) && 'slug' === $settings['display_names_as'] ) { |
| 142 |
$settings['show_labels'] = 'codes'; |
| 143 |
} |
| 144 |
|
| 145 |
if ( isset( $settings['item_spacing'] ) && 'discard' === $settings['item_spacing'] ) { |
| 146 |
$settings['preserve_spacing'] = false; |
| 147 |
} |
| 148 |
|
| 149 |
if ( ! empty( $settings['classes'] ) && is_array( $settings['classes'] ) ) { |
| 150 |
$settings['item_classes'] = $settings['classes']; |
| 151 |
} |
| 152 |
|
| 153 |
return $settings; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Converts the new structure to the legacy one. |
| 158 |
* This preserves the new structure's keys. |
| 159 |
* |
| 160 |
* @since 3.9 |
| 161 |
* |
| 162 |
* @param array $settings Settings in new structure. |
| 163 |
* @return array |
| 164 |
*/ |
| 165 |
protected function convert_to_legacy( array $settings ): array { |
| 166 |
$args = self::DEFAULTS; |
| 167 |
|
| 168 |
if ( isset( $settings['layout'] ) && 'select' === $settings['layout'] ) { |
| 169 |
$args['dropdown'] = 1; |
| 170 |
} |
| 171 |
|
| 172 |
if ( isset( $settings['show_labels'] ) ) { |
| 173 |
if ( empty( $settings['show_labels'] ) ) { |
| 174 |
$args['show_names'] = 0; |
| 175 |
} elseif ( 'codes' === $settings['show_labels'] ) { |
| 176 |
$args['display_names_as'] = 'slug'; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
foreach ( array( 'hide_if_empty', 'show_flags', 'force_home', 'hide_if_no_translation', 'hide_current' ) as $name ) { |
| 181 |
if ( isset( $settings[ $name ] ) ) { |
| 182 |
$args[ $name ] = (int) ! empty( $settings[ $name ] ); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
if ( isset( $settings['preserve_spacing'] ) && ! $settings['preserve_spacing'] ) { |
| 187 |
$args['item_spacing'] = 'discard'; |
| 188 |
} |
| 189 |
|
| 190 |
if ( ! empty( $settings['item_classes'] ) && is_array( $settings['item_classes'] ) ) { |
| 191 |
$args['classes'] = array_filter( $settings['item_classes'] ); |
| 192 |
} |
| 193 |
|
| 194 |
return array_merge( $settings, $args ); |
| 195 |
} |
| 196 |
} |
| 197 |
|