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 |
13.04 | |
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 |
4 | |||
| 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 | * @since 6.0.0 Make static. |
| 41 | */ |
| 42 | public static function init() |
| 43 | { |
| 44 | add_action('admin_init', [self::class, 'addSetting']); |
| 45 | add_action('pre_update_option_' . self::OPTION_NAME, function ($value) { |
| 46 | Sync::syncOptionToDashboard(self::OPTION_NAME); |
| 47 | return $value; |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Add setting. |
| 53 | * |
| 54 | * @since 4.5.0 |
| 55 | * @since 6.0.0 Make static. |
| 56 | * |
| 57 | * @return void |
| 58 | */ |
| 59 | public static function addSetting() |
| 60 | { |
| 61 | register_setting( |
| 62 | 'beyondwords_player_settings', |
| 63 | self::OPTION_NAME, |
| 64 | [ |
| 65 | 'default' => PlayerStyle::STANDARD, |
| 66 | ] |
| 67 | ); |
| 68 | |
| 69 | add_settings_field( |
| 70 | 'beyondwords-player-style', |
| 71 | __('Player style', 'speechkit'), |
| 72 | [self::class, 'render'], |
| 73 | 'beyondwords_player', |
| 74 | 'styling' |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Render setting field. |
| 80 | * |
| 81 | * @since 4.1.0 |
| 82 | * @since 6.0.0 Make static. |
| 83 | * |
| 84 | * @return void |
| 85 | **/ |
| 86 | public static function render() |
| 87 | { |
| 88 | $value = get_option(self::OPTION_NAME); |
| 89 | $selected = PlayerStyle::STANDARD; |
| 90 | $options = self::getOptions(); |
| 91 | |
| 92 | foreach ($options as $option) { |
| 93 | if ($option['value'] === $value) { |
| 94 | $selected = $option['value']; |
| 95 | } |
| 96 | } |
| 97 | ?> |
| 98 | <div class="beyondwords-setting__player beyondwords-setting__player--player-style"> |
| 99 | <select name="<?php echo esc_attr(self::OPTION_NAME) ?>"> |
| 100 | <?php |
| 101 | foreach ($options as $option) { |
| 102 | $disabled = $option['disabled'] ?? false; |
| 103 | |
| 104 | printf( |
| 105 | '<option value="%s" %s %s>%s</option>', |
| 106 | esc_attr($option['value']), |
| 107 | selected($option['value'], $selected), |
| 108 | disabled($disabled, true), |
| 109 | esc_html($option['label']) |
| 110 | ); |
| 111 | } |
| 112 | ?> |
| 113 | </select> |
| 114 | </div> |
| 115 | <p class="description"> |
| 116 | <?php |
| 117 | printf( |
| 118 | /* translators: %s is replaced with the "playerStyle setting" link */ |
| 119 | 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 |
| 120 | sprintf( |
| 121 | '<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 |
| 122 | esc_html__('playerStyle setting', 'speechkit') |
| 123 | ) |
| 124 | ); |
| 125 | ?> |
| 126 | </p> |
| 127 | <?php |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Get all Player styles for the current project. |
| 132 | * |
| 133 | * @since 4.1.0 |
| 134 | * @since 5.0.0 Rename beyondwords_player_styles filter to |
| 135 | * beyondwords_settings_player_styles. |
| 136 | * |
| 137 | * @return string[] Associative array of Player styles and labels. |
| 138 | **/ |
| 139 | public static function getOptions() |
| 140 | { |
| 141 | $styles = [ |
| 142 | PlayerStyle::STANDARD => [ |
| 143 | 'value' => PlayerStyle::STANDARD, |
| 144 | 'label' => __('Standard', 'speechkit'), |
| 145 | ], |
| 146 | PlayerStyle::SMALL => [ |
| 147 | 'value' => PlayerStyle::SMALL, |
| 148 | 'label' => __('Small', 'speechkit'), |
| 149 | ], |
| 150 | PlayerStyle::LARGE => [ |
| 151 | 'value' => PlayerStyle::LARGE, |
| 152 | 'label' => __('Large', 'speechkit'), |
| 153 | ], |
| 154 | PlayerStyle::VIDEO => [ |
| 155 | 'value' => PlayerStyle::VIDEO, |
| 156 | 'label' => __('Video', 'speechkit'), |
| 157 | 'disabled' => true, |
| 158 | ], |
| 159 | ]; |
| 160 | |
| 161 | /** |
| 162 | * Which player style is the default? |
| 163 | * This is used to preselect the default option. |
| 164 | */ |
| 165 | $defaultPlayerStyle = get_option(self::OPTION_NAME, PlayerStyle::STANDARD); |
| 166 | |
| 167 | if (isset($styles[$defaultPlayerStyle])) { |
| 168 | $styles[$defaultPlayerStyle]['default'] = true; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Filters the player styles – the "Player style" `<select>` options |
| 173 | * presented on the plugin settings page and post edit screens. |
| 174 | * |
| 175 | * Each player style is an associative array with the following keys: |
| 176 | * - string `label` The option label e.g. "Standard" |
| 177 | * - string `value` The option value e.g. "standard" |
| 178 | * - boolean `disabled` (Optional) Is this option disabled? |
| 179 | * - boolean `default` (Optional) Is this the default player style, assigned in the plugin settings? |
| 180 | * |
| 181 | * @since 4.1.0 Introduced as beyondwords_player_styles. |
| 182 | * @since 5.0.0 Renamed from beyondwords_player_styles to beyondwords_settings_player_styles. |
| 183 | * |
| 184 | * @param array $styles Associative array of player styles. |
| 185 | */ |
| 186 | $styles = apply_filters('beyondwords_settings_player_styles', $styles); |
| 187 | |
| 188 | if (empty($styles) || ! is_array($styles)) { |
| 189 | return []; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Is video enabled for this project? |
| 194 | * If so, we remove the [disabled] attribute from the video <option>. |
| 195 | * If not, we force a [disabled] attribute on the video <option>. |
| 196 | */ |
| 197 | if (isset($styles[PlayerStyle::VIDEO]) && is_array($styles[PlayerStyle::VIDEO])) { |
| 198 | $videoEnabled = get_option('beyondwords_video_enabled'); |
| 199 | |
| 200 | if ($videoEnabled) { |
| 201 | unset($styles[PlayerStyle::VIDEO]['disabled']); |
| 202 | } else { |
| 203 | $styles[PlayerStyle::VIDEO]['disabled'] = true; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | return $styles; |
| 208 | } |
| 209 | } |