| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handle Dashboard based functionalities of WPMUDEV class. |
| 4 |
* |
| 5 |
* @package WP_Defender\Traits |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Traits; |
| 9 |
|
| 10 |
use WPMUDEV_Dashboard; |
| 11 |
use WPMUDEV\Hub\Connector\API; |
| 12 |
use WPMUDEV\Hub\Connector\Data; |
| 13 |
use WP_Defender\Component\Config\Config_Hub_Helper; |
| 14 |
|
| 15 |
trait Defender_Dashboard_Client { |
| 16 |
use \WP_Defender\Traits\Plugin; |
| 17 |
|
| 18 |
/** |
| 19 |
* Get membership status. |
| 20 |
* |
| 21 |
* @return bool |
| 22 |
*/ |
| 23 |
public function is_pro(): bool { |
| 24 |
return class_exists( '\WPMUDEV_Dashboard' ) && method_exists( $this, 'get_apikey' ) && $this->get_apikey() !== false; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Check if user is a paid one in WPMU DEV. |
| 29 |
* |
| 30 |
* @return bool |
| 31 |
*/ |
| 32 |
public function is_member(): bool { |
| 33 |
if ( |
| 34 |
$this->is_dash_activated() && method_exists( WPMUDEV_Dashboard::$upgrader, 'user_can_install' ) |
| 35 |
) { |
| 36 |
return WPMUDEV_Dashboard::$upgrader->user_can_install( |
| 37 |
Config_Hub_Helper::WDP_ID, |
| 38 |
true |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
return false; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Check if user is a WPMU DEV admin. |
| 47 |
* |
| 48 |
* @return bool |
| 49 |
* @since 2.6.3 |
| 50 |
*/ |
| 51 |
public function is_wpmu_dev_admin(): bool { |
| 52 |
if ( |
| 53 |
'dashboard' === $this->resolve_connection_mode() && |
| 54 |
is_object( WPMUDEV_Dashboard::$site ) && |
| 55 |
method_exists( WPMUDEV_Dashboard::$site, 'allowed_user' ) |
| 56 |
) { |
| 57 |
return WPMUDEV_Dashboard::$site->allowed_user( get_current_user_id() ); |
| 58 |
} |
| 59 |
|
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Returns whether the current install is a Pro version. |
| 65 |
* |
| 66 |
* @return bool |
| 67 |
*/ |
| 68 |
protected function is_pro_install(): bool { |
| 69 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 70 |
$is_pro = file_exists( $this->get_abs_plugin_path_by_slug( WP_DEFENDER_PRO_PATH ) ) |
| 71 |
&& is_plugin_active( WP_DEFENDER_PRO_PATH ); |
| 72 |
|
| 73 |
return $is_pro; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Returns the page title. |
| 78 |
* |
| 79 |
* @return string Page title. |
| 80 |
*/ |
| 81 |
public function get_page_title(): string { |
| 82 |
return $this->is_pro_install() |
| 83 |
? esc_html__( 'Defender Pro', 'defender-security' ) |
| 84 |
: esc_html__( 'Defender', 'defender-security' ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Returns the sidebar menu title, including a "Pro" badge span when on a Pro install. |
| 89 |
* |
| 90 |
* @return string Menu title. |
| 91 |
*/ |
| 92 |
public function get_menu_title(): string { |
| 93 |
if ( $this->is_pro_install() ) { |
| 94 |
return sprintf( |
| 95 |
'%1$s<span class="defender-admin-menu-pro-tag">%2$s</span>', |
| 96 |
esc_html__( 'Defender', 'defender-security' ), |
| 97 |
esc_html__( 'Pro', 'defender-security' ) |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
return esc_html__( 'Defender', 'defender-security' ); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Return icon svg image. |
| 106 |
* |
| 107 |
* @return string |
| 108 |
*/ |
| 109 |
public function get_menu_icon(): string { |
| 110 |
ob_start(); |
| 111 |
?> |
| 112 |
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg"> |
| 113 |
<path d="M7.44 1.12125L2.94 2.43125C2.09 2.68125 1.5 3.46125 1.5 4.35125V8.66125C1.5 11.5312 4.72 14.9612 8 14.9612C11.28 14.9612 14.5 11.5312 14.5 8.66125V4.35125C14.5 3.46125 13.91 2.68125 13.06 2.43125L8.56 1.12125C8.2 1.01125 7.81 1.01125 7.44 1.12125ZM13.06 8.66125C13.06 10.6912 10.53 13.5612 8 13.5612V7.96125H2.94V3.90125L8 2.43125V7.96125H13.06V8.66125Z" fill="#F0F6FC"/> |
| 114 |
</svg> |
| 115 |
<?php |
| 116 |
$svg = ob_get_clean(); |
| 117 |
|
| 118 |
return 'data:image/svg+xml;base64,' . base64_encode( $svg ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Check if WPMU DEV Dashboard plugin is activated. |
| 123 |
* |
| 124 |
* @return bool |
| 125 |
* @since 3.4.0 |
| 126 |
*/ |
| 127 |
public function is_dash_activated(): bool { |
| 128 |
return class_exists( 'WPMUDEV_Dashboard' ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Checks if WPMU DEV Dashboard plugin is installed. |
| 133 |
* |
| 134 |
* @return bool |
| 135 |
* @since 4.10.0 |
| 136 |
*/ |
| 137 |
public function is_dash_installed(): bool { |
| 138 |
return file_exists( WP_PLUGIN_DIR . '/wpmudev-updates/update-notifications.php' ); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Check if site is connected to HUB. |
| 143 |
* Todo: after implementing the HCM, the Free version can get an API key if the site is connected to the Hub using |
| 144 |
* HCM (without the Dashboard plugin). Does it make sense to use hub_connector_connected() method instead of |
| 145 |
* the current one? |
| 146 |
* |
| 147 |
* @return bool |
| 148 |
* @since 3.6.0 Added changes after the implementation of TFH on the hub. |
| 149 |
* @since 3.4.0 |
| 150 |
*/ |
| 151 |
public function is_site_connected_to_hub(): bool { |
| 152 |
// The case if Pro version is activated, it is TFH account and a site is from 3rd party hosting. |
| 153 |
if ( |
| 154 |
WP_DEFENDER_PRO_PATH === DEFENDER_PLUGIN_BASENAME |
| 155 |
&& method_exists( $this, 'is_another_hosted_site_connected_to_tfh' ) |
| 156 |
&& $this->is_another_hosted_site_connected_to_tfh() |
| 157 |
) { |
| 158 |
return '' !== $this->get_api_key(); |
| 159 |
} else { |
| 160 |
$hub_site_id = method_exists( $this, 'get_site_id' ) ? $this->get_site_id() : null; |
| 161 |
|
| 162 |
return is_int( $hub_site_id ) && $hub_site_id > 0; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Check if HUB option is disabled, e.g. Global IP. |
| 168 |
* |
| 169 |
* @return bool |
| 170 |
*/ |
| 171 |
public function is_disabled_hub_option(): bool { |
| 172 |
return 'none' === $this->resolve_connection_mode(); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Get remote access. |
| 177 |
*/ |
| 178 |
public function get_remote_access() { |
| 179 |
if ( ! class_exists( 'WPMUDEV_Dashboard' ) ) { |
| 180 |
return false; |
| 181 |
} |
| 182 |
|
| 183 |
// Use backward compatibility. |
| 184 |
if ( WPMUDEV_Dashboard::$version > '4.11.9' ) { |
| 185 |
return WPMUDEV_Dashboard::$settings->get( 'remote_access' ); |
| 186 |
} else { |
| 187 |
return WPMUDEV_Dashboard::$site->get_option( 'remote_access' ); |
| 188 |
} |
| 189 |
} |
| 190 |
/** |
| 191 |
* Checks if the Hub connector module (HCM) is connected. |
| 192 |
* |
| 193 |
* @return bool True if connected, false otherwise. |
| 194 |
*/ |
| 195 |
public static function get_hcm_status(): bool { |
| 196 |
return class_exists( 'WPMUDEV\Hub\Connector\API' ) && API::get()->is_logged_in(); |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Checks if the Dashboard plugin has a usable Hub connection. |
| 201 |
* |
| 202 |
* @return bool |
| 203 |
*/ |
| 204 |
protected function is_dashboard_connected(): bool { |
| 205 |
return $this->is_dash_activated() |
| 206 |
&& is_object( WPMUDEV_Dashboard::$api ) |
| 207 |
&& WPMUDEV_Dashboard::$api->has_key() |
| 208 |
&& (int) WPMUDEV_Dashboard::$api->get_site_id() > 0; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Resolve the active Hub connection mode for this request. |
| 213 |
* |
| 214 |
* @return string 'dashboard' | 'hub_connector' | 'none' |
| 215 |
* @since 6.0.0 |
| 216 |
*/ |
| 217 |
public function resolve_connection_mode(): string { |
| 218 |
if ( $this->is_dashboard_connected() ) { |
| 219 |
return 'dashboard'; |
| 220 |
} elseif ( self::get_hcm_status() ) { |
| 221 |
return 'hub_connector'; |
| 222 |
} |
| 223 |
|
| 224 |
return 'none'; |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Checks if Hub Connector is connected. If Dash plugin is not installed Hub connector can take over. |
| 229 |
* |
| 230 |
* @return bool |
| 231 |
*/ |
| 232 |
public function hub_connector_connected(): bool { |
| 233 |
return 'none' !== $this->resolve_connection_mode(); |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Upgrade the method to get api key from Dashboard plugin or Hub Connector module. |
| 238 |
* |
| 239 |
* @return string |
| 240 |
*/ |
| 241 |
public function get_api_key(): string { |
| 242 |
if ( 'dashboard' === $this->resolve_connection_mode() ) { |
| 243 |
$api_key = WPMUDEV_Dashboard::$api->get_key(); |
| 244 |
} elseif ( class_exists( 'WPMUDEV\Hub\Connector\API' ) ) { |
| 245 |
$api_key = API::get()->get_api_key(); |
| 246 |
} else { |
| 247 |
$api_key = ''; |
| 248 |
} |
| 249 |
|
| 250 |
return $api_key; |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Get Membership type. |
| 255 |
* |
| 256 |
* @return string |
| 257 |
*/ |
| 258 |
public function get_membership_type() { |
| 259 |
if ( 'dashboard' === $this->resolve_connection_mode() ) { |
| 260 |
return WPMUDEV_Dashboard::$api->get_membership_status(); |
| 261 |
} elseif ( self::get_hcm_status() ) { |
| 262 |
return Data::get()->membership_type(); |
| 263 |
} |
| 264 |
return 'free'; |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Check if the membership type is expired. |
| 269 |
* |
| 270 |
* @return bool True if membership is expired, false otherwise. |
| 271 |
*/ |
| 272 |
public function is_expired_membership_type(): bool { |
| 273 |
return 'expired' === $this->get_membership_type(); |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Logout from hub. |
| 278 |
* |
| 279 |
* @return array|bool|\WP_Error |
| 280 |
*/ |
| 281 |
public function logout() { |
| 282 |
return API::get()->logout(); |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Check if site is connected to HUB via HCM or Dash. |
| 287 |
* |
| 288 |
* @return bool |
| 289 |
*/ |
| 290 |
public function is_site_connected_to_hub_via_hcm_or_dash(): bool { |
| 291 |
return $this->hub_connector_connected() || self::get_hcm_status(); |
| 292 |
} |
| 293 |
} |
| 294 |
|