Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.97% covered (success)
96.97%
64 / 66
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
WidgetStyle
96.92% covered (success)
96.92%
63 / 65
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
 getOptions
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5/**
6 * Setting: Widget style
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\WidgetStyle;
14
15use Beyondwords\Wordpress\Component\Settings\Sync;
16
17/**
18 * WidgetStyle
19 *
20 * @since 5.0.0
21 */
22defined('ABSPATH') || exit;
23
24class WidgetStyle
25{
26    /**
27     * Option name.
28     *
29     * @since 5.0.0
30     */
31    public const OPTION_NAME = 'beyondwords_player_widget_style';
32
33    /**
34     * Constructor
35     *
36     * @since 5.0.0
37     * @since 6.0.0 Make static.
38     */
39    public static function init()
40    {
41        add_action('admin_init', [self::class, 'addSetting']);
42        add_action('pre_update_option_' . self::OPTION_NAME, function ($value) {
43            Sync::syncOptionToDashboard(self::OPTION_NAME);
44            return $value;
45        });
46    }
47
48    /**
49     * Add setting.
50     *
51     * @since 4.5.0
52     * @since 6.0.0 Make static.
53     *
54     * @return void
55     */
56    public static function addSetting()
57    {
58        register_setting(
59            'beyondwords_player_settings',
60            self::OPTION_NAME,
61            [
62                'default' => '',
63            ]
64        );
65
66        add_settings_field(
67            'beyondwords-widget-style',
68            __('Widget style', 'speechkit'),
69            [self::class, 'render'],
70            'beyondwords_player',
71            'widget'
72        );
73    }
74
75    /**
76     * Render setting field.
77     *
78     * @since 5.0.0
79     * @since 6.0.0 Make static.
80     *
81     * @return void
82     **/
83    public static function render()
84    {
85        $current = get_option(self::OPTION_NAME);
86        $options = self::getOptions();
87        ?>
88        <div class="beyondwords-setting__player beyondwords-setting__widget-style">
89            <select name="<?php echo esc_attr(self::OPTION_NAME) ?>">
90                <?php
91                foreach ($options as $option) {
92                    printf(
93                        '<option value="%s" %s>%s</option>',
94                        esc_attr($option['value']),
95                        selected($option['value'], $current),
96                        esc_html($option['label'])
97                    );
98                }
99                ?>
100            </select>
101            <p class="description">
102            <?php
103            printf(
104                /* translators: %s is replaced with the "widgetStyle setting" link */
105                esc_html__('The style of widget to display at the bottom of the page once the user scrolls past the inline player. See %s.', 'speechkit'), // phpcs:ignore Generic.Files.LineLength.TooLong
106                sprintf(
107                    '<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
108                    esc_html__('widgetStyle setting', 'speechkit')
109                )
110            );
111            ?>
112        </p>
113        </div>
114        <?php
115    }
116
117    /**
118     * Get all options for the current component.
119     *
120     * @since 5.0.0
121     * @since 6.0.0 Make static.
122     *
123     * @return string[] Associative array of options.
124     **/
125    public static function getOptions()
126    {
127        return [
128            [
129                'value' => 'standard',
130                'label' => __('Standard', 'speechkit'),
131            ],
132            [
133                'value' => 'none',
134                'label' => __('None', 'speechkit'),
135            ],
136            [
137                'value' => 'small',
138                'label' => __('Small', 'speechkit'),
139            ],
140            [
141                'value' => 'large',
142                'label' => __('Large', 'speechkit'),
143            ],
144            [
145                'value' => 'video',
146                'label' => __('Video', 'speechkit'),
147            ],
148        ];
149    }
150}