| @@ -9,8 +9,9 @@ | ||
| 9 | 9 | |
| 10 | 10 | use Calotes\Component\Response; |
| 11 | 11 | use WP_Defender\Controller; |
| 12 | 12 | use WP_Defender\Component\Hub_Connector as Hub_Connector_Component; |
| 13 | +use WP_Defender\Model\Setting\Antibot_Global_Firewall_Setting; | |
| 13 | 14 | use WP_Defender\Traits\Defender_Dashboard_Client; |
| 14 | 15 | |
| 15 | 16 | /** |
| 16 | 17 | * Handles Hub Connector feature. |
| @@ -36,8 +37,10 @@ | ||
| 36 | 37 | $this->service = $service; |
| 37 | 38 | $this->register_routes(); |
| 38 | 39 | |
| 39 | 40 | add_action( 'admin_init', array( $this, 'maybe_hcm_connection_attempt' ) ); |
| 41 | + add_action( 'wp_ajax_wpdef_check_hub_sync_status', array( $this, 'ajax_check_hub_sync_status' ) ); | |
| 42 | + add_action( 'wpmudev_hub_connector_first_sync_completed', array( $this, 'clear_syncing_state' ) ); | |
| 40 | 43 | } |
| 41 | 44 | |
| 42 | 45 | /** |
| 43 | 46 | * Maybe member is trying to connect via Hub Connection Module. |
| @@ -42,8 +45,12 @@ | ||
| 42 | 45 | /** |
| 43 | 46 | * Maybe member is trying to connect via Hub Connection Module. |
| 44 | 47 | */ |
| 45 | 48 | public function maybe_hcm_connection_attempt() { |
| 49 | + if ( ! current_user_can( 'manage_options' ) ) { | |
| 50 | + return; | |
| 51 | + } | |
| 52 | + | |
| 46 | 53 | $module_slug = defender_get_data_from_request( 'module_slug', 'g' ); |
| 47 | 54 | $is_callback = defender_get_data_from_request( 'hub_connector_callback', 'g' ); |
| 48 | 55 | |
| 49 | 56 | if ( is_string( $module_slug ) && '' !== $module_slug && is_string( $is_callback ) && '' !== $is_callback ) { |
| @@ -57,8 +64,26 @@ | ||
| 57 | 64 | } |
| 58 | 65 | } |
| 59 | 66 | |
| 60 | 67 | /** |
| 68 | + * Disconnect site from Hub. | |
| 69 | + * | |
| 70 | + * @return Response | |
| 71 | + * @defender_route | |
| 72 | + */ | |
| 73 | + public function disconnect_site(): Response { | |
| 74 | + $this->logout(); | |
| 75 | + | |
| 76 | + return new Response( | |
| 77 | + true, | |
| 78 | + array( | |
| 79 | + 'message' => esc_html__( 'Site disconnected successfully!', 'defender-security' ), | |
| 80 | + 'auto_close' => true, | |
| 81 | + ) | |
| 82 | + ); | |
| 83 | + } | |
| 84 | + | |
| 85 | + /** | |
| 61 | 86 | * Activate Dashboard plugin. |
| 62 | 87 | * |
| 63 | 88 | * @return Response |
| 64 | 89 | * @defender_route |
| @@ -84,30 +109,116 @@ | ||
| 84 | 109 | ); |
| 85 | 110 | } |
| 86 | 111 | |
| 87 | 112 | /** |
| 113 | + * AJAX handler to check Hub sync status. | |
| 114 | + * | |
| 115 | + * @return void | |
| 116 | + */ | |
| 117 | + public function ajax_check_hub_sync_status() { | |
| 118 | + check_ajax_referer( 'auth_nonce', 'nonce' ); | |
| 119 | + | |
| 120 | + if ( ! current_user_can( 'manage_options' ) ) { | |
| 121 | + wp_send_json_error( array( 'message' => esc_html__( 'Unauthorized', 'defender-security' ) ), 403 ); | |
| 122 | + } | |
| 123 | + | |
| 124 | + $sync = $this->service->sync(); | |
| 125 | + if ( is_wp_error( $sync ) && 'not_logged_in' !== $sync->get_error_code() ) { | |
| 126 | + $this->clear_syncing_state(); | |
| 127 | + | |
| 128 | + wp_send_json_error( | |
| 129 | + array( | |
| 130 | + 'is_synced' => false, | |
| 131 | + 'error_code' => $sync->get_error_code(), | |
| 132 | + 'error_message' => $sync->get_error_message(), | |
| 133 | + ), | |
| 134 | + 403 | |
| 135 | + ); | |
| 136 | + } | |
| 137 | + | |
| 138 | + $is_logged_in = Hub_Connector_Component::is_logged_in(); | |
| 139 | + $this->clear_syncing_state(); | |
| 140 | + | |
| 141 | + if ( $is_logged_in ) { | |
| 142 | + // Lets feature controllers auto-enable themselves via the module_slug transient. | |
| 143 | + do_action( 'wpdef_hub_connector_synced' ); | |
| 144 | + } | |
| 145 | + | |
| 146 | + wp_send_json_success( | |
| 147 | + array( | |
| 148 | + 'is_synced' => $is_logged_in, | |
| 149 | + ) | |
| 150 | + ); | |
| 151 | + } | |
| 152 | + | |
| 153 | + /** | |
| 154 | + * Clears the temporary Hub syncing state. | |
| 155 | + * | |
| 156 | + * @return void | |
| 157 | + */ | |
| 158 | + public function clear_syncing_state(): void { | |
| 159 | + delete_site_transient( Hub_Connector_Component::SYNCING_TRANSIENT_KEY ); | |
| 160 | + } | |
| 161 | + | |
| 162 | + /** | |
| 88 | 163 | * Provides data for the frontend. |
| 89 | 164 | * |
| 90 | 165 | * @return array An array of data for the frontend. |
| 91 | 166 | */ |
| 92 | 167 | public function data_frontend(): array { |
| 168 | + // Current admin page slug, used to build callbacks that return to the originating page. | |
| 169 | + $current_page = sanitize_text_field( wp_unslash( $_GET['page'] ?? 'wp-defender' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 170 | + $is_hub_connector_logged_in = Hub_Connector_Component::is_logged_in(); | |
| 171 | + | |
| 93 | 172 | return array_merge( |
| 94 | 173 | array( |
| 95 | - 'button_label' => $this->service->get_button_label(), | |
| 96 | - 'is_dash_installed' => $this->is_dash_installed(), | |
| 97 | - 'is_dash_activated' => $this->is_dash_activated(), | |
| 98 | - 'is_hub_connected' => $this->is_site_connected_to_hub_via_hcm_or_dash(), | |
| 99 | - 'hub_connector_url' => array( | |
| 100 | - 'default' => $this->service->get_url(), | |
| 101 | - 'global-ip' => $this->service->get_url( 'wdf-ip-lockout', 'global-ip' ), | |
| 102 | - 'blocklist' => $this->service->get_url( 'wdf-ip-lockout', 'blocklist' ), | |
| 103 | - 'onboard' => $this->service->get_url( 'wp-defender', 'onboard' ), | |
| 104 | - 'dashboard' => $this->service->get_url( 'wp-defender', 'dashboard' ), | |
| 105 | - 'setting' => $this->service->get_url( 'wdf-setting', 'general' ), | |
| 174 | + 'button_label' => $this->service->get_button_label(), | |
| 175 | + 'is_dash_installed' => $this->is_dash_installed(), | |
| 176 | + 'is_dash_activated' => $this->is_dash_activated(), | |
| 177 | + 'dash_url' => add_query_arg( 'page', 'wpmudev', is_multisite() ? network_admin_url( 'admin.php' ) : admin_url( 'admin.php' ) ), | |
| 178 | + 'is_hub_connected' => $this->is_site_connected_to_hub_via_hcm_or_dash(), | |
| 179 | + 'is_logged_in' => $is_hub_connector_logged_in || Hub_Connector_Component::is_wpmudev_dashboard_connected(), | |
| 180 | + 'is_hub_connector_logged_in' => $is_hub_connector_logged_in, | |
| 181 | + 'is_dashboard_connected' => Hub_Connector_Component::is_wpmudev_dashboard_connected(), | |
| 182 | + 'has_login_error' => Hub_Connector_Component::has_error_in_login(), | |
| 183 | + 'login_error_msg' => Hub_Connector_Component::get_auth_error(), | |
| 184 | + 'hub_auth_url' => Hub_Connector_Component::get_hub_google_login_url(), | |
| 185 | + 'redirect_url' => Hub_Connector_Component::get_hub_connector_callback_url( $current_page ), | |
| 186 | + 'domain' => Hub_Connector_Component::get_site_domain(), | |
| 187 | + 'auth_nonce' => wp_create_nonce( 'auth_nonce' ), | |
| 188 | + 'login_auth_url' => Hub_Connector_Component::get_hub_site_login_auth_url(), | |
| 189 | + 'hub_signup_url' => Hub_Connector_Component::get_hub_register_url( $current_page ), | |
| 190 | + 'forgot_password_url' => Hub_Connector_Component::get_hub_forgot_password_url(), | |
| 191 | + 'is_syncing' => Hub_Connector_Component::is_syncing(), | |
| 192 | + 'logout_url' => rest_url( 'hub-connector/v1/logout' ), | |
| 193 | + 'logout_nonce' => wp_create_nonce( 'wp_rest' ), | |
| 194 | + 'hub_connector_url' => array( | |
| 195 | + 'default' => $this->service->get_url(), | |
| 196 | + 'global-ip' => $this->service->get_url( 'wdf-ip-lockout', 'global-ip' ), | |
| 197 | + 'blocklist' => $this->service->get_url( 'wdf-ip-lockout', 'blocklist' ), | |
| 198 | + 'onboard' => $this->service->get_url( 'wp-defender', 'onboard' ), | |
| 199 | + 'dashboard' => $this->service->get_url( 'wp-defender', 'dashboard' ), | |
| 200 | + 'setting' => $this->service->get_url( 'wdf-setting', 'general' ), | |
| 201 | + // Settings > Tools redirect for scan-item HC features (Known Vuln, Suspicious Code). | |
| 202 | + 'settings-redirect' => $this->service->get_url( 'wdf-setting', 'tools' ), | |
| 203 | + // Settings > Tools redirect; module slug auto-enables Blocklist Monitor. | |
| 204 | + 'settings-blocklist' => $this->service->get_url( 'wdf-setting', 'tools', '', '', 'blocklist-monitor' ), | |
| 205 | + // Scan page redirect for the Safe Repair HC feature. | |
| 206 | + 'scan' => $this->service->get_url( 'wdf-scan' ), | |
| 207 | + // Global Firewall redirect; module slug auto-enables AntiBot. | |
| 208 | + 'global-firewall' => $this->service->get_url( 'wdf-ip-lockout', 'global-ip', '', '', Antibot_Global_Firewall_Setting::MODULE_SLUG ), | |
| 209 | + // Custom Rules redirect; module slug auto-enables Custom IP list sync. | |
| 210 | + 'custom-rules' => $this->service->get_url( 'wdf-ip-lockout', 'custom-rules', '', '', Global_Ip::MODULE_SLUG ), | |
| 106 | 211 | // Custom one if a trigger is the Summary section. |
| 107 | - 'summary-box' => $this->service->get_url( 'wdf-ip-lockout', 'summary-box' ), | |
| 212 | + 'summary-box' => $this->service->get_url( 'wdf-ip-lockout', 'summary-box' ), | |
| 108 | 213 | // Custom one for Antibot notice shown on all Defender pages. |
| 109 | - 'antibot-notice' => $this->service->get_url( 'wdf-ip-lockout', '', 'def_antibot_survey_notice', 'hub_connector' ), | |
| 214 | + 'antibot-notice' => $this->service->get_url( 'wdf-ip-lockout', '', 'def_antibot_survey_notice', 'hub_connector' ), | |
| 215 | + // Signup URLs embed post_sync_redirect so the callback lands on the current page, then redirects to the target. | |
| 216 | + 'settings-redirect-signup' => Hub_Connector_Component::get_hub_register_url( $current_page, $this->service->get_url( 'wdf-setting', 'tools' ) ), | |
| 217 | + 'settings-blocklist-signup' => Hub_Connector_Component::get_hub_register_url( $current_page, $this->service->get_url( 'wdf-setting', 'tools', '', '', 'blocklist-monitor' ) ), | |
| 218 | + 'scan-signup' => Hub_Connector_Component::get_hub_register_url( $current_page, $this->service->get_url( 'wdf-scan' ) ), | |
| 219 | + 'global-firewall-signup' => Hub_Connector_Component::get_hub_register_url( $current_page, $this->service->get_url( 'wdf-ip-lockout', 'global-ip', '', '', Antibot_Global_Firewall_Setting::MODULE_SLUG ) ), | |
| 220 | + 'custom-rules-signup' => Hub_Connector_Component::get_hub_register_url( $current_page, $this->service->get_url( 'wdf-ip-lockout', 'custom-rules', '', '', Global_Ip::MODULE_SLUG ) ), | |
| 110 | 221 | ), |
| 111 | 222 | ), |
| 112 | 223 | $this->dump_routes_and_nonces() |
| 113 | 224 | ); |
| @@ -114,10 +225,14 @@ | ||
| 114 | 225 | } |
| 115 | 226 | |
| 116 | 227 | /** |
| 117 | 228 | * Export to array |
| 229 | + * | |
| 230 | + * @return array | |
| 118 | 231 | */ |
| 119 | - public function to_array() {} | |
| 232 | + public function to_array() { | |
| 233 | + return array(); | |
| 234 | + } | |
| 120 | 235 | |
| 121 | 236 | /** |
| 122 | 237 | * Import data |
| 123 | 238 | * |