PluginProbe
Password Protected — Lock Entire Site, Pages, Posts, Categories, and Partial Content / 2.2
Password Protected — Lock Entire Site, Pages, Posts, Categories, and Partial Content v2.2
2.8.4 2.8.3 2.8.2 2.8.1 trunk 1.0 1.1 1.2 1.2.1 1.2.2 1.3 1.4 1.5 1.6 1.6.1 1.6.2 1.7 1.7.1 1.7.2 1.8 1.9 2.0 2.0.1 2.0.2 2.0.3 All 63 releases
password-protected / admin / admin.php

admin.php in Password Protected — Lock Entire Site, Pages, Posts, Categories, and Partial Content 2.2, at admin/admin.php

453 lines 16.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Password_Protected_Admin {
4
5 var $settings_page_id;
6 var $options_group = 'password-protected';
7
8 /**
9 * Constructor
10 */
11 public function __construct() {
12
13 global $wp_version;
14
15 add_action( 'admin_init', array( $this, 'password_protected_settings' ), 5 );
16 add_action( 'admin_menu', array( $this, 'admin_menu' ) );
17 add_action( 'password_protected_help_tabs', array( $this, 'help_tabs' ), 5 );
18 add_action( 'admin_notices', array( $this, 'password_protected_admin_notices' ) );
19 add_filter( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 4 );
20 add_filter( 'plugin_action_links_password-protected/password-protected.php', array( $this, 'plugin_action_links' ) );
21 add_filter( 'pre_update_option_password_protected_password', array( $this, 'pre_update_option_password_protected_password' ), 10, 2 );
22
23 }
24
25 /**
26 * Admin Menu
27 */
28 public function admin_menu() {
29
30 $this->settings_page_id = add_options_page( __( 'Password Protected', 'password-protected' ), __( 'Password Protected', 'password-protected' ), 'manage_options', 'password-protected', array( $this, 'settings_page' ) );
31 add_action( 'load-' . $this->settings_page_id, array( $this, 'add_help_tabs' ), 20 );
32
33 }
34
35 /**
36 * Settings Page
37 */
38 public function settings_page() {
39 ?>
40
41 <div class="wrap">
42 <div id="icon-options-general" class="icon32"><br /></div>
43 <h2><?php _e( 'Password Protected Settings', 'password-protected' ) ?></h2>
44 <form method="post" action="options.php">
45 <?php
46 settings_fields( 'password-protected' );
47 do_settings_sections( 'password-protected' );
48 ?>
49 <p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="<?php _e( 'Save Changes' ) ?>"></p>
50 </form>
51 <?php do_settings_sections( 'password-protected-compat' ); ?>
52 </div>
53
54 <?php
55 }
56
57 /**
58 * Add Help Tabs
59 */
60 public function add_help_tabs() {
61
62 global $wp_version;
63
64 if ( version_compare( $wp_version, '3.3', '<' ) ) {
65 return;
66 }
67
68 do_action( 'password_protected_help_tabs', get_current_screen() );
69
70 }
71
72 /**
73 * Help Tabs
74 *
75 * @param object $current_screen Screen object.
76 */
77 public function help_tabs( $current_screen ) {
78
79 $current_screen->add_help_tab( array(
80 'id' => 'PASSWORD_PROTECTED_SETTINGS',
81 'title' => __( 'Password Protected', 'password-protected' ),
82 'content' => __( '<p><strong>Password Protected Status</strong><br />Turn on/off password protection.</p>', 'password-protected' )
83 . __( '<p><strong>Protected Permissions</strong><br />Allow access for logged in users and administrators without needing to enter a password. You will need to enable this option if you want administrators to be able to preview the site in the Theme Customizer. Also allow RSS Feeds to be accessed when the site is password protected.</p>', 'password-protected' )
84 . __( '<p><strong>Password Fields</strong><br />To set a new password, enter it into both fields. You cannot set an `empty` password. To disable password protection uncheck the Enabled checkbox.</p>', 'password-protected' )
85 ) );
86
87 }
88
89 /**
90 * Settings API
91 */
92 public function password_protected_settings() {
93
94 add_settings_section(
95 'password_protected',
96 '',
97 array( $this, 'password_protected_settings_section' ),
98 $this->options_group
99 );
100
101 add_settings_field(
102 'password_protected_status',
103 __( 'Password Protected Status', 'password-protected' ),
104 array( $this, 'password_protected_status_field' ),
105 $this->options_group,
106 'password_protected'
107 );
108
109 add_settings_field(
110 'password_protected_permissions',
111 __( 'Protected Permissions', 'password-protected' ),
112 array( $this, 'password_protected_permissions_field' ),
113 $this->options_group,
114 'password_protected'
115 );
116
117 add_settings_field(
118 'password_protected_password',
119 __( 'New Password', 'password-protected' ),
120 array( $this, 'password_protected_password_field' ),
121 $this->options_group,
122 'password_protected'
123 );
124
125 add_settings_field(
126 'password_protected_allowed_ip_addresses',
127 __( 'Allow IP Addresses', 'password-protected' ),
128 array( $this, 'password_protected_allowed_ip_addresses_field' ),
129 $this->options_group,
130 'password_protected'
131 );
132
133 add_settings_field(
134 'password_protected_remember_me',
135 __( 'Allow Remember me', 'password-protected' ),
136 array( $this, 'password_protected_remember_me_field' ),
137 $this->options_group,
138 'password_protected'
139 );
140
141 add_settings_field(
142 'password_protected_remember_me_lifetime',
143 __( 'Remember for this many days', 'password-protected' ),
144 array( $this, 'password_protected_remember_me_lifetime_field' ),
145 $this->options_group,
146 'password_protected'
147 );
148
149 register_setting( $this->options_group, 'password_protected_status', 'intval' );
150 register_setting( $this->options_group, 'password_protected_feeds', 'intval' );
151 register_setting( $this->options_group, 'password_protected_rest', 'intval' );
152 register_setting( $this->options_group, 'password_protected_administrators', 'intval' );
153 register_setting( $this->options_group, 'password_protected_users', 'intval' );
154 register_setting( $this->options_group, 'password_protected_password', array( $this, 'sanitize_password_protected_password' ) );
155 register_setting( $this->options_group, 'password_protected_allowed_ip_addresses', array( $this, 'sanitize_ip_addresses' ) );
156 register_setting( $this->options_group, 'password_protected_remember_me', 'boolval' );
157 register_setting( $this->options_group, 'password_protected_remember_me_lifetime', 'intval' );
158
159 }
160
161 /**
162 * Sanitize Password Field Input
163 *
164 * @param string $val Password.
165 * @return string Sanitized password.
166 */
167 public function sanitize_password_protected_password( $val ) {
168
169 $old_val = get_option( 'password_protected_password' );
170
171 if ( is_array( $val ) ) {
172 if ( empty( $val['new'] ) ) {
173 return $old_val;
174 } elseif ( empty( $val['confirm'] ) ) {
175 add_settings_error( 'password_protected_password', 'password_protected_password', __( 'New password not saved. When setting a new password please enter it in both fields.', 'password-protected' ) );
176 return $old_val;
177 } elseif ( $val['new'] != $val['confirm'] ) {
178 add_settings_error( 'password_protected_password', 'password_protected_password', __( 'New password not saved. Password fields did not match.', 'password-protected' ) );
179 return $old_val;
180 } elseif ( $val['new'] == $val['confirm'] ) {
181 add_settings_error( 'password_protected_password', 'password_protected_password', __( 'New password saved.', 'password-protected' ), 'updated' );
182 return $val['new'];
183 }
184 return get_option( 'password_protected_password' );
185 }
186
187 return $val;
188
189 }
190
191 /**
192 * Sanitize IP Addresses
193 *
194 * @param string $val IP addresses.
195 * @return string Sanitized IP addresses.
196 */
197 public function sanitize_ip_addresses( $val ) {
198
199 $ip_addresses = explode( "\n", $val );
200 $ip_addresses = array_map( 'sanitize_text_field', $ip_addresses );
201 $ip_addresses = array_map( 'trim', $ip_addresses );
202 $ip_addresses = array_map( array( $this, 'validate_ip_address' ), $ip_addresses );
203 $ip_addresses = array_filter( $ip_addresses );
204
205 $val = implode( "\n", $ip_addresses );
206
207 return $val;
208
209 }
210
211 /**
212 * Validate IP Address
213 *
214 * @param string $ip_address IP Address.
215 * @return string Validated IP Address.
216 */
217 private function validate_ip_address( $ip_address ) {
218
219 return filter_var( $ip_address, FILTER_VALIDATE_IP );
220
221 }
222
223 /**
224 * Password Protected Section
225 */
226 public function password_protected_settings_section() {
227
228 echo '<p>' . __( 'Password protect your web site. Users will be asked to enter a password to view the site.', 'password-protected' ) . '<br />
229 ' . __( 'For more information about Password Protected settings, view the "Help" tab at the top of this page.', 'password-protected' ) . '</p>';
230
231 }
232
233 /**
234 * Password Protection Status Field
235 */
236 public function password_protected_status_field() {
237
238 echo '<label><input name="password_protected_status" id="password_protected_status" type="checkbox" value="1" ' . checked( 1, get_option( 'password_protected_status' ), false ) . ' /> ' . __( 'Enabled', 'password-protected' ) . '</label>';
239
240 }
241
242 /**
243 * Password Protection Permissions Field
244 */
245 public function password_protected_permissions_field() {
246
247 echo '<label><input name="password_protected_administrators" id="password_protected_administrators" type="checkbox" value="1" ' . checked( 1, get_option( 'password_protected_administrators' ), false ) . ' /> ' . __( 'Allow Administrators', 'password-protected' ) . '</label>';
248 echo '<label><input name="password_protected_users" id="password_protected_users" type="checkbox" value="1" ' . checked( 1, get_option( 'password_protected_users' ), false ) . ' style="margin-left: 20px;" /> ' . __( 'Allow Logged In Users', 'password-protected' ) . '</label>';
249 echo '<label><input name="password_protected_feeds" id="password_protected_feeds" type="checkbox" value="1" ' . checked( 1, get_option( 'password_protected_feeds' ), false ) . ' style="margin-left: 20px;" /> ' . __( 'Allow RSS Feeds', 'password-protected' ) . '</label>';
250 echo '<label><input name="password_protected_rest" id="password_protected_rest" type="checkbox" value="1" ' . checked( 1, get_option( 'password_protected_rest' ), false ) . ' style="margin-left: 20px;" /> ' . __( 'Allow REST API Access', 'password-protected' ) . '</label>';
251
252 }
253
254 /**
255 * Password Field
256 */
257 public function password_protected_password_field() {
258
259 echo '<input type="password" name="password_protected_password[new]" id="password_protected_password_new" size="16" value="" autocomplete="off"> <span class="description">' . __( 'If you would like to change the password type a new one. Otherwise leave this blank.', 'password-protected' ) . '</span><br>
260 <input type="password" name="password_protected_password[confirm]" id="password_protected_password_confirm" size="16" value="" autocomplete="off"> <span class="description">' . __( 'Type your new password again.', 'password-protected' ) . '</span>';
261
262 }
263
264 /**
265 * Allowed IP Addresses Field
266 */
267 public function password_protected_allowed_ip_addresses_field() {
268
269 echo '<textarea name="password_protected_allowed_ip_addresses" id="password_protected_allowed_ip_addresses" rows="3" class="large-text" />' . get_option( 'password_protected_allowed_ip_addresses' ) . '</textarea>';
270 echo '<p class="description">' . esc_html__( 'Enter one IP address per line.', 'password-protected' ) . ' ' . esc_html( sprintf( __( 'Your IP is address %s.', 'password-protected' ), $_SERVER['REMOTE_ADDR'] ) ) . '</p>';
271
272 }
273
274 /**
275 * Remember Me Field
276 */
277 public function password_protected_remember_me_field() {
278
279 echo '<label><input name="password_protected_remember_me" id="password_protected_remember_me" type="checkbox" value="1" ' . checked( 1, get_option( 'password_protected_remember_me' ), false ) . ' /></label>';
280
281 }
282
283 /**
284 * Remember Me lifetime field
285 */
286 public function password_protected_remember_me_lifetime_field() {
287
288 echo '<label><input name="password_protected_remember_me_lifetime" id="password_protected_remember_me_lifetime" type="number" value="' . get_option( 'password_protected_remember_me_lifetime', 14 ) . '" /></label>';
289
290 }
291
292 /**
293 * Pre-update 'password_protected_password' Option
294 *
295 * Before the password is saved, MD5 it!
296 * Doing it in this way allows developers to intercept with an earlier filter if they
297 * need to do something with the plaintext password.
298 *
299 * @param string $newvalue New Value.
300 * @param string $oldvalue Old Value.
301 * @return string Filtered new value.
302 */
303 public function pre_update_option_password_protected_password( $newvalue, $oldvalue ) {
304
305 global $Password_Protected;
306
307 if ( $newvalue != $oldvalue ) {
308 $newvalue = $Password_Protected->encrypt_password( $newvalue );
309 }
310
311 return $newvalue;
312
313 }
314
315 /**
316 * Plugin Row Meta
317 *
318 * Adds GitHub and translate links below the plugin description on the plugins page.
319 *
320 * @param array $plugin_meta Plugin meta display array.
321 * @param string $plugin_file Plugin reference.
322 * @param array $plugin_data Plugin data.
323 * @param string $status Plugin status.
324 * @return array Plugin meta array.
325 */
326 public function plugin_row_meta( $plugin_meta, $plugin_file, $plugin_data, $status ) {
327
328 if ( 'password-protected/password-protected.php' == $plugin_file ) {
329 $plugin_meta[] = sprintf( '<a href="%s">%s</a>', __( 'http://github.com/benhuson/password-protected', 'password-protected' ), __( 'GitHub', 'password-protected' ) );
330 $plugin_meta[] = sprintf( '<a href="%s">%s</a>', __( 'https://translate.wordpress.org/projects/wp-plugins/password-protected', 'password-protected' ), __( 'Translate', 'password-protected' ) );
331 }
332
333 return $plugin_meta;
334
335 }
336
337 /**
338 * Plugin Action Links
339 *
340 * Adds settings link on the plugins page.
341 *
342 * @param array $actions Plugin action links array.
343 * @return array Plugin action links array.
344 */
345 public function plugin_action_links( $actions ) {
346
347 $actions[] = sprintf( '<a href="%s">%s</a>', admin_url( 'options-general.php?page=password-protected' ), __( 'Settings', 'password-protected' ) );
348 return $actions;
349
350 }
351
352 /**
353 * Password Admin Notice
354 * Warns the user if they have enabled password protection but not entered a password
355 */
356 public function password_protected_admin_notices() {
357
358 global $Password_Protected;
359
360 // Check Support
361 $screens = $this->plugin_screen_ids( array( 'dashboard', 'plugins' ) );
362 if ( $this->is_current_screen( $screens ) ) {
363 $supported = $Password_Protected->is_plugin_supported();
364 if ( is_wp_error( $supported ) ) {
365 echo $this->admin_error_display( $supported->get_error_message( $supported->get_error_code() ) );
366 }
367 }
368
369 // Settings
370 if ( $this->is_current_screen( $this->plugin_screen_ids() ) ) {
371 $status = get_option( 'password_protected_status' );
372 $pwd = get_option( 'password_protected_password' );
373
374 if ( (bool) $status && empty( $pwd ) ) {
375 echo $this->admin_error_display( __( 'You have enabled password protection but not yet set a password. Please set one below.', 'password-protected' ) );
376 }
377
378 if ( current_user_can( 'manage_options' ) && ( (bool) get_option( 'password_protected_administrators' ) || (bool) get_option( 'password_protected_users' ) ) ) {
379 if ( (bool) get_option( 'password_protected_administrators' ) && (bool) get_option( 'password_protected_users' ) ) {
380 echo $this->admin_error_display( __( 'You have enabled password protection and allowed administrators and logged in users - other users will still need to enter a password to view the site.', 'password-protected' ) );
381 } elseif ( (bool) get_option( 'password_protected_administrators' ) ) {
382 echo $this->admin_error_display( __( 'You have enabled password protection and allowed administrators - other users will still need to enter a password to view the site.', 'password-protected' ) );
383 } elseif ( (bool) get_option( 'password_protected_users' ) ) {
384 echo $this->admin_error_display( __( 'You have enabled password protection and allowed logged in users - other users will still need to enter a password to view the site.', 'password-protected' ) );
385 }
386 }
387
388 }
389
390 }
391
392 /**
393 * Admin Error Display
394 *
395 * Returns a string wrapped in HTML to display an admin error.
396 *
397 * @param string $string Error string.
398 * @return string HTML error.
399 */
400 private function admin_error_display( $string ) {
401
402 return '<div class="error"><p>' . $string . '</p></div>';
403
404 }
405
406 /**
407 * Is Current Screen
408 *
409 * Checks wether the admin is displaying a specific screen.
410 *
411 * @param string|array $screen_id Admin screen ID(s).
412 * @return boolean
413 */
414 public function is_current_screen( $screen_id ) {
415
416 if ( function_exists( 'get_current_screen' ) ) {
417 $current_screen = get_current_screen();
418 if ( ! is_array( $screen_id ) ) {
419 $screen_id = array( $screen_id );
420 }
421 if ( in_array( $current_screen->id, $screen_id ) ) {
422 return true;
423 }
424 }
425
426 return false;
427
428 }
429
430 /**
431 * Plugin Screen IDs
432 *
433 * @param string|array $screen_id Additional screen IDs to add to the returned array.
434 * @return array Screen IDs.
435 */
436 public function plugin_screen_ids( $screen_id = '' ) {
437
438 $screen_ids = array( 'options-' . $this->options_group, 'settings_page_' . $this->options_group );
439
440 if ( ! empty( $screen_id ) ) {
441 if ( is_array( $screen_id ) ) {
442 $screen_ids = array_merge( $screen_ids, $screen_id );
443 } else {
444 $screen_ids[] = $screen_id;
445 }
446 }
447
448 return $screen_ids;
449
450 }
451
452 }
453