Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
ApiKey
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
4 / 4
5
100.00% covered (success)
100.00%
1 / 1
 init
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addSetting
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 render
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 sanitize
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5/**
6 * Setting: API Key
7 *
8 * @package Beyondwords\Wordpress
9 * @author  Stuart McAlpine <stu@beyondwords.io>
10 * @since   3.0.0
11 */
12
13namespace Beyondwords\Wordpress\Component\Settings\Fields\ApiKey;
14
15use Beyondwords\Wordpress\Component\Settings\SettingsUtils;
16
17/**
18 * ApiKey
19 *
20 * @since 3.0.0
21 */
22defined('ABSPATH') || exit;
23
24class ApiKey
25{
26    /**
27     * Option name.
28     *
29     * @since 5.0.0
30     */
31    public const OPTION_NAME = 'beyondwords_api_key';
32
33    /**
34     * Init.
35     *
36     * @since 4.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    }
43
44    /**
45     * Init setting.
46     *
47     * @since 3.0.0
48     * @since 6.0.0 Make static.
49     *
50     * @return void
51     */
52    public static function addSetting()
53    {
54        register_setting(
55            'beyondwords_credentials_settings',
56            self::OPTION_NAME,
57            [
58                'default'           => '',
59                'sanitize_callback' => [self::class, 'sanitize'],
60            ]
61        );
62
63        add_settings_field(
64            'beyondwords-api-key',
65            __('API key', 'speechkit'),
66            [self::class, 'render'],
67            'beyondwords_credentials',
68            'credentials'
69        );
70    }
71
72    /**
73     * Render setting field.
74     *
75     * @since 3.0.0
76     * @since 6.0.0 Make static.
77     *
78     * @return void
79     **/
80    public static function render()
81    {
82        $value = get_option(self::OPTION_NAME);
83        ?>
84        <input
85            type="text"
86            id="<?php echo esc_attr(self::OPTION_NAME); ?>"
87            name="<?php echo esc_attr(self::OPTION_NAME); ?>"
88            value="<?php echo esc_attr($value); ?>"
89            size="50"
90        />
91        <?php
92    }
93
94    /**
95     * Sanitise the setting value.
96     *
97     * @since 3.0.0
98     * @since 5.2.0 Remove creds validation from here.
99     * @since 6.0.0 Make static.
100     *
101     * @param array $value The submitted value.
102     *
103     * @return void
104     **/
105    public static function sanitize($value)
106    {
107        if (empty($value)) {
108            SettingsUtils::addSettingsErrorMessage(
109                __(
110                    'Please enter the BeyondWords API key. This can be found in your project settings.',
111                    'speechkit'
112                ),
113                'Settings/ApiKey'
114            );
115        }
116
117        return $value;
118    }
119}