Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.92% covered (success)
95.92%
47 / 49
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
PlayerUI
95.83% covered (success)
95.83%
46 / 48
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%
14 / 14
100.00% covered (success)
100.00%
1 / 1
1
 render
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
2
 getAllPlayerUIs
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5/**
6 * Setting: Player UI
7 *
8 * @package Beyondwords\Wordpress
9 * @author  Stuart McAlpine <stu@beyondwords.io>
10 * @since   4.0.0
11 */
12
13namespace Beyondwords\Wordpress\Component\Settings\Fields\PlayerUI;
14
15use Beyondwords\Wordpress\Component\Settings\Sync;
16
17/**
18 * PlayerUI
19 *
20 * @since 4.0.0
21 */
22defined('ABSPATH') || exit;
23
24class PlayerUI
25{
26    /**
27     * Option name.
28     */
29    public const OPTION_NAME = 'beyondwords_player_ui';
30
31    public const ENABLED  = 'enabled';
32
33    public const HEADLESS = 'headless';
34
35    public const DISABLED = 'disabled';
36
37    /**
38     * Init.
39     *
40     * @since 4.0.0
41     * @since 6.0.0 Make static.
42     */
43    public static function init()
44    {
45        add_action('admin_init', [self::class, 'addSetting']);
46        add_action('pre_update_option_' . self::OPTION_NAME, function ($value) {
47            Sync::syncOptionToDashboard(self::OPTION_NAME);
48            return $value;
49        });
50    }
51
52    /**
53     * Add setting.
54     *
55     * @since 4.0.0
56     * @since 6.0.0 Make static.
57     *
58     * @return void
59     */
60    public static function addSetting()
61    {
62        register_setting(
63            'beyondwords_player_settings',
64            self::OPTION_NAME,
65            [
66                'default' => PlayerUI::ENABLED,
67            ]
68        );
69
70        add_settings_field(
71            'beyondwords-player-ui',
72            __('Player UI', 'speechkit'),
73            [self::class, 'render'],
74            'beyondwords_player',
75            'player'
76        );
77    }
78
79    /**
80     * Render setting field.
81     *
82     * @since 4.0.0
83     * @since 6.0.0 Make static.
84     *
85     * @return void
86     **/
87    public static function render()
88    {
89        $currentUi = get_option(self::OPTION_NAME, PlayerUI::ENABLED);
90        $playerUIs = PlayerUI::getAllPlayerUIs();
91        ?>
92        <div class="beyondwords-setting__player--player-ui">
93            <select name="<?php echo esc_attr(self::OPTION_NAME) ?>">
94                <?php
95                foreach ($playerUIs as $value => $label) {
96                    printf(
97                        '<option value="%s" %s>%s</option>',
98                        esc_attr($value),
99                        selected($value, $currentUi),
100                        esc_html($label)
101                    );
102                }
103                ?>
104            </select>
105        </div>
106        <p class="description">
107            <?php
108            printf(
109                /* translators: %s is replaced with the "headless mode" link */
110                esc_html__('Enable or disable the player, or set it to %s.', 'speechkit'),
111                sprintf(
112                    '<a href="https://github.com/beyondwords-io/player/blob/gh-pages/doc/building-your-own-ui.md" target="_blank" rel="nofollow">%s</a>', // phpcs:ignore Generic.Files.LineLength.TooLong
113                    esc_html__('headless mode', 'speechkit')
114                )
115            );
116            ?>
117        </p>
118        <?php
119    }
120
121    /**
122     * Get all Player UIs.
123     *
124     * @since 4.0.0
125     *
126     * @static
127     *
128     * @return string[] Associative array of Player UIs and labels.
129     **/
130    public static function getAllPlayerUIs()
131    {
132        return [
133            PlayerUI::ENABLED  => __('Enabled', 'speechkit'),
134            PlayerUI::HEADLESS => __('Headless', 'speechkit'),
135            PlayerUI::DISABLED => __('Disabled', 'speechkit'),
136        ];
137    }
138}