| 1 |
<?php |
| 2 |
|
| 3 |
class easy_basic_authentication_form_class { |
| 4 |
|
| 5 |
const FORM_FIELD = array( |
| 6 |
'0' => array( |
| 7 |
'id' => 'basic-auth-plugin-admin-enable', |
| 8 |
'title' => 'Enable for wp-admin', |
| 9 |
'callback' => 'basic_auth_plugin_admin_enable_cb', |
| 10 |
), |
| 11 |
'1' => array( |
| 12 |
'id' => 'basic-auth-plugin-enable', |
| 13 |
'title' => 'Enable for the entire site (only if wp-admin is enabled)', |
| 14 |
'callback' => 'basic_auth_plugin_enable_cb', |
| 15 |
), |
| 16 |
'2' => array( |
| 17 |
'id' => 'basic-auth-plugin-username', |
| 18 |
'title' => 'Username', |
| 19 |
'callback' => 'basic_auth_plugin_username_cb', |
| 20 |
), |
| 21 |
'3' => array( |
| 22 |
'id' => 'basic-auth-plugin-password', |
| 23 |
'title' => 'Password', |
| 24 |
'callback' => 'basic_auth_plugin_password_cb', |
| 25 |
), |
| 26 |
'4' => array( |
| 27 |
'id' => 'basic-auth-plugin-admin-log-enable', |
| 28 |
'title' => 'Enable access logs', |
| 29 |
'callback' => 'basic_auth_plugin_admin_log_enable_cb', |
| 30 |
), |
| 31 |
'5' => array( |
| 32 |
'id' => 'basic-auth-plugin-alert-enable', |
| 33 |
'title' => 'Enable email alert', |
| 34 |
'callback' => 'basic_auth_plugin_alert_enable_cb', |
| 35 |
), |
| 36 |
'6' => array( |
| 37 |
'id' => 'basic-auth-plugin-email-alert', |
| 38 |
'title' => 'Email for alert', |
| 39 |
'callback' => 'basic_auth_plugin_alertemail_cb', |
| 40 |
) |
| 41 |
); |
| 42 |
|
| 43 |
public function __construct() |
| 44 |
{ |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
public function basic_auth_plugin_settings_init() { |
| 49 |
register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_admin_enable' ); |
| 50 |
register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_enable' ); |
| 51 |
register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_username' ); |
| 52 |
register_setting( 'basic-auth-plugin-settings', 'basic_auth_plugin_admin_log_enable' ); |
| 53 |
|
| 54 |
add_settings_section( |
| 55 |
'basic-auth-plugin-section', |
| 56 |
__('Configurations for Easy Basic Authentication', 'easy-basic-authentication'), |
| 57 |
array($this,'basic_auth_plugin_section_cb'), |
| 58 |
'basic-auth-plugin-settings' |
| 59 |
); |
| 60 |
|
| 61 |
foreach(self::FORM_FIELD as $field) { |
| 62 |
add_settings_field( |
| 63 |
$field['id'], |
| 64 |
__($field['title'], 'easy-basic-authentication'), |
| 65 |
array($this,$field['callback']), |
| 66 |
'basic-auth-plugin-settings', |
| 67 |
'basic-auth-plugin-section' |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
public function basic_auth_plugin_section_cb() { |
| 74 |
echo __('Configure basic authentication', 'easy-basic-authentication'); |
| 75 |
} |
| 76 |
|
| 77 |
public function basic_auth_plugin_enable_cb() { |
| 78 |
$admin_enable = get_option( 'basic_auth_plugin_admin_enable' ); |
| 79 |
$enable = get_option( 'basic_auth_plugin_enable' ); |
| 80 |
?> |
| 81 |
<input type="checkbox" name="basic_auth_plugin_enable" value="1" <?php checked( $enable, 1 ); ?><?php disabled( !$admin_enable ); ?>> |
| 82 |
<?php |
| 83 |
} |
| 84 |
|
| 85 |
public function basic_auth_plugin_admin_enable_cb() { |
| 86 |
$enable = get_option( 'basic_auth_plugin_admin_enable' ); |
| 87 |
?> |
| 88 |
<input type="checkbox" name="basic_auth_plugin_admin_enable" value="1" <?php checked( $enable, 1 ); ?>> |
| 89 |
<?php |
| 90 |
} |
| 91 |
|
| 92 |
public function basic_auth_plugin_username_cb() { |
| 93 |
$username = get_option( 'basic_auth_plugin_username' ); |
| 94 |
?> |
| 95 |
<input type="text" name="basic_auth_plugin_username" value="<?php echo esc_attr( $username ); ?>"> |
| 96 |
<?php |
| 97 |
} |
| 98 |
|
| 99 |
public function basic_auth_plugin_password_cb() { |
| 100 |
$password = get_option( 'basic_auth_plugin_password' ) |
| 101 |
? __('Password entered', 'easy-basic-authentication') |
| 102 |
: __('Enter the password', 'easy-basic-authentication'); |
| 103 |
?> |
| 104 |
<input type="password" name="basic_auth_plugin_password" value="" placeholder="<?php echo $password; ?>"> |
| 105 |
<?php |
| 106 |
} |
| 107 |
|
| 108 |
public function basic_auth_plugin_admin_log_enable_cb() { |
| 109 |
$enable = get_option( 'basic_auth_plugin_admin_log_enable' ); |
| 110 |
?> |
| 111 |
<input type="checkbox" name="basic_auth_plugin_admin_log_enable" value="1" <?php checked( $enable, 1 ); ?>> |
| 112 |
<?php |
| 113 |
} |
| 114 |
|
| 115 |
public function basic_auth_plugin_alert_enable_cb() { |
| 116 |
$enable = get_option( 'basic_auth_plugin_alert_enable' ); |
| 117 |
?> |
| 118 |
<input type="checkbox" name="basic_auth_plugin_alert_enable" value="1" <?php checked( $enable, 1 ); ?>> |
| 119 |
<?php |
| 120 |
} |
| 121 |
|
| 122 |
public function basic_auth_plugin_alertemail_cb() { |
| 123 |
$email_alert = get_option( 'basic_auth_plugin_alertemail' ); |
| 124 |
?> |
| 125 |
<input type="text" name="basic_auth_plugin_alertemail" value="<?php echo esc_attr( $email_alert ); ?>" placeholder="<?php echo __('Enter the email', 'easy-basic-authentication'); ?>"> |
| 126 |
<?php |
| 127 |
} |
| 128 |
|
| 129 |
public function basic_auth_plugin_save_settings($param) { |
| 130 |
if ( isset( $param['eba_submit'] ) ) { |
| 131 |
$admin_enable = isset( $param['basic_auth_plugin_admin_enable'] ) ? 1 : 0; |
| 132 |
$enable = isset( $param['basic_auth_plugin_enable'] ) ? 1 : 0; |
| 133 |
$username = sanitize_text_field( $param['basic_auth_plugin_username'] ); |
| 134 |
$password = sanitize_text_field( $param['basic_auth_plugin_password'] ); |
| 135 |
$log_enable = isset( $param['basic_auth_plugin_admin_log_enable'] ) ? 1 : 0; |
| 136 |
$alert_enable = isset( $param['basic_auth_plugin_alert_enable'] ) ? 1 : 0; |
| 137 |
$alert_email = sanitize_text_field( $param['basic_auth_plugin_alertemail'] ); |
| 138 |
|
| 139 |
if ( !is_email( $alert_email ) ) { |
| 140 |
add_settings_error( |
| 141 |
'basic_auth_plugin_alertemail', |
| 142 |
'basic_auth_plugin_alertemail_error', |
| 143 |
__('Invalid email address', 'easy-basic-authentication'), |
| 144 |
'error' |
| 145 |
); |
| 146 |
} else { |
| 147 |
update_option( 'basic_auth_plugin_alertemail', $alert_email ); |
| 148 |
} |
| 149 |
|
| 150 |
update_option( 'basic_auth_plugin_admin_enable', $admin_enable ); |
| 151 |
update_option( 'basic_auth_plugin_enable', $enable ); |
| 152 |
update_option( 'basic_auth_plugin_username', $username ); |
| 153 |
update_option( 'basic_auth_plugin_admin_log_enable', $log_enable ); |
| 154 |
update_option( 'basic_auth_plugin_alert_enable', $alert_enable ); |
| 155 |
|
| 156 |
if ( ! empty( $password ) ) { |
| 157 |
$hashed_password = wp_hash_password( $password ); |
| 158 |
update_option( 'basic_auth_plugin_password', $hashed_password ); |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
public function basic_auth_plugin_settings_page() { |
| 164 |
include plugin_dir_path( __FILE__ ) . '../template/settings_page.php'; |
| 165 |
} |
| 166 |
} |