| 1 |
<?php |
| 2 |
|
| 3 |
class Password_Protected_Admin { |
| 4 |
|
| 5 |
/** |
| 6 |
* Constructor |
| 7 |
*/ |
| 8 |
function Password_Protected_Admin() { |
| 9 |
add_action( 'admin_init', array( $this, 'privacy_settings' ) ); |
| 10 |
add_action( 'admin_notices', array( $this, 'password_protected_admin_notices' ) ); |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Settings API |
| 15 |
*/ |
| 16 |
function privacy_settings() { |
| 17 |
add_settings_section( |
| 18 |
'password_protected', |
| 19 |
'Password Protected Settings', |
| 20 |
array( $this, 'password_protected_settings_section' ), |
| 21 |
'privacy' |
| 22 |
); |
| 23 |
add_settings_field( |
| 24 |
'password_protected_status', |
| 25 |
'Password Protection Status', |
| 26 |
array( $this, 'password_protected_status_field' ), |
| 27 |
'privacy', |
| 28 |
'password_protected' |
| 29 |
); |
| 30 |
add_settings_field( |
| 31 |
'password_protected_password', |
| 32 |
'New Password', |
| 33 |
array( $this, 'password_protected_password_field' ), |
| 34 |
'privacy', |
| 35 |
'password_protected' |
| 36 |
); |
| 37 |
register_setting( 'privacy', 'password_protected_status', 'intval' ); |
| 38 |
register_setting( 'privacy', 'password_protected_password', array( $this, 'sanitize_password_protected_password' ) ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Sanitize Password Field Input |
| 43 |
*/ |
| 44 |
function sanitize_password_protected_password( $val ) { |
| 45 |
$old_val = get_option( 'password_protected_password' ); |
| 46 |
if ( is_array( $val ) ) { |
| 47 |
if ( empty( $val['new'] ) ) { |
| 48 |
return $old_val; |
| 49 |
} elseif ( empty( $val['confirm'] ) ) { |
| 50 |
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' ) ); |
| 51 |
return $old_val; |
| 52 |
} elseif ( $val['new'] != $val['confirm'] ) { |
| 53 |
add_settings_error( 'password_protected_password', 'password_protected_password', __( 'New password not saved. Password fields did not match.', 'password_protected' ) ); |
| 54 |
return $old_val; |
| 55 |
} elseif ( $val['new'] == $val['confirm'] ) { |
| 56 |
add_settings_error( 'password_protected_password', 'password_protected_password', __( 'New password saved.', 'password_protected' ), 'updated' ); |
| 57 |
return md5( $val['new'] ); |
| 58 |
} |
| 59 |
return get_option( 'password_protected_password' ); |
| 60 |
} |
| 61 |
return $val; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Password Protected Section |
| 66 |
*/ |
| 67 |
function password_protected_settings_section() { |
| 68 |
echo '<p>' . __( 'Password protect your web site. Users will be asked to enter a password to view the site.', 'password_protected' ) . '</p>'; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Password Protection Status Field |
| 73 |
*/ |
| 74 |
function password_protected_status_field() { |
| 75 |
echo '<input name="password_protected_status" id="password_protected_status" type="checkbox" value="1" ' . checked( 1, get_option( 'password_protected_status' ), false ) . ' /> ' . __( 'Enabled', 'password_protected' ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Password Field |
| 80 |
*/ |
| 81 |
function password_protected_password_field() { |
| 82 |
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> |
| 83 |
<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>'; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Password Admin Notice |
| 88 |
* Warns the user if they have enabled password protection but not entered a password |
| 89 |
*/ |
| 90 |
function password_protected_admin_notices(){ |
| 91 |
global $current_screen; |
| 92 |
if ( $current_screen->id == 'options-privacy' ) { |
| 93 |
$status = get_option( 'password_protected_status' ); |
| 94 |
$pwd = get_option( 'password_protected_password' ); |
| 95 |
if ( (bool) $status && empty( $pwd ) ) { |
| 96 |
echo '<div class="error"><p>' . __( 'You have enabled password protection but not yet set a password. Please set one below.', 'password_protected' ) . '</p></div>'; |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
?> |