Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
5.77% covered (danger)
5.77%
3 / 52
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
WidgetPosition
5.77% covered (danger)
5.77%
3 / 52
0.00% covered (danger)
0.00%
0 / 4
25.92
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 / 14
0.00% covered (danger)
0.00%
0 / 1
6
 getOptions
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
2
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     */
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' => 'auto',
59            ]
60        );
61
62        add_settings_field(
63            'beyondwords-widget-position',
64            __('Widget position', '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-position">
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        </div>
97        <?php
98    }
99
100    /**
101     * Get all options for the current component.
102     *
103     * @since 5.0.0
104     *
105     * @return string[] Associative array of options.
106     **/
107    public function getOptions()
108    {
109        $options = [
110            [
111                'value' => 'auto',
112                'label' => __('Auto (default)', 'speechkit'),
113            ],
114            [
115                'value' => 'center',
116                'label' => __('Center', 'speechkit'),
117            ],
118            [
119                'value' => 'left',
120                'label' => __('Left', 'speechkit'),
121            ],
122            [
123                'value' => 'right',
124                'label' => __('Right', 'speechkit'),
125            ],
126        ];
127
128        return $options;
129    }
130}