| @@ -15,8 +15,9 @@ | ||
| 15 | 15 | use WP_Defender\Behavior\WPMUDEV; |
| 16 | 16 | use WP_Defender\Component\Network_Cron_Manager; |
| 17 | 17 | use WP_Defender\Model\Setting\Global_Ip_Lockout; |
| 18 | 18 | use WP_Defender\Component\Config\Config_Hub_Helper; |
| 19 | +use WP_Defender\Component\IP\Antibot_Global_Firewall; | |
| 19 | 20 | use WP_Defender\Component\IP\Global_IP as Global_IP_Component; |
| 20 | 21 | |
| 21 | 22 | /** |
| 22 | 23 | * Handles global IP lockout settings. |
| @@ -82,14 +83,15 @@ | ||
| 82 | 83 | array( $this, 'fetch_global_ip_list' ), |
| 83 | 84 | HOUR_IN_SECONDS |
| 84 | 85 | ); |
| 85 | 86 | |
| 86 | - if ( $this->service->can_blocklist_autosync() ) { | |
| 87 | - // No need to run Rate mechanism for IP lockouts because we do it in Blacklist class. | |
| 88 | - add_action( 'wd_blacklist_this_ip', array( $this, 'blacklist_an_ip' ) ); | |
| 87 | + add_action( 'init', array( $this->service, 'handle_expired_membership' ) ); | |
| 88 | + if ( $this->wpmudev->is_wpmu_hosting() ) { | |
| 89 | + // sync_state must run before can_blocklist_autosync is evaluated, so both hooks use priority 10. | |
| 90 | + add_action( 'init', array( $this, 'sync_state' ) ); | |
| 89 | 91 | } |
| 90 | - add_action( 'init', array( $this->service, 'handle_expired_membership' ) ); | |
| 91 | - | |
| 92 | + // Register after sync_state so the model reflects the current Hosting state. | |
| 93 | + add_action( 'init', array( $this, 'maybe_register_blocklist_autosync_hook' ) ); | |
| 92 | 94 | // admin_init priority 20 runs after Hub_Connector sets the transient (priority 10); |
| 93 | 95 | // the other two hooks cover async-sync cases. |
| 94 | 96 | add_action( 'admin_init', array( $this, 'maybe_hcm_connection_attempt' ), 20 ); |
| 95 | 97 | add_action( 'wpdef_hub_connector_synced', array( $this, 'maybe_hcm_connection_attempt' ) ); |
| @@ -109,11 +111,8 @@ | ||
| 109 | 111 | return; |
| 110 | 112 | } |
| 111 | 113 | |
| 112 | 114 | delete_site_transient( Hub_Connector::TRANSIENT_KEY ); |
| 113 | - | |
| 114 | - $this->model->blocklist_autosync = true; | |
| 115 | - $this->model->save(); | |
| 116 | 115 | } |
| 117 | 116 | |
| 118 | 117 | /** |
| 119 | 118 | * Save settings. |
| @@ -149,12 +148,14 @@ | ||
| 149 | 148 | $old_self_unlock = $this->model->allow_self_unlock; |
| 150 | 149 | |
| 151 | 150 | $this->model->import( $data ); |
| 152 | 151 | if ( $this->model->validate() ) { |
| 153 | - $this->model->enabled = $this->model->allow_self_unlock || $this->model->blocklist_autosync; | |
| 154 | 152 | $this->model->save(); |
| 155 | 153 | Config_Hub_Helper::set_clear_active_flag(); |
| 156 | 154 | if ( 'central_ip' === $message_type ) { |
| 155 | + if ( isset( $data['enabled'] ) && $old_enabled !== $this->model->enabled ) { | |
| 156 | + $this->service->toggle_on_hosting( $this->model->enabled ); | |
| 157 | + } | |
| 157 | 158 | $message = $this->get_update_message( $data, $old_enabled, Global_Ip_Lockout::get_module_name() ); |
| 158 | 159 | } else { |
| 159 | 160 | $message = ''; |
| 160 | 161 | if ( $data['allow_self_unlock'] ?? false ) { |
| @@ -214,9 +215,11 @@ | ||
| 214 | 215 | 'is_expired_membership' => $this->service->is_expired_membership_type(), |
| 215 | 216 | ), |
| 216 | 217 | 'hub' => array( |
| 217 | 218 | 'global_ip_list' => $this->service->get_formated_global_ip_list(), |
| 218 | - 'global_ip_setting_url' => $this->wpmudev->get_api_base_url() . 'hub2/ip-banning', | |
| 219 | + 'global_ip_setting_url' => defender_is_unlimited_hosting() | |
| 220 | + ? wd_di()->get( Antibot_Global_Firewall::class )->get_uh_site_tools_link() | |
| 221 | + : $this->wpmudev->get_api_base_url() . 'hub2/security-center/custom-ip-list', | |
| 219 | 222 | ), |
| 220 | 223 | ), |
| 221 | 224 | $this->dump_routes_and_nonces() |
| 222 | 225 | ); |
| @@ -331,6 +334,36 @@ | ||
| 331 | 334 | * @return array An array of strings. |
| 332 | 335 | */ |
| 333 | 336 | public function export_strings() { |
| 334 | 337 | return array(); |
| 338 | + } | |
| 339 | + | |
| 340 | + /** | |
| 341 | + * Register the blocklist autosync hook after sync_state has updated the model. | |
| 342 | + * | |
| 343 | + * @return void | |
| 344 | + */ | |
| 345 | + public function maybe_register_blocklist_autosync_hook(): void { | |
| 346 | + if ( $this->service->can_blocklist_autosync() ) { | |
| 347 | + // No need to run Rate mechanism for IP lockouts because we do it in Blacklist class. | |
| 348 | + add_action( 'wd_blacklist_this_ip', array( $this, 'blacklist_an_ip' ) ); | |
| 349 | + } | |
| 350 | + } | |
| 351 | + | |
| 352 | + /** | |
| 353 | + * Sync custom ip list status with Hosting. | |
| 354 | + * | |
| 355 | + * @return void | |
| 356 | + */ | |
| 357 | + public function sync_state(): void { | |
| 358 | + $hosting_enabled = defender_get_hosting_feature_state( 'globaliplist' ); | |
| 359 | + if ( '' === $hosting_enabled ) { | |
| 360 | + return; | |
| 361 | + } | |
| 362 | + $hosting_enabled = (bool) $hosting_enabled; | |
| 363 | + if ( $hosting_enabled !== $this->model->enabled ) { | |
| 364 | + $this->model->enabled = $hosting_enabled; | |
| 365 | + $this->model->blocklist_autosync = $hosting_enabled; | |
| 366 | + $this->model->save(); | |
| 367 | + } | |
| 335 | 368 | } |
| 336 | 369 | } |