| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handle password protection settings. |
| 4 |
* |
| 5 |
* @package WP_Defender\Model\Setting |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Model\Setting; |
| 9 |
|
| 10 |
use Calotes\Model\Setting; |
| 11 |
|
| 12 |
/** |
| 13 |
* Model for password protection settings. |
| 14 |
*/ |
| 15 |
class Password_Protection extends Setting { |
| 16 |
|
| 17 |
/** |
| 18 |
* Option name. |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
protected $table = 'wd_password_protection_settings'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Feature status |
| 26 |
* |
| 27 |
* @defender_property |
| 28 |
* @var bool |
| 29 |
*/ |
| 30 |
public $enabled = false; |
| 31 |
|
| 32 |
/** |
| 33 |
* List of user roles. |
| 34 |
* |
| 35 |
* @defender_property |
| 36 |
* @var array |
| 37 |
*/ |
| 38 |
public $user_roles = array(); |
| 39 |
|
| 40 |
/** |
| 41 |
* List of pwned actions. |
| 42 |
* |
| 43 |
* @defender_property |
| 44 |
* @var array |
| 45 |
*/ |
| 46 |
public $pwned_actions = array(); |
| 47 |
|
| 48 |
/** |
| 49 |
* Get default values. |
| 50 |
* |
| 51 |
* @return array |
| 52 |
*/ |
| 53 |
public function get_default_values(): array { |
| 54 |
return array( |
| 55 |
'message' => esc_html__( |
| 56 |
'This password has been exposed in a data breach and can’t be used. Choose a different password.', |
| 57 |
'defender-security' |
| 58 |
), |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Initializes the object before loading. |
| 64 |
* |
| 65 |
* @return void |
| 66 |
*/ |
| 67 |
protected function before_load(): void { |
| 68 |
$default_values = $this->get_default_values(); |
| 69 |
// Default we will load all rules. |
| 70 |
if ( function_exists( 'get_editable_roles' ) ) { |
| 71 |
// We only need this inside admin, no need to load the user.php everywhere. |
| 72 |
$this->user_roles = array_keys( get_editable_roles() ); |
| 73 |
} else { |
| 74 |
// Define defaults user roles. |
| 75 |
$this->user_roles = array( 'administrator' ); |
| 76 |
} |
| 77 |
$this->pwned_actions = array( |
| 78 |
'force_change_message' => $default_values['message'], |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Define settings labels. |
| 84 |
* |
| 85 |
* @return array |
| 86 |
*/ |
| 87 |
public function labels(): array { |
| 88 |
return array( |
| 89 |
'enabled' => self::get_module_name(), |
| 90 |
'pwned_actions' => esc_html__( 'Force password change', 'defender-security' ), |
| 91 |
'user_roles' => esc_html__( 'User Roles', 'defender-security' ), |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Check if the model is activated. |
| 97 |
* |
| 98 |
* @return bool |
| 99 |
*/ |
| 100 |
public function is_active(): bool { |
| 101 |
$enable = apply_filters( |
| 102 |
'wd_password_protection_enable', |
| 103 |
$this->enabled && count( $this->user_roles ) > 0 |
| 104 |
); |
| 105 |
|
| 106 |
return is_bool( $enable ) ? $enable : (bool) $enable; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Returns the module name for Pwned Passwords. |
| 111 |
* |
| 112 |
* @return string The module name. |
| 113 |
*/ |
| 114 |
public static function get_module_name(): string { |
| 115 |
return esc_html__( 'Pwned Passwords', 'defender-security' ); |
| 116 |
} |
| 117 |
} |
| 118 |
|