|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Elements: Language Select. |
|
4
|
|
|
* |
|
5
|
|
|
* An Select element with the list of languages. |
|
6
|
|
|
* |
|
7
|
|
|
* @since 3.11.0 |
|
8
|
|
|
* @package Wordlift |
|
9
|
|
|
* @subpackage Wordlift/admin |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Define the {@link Wordlift_Admin_Language_Select_Element} class. |
|
14
|
|
|
* |
|
15
|
|
|
* @since 3.11.0 |
|
16
|
|
|
* @package Wordlift |
|
17
|
|
|
* @subpackage Wordlift/admin |
|
18
|
|
|
*/ |
|
19
|
|
|
class Wordlift_Admin_Language_Select_Element implements Wordlift_Admin_Element { |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @inheritdoc |
|
23
|
|
|
*/ |
|
24
|
|
|
public function render( $args ) { |
|
25
|
|
|
|
|
26
|
|
|
// Parse the arguments and merge with default values. |
|
27
|
|
|
$params = wp_parse_args( $args, array( |
|
28
|
|
|
'id' => uniqid( 'wl-input-' ), |
|
29
|
|
|
'name' => uniqid( 'wl-input-' ), |
|
30
|
|
|
'value' => '', |
|
31
|
|
|
'description' => false, |
|
32
|
|
|
) ); |
|
33
|
|
|
|
|
34
|
|
|
$description = $params['description'] ? '<p>' . wp_kses( $params['description'], array( 'a' => array( 'href' => array() ) ) ) . '</p>' : ''; |
|
35
|
|
|
|
|
36
|
|
|
?> |
|
37
|
|
|
<select id="<?php echo esc_attr( $params['id'] ); ?>" |
|
38
|
|
|
name="<?php echo esc_attr( $params['name'] ); ?>"> |
|
39
|
|
|
<?php |
|
40
|
|
|
// Print all the supported language, preselecting the one configured |
|
41
|
|
|
// in WP (or English if not supported). We now use the `Wordlift_Languages` |
|
42
|
|
|
// class which provides the list of languages supported by WordLift. |
|
43
|
|
|
// |
|
44
|
|
|
// See https://github.com/insideout10/wordlift-plugin/issues/349 |
|
45
|
|
|
|
|
46
|
|
|
// Get WordLift's supported languages. |
|
47
|
|
|
$languages = Wordlift_Languages::get_languages(); |
|
48
|
|
|
|
|
49
|
|
|
// If we support WP's configured language, then use that, otherwise use English by default. |
|
50
|
|
|
$language = isset( $languages[ $params['value'] ] ) ? $params['value'] : 'en'; |
|
51
|
|
|
|
|
52
|
|
View Code Duplication |
foreach ( $languages as $code => $label ) { ?> |
|
|
|
|
|
|
53
|
|
|
<option value="<?php echo esc_attr( $code ); ?>" |
|
54
|
|
|
<?php echo selected( $code, $language, false ) ?>><?php |
|
55
|
|
|
echo esc_html( $label ) ?></option> |
|
56
|
|
|
<?php } ?> |
|
57
|
|
|
</select> |
|
58
|
|
|
<?php echo $description; ?> |
|
59
|
|
|
|
|
60
|
|
|
<?php |
|
61
|
|
|
|
|
62
|
|
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.