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