Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Admin_Enable
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
3 / 3
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 print_field_callback
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 sanitize_callback
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * This settings field is a checkbox to signify if autologin codes should be added to emails sent to admins.
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;
15
16/**
17 * Class Admin_Enable
18 */
19class Admin_Enable extends Settings_Section_Element_Abstract {
20
21    /**
22     * Admin_Enable constructor.
23     *
24     * @param string             $settings_page_slug_name The slug of the page this setting is being displayed on.
25     * @param Settings_Interface $settings The existing settings saved in the database.
26     */
27    public function __construct( $settings_page_slug_name, $settings ) {
28
29        parent::__construct( $settings_page_slug_name );
30
31        $this->value = $settings->get_add_autologin_for_admins_is_enabled() ? 'admin_is_enabled' : 'admin_is_not_enabled';
32
33        $this->id    = Settings::ADMIN_ENABLED;
34        $this->title = __( 'Add to admin emails?', 'bh-wp-autologin-urls' );
35        $this->page  = $settings_page_slug_name;
36
37        $this->add_settings_field_args['helper']       = __( 'When enabled, emails to administrators <i>will</i> contain autologin URLs.', 'bh-wp-autologin-urls' );
38        $this->add_settings_field_args['supplemental'] = __( 'default: false', 'bh-wp-autologin-urls' );
39    }
40
41    /**
42     * Prints the checkbox as displayed in the right-hand column of the settings table.
43     *
44     * @param array{helper:string, supplemental:string} $arguments The data registered with add_settings_field().
45     */
46    public function print_field_callback( $arguments ): void {
47
48        $value = $this->value;
49
50        // This is what is POSTed when the checkbox is ticked.
51        $checkbox_value = 'admin_is_enabled';
52        $is_checked     = 'admin_is_enabled' === $value ? 'checked ' : '';
53        $label          = $arguments['helper'];
54
55        printf( '<fieldset><label for="%1$s"><input id="%1$s" name="%1$s" type="checkbox" value="%2$s" %3$s />%4$s</label></fieldset>', esc_attr( $this->id ), esc_attr( $checkbox_value ), esc_attr( $is_checked ), wp_kses( $label, array( 'i' => array() ) ) );
56
57        printf( '<p class="description">%s</p>', esc_html( $arguments['supplemental'] ) );
58    }
59
60    /**
61     * If an unexpected value is POSTed, don't make any change to what's in the database.
62     *
63     * @param ?string $value The data posted from the HTML form.
64     *
65     * @return string The value to save in the database.
66     */
67    public function sanitize_callback( $value ) {
68
69        if ( 'admin_is_enabled' === $value ) {
70            return 'admin_is_enabled';
71        } elseif ( null === $value ) {
72            return 'admin_is_not_enabled';
73        } else {
74            return $this->value;
75        }
76    }
77}