-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathPasswordPolicy.php
More file actions
213 lines (188 loc) · 5.67 KB
/
PasswordPolicy.php
File metadata and controls
213 lines (188 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
/**
* Admin password Policy
*
* @package 10up-experience
*/
namespace TenUpExperience\AdminCustomizations;
use TenUpExperience\Singleton;
/**
* Admin PasswordPolicy class
*/
class PasswordPolicy extends Singleton {
/**
* Password policy option name
*/
const PASSWORD_POLICY_OPTION_NAME = 'tenup_password_policy_settings';
/**
* Setup module
*
* @since 1.7
*/
public function setup() {
add_action( 'admin_menu', [ $this, 'register_admin_pages' ] );
add_filter( 'admin_init', [ $this, 'register_password_policy_settings' ], 10, 2 );
}
/**
* Register admin pages with output callbacks
*/
public function register_admin_pages() {
if ( apply_filters( 'tenup_experience_enable_password_policy', true ) ) {
add_users_page( esc_html__( 'Password Policy', 'tenup' ), esc_html__( 'Password Policy', 'tenup' ), 'manage_options', '10up-password-policy', [ $this, 'password_policy_screen' ] );
}
}
/**
* Register password policy settings
*
* @return void
*/
public function register_password_policy_settings() {
register_setting(
self::PASSWORD_POLICY_OPTION_NAME,
self::PASSWORD_POLICY_OPTION_NAME,
[
'sanitize_callback' => [ $this, 'sanitize_settings' ],
]
);
add_settings_section(
self::PASSWORD_POLICY_OPTION_NAME,
'',
'__return_empty_string',
self::PASSWORD_POLICY_OPTION_NAME
);
$settings = [
'enabled' => [
'label' => __( 'Enable Password Policy', 'tenup' ),
'type' => 'checkbox',
],
'expires' => [
'label' => __( 'Password Expires', 'tenup' ),
'type' => 'number',
'description' => __( 'The number of days a passwords is good for before it needs to be changed.', 'tenup' ),
],
'reminder' => [
'label' => __( 'Send Password Reminder', 'tenup' ),
'type' => 'number',
'description' => __( 'The number of days before a password expires to send an email reminder.', 'tenup' ),
],
'past_passwords' => [
'label' => __( 'Past Passwords', 'tenup' ),
'type' => 'number',
'description' => __( 'The number of past passwords a user can\'t repeat.', 'tenup' ),
],
'reminder_email' => [
'label' => __( 'Reminder Email', 'tenup' ),
'description' => __( '###USERNAME###, ###ADMIN_EMAIL###, ###EMAIL###, ###SITENAME###, ###SITEURL###, ###DAYSLEFT###, and ###EXPIRATIONDATE### are replacement tags that can be used to populate the reminder email with dynamic information.', 'tenup' ),
'type' => 'tinymce',
],
];
foreach ( $settings as $setting_id => $setting ) {
$options = [
'name' => self::PASSWORD_POLICY_OPTION_NAME . "[$setting_id]",
'id' => $setting_id,
'type' => $setting['type'] ?? 'text',
'description' => $setting['description'] ?? '',
];
add_settings_field(
$setting_id,
$setting['label'],
[ $this, 'field' ],
self::PASSWORD_POLICY_OPTION_NAME,
self::PASSWORD_POLICY_OPTION_NAME,
$options
);
}
}
/**
* Output setting fields
*
* @param array $args field options
*/
public function field( $args ) {
$settings = get_option( self::PASSWORD_POLICY_OPTION_NAME, [] );
$value = $settings[ $args['id'] ] ?? '';
if ( 'checkbox' === $args['type'] ) {
printf( '<input type="%s" id="%s" name="%s" value="on" %s/>', esc_attr( $args['type'] ), esc_attr( $args['id'] ), esc_attr( $args['name'] ), esc_attr( checked( 'on', $value, false ) ) );
if ( ! empty( $args['description'] ) ) {
printf( '<label for="%s">%s</label>', esc_attr( $args['id'] ), esc_html( $args['description'] ) );
}
} elseif ( 'tinymce' === $args['type'] ) {
wp_editor(
$value,
$args['id'],
[
'media_buttons' => false,
'textarea_name' => $args['name'],
]
);
if ( ! empty( $args['description'] ) ) {
printf( '<p class="description">%s</p>', wp_kses_post( $args['description'] ) );
}
} else {
printf( '<input type="%s" id="%s" name="%s" value="%s" class="regular-text"/>', esc_attr( $args['type'] ), esc_attr( $args['id'] ), esc_attr( $args['name'] ), esc_attr( $value ) );
if ( ! empty( $args['description'] ) ) {
printf( '<p class="description">%s</p>', esc_html( $args['description'] ) );
}
}
}
/**
* Sanitize settings fields
*
* @param array $settings setting being saved
*
* @return array
*/
public function sanitize_settings( $settings ) {
$clean_settings = array();
foreach ( $settings as $key => $setting ) {
if ( in_array( $key, [ 'reminder_email', 'token_email' ], true ) ) {
$clean_settings[ $key ] = wp_kses_post( $setting );
} else {
$clean_settings[ $key ] = sanitize_text_field( $setting );
}
}
return $clean_settings;
}
/**
* Password policy screen
*
* @return void
*/
public function password_policy_screen() {
?>
<div class="wrap">
<h2><?php esc_html_e( 'Password Policy', 'tenup' ); ?></h2>
<?php settings_errors(); ?>
<form action="options.php" method="POST">
<?php
settings_fields( self::PASSWORD_POLICY_OPTION_NAME );
do_settings_sections( self::PASSWORD_POLICY_OPTION_NAME );
submit_button( __( 'Save Settings', 'tenup' ) );
?>
</form>
</div>
<?php
}
/**
* Return password policy settings
*
* @param string $name Setting key
*
* @return string
*/
public function get_setting( $name = '' ) {
$settings = get_option( self::PASSWORD_POLICY_OPTION_NAME, [] );
if ( empty( $name ) ) {
return $settings;
}
return $settings[ $name ] ?? '';
}
/**
* Is the password policy feature enabled
*
* @return bool
*/
public function is_enabled() {
return ! empty( $this->get_setting( 'enabled' ) ) && apply_filters( 'tenup_experience_enable_password_policy', true );
}
}