| 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 |
|
| 17 |
/** |
| 18 |
* Get membership status. |
| 19 |
* |
| 20 |
* @return bool |
| 21 |
*/ |
| 22 |
public function is_pro(): bool { |
| 23 |
return $this->get_apikey() !== false; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Check if user is a paid one in WPMU DEV. |
| 28 |
* |
| 29 |
* @return bool |
| 30 |
*/ |
| 31 |
public function is_member(): bool { |
| 32 |
if ( |
| 33 |
$this->is_dash_activated() && method_exists( WPMUDEV_Dashboard::$upgrader, 'user_can_install' ) |
| 34 |
) { |
| 35 |
return WPMUDEV_Dashboard::$upgrader->user_can_install( |
| 36 |
Config_Hub_Helper::WDP_ID, |
| 37 |
true |
| 38 |
); |
| 39 |
} |
| 40 |
|
| 41 |
return false; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Check if user is a WPMU DEV admin. |
| 46 |
* |
| 47 |
* @return bool |
| 48 |
* @since 2.6.3 |
| 49 |
*/ |
| 50 |
public function is_wpmu_dev_admin(): bool { |
| 51 |
if ( $this->is_dash_activated() && method_exists( 'WPMUDEV_Dashboard_Site', 'allowed_user' ) ) { |
| 52 |
return WPMUDEV_Dashboard::$site->allowed_user( get_current_user_id() ); |
| 53 |
} |
| 54 |
|
| 55 |
return false; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Bring the plugin menu title. |
| 60 |
* |
| 61 |
* @return string Menu title. |
| 62 |
*/ |
| 63 |
public function get_menu_title(): string { |
| 64 |
if ( $this->is_pro() ) { |
| 65 |
$menu_title = esc_html__( 'Defender Pro', 'defender-security' ); |
| 66 |
} else { |
| 67 |
// Check if it's Pro but user logged the WPMU DEV Dashboard out. |
| 68 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 69 |
$menu_title = file_exists( WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . WP_DEFENDER_PRO_PATH ) |
| 70 |
&& is_plugin_active( WP_DEFENDER_PRO_PATH ) |
| 71 |
? esc_html__( 'Defender Pro', 'defender-security' ) |
| 72 |
: esc_html__( 'Defender', 'defender-security' ); |
| 73 |
} |
| 74 |
|
| 75 |
return $menu_title; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Return icon svg image. |
| 80 |
* |
| 81 |
* @return string |
| 82 |
*/ |
| 83 |
public function get_menu_icon(): string { |
| 84 |
ob_start(); |
| 85 |
?> |
| 86 |
<svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg"> |
| 87 |
<path fill-rule="evenodd" clip-rule="evenodd" |
| 88 |
d="M9.99999 2.08899L3 4.21792V9.99502H9.99912V18.001H10C13.47 18.001 17 13.9231 17 11.0045V9.99501H9.99999V2.08899ZM10 0L1 2.73862V11.0045C1 15.1125 5.49 20 10 20C14.51 20 19 15.1225 19 11.0045V2.73862L10 0Z" |
| 89 |
fill="#F0F6FC"/> |
| 90 |
</svg> |
| 91 |
<?php |
| 92 |
$svg = ob_get_clean(); |
| 93 |
|
| 94 |
return 'data:image/svg+xml;base64,' . base64_encode( $svg ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Check if WPMU DEV Dashboard plugin is activated. |
| 99 |
* |
| 100 |
* @return bool |
| 101 |
* @since 3.4.0 |
| 102 |
*/ |
| 103 |
public function is_dash_activated(): bool { |
| 104 |
return class_exists( 'WPMUDEV_Dashboard' ); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Checks if WPMU DEV Dashboard plugin is installed. |
| 109 |
* |
| 110 |
* @return bool |
| 111 |
* @since 4.10.0 |
| 112 |
*/ |
| 113 |
public function is_dash_installed(): bool { |
| 114 |
return file_exists( WP_PLUGIN_DIR . '/wpmudev-updates/update-notifications.php' ); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Check if site is connected to HUB. |
| 119 |
* Todo: after implementing the HCM, the Free version can get an API key if the site is connected to the Hub using |
| 120 |
* HCM (without the Dashboard plugin). Does it make sense to use hub_connector_connected() method instead of |
| 121 |
* the current one? |
| 122 |
* |
| 123 |
* @return bool |
| 124 |
* @since 3.6.0 Added changes after the implementation of TFH on the hub. |
| 125 |
* @since 3.4.0 |
| 126 |
*/ |
| 127 |
public function is_site_connected_to_hub(): bool { |
| 128 |
// The case if Pro version is activated, it is TFH account and a site is from 3rd party hosting. |
| 129 |
if ( WP_DEFENDER_PRO_PATH === DEFENDER_PLUGIN_BASENAME && $this->is_another_hosted_site_connected_to_tfh() ) { |
| 130 |
return '' !== $this->get_api_key(); |
| 131 |
} else { |
| 132 |
$hub_site_id = $this->get_site_id(); |
| 133 |
|
| 134 |
return is_int( $hub_site_id ) && $hub_site_id > 0; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Check if HUB option is disabled, e.g. Global IP. |
| 140 |
* |
| 141 |
* @return bool |
| 142 |
*/ |
| 143 |
public function is_disabled_hub_option(): bool { |
| 144 |
return ! $this->is_dash_activated() || ! $this->is_site_connected_to_hub(); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Get remote access. |
| 149 |
*/ |
| 150 |
public function get_remote_access() { |
| 151 |
// Use backward compatibility. |
| 152 |
if ( WPMUDEV_Dashboard::$version > '4.11.9' ) { |
| 153 |
return WPMUDEV_Dashboard::$settings->get( 'remote_access' ); |
| 154 |
} else { |
| 155 |
return WPMUDEV_Dashboard::$site->get_option( 'remote_access' ); |
| 156 |
} |
| 157 |
} |
| 158 |
/** |
| 159 |
* Checks if the Hub connector is connected. |
| 160 |
* |
| 161 |
* @return bool True if connected, false otherwise. |
| 162 |
*/ |
| 163 |
public static function get_status(): bool { |
| 164 |
return API::get()->is_logged_in(); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Checks if Hub Connector is connected. If Dash plugin is not installed Hub connector can take over. |
| 169 |
* |
| 170 |
* @return bool |
| 171 |
*/ |
| 172 |
public function hub_connector_connected(): bool { |
| 173 |
if ( $this->is_dash_activated() ) { |
| 174 |
$dash_api = WPMUDEV_Dashboard::$api; |
| 175 |
$connected = (bool) $dash_api->has_key(); |
| 176 |
} else { |
| 177 |
$connected = self::get_status(); |
| 178 |
} |
| 179 |
|
| 180 |
return $connected; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Upgrade the method to get api key from Dashboard plugin or Hub Connector module. |
| 185 |
* |
| 186 |
* @return string |
| 187 |
*/ |
| 188 |
public function get_api_key(): string { |
| 189 |
if ( $this->is_dash_activated() ) { |
| 190 |
$api_key = WPMUDEV_Dashboard::$api->get_key(); |
| 191 |
} else { |
| 192 |
$api_key = API::get()->get_api_key(); |
| 193 |
} |
| 194 |
|
| 195 |
return $api_key; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Get Membership type. |
| 200 |
* |
| 201 |
* @return string |
| 202 |
*/ |
| 203 |
public function get_membership_type() { |
| 204 |
if ( $this->is_dash_activated() ) { |
| 205 |
return WPMUDEV_Dashboard::$api->get_membership_status(); |
| 206 |
} elseif ( self::get_status() ) { |
| 207 |
return Data::get()->membership_type(); |
| 208 |
} |
| 209 |
return 'free'; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Check if the membership type is expired. |
| 214 |
* |
| 215 |
* @return bool True if membership is expired, false otherwise. |
| 216 |
*/ |
| 217 |
public function is_expired_membership_type(): bool { |
| 218 |
return 'expired' === $this->get_membership_type(); |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Logout from hub. |
| 223 |
* |
| 224 |
* @return array|bool|\WP_Error |
| 225 |
*/ |
| 226 |
public function logout() { |
| 227 |
return API::get()->logout(); |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Check if site is connected to HUB via HCM or Dash. |
| 232 |
* |
| 233 |
* @return bool |
| 234 |
*/ |
| 235 |
public function is_site_connected_to_hub_via_hcm_or_dash(): bool { |
| 236 |
return $this->hub_connector_connected() || self::get_status(); |
| 237 |
} |
| 238 |
} |
| 239 |
|