| @@ -45,17 +45,35 @@ | ||
| 45 | 45 | */ |
| 46 | 46 | public $message = ''; |
| 47 | 47 | |
| 48 | 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 | + /** | |
| 49 | 65 | * Check if the model is activated. |
| 50 | 66 | * |
| 51 | 67 | * @return bool |
| 52 | 68 | */ |
| 53 | 69 | public function is_active(): bool { |
| 54 | - return (bool) apply_filters( | |
| 70 | + $enable = apply_filters( | |
| 55 | 71 | 'wd_strong_password_enable', |
| 56 | 72 | $this->enabled && count( $this->user_roles ) > 0 |
| 57 | 73 | ); |
| 74 | + | |
| 75 | + return is_bool( $enable ) ? $enable : (bool) $enable; | |
| 58 | 76 | } |
| 59 | 77 | |
| 60 | 78 | /** |
| 61 | 79 | * Initializes the object before loading. |
| @@ -64,8 +82,15 @@ | ||
| 64 | 82 | */ |
| 65 | 83 | protected function before_load(): void { |
| 66 | 84 | $this->user_roles = $this->get_default_roles(); |
| 67 | 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 | + } | |
| 68 | 93 | } |
| 69 | 94 | |
| 70 | 95 | /** |
| 71 | 96 | * Get applicable user roles. |
| @@ -83,9 +108,9 @@ | ||
| 83 | 108 | * |
| 84 | 109 | * @return string |
| 85 | 110 | */ |
| 86 | 111 | public function get_message(): string { |
| 87 | - if ( empty( $this->message ) ) { | |
| 112 | + if ( '' === $this->message ) { | |
| 88 | 113 | return $this->get_default_message(); |
| 89 | 114 | } |
| 90 | 115 | return $this->message; |
| 91 | 116 | } |
| @@ -93,10 +118,67 @@ | ||
| 93 | 118 | /** |
| 94 | 119 | * Get localized default message. |
| 95 | 120 | */ |
| 96 | 121 | protected function get_default_message(): string { |
| 97 | - return __( | |
| 122 | + return esc_html__( | |
| 98 | 123 | "You are required to change your password because your password doesn't meet the strong password guidelines set by the administrator.", |
| 99 | 124 | 'defender-security' |
| 100 | 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; | |
| 101 | 183 | } |
| 102 | 184 | } |