Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.43% |
54 / 56 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| TextHighlighting | |
96.43% |
54 / 56 |
|
75.00% |
3 / 4 |
5 | |
0.00% |
0 / 1 |
| init | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
1.06 | |||
| addSetting | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
1 | |||
| render | |
100.00% |
32 / 32 |
|
100.00% |
1 / 1 |
1 | |||
| sanitize | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * Setting: Text highlighting |
| 7 | * |
| 8 | * @package Beyondwords\Wordpress |
| 9 | * @author Stuart McAlpine <stu@beyondwords.io> |
| 10 | * @since 5.0.0 |
| 11 | */ |
| 12 | |
| 13 | namespace Beyondwords\Wordpress\Component\Settings\Fields\TextHighlighting; |
| 14 | |
| 15 | use Beyondwords\Wordpress\Component\Settings\SettingsUtils; |
| 16 | use Beyondwords\Wordpress\Component\Settings\Sync; |
| 17 | |
| 18 | /** |
| 19 | * TextHighlighting |
| 20 | * |
| 21 | * @since 5.0.0 |
| 22 | */ |
| 23 | class TextHighlighting |
| 24 | { |
| 25 | /** |
| 26 | * Default value. |
| 27 | * |
| 28 | * @since 5.0.0 |
| 29 | */ |
| 30 | public const DEFAULT_VALUE = ''; |
| 31 | |
| 32 | /** |
| 33 | * Option name. |
| 34 | * |
| 35 | * @since 5.0.0 |
| 36 | */ |
| 37 | public const OPTION_NAME = 'beyondwords_player_highlight_sections'; |
| 38 | |
| 39 | /** |
| 40 | * Init. |
| 41 | * |
| 42 | * @since 5.0.0 |
| 43 | * @since 6.0.0 Make static. |
| 44 | */ |
| 45 | public static function init() |
| 46 | { |
| 47 | add_action('admin_init', [self::class, 'addSetting']); |
| 48 | add_action('pre_update_option_' . self::OPTION_NAME, function ($value) { |
| 49 | Sync::syncOptionToDashboard(self::OPTION_NAME); |
| 50 | return $value; |
| 51 | }); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Init setting. |
| 56 | * |
| 57 | * @since 5.0.0 |
| 58 | * @since 6.0.0 Make static. |
| 59 | * |
| 60 | * @return void |
| 61 | */ |
| 62 | public static function addSetting() |
| 63 | { |
| 64 | register_setting( |
| 65 | 'beyondwords_player_settings', |
| 66 | self::OPTION_NAME, |
| 67 | [ |
| 68 | 'type' => 'string', |
| 69 | 'sanitize_callback' => [self::class, 'sanitize'], |
| 70 | 'default' => self::DEFAULT_VALUE, |
| 71 | ] |
| 72 | ); |
| 73 | |
| 74 | add_settings_field( |
| 75 | 'beyondwords-text-highlighting', |
| 76 | __('Text highlighting', 'speechkit'), |
| 77 | [self::class, 'render'], |
| 78 | 'beyondwords_player', |
| 79 | 'styling' |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Render setting field. |
| 85 | * |
| 86 | * @since 5.0.0 |
| 87 | * @since 6.0.0 Make static. |
| 88 | * |
| 89 | * @return void |
| 90 | **/ |
| 91 | public static function render() |
| 92 | { |
| 93 | $value = get_option(self::OPTION_NAME); |
| 94 | $lightTheme = get_option('beyondwords_player_theme_light'); |
| 95 | $darkTheme = get_option('beyondwords_player_theme_dark'); |
| 96 | ?> |
| 97 | <div class="beyondwords-setting__player beyondwords-setting__player--text-highlighting"> |
| 98 | <label> |
| 99 | <input type="hidden" name="<?php echo esc_attr(self::OPTION_NAME); ?>" value="" /> |
| 100 | <input |
| 101 | type="checkbox" |
| 102 | id="<?php echo esc_attr(self::OPTION_NAME); ?>" |
| 103 | name="<?php echo esc_attr(self::OPTION_NAME); ?>" |
| 104 | value="1" |
| 105 | <?php checked($value, 'body'); ?> |
| 106 | /> |
| 107 | <?php esc_html_e('Highlight the current paragraph during audio playback', 'speechkit'); ?> |
| 108 | </label> |
| 109 | </div> |
| 110 | <div> |
| 111 | <h3 class="subheading">Light theme settings</h3> |
| 112 | <?php |
| 113 | SettingsUtils::colorInput( |
| 114 | __('Highlight color', 'speechkit'), |
| 115 | 'beyondwords_player_theme_light[highlight_color]', |
| 116 | $lightTheme['highlight_color'] ?? '', |
| 117 | ); |
| 118 | ?> |
| 119 | </div> |
| 120 | <div> |
| 121 | <h3 class="subheading">Dark theme settings</h3> |
| 122 | <?php |
| 123 | SettingsUtils::colorInput( |
| 124 | __('Highlight color', 'speechkit'), |
| 125 | 'beyondwords_player_theme_dark[highlight_color]', |
| 126 | $darkTheme['highlight_color'] ?? '', |
| 127 | ); |
| 128 | ?> |
| 129 | </div> |
| 130 | <?php |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Sanitise the setting value. |
| 135 | * |
| 136 | * @since 5.0.0 |
| 137 | * @since 6.0.0 Make static. |
| 138 | * |
| 139 | * @param string $value The submitted value. |
| 140 | * |
| 141 | * @return void |
| 142 | **/ |
| 143 | public static function sanitize($value) |
| 144 | { |
| 145 | if ($value) { |
| 146 | return 'body'; |
| 147 | } |
| 148 | |
| 149 | return ''; |
| 150 | } |
| 151 | } |