Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
23 / 23 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
Expiry_Age | |
100.00% |
23 / 23 |
|
100.00% |
3 / 3 |
5 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
print_field_callback | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
sanitize_callback | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | /** |
3 | * This settings field is a text field to configure the number of seconds the autologin codes should be valid for. |
4 | * |
5 | * @link https://BrianHenry.ie |
6 | * @since 1.0.0 |
7 | * |
8 | * @package BH_WP_Autologin_URLs\admin\partials |
9 | */ |
10 | |
11 | namespace BrianHenryIE\WP_Autologin_URLs\Admin\Settings_Fields; |
12 | |
13 | use BrianHenryIE\WP_Autologin_URLs\Settings_Interface; |
14 | |
15 | /** |
16 | * Class Expiry_Age |
17 | * |
18 | * @package BH_WP_Autologin_URLs\admin\partials |
19 | */ |
20 | class Expiry_Age extends Settings_Section_Element_Abstract { |
21 | |
22 | /** |
23 | * Expiry_Age constructor. |
24 | * |
25 | * @param string $settings_page_slug_name The slug of the page this setting is being displayed on. |
26 | * @param Settings_Interface $settings The existing settings saved in the database. |
27 | */ |
28 | public function __construct( $settings_page_slug_name, $settings ) { |
29 | |
30 | parent::__construct( $settings_page_slug_name ); |
31 | |
32 | $this->value = $settings->get_expiry_age(); |
33 | |
34 | $this->id = 'bh_wp_autologin_urls_seconds_until_expiry'; |
35 | $this->title = 'Expiry age:'; |
36 | $this->page = $settings_page_slug_name; |
37 | |
38 | $this->add_settings_field_args['helper'] = 'Number of seconds until each code expires.'; |
39 | $this->add_settings_field_args['supplemental'] = 'default: 604800 (one week)'; |
40 | $this->add_settings_field_args['default'] = 604800; |
41 | $this->add_settings_field_args['placeholder'] = ''; |
42 | |
43 | $this->register_setting_args['default'] = 604800; |
44 | } |
45 | |
46 | /** |
47 | * The function used by WordPress Settings API to output the field. |
48 | * |
49 | * @param array{placeholder:string, helper:string, supplemental:string} $arguments Settings passed from WordPress do_settings_fields() function. |
50 | */ |
51 | public function print_field_callback( $arguments ): void { |
52 | |
53 | $value = $this->value; |
54 | |
55 | printf( '<input name="%1$s" id="%1$s" type="text" placeholder="%2$s" value="%3$s" />', esc_attr( $this->id ), esc_attr( $arguments['placeholder'] ), esc_attr( $value ) ); |
56 | |
57 | printf( '<span class="helper">%s</span>', esc_html( $arguments['helper'] ) ); |
58 | |
59 | printf( '<p class="description">%s</p>', esc_html( $arguments['supplemental'] ) ); |
60 | } |
61 | |
62 | /** |
63 | * Expiry age should always be a number. Accepts arithmetic, e.g. 60*60*24. |
64 | * |
65 | * @param string $value The value POSTed by the Settings API. |
66 | * |
67 | * @return int |
68 | */ |
69 | public function sanitize_callback( $value ) { |
70 | |
71 | if ( empty( $value ) ) { |
72 | |
73 | add_settings_error( $this->id, 'expiry-age-empty-error', 'Expiry age was empty. Previous value ' . $this->value . ' was saved.', 'error' ); |
74 | |
75 | return $this->value; |
76 | } |
77 | |
78 | // Remove anything that is not a digit or "*". |
79 | $value = (string) preg_replace( '/[^0-9*]/', '', $value ); |
80 | |
81 | if ( ! stristr( $value, '*' ) ) { |
82 | return intval( abs( $value ) ); |
83 | } |
84 | |
85 | $value = explode( '*', $value ); |
86 | |
87 | $result = array_product( $value ); |
88 | |
89 | return intval( abs( $result ) ); |
90 | } |
91 | } |