| 1 |
<?php |
| 2 |
/** |
| 3 |
* Hub Connector class. |
| 4 |
* |
| 5 |
* @package WP_Defender\Component |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Component; |
| 9 |
|
| 10 |
use WPMUDEV\Hub\Connector; |
| 11 |
use Calotes\Base\Component; |
| 12 |
use WP_Defender\Traits\Defender_Dashboard_Client; |
| 13 |
|
| 14 |
/** |
| 15 |
* Handles the functionality related to the Hub Connector module. |
| 16 |
* |
| 17 |
* @since 4.10.0 |
| 18 |
*/ |
| 19 |
class Hub_Connector extends Component { |
| 20 |
use Defender_Dashboard_Client; |
| 21 |
|
| 22 |
/** |
| 23 |
* The identifier for the WP Defender plugin in the Hub. |
| 24 |
* |
| 25 |
* @const string |
| 26 |
*/ |
| 27 |
public const PLUGIN_IDENTIFIER = 'defender'; |
| 28 |
|
| 29 |
/** |
| 30 |
* The action name used for the Hub connection. |
| 31 |
* |
| 32 |
* @const string |
| 33 |
*/ |
| 34 |
public const CONNECTION_ACTION = 'hub_connection'; |
| 35 |
|
| 36 |
/** |
| 37 |
* Checks if the Hub connector should render its UI. |
| 38 |
* |
| 39 |
* Verifies the nonce from the global-ip page to determine if the Hub connector should render its UI. |
| 40 |
* |
| 41 |
* @return bool The nonce value on success, false on failure. |
| 42 |
*/ |
| 43 |
public static function should_render() { |
| 44 |
return ! self::get_status() && wp_verify_nonce( defender_get_data_from_request( '_def_nonce', 'g' ), self::CONNECTION_ACTION ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Initialize the Hub Connector module and set its options. |
| 49 |
* |
| 50 |
* The `extra/hub-connector/connector.php` file is required, and the options are set for the Hub Connector module. |
| 51 |
* |
| 52 |
* @return void |
| 53 |
*/ |
| 54 |
public function init() { |
| 55 |
require_once defender_path( 'extra/hub-connector/connector.php' ); |
| 56 |
$options = array( |
| 57 |
'screens' => array( |
| 58 |
'toplevel_page_wp-defender', |
| 59 |
'toplevel_page_wp-defender-network', |
| 60 |
'defender_page_wdf-ip-lockout', |
| 61 |
'defender_page_wdf-ip-lockout-network', |
| 62 |
'defender-pro_page_wdf-ip-lockout', |
| 63 |
'defender-pro_page_wdf-ip-lockout-network', |
| 64 |
), |
| 65 |
'extra_args' => array( |
| 66 |
'register' => array( |
| 67 |
'connect_ref' => self::PLUGIN_IDENTIFIER, |
| 68 |
'utm_medium' => 'plugin', |
| 69 |
'utm_source' => self::PLUGIN_IDENTIFIER, |
| 70 |
), |
| 71 |
), |
| 72 |
); |
| 73 |
// Get advanced params. |
| 74 |
$page = defender_get_data_from_request( 'page', 'g' ); |
| 75 |
$view = defender_get_data_from_request( 'view', 'g' ); |
| 76 |
$res = $this->get_utm_tags( $page, $view ); |
| 77 |
if ( ! empty( $res['utm_campaign'] ) ) { |
| 78 |
$options['extra_args']['register']['utm_campaign'] = $res['utm_campaign']; |
| 79 |
} |
| 80 |
if ( ! empty( $res['utm_content'] ) ) { |
| 81 |
$options['extra_args']['register']['utm_content'] = $res['utm_content']; |
| 82 |
} |
| 83 |
|
| 84 |
Connector::get()->set_options( self::PLUGIN_IDENTIFIER, $options ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Gets the label for the button to connect the site to the Hub. |
| 89 |
* |
| 90 |
* @return string The label for the button. |
| 91 |
*/ |
| 92 |
public function get_button_label(): string { |
| 93 |
if ( $this->is_dash_installed() && ! $this->is_dash_activated() ) { |
| 94 |
return __( 'Activate WPMU DEV Dashboard', 'defender-security' ); |
| 95 |
} |
| 96 |
if ( $this->is_dash_activated() ) { |
| 97 |
return __( 'Log in to WPMU DEV', 'defender-security' ); |
| 98 |
} |
| 99 |
return __( 'Connect site for Full protection', 'defender-security' ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Modify text string vars. |
| 104 |
* |
| 105 |
* @param array $texts Vars. |
| 106 |
* @param string $plugin Plugin identifier. |
| 107 |
* |
| 108 |
* @return array |
| 109 |
*/ |
| 110 |
public static function customize_text_vars( $texts, $plugin ): array { |
| 111 |
if ( self::PLUGIN_IDENTIFIER === $plugin ) { |
| 112 |
$feature_name = self::get_feature_name(); |
| 113 |
$texts['create_account_desc'] = sprintf( |
| 114 |
/* translators: 1. Feature name. 2. Opened tag. 3. Closed tag. */ |
| 115 |
esc_html__( 'Create a free account to connect your site to WPMU DEV and activate %1$s. %2$sIt`s fast, seamless, and free%3$s.', 'defender-security' ), |
| 116 |
'<strong>' . __( 'Defender - ', 'defender-security' ) . $feature_name . '</strong>', |
| 117 |
'<i>', |
| 118 |
'</i>' |
| 119 |
); |
| 120 |
$texts['login_desc'] = sprintf( |
| 121 |
/* translators: %s: Feature name. */ |
| 122 |
esc_html__( 'Log in with your WPMU DEV account credentials to activate %s.', 'defender-security' ), |
| 123 |
$feature_name |
| 124 |
); |
| 125 |
} |
| 126 |
|
| 127 |
return $texts; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Get feature's name. |
| 132 |
* |
| 133 |
* @return string |
| 134 |
*/ |
| 135 |
public static function get_feature_name(): string { |
| 136 |
$feature_name = defender_get_data_from_request( 'module_name', 'g' ); |
| 137 |
if ( ! empty( $feature_name ) ) { |
| 138 |
return $feature_name; |
| 139 |
} |
| 140 |
|
| 141 |
$view = defender_get_data_from_request( 'view', 'g' ); |
| 142 |
switch ( $view ) { |
| 143 |
case 'blocklist': |
| 144 |
return __( 'Custom IP Allow/Block list', 'defender-security' ); |
| 145 |
default: |
| 146 |
return __( 'AntiBot Global Firewall', 'defender-security' ); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Get UTM tags. |
| 152 |
* |
| 153 |
* @param string $page The page to load. |
| 154 |
* @param string $view The view to load. |
| 155 |
* |
| 156 |
* @return array |
| 157 |
*/ |
| 158 |
private function get_utm_tags( string $page = '', string $view = '' ): array { |
| 159 |
$utm_campaign = ''; |
| 160 |
$utm_content = ''; |
| 161 |
if ( ! empty( $page ) ) { |
| 162 |
switch ( $page ) { |
| 163 |
// There are buttons on notice or widget on the Dashboard page. |
| 164 |
case 'wp-defender': |
| 165 |
$utm_content = ( 'dashboard' === $view ) |
| 166 |
? 'dashboard' |
| 167 |
: 'onboarding'; |
| 168 |
$utm_campaign = ( 'dashboard' === $view ) |
| 169 |
? 'defender_dashboard' |
| 170 |
: 'defender_onboarding'; |
| 171 |
break; |
| 172 |
case 'wdf-ip-lockout': |
| 173 |
$utm_content = ( 'blocklist' === $view ) |
| 174 |
? 'firewall_page_connect_site' |
| 175 |
: 'feature_page'; |
| 176 |
$utm_campaign = ( 'blocklist' === $view ) |
| 177 |
? 'defender_firewall_centralip' |
| 178 |
: 'defender_firewall_antibot'; |
| 179 |
break; |
| 180 |
default: |
| 181 |
break; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
return array( |
| 186 |
'utm_campaign' => $utm_campaign, |
| 187 |
'utm_content' => $utm_content, |
| 188 |
); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Retrieve the Hub connector URL. |
| 193 |
* |
| 194 |
* @param string $page Optional. The page to load. Default is empty string. |
| 195 |
* @param string $view Optional. The view to load. Default is empty string. |
| 196 |
* |
| 197 |
* @return string |
| 198 |
*/ |
| 199 |
public function get_url( string $page = '', string $view = '' ): string { |
| 200 |
if ( $this->is_dash_activated() ) { |
| 201 |
$args = array( |
| 202 |
'page' => 'wpmudev', |
| 203 |
'utm_source' => self::PLUGIN_IDENTIFIER, |
| 204 |
'utm_medium' => 'plugin', |
| 205 |
); |
| 206 |
// Get advanced params. |
| 207 |
$res = $this->get_utm_tags( $page, $view ); |
| 208 |
if ( ! empty( $res['utm_campaign'] ) ) { |
| 209 |
$args['utm_campaign'] = $res['utm_campaign']; |
| 210 |
} |
| 211 |
if ( ! empty( $res['utm_content'] ) ) { |
| 212 |
$args['utm_content'] = $res['utm_content']; |
| 213 |
} |
| 214 |
|
| 215 |
return add_query_arg( $args, network_admin_url( 'admin.php' ) ); |
| 216 |
} |
| 217 |
|
| 218 |
$query = array( |
| 219 |
'_def_nonce' => wp_create_nonce( self::CONNECTION_ACTION ), |
| 220 |
); |
| 221 |
if ( ! empty( $page ) ) { |
| 222 |
$query['page'] = $page; |
| 223 |
} |
| 224 |
if ( ! empty( $view ) ) { |
| 225 |
$query['view'] = $view; |
| 226 |
} |
| 227 |
|
| 228 |
return add_query_arg( $query, network_admin_url( 'admin.php' ) ); |
| 229 |
} |
| 230 |
} |
| 231 |
|