Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
44 / 44
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
BodyVoice
100.00% covered (success)
100.00%
44 / 44
100.00% covered (success)
100.00%
3 / 3
4
100.00% covered (success)
100.00%
1 / 1
 init
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 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%
25 / 25
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5/**
6 * Setting: BodyVoice
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\Voice;
14
15use Beyondwords\Wordpress\Component\Settings\Fields\Voice\Voice;
16use Beyondwords\Wordpress\Component\Settings\Sync;
17
18/**
19 * BodyVoice
20 *
21 * @since 5.0.0
22 */
23class BodyVoice extends Voice
24{
25    /**
26     * Option name.
27     *
28     * @since 5.0.0
29     */
30    public const OPTION_NAME = 'beyondwords_project_body_voice_id';
31
32    /**
33     * Init.
34     *
35     * @since 5.0.0
36     * @since 6.0.0 Make static.
37     */
38    public static function init()
39    {
40        add_action('admin_init', [self::class, 'addSetting']);
41        add_action('pre_update_option_' . self::OPTION_NAME, function ($value) {
42            Sync::syncOptionToDashboard(self::OPTION_NAME);
43            return $value;
44        });
45    }
46
47    /**
48     * Add setting.
49     *
50     * @since 4.5.0
51     * @since 6.0.0 Make static.
52     *
53     * @return void
54     */
55    public static function addSetting()
56    {
57        register_setting(
58            'beyondwords_voices_settings',
59            self::OPTION_NAME,
60            [
61                'sanitize_callback' => 'absint',
62            ]
63        );
64
65        add_settings_field(
66            'beyondwords-body-voice',
67            __('Body voice', 'speechkit'),
68            [self::class, 'render'],
69            'beyondwords_voices',
70            'voices'
71        );
72    }
73
74    /**
75     * Render setting field.
76     *
77     * @since 5.0.0
78     * @since 6.0.0 Make static.
79     *
80     * @return void
81     **/
82    public static function render()
83    {
84        $current = get_option(self::OPTION_NAME);
85        $options = self::getOptions();
86        // phpcs:disable PluginCheck.CodeAnalysis.ImageFunctions.NonEnqueuedImage
87        ?>
88        <div class="beyondwords-setting__body-voice">
89            <select
90                id="<?php echo esc_attr(self::OPTION_NAME) ?>"
91                name="<?php echo esc_attr(self::OPTION_NAME) ?>"
92                class="beyondwords_project_voice"
93                style="width: 300px;"
94            >
95                <?php
96                foreach ($options as $option) {
97                    printf(
98                        '<option value="%s" data-language-code="%s" %s>%s</option>',
99                        esc_attr($option['value']),
100                        esc_attr($option['language_code']),
101                        selected($option['value'], $current),
102                        esc_html($option['label'])
103                    );
104                }
105                ?>
106            </select>
107            <img src="/wp-admin/images/spinner.gif" class="beyondwords-settings__loader" style="display:none;" />
108        </div>
109        <p class="description">
110            <?php
111            esc_html_e(
112                'Choose the default voice for your article body sections.',
113                'speechkit'
114            );
115            ?>
116        </p>
117        <?php
118        // phpcs:enable PluginCheck.CodeAnalysis.ImageFunctions.NonEnqueuedImage
119    }
120}