Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
95.83% |
46 / 48 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
PlayerUI | |
95.83% |
46 / 48 |
|
75.00% |
3 / 4 |
5 | |
0.00% |
0 / 1 |
init | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
1.06 | |||
addSetting | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
1 | |||
render | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
2 | |||
getAllPlayerUIs | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | /** |
6 | * Setting: Player UI |
7 | * |
8 | * @package Beyondwords\Wordpress |
9 | * @author Stuart McAlpine <stu@beyondwords.io> |
10 | * @since 4.0.0 |
11 | */ |
12 | |
13 | namespace Beyondwords\Wordpress\Component\Settings\Fields\PlayerUI; |
14 | |
15 | use Beyondwords\Wordpress\Component\Settings\Sync; |
16 | |
17 | /** |
18 | * PlayerUI |
19 | * |
20 | * @since 4.0.0 |
21 | */ |
22 | class PlayerUI |
23 | { |
24 | /** |
25 | * Option name. |
26 | */ |
27 | public const OPTION_NAME = 'beyondwords_player_ui'; |
28 | |
29 | public const ENABLED = 'enabled'; |
30 | |
31 | public const HEADLESS = 'headless'; |
32 | |
33 | public const DISABLED = 'disabled'; |
34 | |
35 | /** |
36 | * Init. |
37 | * |
38 | * @since 4.0.0 |
39 | */ |
40 | public function init() |
41 | { |
42 | add_action('admin_init', array($this, 'addSetting')); |
43 | add_action('pre_update_option_' . self::OPTION_NAME, function ($value) { |
44 | Sync::syncOptionToDashboard(self::OPTION_NAME); |
45 | return $value; |
46 | }); |
47 | } |
48 | |
49 | /** |
50 | * Add setting. |
51 | * |
52 | * @since 4.0.0 |
53 | * |
54 | * @return void |
55 | */ |
56 | public function addSetting() |
57 | { |
58 | register_setting( |
59 | 'beyondwords_player_settings', |
60 | self::OPTION_NAME, |
61 | [ |
62 | 'default' => PlayerUI::ENABLED, |
63 | ] |
64 | ); |
65 | |
66 | add_settings_field( |
67 | 'beyondwords-player-ui', |
68 | __('Player UI', 'speechkit'), |
69 | array($this, 'render'), |
70 | 'beyondwords_player', |
71 | 'player' |
72 | ); |
73 | } |
74 | |
75 | /** |
76 | * Render setting field. |
77 | * |
78 | * @since 4.0.0 |
79 | * |
80 | * @return void |
81 | **/ |
82 | public function render() |
83 | { |
84 | $currentUi = get_option(self::OPTION_NAME, PlayerUI::ENABLED); |
85 | $playerUIs = PlayerUI::getAllPlayerUIs(); |
86 | ?> |
87 | <div class="beyondwords-setting__player--player-ui"> |
88 | <select name="<?php echo esc_attr(self::OPTION_NAME) ?>"> |
89 | <?php |
90 | foreach ($playerUIs as $value => $label) { |
91 | printf( |
92 | '<option value="%s" %s>%s</option>', |
93 | esc_attr($value), |
94 | selected($value, $currentUi), |
95 | esc_html($label) |
96 | ); |
97 | } |
98 | ?> |
99 | </select> |
100 | </div> |
101 | <p class="description"> |
102 | <?php |
103 | printf( |
104 | /* translators: %s is replaced with the "headless mode" link */ |
105 | esc_html__('Enable or disable the player, or set it to %s.', 'speechkit'), |
106 | sprintf( |
107 | '<a href="https://github.com/beyondwords-io/player/blob/gh-pages/doc/building-your-own-ui.md" target="_blank" rel="nofollow">%s</a>', // phpcs:ignore Generic.Files.LineLength.TooLong |
108 | esc_html__('headless mode', 'speechkit') |
109 | ) |
110 | ); |
111 | ?> |
112 | </p> |
113 | <?php |
114 | } |
115 | |
116 | /** |
117 | * Get all Player UIs. |
118 | * |
119 | * @since 4.0.0 |
120 | * |
121 | * @static |
122 | * |
123 | * @return string[] Associative array of Player UIs and labels. |
124 | **/ |
125 | public static function getAllPlayerUIs() |
126 | { |
127 | return [ |
128 | PlayerUI::ENABLED => __('Enabled', 'speechkit'), |
129 | PlayerUI::HEADLESS => __('Headless', 'speechkit'), |
130 | PlayerUI::DISABLED => __('Disabled', 'speechkit'), |
131 | ]; |
132 | } |
133 | } |