Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
40.00% covered (danger)
40.00%
8 / 20
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Log_Level
40.00% covered (danger)
40.00%
8 / 20
33.33% covered (danger)
33.33%
1 / 3
17.58
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 print_field_callback
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 sanitize_callback
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * This settings field is a checkbox to signify if autologin URLs should be redirect URLs via wp-login.php.
4 *
5 * @link       https://BrianHenry.ie
6 * @since      1.0.0
7 *
8 * @package    bh-wp-autologin-urls
9 */
10
11namespace BrianHenryIE\WP_Autologin_URLs\Admin\Settings_Fields;
12
13use BrianHenryIE\WP_Autologin_URLs\Settings_Interface;
14use BrianHenryIE\WP_Autologin_URLs\API\Settings;
15use Psr\Log\LogLevel;
16
17/**
18 * Class
19 */
20class Log_Level extends Settings_Section_Element_Abstract {
21
22    /**
23     * Array of PSR log levels we might log to.
24     *
25     * @var string[]
26     */
27    protected $log_levels;
28
29    /**
30     * Log_Level constructor.
31     *
32     * @param string             $settings_page_slug_name The slug of the page this setting is being displayed on.
33     * @param Settings_Interface $settings The existing settings saved in the database.
34     */
35    public function __construct( $settings_page_slug_name, $settings ) {
36
37        parent::__construct( $settings_page_slug_name );
38
39        $this->value = $settings->get_log_level();
40
41        $this->id    = Settings::LOG_LEVEL;
42        $this->title = __( 'Log level', 'bh-wp-autologin-urls' );
43        $this->page  = $settings_page_slug_name;
44
45        $this->add_settings_field_args['helper']       = __( 'Set to Debug to diagnose problems, Info to see times this plugin is logging users in. Warning: debug logs will contain autologin codes.', 'bh-wp-autologin-urls' );
46        $this->add_settings_field_args['supplemental'] = __( 'default: Notice', 'bh-wp-autologin-urls' );
47
48        // TODO: Consider removing the ones that are never used in the plugin.
49        $this->log_levels = array( 'none', LogLevel::ERROR, LogLevel::WARNING, LogLevel::NOTICE, LogLevel::INFO, LogLevel::DEBUG );
50    }
51
52    /**
53     * Prints the checkbox as displayed in the right-hand column of the settings table.
54     *
55     * @param array{helper:string, supplemental:string} $arguments The data registered with add_settings_field().
56     */
57    public function print_field_callback( $arguments ): void {
58
59        $label = $arguments['helper'];
60
61        printf( '<fieldset><label for="%1$s"><select id="%1$s" name="%1$s" />', esc_attr( $this->id ) );
62
63        foreach ( $this->log_levels as $level ) {
64
65            echo '<option value="' . esc_attr( $level ) . '"' . ( $this->value === $level ? ' selected' : '' ) . '>' . esc_html( ucfirst( $level ) ) . '</option>';
66        }
67
68        echo '</select>';
69
70        printf( '%1$s</label></fieldset>', wp_kses( $label, array( 'i' => array() ) ) );
71
72        $logs_url = admin_url( 'admin.php?page=bh-wp-autologin-urls-logs' );
73
74        printf( '<p class="description">%s – %s</p>', esc_html( $arguments['supplemental'] ), sprintf( '<a href="%s">View Logs</a>', esc_url( $logs_url ) ) );
75    }
76
77    /**
78     * Check it's a valid log PSR log level.
79     *
80     * @param string $value The data posted from the HTML form.
81     *
82     * @return string The value to save in the database.
83     */
84    public function sanitize_callback( $value ) {
85
86        if ( is_string( $value ) ) {
87
88            if ( in_array( $value, $this->log_levels, true ) ) {
89                return $value;
90            }
91        }
92
93        // Return the previous value.
94        return $this->value;
95    }
96}