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