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 */
22class ApiKey
23{
24    /**
25     * Option name.
26     *
27     * @since 5.0.0
28     */
29    public const OPTION_NAME = 'beyondwords_api_key';
30
31    /**
32     * Init.
33     *
34     * @since 4.0.0
35     */
36    public function init()
37    {
38        add_action('admin_init', array($this, 'addSetting'));
39    }
40
41    /**
42     * Init setting.
43     *
44     * @since  3.0.0
45     *
46     * @return void
47     */
48    public function addSetting()
49    {
50        register_setting(
51            'beyondwords_credentials_settings',
52            self::OPTION_NAME,
53            [
54                'default'           => '',
55                'sanitize_callback' => array($this, 'sanitize'),
56            ]
57        );
58
59        add_settings_field(
60            'beyondwords-api-key',
61            __('API key', 'speechkit'),
62            array($this, 'render'),
63            'beyondwords_credentials',
64            'credentials'
65        );
66    }
67
68    /**
69     * Render setting field.
70     *
71     * @since 3.0.0
72     *
73     * @return void
74     **/
75    public function render()
76    {
77        $value = get_option(self::OPTION_NAME);
78        ?>
79        <input
80            type="text"
81            id="<?php echo esc_attr(self::OPTION_NAME); ?>"
82            name="<?php echo esc_attr(self::OPTION_NAME); ?>"
83            value="<?php echo esc_attr($value); ?>"
84            size="50"
85        />
86        <?php
87    }
88
89    /**
90     * Sanitise the setting value.
91     *
92     * @since 3.0.0
93     * @since 5.2.0 Remove creds validation from here.
94     *
95     * @param array $value The submitted value.
96     *
97     * @return void
98     **/
99    public function sanitize($value)
100    {
101        if (empty($value)) {
102            SettingsUtils::addSettingsErrorMessage(
103                __(
104                    'Please enter the BeyondWords API key. This can be found in your project settings.',
105                    'speechkit'
106                ),
107                'Settings/ApiKey'
108            );
109        }
110
111        return $value;
112    }
113}