Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.15% covered (success)
96.15%
50 / 52
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 */
22defined('ABSPATH') || exit;
23
24class WidgetPosition
25{
26    /**
27     * Option name.
28     *
29     * @since 5.0.0
30     */
31    public const OPTION_NAME = 'beyondwords_player_widget_position';
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' => 'auto',
63            ]
64        );
65
66        add_settings_field(
67            'beyondwords-widget-position',
68            __('Widget position', '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-position">
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        </div>
102        <?php
103    }
104
105    /**
106     * Get all options for the current component.
107     *
108     * @since 5.0.0
109     * @since 6.0.0 Make static.
110     *
111     * @return string[] Associative array of options.
112     **/
113    public static function getOptions()
114    {
115        return [
116            [
117                'value' => 'auto',
118                'label' => __('Auto (default)', 'speechkit'),
119            ],
120            [
121                'value' => 'center',
122                'label' => __('Center', 'speechkit'),
123            ],
124            [
125                'value' => 'left',
126                'label' => __('Left', 'speechkit'),
127            ],
128            [
129                'value' => 'right',
130                'label' => __('Right', 'speechkit'),
131            ],
132        ];
133    }
134}