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