PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.0.1
Defender Security – Malware Scanner, Login Security & Firewall v6.0.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / controller / class-hub-connector.php

class-hub-connector.php in Defender Security – Malware Scanner, Login Security & Firewall 6.0.1, at src/controller/class-hub-connector.php

255 lines 8.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles Hub Connector feature.
4 *
5 * @package WP_Defender\Controller
6 */
7
8 namespace WP_Defender\Controller;
9
10 use Calotes\Component\Response;
11 use WP_Defender\Controller;
12 use WP_Defender\Component\Hub_Connector as Hub_Connector_Component;
13 use WP_Defender\Model\Setting\Antibot_Global_Firewall_Setting;
14 use WP_Defender\Traits\Defender_Dashboard_Client;
15
16 /**
17 * Handles Hub Connector feature.
18 */
19 class Hub_Connector extends Controller {
20 use Defender_Dashboard_Client;
21
22 public const TRANSIENT_KEY = 'wpdef_maybe_hub_connection_attempt';
23
24 /**
25 * Service for handling logic.
26 *
27 * @var Hub_Connector_Component
28 */
29 public $service;
30
31 /**
32 * Constructor for the Hub_Connector class.
33 *
34 * @param Hub_Connector_Component $service The service object.
35 */
36 public function __construct( Hub_Connector_Component $service ) {
37 $this->service = $service;
38 $this->register_routes();
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' ) );
43 }
44
45 /**
46 * Maybe member is trying to connect via Hub Connection Module.
47 */
48 public function maybe_hcm_connection_attempt() {
49 $module_slug = defender_get_data_from_request( 'module_slug', 'g' );
50 $is_callback = defender_get_data_from_request( 'hub_connector_callback', 'g' );
51
52 if ( is_string( $module_slug ) && '' !== $module_slug && is_string( $is_callback ) && '' !== $is_callback ) {
53 set_site_transient(
54 self::TRANSIENT_KEY,
55 array(
56 'module_slug' => $module_slug,
57 ),
58 MINUTE_IN_SECONDS
59 );
60 }
61 }
62
63 /**
64 * Disconnect site from Hub.
65 *
66 * @return Response
67 * @defender_route
68 */
69 public function disconnect_site(): Response {
70 $this->logout();
71
72 return new Response(
73 true,
74 array(
75 'message' => esc_html__( 'Site disconnected successfully!', 'defender-security' ),
76 'auto_close' => true,
77 )
78 );
79 }
80
81 /**
82 * Activate Dashboard plugin.
83 *
84 * @return Response
85 * @defender_route
86 */
87 public function activate_dashboard_plugin(): Response {
88 $plugin_path = 'wpmudev-updates/update-notifications.php';
89 $result = activate_plugin( $plugin_path );
90 if ( is_wp_error( $result ) ) {
91 return new Response(
92 false,
93 array(
94 'message' => $result->get_error_message(),
95 )
96 );
97 }
98
99 return new Response(
100 true,
101 array(
102 'message' => esc_html__( 'Dashboard plugin has been activated. You should be redirected to sign-in page shortly.', 'defender-security' ),
103 'redirect' => $this->service->get_url(),
104 )
105 );
106 }
107
108 /**
109 * AJAX handler to check Hub sync status.
110 *
111 * @return void
112 */
113 public function ajax_check_hub_sync_status() {
114 check_ajax_referer( 'auth_nonce', 'nonce' );
115
116 if ( ! current_user_can( 'manage_options' ) ) {
117 wp_send_json_error( array( 'message' => esc_html__( 'Unauthorized', 'defender-security' ) ), 403 );
118 }
119
120 $sync = $this->service->sync();
121 if ( is_wp_error( $sync ) && 'not_logged_in' !== $sync->get_error_code() ) {
122 $this->clear_syncing_state();
123
124 wp_send_json_error(
125 array(
126 'is_synced' => false,
127 'error_code' => $sync->get_error_code(),
128 'error_message' => $sync->get_error_message(),
129 ),
130 403
131 );
132 }
133
134 $is_logged_in = Hub_Connector_Component::is_logged_in();
135 $this->clear_syncing_state();
136
137 if ( $is_logged_in ) {
138 // Lets feature controllers auto-enable themselves via the module_slug transient.
139 do_action( 'wpdef_hub_connector_synced' );
140 }
141
142 wp_send_json_success(
143 array(
144 'is_synced' => $is_logged_in,
145 )
146 );
147 }
148
149 /**
150 * Clears the temporary Hub syncing state.
151 *
152 * @return void
153 */
154 public function clear_syncing_state(): void {
155 delete_site_transient( Hub_Connector_Component::SYNCING_TRANSIENT_KEY );
156 }
157
158 /**
159 * Provides data for the frontend.
160 *
161 * @return array An array of data for the frontend.
162 */
163 public function data_frontend(): array {
164 return array_merge(
165 array(
166 'button_label' => $this->service->get_button_label(),
167 'is_dash_installed' => $this->is_dash_installed(),
168 'is_dash_activated' => $this->is_dash_activated(),
169 'dash_url' => add_query_arg( 'page', 'wpmudev', is_multisite() ? network_admin_url( 'admin.php' ) : admin_url( 'admin.php' ) ),
170 'is_hub_connected' => $this->is_site_connected_to_hub_via_hcm_or_dash(),
171 'is_logged_in' => Hub_Connector_Component::is_logged_in() || Hub_Connector_Component::is_wpmudev_dashboard_connected(),
172 'is_dashboard_connected' => Hub_Connector_Component::is_wpmudev_dashboard_connected(),
173 'has_login_error' => Hub_Connector_Component::has_error_in_login(),
174 'login_error_msg' => Hub_Connector_Component::get_auth_error(),
175 'hub_auth_url' => Hub_Connector_Component::get_hub_google_login_url(),
176 'redirect_url' => Hub_Connector_Component::get_hub_connector_callback_url(),
177 'domain' => Hub_Connector_Component::get_site_domain(),
178 'auth_nonce' => wp_create_nonce( 'auth_nonce' ),
179 'login_auth_url' => Hub_Connector_Component::get_hub_site_login_auth_url(),
180 'hub_signup_url' => Hub_Connector_Component::get_hub_register_url(),
181 'forgot_password_url' => Hub_Connector_Component::get_hub_forgot_password_url(),
182 'is_syncing' => Hub_Connector_Component::is_syncing(),
183 'logout_url' => rest_url( 'hub-connector/v1/logout' ),
184 'logout_nonce' => wp_create_nonce( 'wp_rest' ),
185 'hub_connector_url' => array(
186 'default' => $this->service->get_url(),
187 'global-ip' => $this->service->get_url( 'wdf-ip-lockout', 'global-ip' ),
188 'blocklist' => $this->service->get_url( 'wdf-ip-lockout', 'blocklist' ),
189 'onboard' => $this->service->get_url( 'wp-defender', 'onboard' ),
190 'dashboard' => $this->service->get_url( 'wp-defender', 'dashboard' ),
191 'setting' => $this->service->get_url( 'wdf-setting', 'general' ),
192 // Settings > Tools redirect for scan-item HC features (Known Vuln, Suspicious Code).
193 'settings-redirect' => $this->service->get_url( 'wdf-setting', 'tools' ),
194 // Settings > Tools redirect; module slug auto-enables Blocklist Monitor.
195 'settings-blocklist' => $this->service->get_url( 'wdf-setting', 'tools', '', '', 'blocklist-monitor' ),
196 // Scan page redirect for the Safe Repair HC feature.
197 'scan' => $this->service->get_url( 'wdf-scan' ),
198 // Global Firewall redirect; module slug auto-enables AntiBot.
199 'global-firewall' => $this->service->get_url( 'wdf-ip-lockout', 'global-ip', '', '', Antibot_Global_Firewall_Setting::MODULE_SLUG ),
200 // Custom Rules redirect; module slug auto-enables Custom IP list sync.
201 'custom-rules' => $this->service->get_url( 'wdf-ip-lockout', 'custom-rules', '', '', Global_Ip::MODULE_SLUG ),
202 // Custom one if a trigger is the Summary section.
203 'summary-box' => $this->service->get_url( 'wdf-ip-lockout', 'summary-box' ),
204 // Custom one for Antibot notice shown on all Defender pages.
205 'antibot-notice' => $this->service->get_url( 'wdf-ip-lockout', '', 'def_antibot_survey_notice', 'hub_connector' ),
206 // Signup URLs with post_sync_redirect embedded — Hub always calls back to Dashboard.
207 'settings-redirect-signup' => Hub_Connector_Component::get_hub_register_url( 'wp-defender', $this->service->get_url( 'wdf-setting', 'tools' ) ),
208 'settings-blocklist-signup' => Hub_Connector_Component::get_hub_register_url( 'wp-defender', $this->service->get_url( 'wdf-setting', 'tools', '', '', 'blocklist-monitor' ) ),
209 'scan-signup' => Hub_Connector_Component::get_hub_register_url( 'wp-defender', $this->service->get_url( 'wdf-scan' ) ),
210 'global-firewall-signup' => Hub_Connector_Component::get_hub_register_url( 'wp-defender', $this->service->get_url( 'wdf-ip-lockout', 'global-ip', '', '', Antibot_Global_Firewall_Setting::MODULE_SLUG ) ),
211 'custom-rules-signup' => Hub_Connector_Component::get_hub_register_url( 'wp-defender', $this->service->get_url( 'wdf-ip-lockout', 'custom-rules', '', '', Global_Ip::MODULE_SLUG ) ),
212 ),
213 ),
214 $this->dump_routes_and_nonces()
215 );
216 }
217
218 /**
219 * Export to array
220 *
221 * @return array
222 */
223 public function to_array() {
224 return array();
225 }
226
227 /**
228 * Import data
229 *
230 * @param array $data The data to import.
231 */
232 public function import_data( $data ) {}
233
234 /**
235 * Export strings
236 *
237 * @return array
238 */
239 public function export_strings() {
240 return array();
241 }
242
243 /**
244 * Removes settings.
245 */
246 public function remove_settings() {
247 }
248
249 /**
250 * Delete all the data & the cache.
251 */
252 public function remove_data() {
253 }
254 }
255