WordPress_Security_Txt_Builder::radios()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 15
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * The admin-specific UI builder of the plugin.
5
 *
6
 * @link       https://github.com/austinheap/wordpress-security-txt
7
 * @since      1.0.0
8
 *
9
 * @package    WordPress_Security_Txt
10
 * @subpackage WordPress_Security_Txt/admin
11
 */
12
13
/**
14
 * The admin-specific UI builder of the plugin.
15
 *
16
 * @package    WordPress_Security_Txt
17
 * @subpackage WordPress_Security_Txt/admin
18
 * @author     Austin Heap <[email protected]>
19
 */
20
class WordPress_Security_Txt_Builder
21
{
22
23
    /**
24
     * The ID of this plugin.
25
     *
26
     * @since    1.0.0
27
     * @access   private
28
     * @var      string $plugin_name The ID of this plugin.
29
     */
30
    private $plugin_name;
31
32
    /**
33
     * The version of this plugin.
34
     *
35
     * @since    1.0.0
36
     * @access   private
37
     * @var      string $version The current version of this plugin.
38
     */
39
    private $version;
40
41
    /**
42
     * The builder options.
43
     *
44
     * @since         1.0.0
45
     * @access        private
46
     * @var        array $options The plugin options.
47
     */
48
    private $options;
49
50
    /**
51
     * Initialize the class and set its properties.
52
     *
53
     * @since    1.0.0
54
     *
55
     * @param      string $plugin_name The name of this plugin.
56
     * @param      string $version     The version of this plugin.
57
     * @param      array  $options     The options of this plugin.
58
     */
59
    public function __construct($plugin_name, $version, $options)
60
    {
61
        $this->plugin_name = $plugin_name;
62
        $this->version     = $version;
63
        $this->options     = $options;
64
65
        if ($this->version != WORDPRESS_SECURITY_TXT_VERSION) {
66
            throw new Exception('Internal version mismatch in plugin wordpress-security-txt; it needs to be reinstalled.');
67
        }
68
    }
69
70
    /**
71
     * Creates a checkbox field
72
     *
73
     * @param    array $args The arguments for the field
74
     *
75
     * @return    string                        The HTML field
76
     */
77
    public function checkbox($args)
78
    {
79
        $defaults['class'] = $defaults['description'] = $defaults['label'] = '';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$defaults was never initialized. Although not strictly required by PHP, it is generally a good practice to add $defaults = array(); before regardless.
Loading history...
80
        $defaults['name']  = $this->plugin_name . '-options[' . $args['id'] . ']';
81
        $defaults['value'] = 0;
82
83
        apply_filters($this->plugin_name . '-field-checkbox-options-defaults', $defaults);
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
        /** @scrutinizer ignore-call */ 
84
        apply_filters($this->plugin_name . '-field-checkbox-options-defaults', $defaults);
Loading history...
84
85
        $atts = wp_parse_args($args, $defaults);
0 ignored issues
show
Bug introduced by
The function wp_parse_args was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
        $atts = /** @scrutinizer ignore-call */ wp_parse_args($args, $defaults);
Loading history...
86
87
        if (! empty($this->options[$atts['id']])) {
88
            $atts['value'] = $this->options[$atts['id']];
89
        }
90
91
        require plugin_dir_path(__FILE__) . 'partials/' . $this->plugin_name . '-field-checkbox.php';
0 ignored issues
show
Bug introduced by
The function plugin_dir_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
        require /** @scrutinizer ignore-call */ plugin_dir_path(__FILE__) . 'partials/' . $this->plugin_name . '-field-checkbox.php';
Loading history...
92
    }
93
94
    /**
95
     * Creates an editor field
96
     *
97
     * NOTE: ID must only be lowercase letter, no spaces, dashes, or underscores.
98
     *
99
     * @param    array $args The arguments for the field
100
     *
101
     * @return    string                        The HTML field
102
     */
103
    public function editor($args)
104
    {
105
        $defaults['description'] = $defaults['value'] = '';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$defaults was never initialized. Although not strictly required by PHP, it is generally a good practice to add $defaults = array(); before regardless.
Loading history...
106
        $defaults['settings']    = ['textarea_name' => $this->plugin_name . '-options[' . $args['id'] . ']'];
107
108
        apply_filters($this->plugin_name . '-field-editor-options-defaults', $defaults);
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

108
        /** @scrutinizer ignore-call */ 
109
        apply_filters($this->plugin_name . '-field-editor-options-defaults', $defaults);
Loading history...
109
110
        $atts = wp_parse_args($args, $defaults);
0 ignored issues
show
Bug introduced by
The function wp_parse_args was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

110
        $atts = /** @scrutinizer ignore-call */ wp_parse_args($args, $defaults);
Loading history...
111
112
        if (! empty($this->options[$atts['id']])) {
113
            $atts['value'] = $this->options[$atts['id']];
114
        }
115
116
        require plugin_dir_path(__FILE__) . 'partials/' . $this->plugin_name . '-field-editor.php';
0 ignored issues
show
Bug introduced by
The function plugin_dir_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

116
        require /** @scrutinizer ignore-call */ plugin_dir_path(__FILE__) . 'partials/' . $this->plugin_name . '-field-editor.php';
Loading history...
117
    }
118
119
    /**
120
     * Creates a set of radios field
121
     *
122
     * @param    array $args The arguments for the field
123
     *
124
     * @return    string                        The HTML field
125
     */
126
    public function radios($args)
127
    {
128
        $defaults['class'] = $defaults['label'] = $defaults['description'] = '';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$defaults was never initialized. Although not strictly required by PHP, it is generally a good practice to add $defaults = array(); before regardless.
Loading history...
129
        $defaults['name']  = $this->plugin_name . '-options[' . $args['id'] . ']';
130
        $defaults['value'] = 0;
131
132
        apply_filters($this->plugin_name . '-field-radios-options-defaults', $defaults);
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

132
        /** @scrutinizer ignore-call */ 
133
        apply_filters($this->plugin_name . '-field-radios-options-defaults', $defaults);
Loading history...
133
134
        $atts = wp_parse_args($args, $defaults);
0 ignored issues
show
Bug introduced by
The function wp_parse_args was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

134
        $atts = /** @scrutinizer ignore-call */ wp_parse_args($args, $defaults);
Loading history...
135
136
        if (! empty($this->options[$atts['id']])) {
137
            $atts['value'] = $this->options[$atts['id']];
138
        }
139
140
        require plugin_dir_path(__FILE__) . 'partials/' . $this->plugin_name . '-field-radios.php';
0 ignored issues
show
Bug introduced by
The function plugin_dir_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

140
        require /** @scrutinizer ignore-call */ plugin_dir_path(__FILE__) . 'partials/' . $this->plugin_name . '-field-radios.php';
Loading history...
141
    }
142
143
    public function repeater($args)
144
    {
145
        $defaults['class']        = 'repeater';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$defaults was never initialized. Although not strictly required by PHP, it is generally a good practice to add $defaults = array(); before regardless.
Loading history...
146
        $defaults['fields']       = [];
147
        $defaults['id']           = $defaults['title-field'] = '';
148
        $defaults['label-add']    = 'Add Item';
149
        $defaults['label-edit']   = 'Edit Item';
150
        $defaults['label-header'] = 'Item Name';
151
        $defaults['label-remove'] = 'Remove Item';
152
153
        apply_filters($this->plugin_name . '-field-repeater-options-defaults', $defaults);
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

153
        /** @scrutinizer ignore-call */ 
154
        apply_filters($this->plugin_name . '-field-repeater-options-defaults', $defaults);
Loading history...
154
155
        $setatts  = wp_parse_args($args, $defaults);
0 ignored issues
show
Bug introduced by
The function wp_parse_args was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

155
        $setatts  = /** @scrutinizer ignore-call */ wp_parse_args($args, $defaults);
Loading history...
156
        $count    = 1;
157
        $repeater = [];
158
159
        if (! empty($this->options[$setatts['id']])) {
160
            $repeater = maybe_unserialize($this->options[$setatts['id']][0]);
0 ignored issues
show
Bug introduced by
The function maybe_unserialize was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

160
            $repeater = /** @scrutinizer ignore-call */ maybe_unserialize($this->options[$setatts['id']][0]);
Loading history...
161
        }
162
163
        if (! empty($repeater)) {
164
            $count = count($repeater);
165
        }
166
167
        require plugin_dir_path(__FILE__) . 'partials/' . $this->plugin_name . '-field-repeater.php';
0 ignored issues
show
Bug introduced by
The function plugin_dir_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

167
        require /** @scrutinizer ignore-call */ plugin_dir_path(__FILE__) . 'partials/' . $this->plugin_name . '-field-repeater.php';
Loading history...
168
    }
169
170
    /**
171
     * Creates a select field
172
     *
173
     * Note: label is blank since its created in the Settings API
174
     *
175
     * @param    array $args The arguments for the field
176
     *
177
     * @return    string                        The HTML field
178
     */
179
    public function select($args)
180
    {
181
        $defaults['aria']       = $defaults['blank'] = $defaults['context'] = $defaults['description'] = $defaults['label'] = $defaults['value'] = '';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$defaults was never initialized. Although not strictly required by PHP, it is generally a good practice to add $defaults = array(); before regardless.
Loading history...
182
        $defaults['class']      = 'widefat';
183
        $defaults['name']       = $this->plugin_name . '-options[' . $args['id'] . ']';
184
        $defaults['selections'] = [];
185
186
        apply_filters($this->plugin_name . '-field-select-options-defaults', $defaults);
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

186
        /** @scrutinizer ignore-call */ 
187
        apply_filters($this->plugin_name . '-field-select-options-defaults', $defaults);
Loading history...
187
188
        $atts = wp_parse_args($args, $defaults);
0 ignored issues
show
Bug introduced by
The function wp_parse_args was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

188
        $atts = /** @scrutinizer ignore-call */ wp_parse_args($args, $defaults);
Loading history...
189
190
        if (! empty($this->options[$atts['id']])) {
191
            $atts['value'] = $this->options[$atts['id']];
192
        }
193
194
        if (empty($atts['aria']) && ! empty($atts['description'])) {
195
            $atts['aria'] = $atts['description'];
196
        } elseif (empty($atts['aria']) && ! empty($atts['label'])) {
197
            $atts['aria'] = $atts['label'];
198
        }
199
200
        require plugin_dir_path(__FILE__) . 'partials/' . $this->plugin_name . '-field-select.php';
0 ignored issues
show
Bug introduced by
The function plugin_dir_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

200
        require /** @scrutinizer ignore-call */ plugin_dir_path(__FILE__) . 'partials/' . $this->plugin_name . '-field-select.php';
Loading history...
201
    }
202
203
    /**
204
     * Creates a text field
205
     *
206
     * @param    array $args The arguments for the field
207
     *
208
     * @return    string                        The HTML field
209
     */
210
    public function text($args)
211
    {
212
        $defaults['placeholder'] = $defaults['description'] = $defaults['label'] = '';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$defaults was never initialized. Although not strictly required by PHP, it is generally a good practice to add $defaults = array(); before regardless.
Loading history...
213
        $defaults['class']       = 'text widefat';
214
        $defaults['name']        = $this->plugin_name . '-options[' . $args['id'] . ']';
215
        $defaults['type']        = 'text';
216
        $defaults['value']       = isset($args['value']) && ! empty($args['value']) ? $args['value'] : '';
217
218
        apply_filters($this->plugin_name . '-field-text-options-defaults', $defaults);
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

218
        /** @scrutinizer ignore-call */ 
219
        apply_filters($this->plugin_name . '-field-text-options-defaults', $defaults);
Loading history...
219
220
        $atts = wp_parse_args($args, $defaults);
0 ignored issues
show
Bug introduced by
The function wp_parse_args was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

220
        $atts = /** @scrutinizer ignore-call */ wp_parse_args($args, $defaults);
Loading history...
221
222
        if (! empty($this->options[$atts['id']])) {
223
            $atts['value'] = $this->options[$atts['id']];
224
        }
225
226
        require plugin_dir_path(__FILE__) . 'partials/' . $this->plugin_name . '-field-text.php';
0 ignored issues
show
Bug introduced by
The function plugin_dir_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

226
        require /** @scrutinizer ignore-call */ plugin_dir_path(__FILE__) . 'partials/' . $this->plugin_name . '-field-text.php';
Loading history...
227
    }
228
229
    /**
230
     * Creates a textarea field
231
     *
232
     * @param    array $args The arguments for the field
233
     *
234
     * @return    string                        The HTML field
235
     */
236
    public function textarea($args)
237
    {
238
        $defaults['context'] = $defaults['description'] = $defaults['label'] = $defaults['placeholder'] = '';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$defaults was never initialized. Although not strictly required by PHP, it is generally a good practice to add $defaults = array(); before regardless.
Loading history...
239
        $defaults['class']   = 'large-text';
240
        $defaults['cols']    = 50;
241
        $defaults['name']    = $this->plugin_name . '-options[' . $args['id'] . ']';
242
        $defaults['rows']    = 10;
243
        $defaults['value']   = isset($args['value']) && ! empty($args['value']) ? $args['value'] : '';
244
245
        apply_filters($this->plugin_name . '-field-textarea-options-defaults', $defaults);
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

245
        /** @scrutinizer ignore-call */ 
246
        apply_filters($this->plugin_name . '-field-textarea-options-defaults', $defaults);
Loading history...
246
247
        $atts = wp_parse_args($args, $defaults);
0 ignored issues
show
Bug introduced by
The function wp_parse_args was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

247
        $atts = /** @scrutinizer ignore-call */ wp_parse_args($args, $defaults);
Loading history...
248
249
        if (! empty($this->options[$atts['id']])) {
250
            $atts['value'] = $this->options[$atts['id']];
251
        }
252
253
        require plugin_dir_path(__FILE__) . 'partials/' . $this->plugin_name . '-field-textarea.php';
0 ignored issues
show
Bug introduced by
The function plugin_dir_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

253
        require /** @scrutinizer ignore-call */ plugin_dir_path(__FILE__) . 'partials/' . $this->plugin_name . '-field-textarea.php';
Loading history...
254
    }
255
}
256