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