| 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\Traits\Defender_Dashboard_Client; |
| 14 |
|
| 15 |
/** |
| 16 |
* Handles Hub Connector feature. |
| 17 |
*/ |
| 18 |
class Hub_Connector extends Controller { |
| 19 |
use Defender_Dashboard_Client; |
| 20 |
|
| 21 |
public const TRANSIENT_KEY = 'wpdef_maybe_hub_connection_attempt'; |
| 22 |
|
| 23 |
/** |
| 24 |
* Service for handling logic. |
| 25 |
* |
| 26 |
* @var Hub_Connector_Component |
| 27 |
*/ |
| 28 |
public $service; |
| 29 |
|
| 30 |
/** |
| 31 |
* Constructor for the Hub_Connector class. |
| 32 |
* |
| 33 |
* @param Hub_Connector_Component $service The service object. |
| 34 |
*/ |
| 35 |
public function __construct( Hub_Connector_Component $service ) { |
| 36 |
$this->service = $service; |
| 37 |
$this->register_routes(); |
| 38 |
|
| 39 |
add_action( 'admin_init', array( $this, 'maybe_hcm_connection_attempt' ) ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Maybe member is trying to connect via Hub Connection Module. |
| 44 |
*/ |
| 45 |
public function maybe_hcm_connection_attempt() { |
| 46 |
$module_slug = defender_get_data_from_request( 'module_slug', 'g' ); |
| 47 |
$is_callback = defender_get_data_from_request( 'hub_connector_callback', 'g' ); |
| 48 |
|
| 49 |
if ( ! empty( $module_slug ) && ! empty( $is_callback ) ) { |
| 50 |
set_site_transient( |
| 51 |
self::TRANSIENT_KEY, |
| 52 |
array( |
| 53 |
'module_slug' => $module_slug, |
| 54 |
), |
| 55 |
MINUTE_IN_SECONDS |
| 56 |
); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Activate Dashboard plugin. |
| 62 |
* |
| 63 |
* @return Response |
| 64 |
* @defender_route |
| 65 |
*/ |
| 66 |
public function activate_dashboard_plugin(): Response { |
| 67 |
$plugin_path = 'wpmudev-updates/update-notifications.php'; |
| 68 |
$result = activate_plugin( $plugin_path ); |
| 69 |
if ( is_wp_error( $result ) ) { |
| 70 |
return new Response( |
| 71 |
false, |
| 72 |
array( |
| 73 |
'message' => $result->get_error_message(), |
| 74 |
) |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
return new Response( |
| 79 |
true, |
| 80 |
array( |
| 81 |
'message' => esc_html__( 'Dashboard plugin has been activated. You should be redirected to sign-in page shortly.', 'defender-security' ), |
| 82 |
'redirect' => $this->service->get_url(), |
| 83 |
) |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Provides data for the frontend. |
| 89 |
* |
| 90 |
* @return array An array of data for the frontend. |
| 91 |
*/ |
| 92 |
public function data_frontend(): array { |
| 93 |
return array_merge( |
| 94 |
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 |
// Custom one if a trigger is the Summary section. |
| 106 |
'summary-box' => $this->service->get_url( 'wdf-ip-lockout', 'summary-box' ), |
| 107 |
// Custom one for Antibot notice shown on all Defender pages. |
| 108 |
'antibot-notice' => $this->service->get_url( 'wdf-ip-lockout', '', 'def_antibot_survey_notice', 'hub_connector' ), |
| 109 |
), |
| 110 |
), |
| 111 |
$this->dump_routes_and_nonces() |
| 112 |
); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Export to array |
| 117 |
*/ |
| 118 |
public function to_array() {} |
| 119 |
|
| 120 |
/** |
| 121 |
* Import data |
| 122 |
* |
| 123 |
* @param array $data The data to import. |
| 124 |
*/ |
| 125 |
public function import_data( $data ) {} |
| 126 |
|
| 127 |
/** |
| 128 |
* Export strings |
| 129 |
* |
| 130 |
* @return array |
| 131 |
*/ |
| 132 |
public function export_strings() { |
| 133 |
return array(); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Removes settings. |
| 138 |
*/ |
| 139 |
public function remove_settings() { |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Delete all the data & the cache. |
| 144 |
*/ |
| 145 |
public function remove_data() { |
| 146 |
} |
| 147 |
} |
| 148 |
|