Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
93.75% |
75 / 80 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
PlayerStyle | |
93.75% |
75 / 80 |
|
25.00% |
1 / 4 |
14.05 | |
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 | |
96.67% |
29 / 30 |
|
0.00% |
0 / 1 |
5 | |||
getOptions | |
93.55% |
29 / 31 |
|
0.00% |
0 / 1 |
7.01 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | /** |
6 | * Setting: Player style |
7 | * |
8 | * @package Beyondwords\Wordpress |
9 | * @author Stuart McAlpine <stu@beyondwords.io> |
10 | * @since 4.1.0 |
11 | */ |
12 | |
13 | namespace Beyondwords\Wordpress\Component\Settings\Fields\PlayerStyle; |
14 | |
15 | use Beyondwords\Wordpress\Component\Settings\Sync; |
16 | |
17 | /** |
18 | * PlayerStyle |
19 | * |
20 | * @since 4.1.0 |
21 | */ |
22 | class PlayerStyle |
23 | { |
24 | /** |
25 | * Option name. |
26 | */ |
27 | public const OPTION_NAME = 'beyondwords_player_style'; |
28 | |
29 | public const STANDARD = 'standard'; |
30 | |
31 | public const SMALL = 'small'; |
32 | |
33 | public const LARGE = 'large'; |
34 | |
35 | public const VIDEO = 'video'; |
36 | |
37 | /** |
38 | * Constructor |
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.5.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' => PlayerStyle::STANDARD, |
63 | ] |
64 | ); |
65 | |
66 | add_settings_field( |
67 | 'beyondwords-player-style', |
68 | __('Player style', 'speechkit'), |
69 | array($this, 'render'), |
70 | 'beyondwords_player', |
71 | 'styling' |
72 | ); |
73 | } |
74 | |
75 | /** |
76 | * Render setting field. |
77 | * |
78 | * @since 4.1.0 |
79 | * |
80 | * @return void |
81 | **/ |
82 | public function render() |
83 | { |
84 | $value = get_option(self::OPTION_NAME); |
85 | $selected = PlayerStyle::STANDARD; |
86 | $options = self::getOptions(); |
87 | |
88 | foreach ($options as $option) { |
89 | if ($option['value'] === $value) { |
90 | $selected = $option['value']; |
91 | } |
92 | } |
93 | ?> |
94 | <div class="beyondwords-setting__player beyondwords-setting__player--player-style"> |
95 | <select name="<?php echo esc_attr(self::OPTION_NAME) ?>"> |
96 | <?php |
97 | foreach ($options as $option) { |
98 | $disabled = isset($option['disabled']) ? $option['disabled'] : false; |
99 | |
100 | printf( |
101 | '<option value="%s" %s %s>%s</option>', |
102 | esc_attr($option['value']), |
103 | selected($option['value'], $selected), |
104 | disabled($disabled, true), |
105 | esc_html($option['label']) |
106 | ); |
107 | } |
108 | ?> |
109 | </select> |
110 | </div> |
111 | <p class="description"> |
112 | <?php |
113 | printf( |
114 | /* translators: %s is replaced with the "playerStyle setting" link */ |
115 | esc_html__('The default player style (%s) for the audio player. This can be overridden for each post.', 'speechkit'), // phpcs:ignore Generic.Files.LineLength.TooLong |
116 | sprintf( |
117 | '<a href="https://github.com/beyondwords-io/player/blob/main/doc/player-settings.md" target="_blank" rel="nofollow">%s</a>', // phpcs:ignore Generic.Files.LineLength.TooLong |
118 | esc_html__('playerStyle setting', 'speechkit') |
119 | ) |
120 | ); |
121 | ?> |
122 | </p> |
123 | <?php |
124 | } |
125 | |
126 | /** |
127 | * Get all Player styles for the current project. |
128 | * |
129 | * @since 4.1.0 |
130 | * @since 5.0.0 Rename beyondwords_player_styles filter to |
131 | * beyondwords_settings_player_styles. |
132 | * |
133 | * @return string[] Associative array of Player styles and labels. |
134 | **/ |
135 | public static function getOptions() |
136 | { |
137 | $styles = [ |
138 | PlayerStyle::STANDARD => [ |
139 | 'value' => PlayerStyle::STANDARD, |
140 | 'label' => __('Standard', 'speechkit'), |
141 | ], |
142 | PlayerStyle::SMALL => [ |
143 | 'value' => PlayerStyle::SMALL, |
144 | 'label' => __('Small', 'speechkit'), |
145 | ], |
146 | PlayerStyle::LARGE => [ |
147 | 'value' => PlayerStyle::LARGE, |
148 | 'label' => __('Large', 'speechkit'), |
149 | ], |
150 | PlayerStyle::VIDEO => [ |
151 | 'value' => PlayerStyle::VIDEO, |
152 | 'label' => __('Video', 'speechkit'), |
153 | 'disabled' => true, |
154 | ], |
155 | ]; |
156 | |
157 | /** |
158 | * Which player style is the default? |
159 | * This is used to preselect the default option. |
160 | */ |
161 | $defaultPlayerStyle = get_option(self::OPTION_NAME, PlayerStyle::STANDARD); |
162 | |
163 | if (isset($styles[$defaultPlayerStyle])) { |
164 | $styles[$defaultPlayerStyle]['default'] = true; |
165 | } |
166 | |
167 | /** |
168 | * Filters the player styles – the "Player style" `<select>` options |
169 | * presented on the plugin settings page and post edit screens. |
170 | * |
171 | * Each player style is an associative array with the following keys: |
172 | * - string `label` The option label e.g. "Standard" |
173 | * - string `value` The option value e.g. "standard" |
174 | * - boolean `disabled` (Optional) Is this option disabled? |
175 | * - boolean `default` (Optional) Is this the default player style, assigned in the plugin settings? |
176 | * |
177 | * @since 4.1.0 Introduced as beyondwords_player_styles. |
178 | * @since 5.0.0 Renamed from beyondwords_player_styles to beyondwords_settings_player_styles. |
179 | * |
180 | * @param array $styles Associative array of player styles. |
181 | */ |
182 | $styles = apply_filters('beyondwords_settings_player_styles', $styles); |
183 | |
184 | if (empty($styles) || ! is_array($styles)) { |
185 | return []; |
186 | } |
187 | |
188 | /** |
189 | * Is video enabled for this project? |
190 | * If so, we remove the [disabled] attribute from the video <option>. |
191 | * If not, we force a [disabled] attribute on the video <option>. |
192 | */ |
193 | if (isset($styles[PlayerStyle::VIDEO]) && is_array($styles[PlayerStyle::VIDEO])) { |
194 | $videoEnabled = get_option('beyondwords_video_enabled'); |
195 | |
196 | if ($videoEnabled) { |
197 | unset($styles[PlayerStyle::VIDEO]['disabled']); |
198 | } else { |
199 | $styles[PlayerStyle::VIDEO]['disabled'] = true; |
200 | } |
201 | } |
202 | |
203 | return $styles; |
204 | } |
205 | } |