WordPress_Security_Txt_Admin::enqueue_scripts()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * The admin-specific functionality 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 functionality of the plugin.
15
 *
16
 * Defines the plugin name, version, and hooks for the admin-specific stylesheet and JavaScript.
17
 *
18
 * @package    WordPress_Security_Txt
19
 * @subpackage WordPress_Security_Txt/admin
20
 * @author     Austin Heap <[email protected]>
21
 */
22
class WordPress_Security_Txt_Admin
23
{
24
    /**
25
     * The ID of this plugin.
26
     *
27
     * @since    1.0.0
28
     * @access   private
29
     * @var      string $plugin_name The ID of this plugin.
30
     */
31
    private $plugin_name;
32
33
    /**
34
     * The version of this plugin.
35
     *
36
     * @since    1.0.0
37
     * @access   private
38
     * @var      string $version The current version of this plugin.
39
     */
40
    private $version;
41
42
    /**
43
     * The plugin options.
44
     *
45
     * @since         1.0.0
46
     * @access        private
47
     * @var        array $options The plugin options.
48
     */
49
    private $options;
50
51
    /**
52
     * Initialize the class and set its properties.
53
     *
54
     * @since    1.0.0
55
     *
56
     * @param      string $plugin_name The name of this plugin.
57
     * @param      string $version     The version of this plugin.
58
     */
59
    public function __construct($plugin_name, $version)
60
    {
61
        $this->plugin_name = $plugin_name;
62
        $this->version     = $version;
63
        $this->options     = $this::get_options($plugin_name);
64
    }
65
66
    /**
67
     * Sets the class variable $options
68
     *
69
     * @param string $plugin_name The name of the plugin.
70
     */
71
    public static function get_options($plugin_name = 'wordpress-security-txt')
72
    {
73
        return get_option($plugin_name . '-options');
0 ignored issues
show
Bug introduced by
The function get_option 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

73
        return /** @scrutinizer ignore-call */ get_option($plugin_name . '-options');
Loading history...
74
    }
75
76
    /**
77
     * Register the stylesheets for the admin area.
78
     *
79
     * @since    1.0.0
80
     */
81
    public function enqueue_styles()
82
    {
83
        wp_enqueue_style($this->plugin_name, plugin_dir_url(__FILE__) . 'css/' . $this->plugin_name . '-admin.css', [],
0 ignored issues
show
Bug introduced by
The function wp_enqueue_style 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
        wp_enqueue_style($this->plugin_name, plugin_dir_url(__FILE__) . 'css/' . $this->plugin_name . '-admin.css', [],
Loading history...
Bug introduced by
The function plugin_dir_url 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
        wp_enqueue_style($this->plugin_name, /** @scrutinizer ignore-call */ plugin_dir_url(__FILE__) . 'css/' . $this->plugin_name . '-admin.css', [],
Loading history...
84
            $this->version, 'all');
85
    }
86
87
    /**
88
     * Register the JavaScript for the admin area.
89
     *
90
     * @since    1.0.0
91
     */
92
    public function enqueue_scripts()
93
    {
94
        wp_enqueue_script($this->plugin_name . '-repeater',
0 ignored issues
show
Bug introduced by
The function wp_enqueue_script 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

94
        /** @scrutinizer ignore-call */ 
95
        wp_enqueue_script($this->plugin_name . '-repeater',
Loading history...
95
            plugin_dir_url(__FILE__) . 'js/' . $this->plugin_name . '-repeater.min.js',
0 ignored issues
show
Bug introduced by
The function plugin_dir_url 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

95
            /** @scrutinizer ignore-call */ 
96
            plugin_dir_url(__FILE__) . 'js/' . $this->plugin_name . '-repeater.min.js',
Loading history...
96
            ['jquery', 'jquery-ui-core', 'jquery-ui-sortable'], $this->version, true);
97
98
        wp_enqueue_script($this->plugin_name . '-validator',
99
            plugin_dir_url(__FILE__) . 'js/' . $this->plugin_name . '-validator.js',
100
            ['jquery'], $this->version, true);
101
102
        wp_enqueue_script($this->plugin_name, plugin_dir_url(__FILE__) . 'js/' . $this->plugin_name . '-admin.js',
103
            ['jquery'], $this->version, true);
104
    }
105
106
    /**
107
     * Add an options page under the Settings submenu
108
     *
109
     * @since  1.0.0
110
     */
111
    public function add_options_page()
112
    {
113
        add_options_page(
0 ignored issues
show
Bug introduced by
The function add_options_page 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

113
        /** @scrutinizer ignore-call */ 
114
        add_options_page(
Loading history...
114
            __('security.txt', $this->plugin_name), __('security.txt', $this->plugin_name),
0 ignored issues
show
Bug introduced by
The function __ 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

114
            /** @scrutinizer ignore-call */ 
115
            __('security.txt', $this->plugin_name), __('security.txt', $this->plugin_name),
Loading history...
115
            'manage_options', $this->plugin_name, [$this, 'display_settings_page']
116
        );
117
118
        add_submenu_page(
0 ignored issues
show
Bug introduced by
The function add_submenu_page 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

118
        /** @scrutinizer ignore-call */ 
119
        add_submenu_page(
Loading history...
119
            'options-general.php',
120
            __('security.txt Help', $this->plugin_name), __('security.txt Help', $this->plugin_name),
121
            'manage_options', $this->plugin_name . '-help', [$this, 'display_help_page']
122
        );
123
124
        remove_submenu_page('options-general.php', 'wordpress-security-txt-help');
0 ignored issues
show
Bug introduced by
The function remove_submenu_page 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

124
        /** @scrutinizer ignore-call */ 
125
        remove_submenu_page('options-general.php', 'wordpress-security-txt-help');
Loading history...
125
    }
126
127
    /**
128
     * Render the settings page for plugin
129
     *
130
     * @since  1.0.0
131
     */
132
    public function display_settings_page()
133
    {
134
        WordPress_Security_Txt::import_lib();
135
        require plugin_dir_path(__FILE__) . 'partials/wordpress-security-txt-page-settings.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

135
        require /** @scrutinizer ignore-call */ plugin_dir_path(__FILE__) . 'partials/wordpress-security-txt-page-settings.php';
Loading history...
136
    }
137
138
    /**
139
     * Render the help page for plugin
140
     *
141
     * @since  1.0.0
142
     */
143
    public function display_help_page()
144
    {
145
        WordPress_Security_Txt::import_lib();
146
        require plugin_dir_path(__FILE__) . 'partials/wordpress-security-txt-page-help.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

146
        require /** @scrutinizer ignore-call */ plugin_dir_path(__FILE__) . 'partials/wordpress-security-txt-page-help.php';
Loading history...
147
    }
148
149
    /**
150
     * Registers settings sections with WordPress
151
     *
152
     * @since        1.0.0
153
     * @return        void
154
     */
155
    public function register_sections()
156
    {
157
        add_settings_section(
0 ignored issues
show
Bug introduced by
The function add_settings_section 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

157
        /** @scrutinizer ignore-call */ 
158
        add_settings_section(
Loading history...
158
            $this->plugin_name . '-general',
159
            apply_filters($this->plugin_name . 'section-title-general', esc_html__('General', $this->plugin_name)),
0 ignored issues
show
Bug introduced by
The function esc_html__ 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

159
            apply_filters($this->plugin_name . 'section-title-general', /** @scrutinizer ignore-call */ esc_html__('General', $this->plugin_name)),
Loading history...
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

159
            /** @scrutinizer ignore-call */ 
160
            apply_filters($this->plugin_name . 'section-title-general', esc_html__('General', $this->plugin_name)),
Loading history...
160
            [$this, 'section_general'],
161
            $this->plugin_name
162
        );
163
164
        add_settings_section(
165
            $this->plugin_name . '-directives',
166
            apply_filters($this->plugin_name . 'section-title-directives',
167
                esc_html__('Directives', $this->plugin_name)),
168
            [$this, 'section_directives'],
169
            $this->plugin_name
170
        );
171
172
        add_settings_section(
173
            $this->plugin_name . '-library',
174
            apply_filters($this->plugin_name . 'section-title-library', esc_html__('Library', $this->plugin_name)),
175
            [$this, 'section_library'],
176
            $this->plugin_name
177
        );
178
179
        add_settings_section(
180
            $this->plugin_name . '-debug',
181
            apply_filters($this->plugin_name . 'section-title-debug', esc_html__('Debug', $this->plugin_name)),
182
            [$this, 'section_debug'],
183
            $this->plugin_name
184
        );
185
    }
186
187
    /**
188
     * Registers settings fields with WordPress
189
     *
190
     * @since        1.0.0
191
     * @return        void
192
     */
193
    public function register_fields()
194
    {
195
        (new WordPress_Security_Txt_Field($this->plugin_name, $this->version, $this->options))
196
            ->register_all();
197
    }
198
199
    /**
200
     * Creates a general section
201
     *
202
     * @since        1.0.0
203
     *
204
     * @param        array $params Array of parameters for the section
205
     *
206
     * @return        mixed                        The settings section
207
     */
208
    public function section_general($params)
209
    {
210
        require plugin_dir_path(__FILE__) . 'partials/wordpress-security-txt-section-general.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

210
        require /** @scrutinizer ignore-call */ plugin_dir_path(__FILE__) . 'partials/wordpress-security-txt-section-general.php';
Loading history...
211
    }
212
213
    /**
214
     * Creates a directives section
215
     *
216
     * @since        1.0.0
217
     *
218
     * @param        array $params Array of parameters for the section
219
     *
220
     * @return        mixed                        The settings section
221
     */
222
    public function section_directives($params)
223
    {
224
        require plugin_dir_path(__FILE__) . 'partials/wordpress-security-txt-section-directives.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

224
        require /** @scrutinizer ignore-call */ plugin_dir_path(__FILE__) . 'partials/wordpress-security-txt-section-directives.php';
Loading history...
225
    }
226
227
    /**
228
     * Creates a library section
229
     *
230
     * @since        1.0.0
231
     *
232
     * @param        array $params Array of parameters for the section
233
     *
234
     * @return        mixed                        The settings section
235
     */
236
    public function section_library($params)
237
    {
238
        require plugin_dir_path(__FILE__) . 'partials/wordpress-security-txt-section-library.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

238
        require /** @scrutinizer ignore-call */ plugin_dir_path(__FILE__) . 'partials/wordpress-security-txt-section-library.php';
Loading history...
239
    }
240
241
    /**
242
     * Creates a debug section
243
     *
244
     * @since        1.0.0
245
     *
246
     * @param        array $params Array of parameters for the section
247
     *
248
     * @return        mixed                        The settings section
249
     */
250
    public function section_debug($params)
251
    {
252
        require plugin_dir_path(__FILE__) . 'partials/wordpress-security-txt-section-debug.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

252
        require /** @scrutinizer ignore-call */ plugin_dir_path(__FILE__) . 'partials/wordpress-security-txt-section-debug.php';
Loading history...
253
    }
254
255
    /**
256
     * Registers plugin settings
257
     *
258
     * @since        1.0.0
259
     * @return        void
260
     */
261
    public function register_settings()
262
    {
263
        register_setting(
0 ignored issues
show
Bug introduced by
The function register_setting 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

263
        /** @scrutinizer ignore-call */ 
264
        register_setting(
Loading history...
264
            $this->plugin_name,// . '-options',
265
            $this->plugin_name . '-options',
266
            [$this, 'validate_options']
267
        );
268
    }
269
270
    public function validate_options($input)
271
    {
272
        WordPress_Security_Txt_Public::cache_clear();
273
274
        $valid   = [];
275
        $options = $this->get_options_list();
276
277
        foreach ($options as $option) {
278
            $name = $option[0];
279
            $type = $option[1];
280
281
            if ($type == 'repeater' && is_array($option[2])) {
282
                $this->validate_repeater($input, $valid);
283
            } else {
284
                $valid[$option[0]] = $this->sanitizer($type, isset($input[$name]) ? $input[$name] : null);
285
            }
286
        }
287
288
        return $valid;
289
    }
290
291
    private function validate_repeater($input, &$valid)
292
    {
293
        $clean = [];
294
295
        foreach ($option[2] as $field) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $option seems to be never defined.
Loading history...
296
            foreach ($input[$field[0]] as $data) {
297
                if (!empty($data)) {
298
                    $clean[$field[0]][] = $this->sanitizer($field[1], $data);
299
                }
300
            }
301
        }
302
303
        $count = 10;
304
305
        for ($i = 0; $i < $count; $i++) {
306
            foreach ($clean as $field_name => $field) {
307
                if (isset($valid[$option[0]][$i])) {
308
                    $valid[$option[0]][$i][$field_name] = $field[$i];
309
                }
310
            }
311
        }
312
    }
313
314
    /**
315
     * Returns an array of options names, fields types, and default values
316
     *
317
     * @return        array            An array of options
318
     */
319
    public static function get_options_list()
320
    {
321
        $options = [];
322
323
        $options[] = ['enable', 'checkbox', true];
324
        $options[] = ['redirect', 'checkbox', true];
325
        $options[] = ['menu', 'checkbox', true];
326
        $options[] = ['contact', 'text', get_bloginfo('admin_email')];
0 ignored issues
show
Bug introduced by
The function get_bloginfo 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

326
        $options[] = ['contact', 'text', /** @scrutinizer ignore-call */ get_bloginfo('admin_email')];
Loading history...
327
        $options[] = ['encryption', 'textarea', null];
328
        $options[] = ['acknowledgement', 'text', null];
329
        $options[] = ['disclosure', 'select', 'default'];
330
        $options[] = ['cache', 'checkbox', true];
331
        $options[] = ['credits', 'checkbox', true];
332
        $options[] = ['statistics', 'checkbox', false];
333
        $options[] = ['debug', 'checkbox', false];
334
335
        return $options;
336
    }
337
338
    private function sanitizer($type, $data)
339
    {
340
        if (empty($type)) {
341
            throw new Exception(__('Cannot sanitize data type NULL.', $this->plugin_name));
0 ignored issues
show
Bug introduced by
The function __ 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

341
            throw new Exception(/** @scrutinizer ignore-call */ __('Cannot sanitize data type NULL.', $this->plugin_name));
Loading history...
342
        }
343
344
        return (new WordPress_Security_Txt_Sanitizer($this->plugin_name, $this->version, $data, $type))->clean();
345
    }
346
347
    /**
348
     * Adds links to the plugin links row
349
     *
350
     * @since        1.0.0
351
     *
352
     * @param        array  $links The current array of row links
353
     * @param        string $file  The name of the file
354
     *
355
     * @return        array                    The modified array of row links
356
     */
357
    public function link_row($links, $file)
358
    {
359
        if (WORDPRESS_SECURITY_TXT_FILE === $file) {
360
            $links[] = '<a href="http://twitter.com/austinheap">@austinheap</a>';
361
            $links[] = '<a href="http://twitter.com/EdOverflow">@EdOverflow</a>';
362
        }
363
364
        return $links;
365
    }
366
367
    /**
368
     * Adds a link to the plugin settings page
369
     *
370
     * @since        1.0.0
371
     *
372
     * @param        array $links The current array of links
373
     *
374
     * @return        array                    The modified array of links
375
     */
376
    public function link_settings($links)
377
    {
378
        $links[] = sprintf('<a href="%s">%s</a>',
379
            esc_url(admin_url('options-general.php?page=' . $this->plugin_name)),
0 ignored issues
show
Bug introduced by
The function admin_url 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

379
            esc_url(/** @scrutinizer ignore-call */ admin_url('options-general.php?page=' . $this->plugin_name)),
Loading history...
Bug introduced by
The function esc_url 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

379
            /** @scrutinizer ignore-call */ 
380
            esc_url(admin_url('options-general.php?page=' . $this->plugin_name)),
Loading history...
380
            esc_html__('Settings', $this->plugin_name));
0 ignored issues
show
Bug introduced by
The function esc_html__ 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

380
            /** @scrutinizer ignore-call */ 
381
            esc_html__('Settings', $this->plugin_name));
Loading history...
381
382
        $links[] = sprintf('<a href="%s">%s</a>',
383
            esc_url(admin_url('options-general.php?page=' . $this->plugin_name . '-help')),
384
            esc_html__('Help', $this->plugin_name));
385
386
        return $links;
387
    }
388
389
    /**
390
     * Adds an item to the admin bar
391
     *
392
     * @since        1.0.0
393
     * @return       void
394
     */
395
    public function admin_bar()
396
    {
397
        global $wp_admin_bar;
398
        if (isset($this->options['menu']) && $this->options['menu']) {
399
            $wp_admin_bar->add_menu([
400
                    'id'    => $this->plugin_name . '_root_toolbar',
401
                    'title' => __('security.txt', $this->plugin_name),
0 ignored issues
show
Bug introduced by
The function __ 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

401
                    'title' => /** @scrutinizer ignore-call */ __('security.txt', $this->plugin_name),
Loading history...
402
                    'href'  => '#',
403
                ]
404
            );
405
406
            $wp_admin_bar->add_menu([
407
                    'id'     => $this->plugin_name . '_settings_toolbar',
408
                    'title'  => __('Settings', $this->plugin_name),
409
                    'href'   => 'options-general.php?page=wordpress-security-txt',
410
                    'parent' => $this->plugin_name . '_root_toolbar',
411
                ]
412
            );
413
414
            $wp_admin_bar->add_menu([
415
                    'id'     => $this->plugin_name . '_help_toolbar',
416
                    'title'  => __('Help', $this->plugin_name),
417
                    'href'   => 'options-general.php?page=wordpress-security-txt-help',
418
                    'parent' => $this->plugin_name . '_root_toolbar',
419
                ]
420
            );
421
        }
422
    }
423
}
424