Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.08% covered (success)
96.08%
49 / 51
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
WidgetPosition
96.08% covered (success)
96.08%
49 / 51
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%
14 / 14
100.00% covered (success)
100.00%
1 / 1
2
 getOptions
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5/**
6 * Setting: Widget position
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\WidgetPosition;
14
15use Beyondwords\Wordpress\Component\Settings\Sync;
16
17/**
18 * WidgetPosition
19 *
20 * @since 5.0.0
21 */
22class WidgetPosition
23{
24    /**
25     * Option name.
26     *
27     * @since 5.0.0
28     */
29    public const OPTION_NAME = 'beyondwords_player_widget_position';
30
31    /**
32     * Constructor
33     *
34     * @since 5.0.0
35     * @since 6.0.0 Make static.
36     */
37    public static function init()
38    {
39        add_action('admin_init', [self::class, 'addSetting']);
40        add_action('pre_update_option_' . self::OPTION_NAME, function ($value) {
41            Sync::syncOptionToDashboard(self::OPTION_NAME);
42            return $value;
43        });
44    }
45
46    /**
47     * Add setting.
48     *
49     * @since 4.5.0
50     * @since 6.0.0 Make static.
51     *
52     * @return void
53     */
54    public static function addSetting()
55    {
56        register_setting(
57            'beyondwords_player_settings',
58            self::OPTION_NAME,
59            [
60                'default' => 'auto',
61            ]
62        );
63
64        add_settings_field(
65            'beyondwords-widget-position',
66            __('Widget position', 'speechkit'),
67            [self::class, 'render'],
68            'beyondwords_player',
69            'widget'
70        );
71    }
72
73    /**
74     * Render setting field.
75     *
76     * @since 5.0.0
77     * @since 6.0.0 Make static.
78     *
79     * @return void
80     **/
81    public static function render()
82    {
83        $current = get_option(self::OPTION_NAME);
84        $options = self::getOptions();
85        ?>
86        <div class="beyondwords-setting__player beyondwords-setting__widget-position">
87            <select name="<?php echo esc_attr(self::OPTION_NAME) ?>">
88                <?php
89                foreach ($options as $option) {
90                    printf(
91                        '<option value="%s" %s>%s</option>',
92                        esc_attr($option['value']),
93                        selected($option['value'], $current),
94                        esc_html($option['label'])
95                    );
96                }
97                ?>
98            </select>
99        </div>
100        <?php
101    }
102
103    /**
104     * Get all options for the current component.
105     *
106     * @since 5.0.0
107     * @since 6.0.0 Make static.
108     *
109     * @return string[] Associative array of options.
110     **/
111    public static function getOptions()
112    {
113        return [
114            [
115                'value' => 'auto',
116                'label' => __('Auto (default)', 'speechkit'),
117            ],
118            [
119                'value' => 'center',
120                'label' => __('Center', 'speechkit'),
121            ],
122            [
123                'value' => 'left',
124                'label' => __('Left', 'speechkit'),
125            ],
126            [
127                'value' => 'right',
128                'label' => __('Right', 'speechkit'),
129            ],
130        ];
131    }
132}