| @@ -6,8 +6,9 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace WP_Defender\Controller; |
| 9 | 9 | |
| 10 | +use Countable; | |
| 10 | 11 | use WP_Defender\Event; |
| 11 | 12 | use Calotes\Component\Request; |
| 12 | 13 | use Calotes\Component\Response; |
| 13 | 14 | use WP_Defender\Integrations\Woocommerce; |
| @@ -55,17 +56,11 @@ | ||
| 55 | 56 | $this->model = wd_di()->get( Settings::class ); |
| 56 | 57 | $this->service = wd_di()->get( Service::class ); |
| 57 | 58 | $this->helper = wd_di()->get( Password_Protection::class ); |
| 58 | 59 | $this->woo = wd_di()->get( Woocommerce::class ); |
| 59 | - add_filter( 'wp_defender_advanced_tools_data', array( $this, 'script_data' ) ); | |
| 60 | 60 | $this->register_routes(); |
| 61 | 61 | if ( $this->model->is_active() ) { |
| 62 | - if ( ! wp_using_themes() ) { | |
| 63 | - $this->setup_core_hooks(); | |
| 64 | - } | |
| 65 | - if ( $this->woo->is_activated() ) { | |
| 66 | - add_action( 'woocommerce_init', array( $this, 'setup_woo_hooks' ) ); | |
| 67 | - } | |
| 62 | + $this->setup_hooks(); | |
| 68 | 63 | } |
| 69 | 64 | } |
| 70 | 65 | |
| 71 | 66 | /** |
| @@ -71,17 +66,9 @@ | ||
| 71 | 66 | /** |
| 72 | 67 | * Set up core WordPress hooks for strong password functionality. |
| 73 | 68 | * These hooks work independently of WooCommerce settings. |
| 74 | 69 | */ |
| 75 | - private function setup_core_hooks() { | |
| 76 | - // Update site url on sub-site when MaskLogin is disabled. | |
| 77 | - if ( | |
| 78 | - is_multisite() && ! is_main_site() | |
| 79 | - && ! wd_di()->get( \WP_Defender\Model\Setting\Mask_Login::class )->is_active() | |
| 80 | - ) { | |
| 81 | - add_filter( 'network_site_url', array( $this->helper, 'filter_site_url' ), 100, 2 ); | |
| 82 | - } | |
| 83 | - | |
| 70 | + private function setup_hooks() { | |
| 84 | 71 | // Remove conflicting plugins. |
| 85 | 72 | remove_action( 'user_profile_update_errors', 'slt_fsp_validate_profile_update', 0 ); |
| 86 | 73 | remove_action( 'resetpass_form', 'slt_fsp_validate_resetpass_form', 10 ); |
| 87 | 74 | |
| @@ -95,68 +82,54 @@ | ||
| 95 | 82 | add_action( 'login_enqueue_scripts', array( $this->service, 'scripts' ) ); |
| 96 | 83 | add_action( 'validate_password_reset', array( $this->service, 'on_password_reset' ), 100, 2 ); |
| 97 | 84 | add_action( 'wp_authenticate_user', array( $this->service, 'during_core_authentication' ), 100, 2 ); |
| 98 | 85 | add_filter( 'random_password', array( $this->service, 'generate_password' ), 10, 2 ); |
| 99 | - add_filter( 'lostpassword_url', array( $this->service, 'lostpassword_url' ), 10, 1 ); | |
| 100 | - } | |
| 101 | 86 | |
| 102 | - /** | |
| 103 | - * Add hooks for handling strong password in WooCommerce forms. | |
| 104 | - */ | |
| 105 | - public function setup_woo_hooks() { | |
| 106 | - $woo_locations = $this->model->forms['woocommerce'] ?? array(); | |
| 87 | + // WooCommerce-specific hooks — each gated on its own form setting. | |
| 88 | + if ( | |
| 89 | + $this->woo->is_activated() && | |
| 90 | + isset( $this->model->plugins['woocommerce'] ) && | |
| 91 | + $this->model->plugins['woocommerce'] | |
| 92 | + ) { | |
| 93 | + $woo_forms = $this->model->forms['woocommerce'] ?? array(); | |
| 107 | 94 | |
| 108 | - if ( array() === $woo_locations ) { | |
| 109 | - return; | |
| 110 | - } | |
| 95 | + if ( array() !== $woo_forms ) { | |
| 96 | + add_action( 'wp_enqueue_scripts', array( $this->service, 'scripts' ) ); | |
| 97 | + add_action( | |
| 98 | + 'woocommerce_save_account_details_errors', | |
| 99 | + array( $this->service, 'on_woo_account_update' ), | |
| 100 | + 100, | |
| 101 | + 2 | |
| 102 | + ); | |
| 103 | + } | |
| 111 | 104 | |
| 112 | - if ( in_array( Woocommerce::WOO_LOGIN_FORM, $woo_locations, true ) ) { | |
| 113 | - add_filter( 'woocommerce_process_login_errors', array( $this->service, 'during_woo_authentication' ), 10, 3 ); | |
| 114 | - } | |
| 105 | + if ( in_array( Woocommerce::WOO_REGISTER_FORM, $woo_forms, true ) ) { | |
| 106 | + add_filter( | |
| 107 | + 'woocommerce_process_registration_errors', | |
| 108 | + array( $this->service, 'during_woo_registration' ), | |
| 109 | + 100 | |
| 110 | + ); | |
| 111 | + } | |
| 115 | 112 | |
| 116 | - $referer = wp_get_referer(); | |
| 117 | - $add_woo_reset_password_hooks = false; | |
| 118 | - if ( $referer && function_exists( 'wc_get_page_id' ) ) { | |
| 119 | - $my_account_page_id = wc_get_page_id( 'myaccount' ); | |
| 120 | - if ( $my_account_page_id > 0 ) { | |
| 121 | - $my_account_url = get_permalink( $my_account_page_id ); | |
| 122 | - $add_woo_reset_password_hooks = strpos( $referer, $my_account_url ) === 0; | |
| 113 | + if ( in_array( Woocommerce::WOO_LOGIN_FORM, $woo_forms, true ) || in_array( Woocommerce::WOO_CHECKOUT_FORM, $woo_forms, true ) ) { | |
| 114 | + add_filter( | |
| 115 | + 'woocommerce_process_login_errors', | |
| 116 | + array( $this->service, 'during_woo_authentication' ), | |
| 117 | + 100, | |
| 118 | + 3 | |
| 119 | + ); | |
| 123 | 120 | } |
| 124 | - } | |
| 125 | 121 | |
| 126 | - if ( | |
| 127 | - in_array( Woocommerce::WOO_LOST_PASSWORD_FORM, $woo_locations, true ) || | |
| 128 | - $add_woo_reset_password_hooks | |
| 129 | - ) { | |
| 130 | - add_action( 'wp_enqueue_scripts', array( $this->service, 'scripts' ) ); | |
| 131 | - add_action( 'validate_password_reset', array( $this->service, 'on_password_reset' ), 100, 2 ); | |
| 132 | - add_filter( 'woocommerce_reset_password_message', array( $this->service, 'add_woocommerce_error_message' ) ); | |
| 133 | - add_action( 'woocommerce_save_account_details_errors', array( $this->service, 'on_woo_account_update' ), 10, 2 ); | |
| 122 | + if ( in_array( Woocommerce::WOO_LOST_PASSWORD_FORM, $woo_forms, true ) ) { | |
| 123 | + add_filter( | |
| 124 | + 'woocommerce_reset_password_message', | |
| 125 | + array( $this->service, 'add_woocommerce_error_message' ) | |
| 126 | + ); | |
| 127 | + } | |
| 134 | 128 | } |
| 135 | - | |
| 136 | - if ( | |
| 137 | - in_array( Woocommerce::WOO_REGISTER_FORM, $woo_locations, true ) || | |
| 138 | - in_array( Woocommerce::WOO_CHECKOUT_FORM, $woo_locations, true ) | |
| 139 | - ) { | |
| 140 | - add_action( 'wp_enqueue_scripts', array( $this->service, 'scripts' ) ); | |
| 141 | - add_filter( 'woocommerce_registration_errors', array( $this->service, 'during_woo_registration' ) ); | |
| 142 | - } | |
| 143 | 129 | } |
| 144 | 130 | |
| 145 | 131 | /** |
| 146 | - * Provide data to the frontend via localized script. | |
| 147 | - * | |
| 148 | - * @param array $data Data collection is ready to passed. | |
| 149 | - * | |
| 150 | - * @return array Modified data array with added this controller data. | |
| 151 | - */ | |
| 152 | - public function script_data( array $data ): array { | |
| 153 | - $data['strong_password'] = $this->data_frontend(); | |
| 154 | - | |
| 155 | - return $data; | |
| 156 | - } | |
| 157 | - | |
| 158 | - /** | |
| 159 | 132 | * All the variables that we will show on frontend, both in the main page, or dashboard widget. |
| 160 | 133 | * |
| 161 | 134 | * @return array |
| 162 | 135 | */ |
| @@ -249,10 +222,8 @@ | ||
| 249 | 222 | /** |
| 250 | 223 | * Import the data of other source into this, it can be when HUB trigger the import, or user apply a preset. |
| 251 | 224 | * |
| 252 | 225 | * @param array $data Data from other source. |
| 253 | - * | |
| 254 | - * @return null|void | |
| 255 | 226 | */ |
| 256 | 227 | public function import_data( array $data ) { |
| 257 | 228 | $this->model->import( $data ); |
| 258 | 229 | if ( $this->model->validate() ) { |
| @@ -261,10 +232,8 @@ | ||
| 261 | 232 | } |
| 262 | 233 | |
| 263 | 234 | /** |
| 264 | 235 | * Remove all settings, configs generated in this container runtime. |
| 265 | - * | |
| 266 | - * @return mixed | |
| 267 | 236 | */ |
| 268 | 237 | public function remove_settings() { |
| 269 | 238 | } |
| 270 | 239 | |
| @@ -280,8 +249,33 @@ | ||
| 280 | 249 | */ |
| 281 | 250 | public function export_strings(): array { |
| 282 | 251 | return array( |
| 283 | 252 | $this->model->is_active() ? esc_html__( 'Active', 'defender-security' ) : esc_html__( 'Inactive', 'defender-security' ), |
| 253 | + ); | |
| 254 | + } | |
| 255 | + | |
| 256 | + /** | |
| 257 | + * Generates configuration strings based on the provided configuration. | |
| 258 | + * | |
| 259 | + * @param mixed $config The configuration data. | |
| 260 | + * | |
| 261 | + * @return array Returns an array of configuration strings. | |
| 262 | + */ | |
| 263 | + public function config_strings( $config ): array { | |
| 264 | + $is_enabled = isset( $config['enabled'] ) ? (bool) $config['enabled'] : false; | |
| 265 | + if ( | |
| 266 | + ! $is_enabled || | |
| 267 | + ! isset( $config['user_roles'] ) || | |
| 268 | + ! is_array( $config['user_roles'] ) || | |
| 269 | + array() === $config['user_roles'] | |
| 270 | + ) { | |
| 271 | + return array( esc_html__( 'Inactive', 'defender-security' ) ); | |
| 272 | + } | |
| 273 | + | |
| 274 | + return array( | |
| 275 | + $is_enabled && ( is_array( $config['user_roles'] ) || $config['user_roles'] instanceof Countable ? count( $config['user_roles'] ) : 0 ) > 0 | |
| 276 | + ? esc_html__( 'Active', 'defender-security' ) | |
| 277 | + : esc_html__( 'Inactive', 'defender-security' ), | |
| 284 | 278 | ); |
| 285 | 279 | } |
| 286 | 280 | |
| 287 | 281 | /** |