| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handle strong password module. |
| 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 strong password module. |
| 14 |
*/ |
| 15 |
class Strong_Password extends Setting { |
| 16 |
|
| 17 |
/** |
| 18 |
* Option name. |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
protected $table = 'wd_strong_password_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 |
* Default message. |
| 42 |
* |
| 43 |
* @defender_property |
| 44 |
* @var string |
| 45 |
*/ |
| 46 |
public $message = ''; |
| 47 |
|
| 48 |
/** |
| 49 |
* Plugin integrations settings. |
| 50 |
* |
| 51 |
* @defender_property |
| 52 |
* @var array |
| 53 |
*/ |
| 54 |
public $plugins = array(); |
| 55 |
|
| 56 |
/** |
| 57 |
* Form integrations settings. |
| 58 |
* |
| 59 |
* @defender_property |
| 60 |
* @var array |
| 61 |
*/ |
| 62 |
public $forms = array(); |
| 63 |
|
| 64 |
/** |
| 65 |
* Check if the model is activated. |
| 66 |
* |
| 67 |
* @return bool |
| 68 |
*/ |
| 69 |
public function is_active(): bool { |
| 70 |
$enable = apply_filters( |
| 71 |
'wd_strong_password_enable', |
| 72 |
$this->enabled && count( $this->user_roles ) > 0 |
| 73 |
); |
| 74 |
|
| 75 |
return is_bool( $enable ) ? $enable : (bool) $enable; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Initializes the object before loading. |
| 80 |
* |
| 81 |
* @return void |
| 82 |
*/ |
| 83 |
protected function before_load(): void { |
| 84 |
$this->user_roles = $this->get_default_roles(); |
| 85 |
$this->message = $this->get_message(); |
| 86 |
// Only set defaults if not already configured. |
| 87 |
if ( 0 === count( $this->plugins ) ) { |
| 88 |
$this->plugins = $this->get_default_plugins(); |
| 89 |
} |
| 90 |
if ( 0 === count( $this->forms ) ) { |
| 91 |
$this->forms = $this->get_default_forms(); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Get applicable user roles. |
| 97 |
*/ |
| 98 |
protected function get_default_roles(): array { |
| 99 |
if ( ! function_exists( 'get_editable_roles' ) ) { |
| 100 |
return array( 'administrator' ); |
| 101 |
} |
| 102 |
|
| 103 |
return array_keys( get_editable_roles() ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Get error message to show in ui. |
| 108 |
* |
| 109 |
* @return string |
| 110 |
*/ |
| 111 |
public function get_message(): string { |
| 112 |
if ( '' === $this->message ) { |
| 113 |
return $this->get_default_message(); |
| 114 |
} |
| 115 |
return $this->message; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Get localized default message. |
| 120 |
*/ |
| 121 |
protected function get_default_message(): string { |
| 122 |
return esc_html__( |
| 123 |
"You are required to change your password because your password doesn't meet the strong password guidelines set by the administrator.", |
| 124 |
'defender-security' |
| 125 |
); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Get default plugin integrations. |
| 130 |
*/ |
| 131 |
protected function get_default_plugins(): array { |
| 132 |
return array( |
| 133 |
'woocommerce' => false, |
| 134 |
); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Get default form integrations. |
| 139 |
*/ |
| 140 |
protected function get_default_forms(): array { |
| 141 |
return array( |
| 142 |
'woocommerce' => array(), |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Check by deactivated locations. Only if the plugin is enabled. |
| 148 |
* |
| 149 |
* @return bool |
| 150 |
*/ |
| 151 |
public function is_unchecked_woo_locations(): bool { |
| 152 |
return isset( $this->plugins['woocommerce'] ) && $this->plugins['woocommerce'] && array() === $this->forms['woocommerce']; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Validates the form after submission. |
| 157 |
* |
| 158 |
* @return void |
| 159 |
*/ |
| 160 |
protected function after_validate(): void { |
| 161 |
if ( $this->is_unchecked_woo_locations() ) { |
| 162 |
$this->errors['enable_woo'] = esc_html__( |
| 163 |
"You have enabled Strong password rule for WooCommerce but you've not selected any form yet. Please select at least one form to proceed.", |
| 164 |
'defender-security' |
| 165 |
); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Export model data with proper type casting. |
| 171 |
* |
| 172 |
* @return array |
| 173 |
*/ |
| 174 |
public function export(): array { |
| 175 |
$data = parent::export(); |
| 176 |
|
| 177 |
// Cast plugin booleans. |
| 178 |
if ( isset( $data['plugins']['woocommerce'] ) ) { |
| 179 |
$data['plugins']['woocommerce'] = (bool) $data['plugins']['woocommerce']; |
| 180 |
} |
| 181 |
|
| 182 |
return $data; |
| 183 |
} |
| 184 |
} |
| 185 |
|