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