| @@ -1,24 +1,28 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * Handle Dashboard based functionalities of WPMUDEV class. |
| 4 | 4 | * |
| 5 | - * @package WP_Defender\Behavior | |
| 5 | + * @package WP_Defender\Traits | |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace WP_Defender\Traits; |
| 9 | 9 | |
| 10 | -/** | |
| 11 | - * Traits to handle Dashboard based functionalities of WPMUDEV class. | |
| 12 | - */ | |
| 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 | + | |
| 13 | 15 | trait Defender_Dashboard_Client { |
| 16 | + use \WP_Defender\Traits\Plugin; | |
| 17 | + | |
| 14 | 18 | /** |
| 15 | 19 | * Get membership status. |
| 16 | 20 | * |
| 17 | 21 | * @return bool |
| 18 | 22 | */ |
| 19 | - public function is_pro() { | |
| 20 | - return $this->get_apikey() !== false; | |
| 23 | + public function is_pro(): bool { | |
| 24 | + return class_exists( '\WPMUDEV_Dashboard' ) && method_exists( $this, 'get_apikey' ) && $this->get_apikey() !== false; | |
| 21 | 25 | } |
| 22 | 26 | |
| 23 | 27 | /** |
| 24 | 28 | * Check if user is a paid one in WPMU DEV. |
| @@ -24,36 +28,16 @@ | ||
| 24 | 28 | * Check if user is a paid one in WPMU DEV. |
| 25 | 29 | * |
| 26 | 30 | * @return bool |
| 27 | 31 | */ |
| 28 | - public function is_member() { | |
| 32 | + public function is_member(): bool { | |
| 29 | 33 | if ( |
| 30 | - class_exists( 'WPMUDEV_Dashboard' ) | |
| 31 | - && method_exists( 'WPMUDEV_Dashboard_Api', 'get_membership_projects' ) | |
| 34 | + $this->is_dash_activated() && method_exists( WPMUDEV_Dashboard::$upgrader, 'user_can_install' ) | |
| 32 | 35 | ) { |
| 33 | - if ( method_exists( 'WPMUDEV_Dashboard_Api', 'get_membership_status' ) ) { | |
| 34 | - $type = \WPMUDEV_Dashboard::$api->get_membership_status(); | |
| 35 | - } elseif ( method_exists( 'WPMUDEV_Dashboard_Api', 'get_membership_type' ) ) { | |
| 36 | - $type = \WPMUDEV_Dashboard::$api->get_membership_type(); | |
| 37 | - } else { | |
| 38 | - return false; | |
| 39 | - } | |
| 40 | - | |
| 41 | - $projects = \WPMUDEV_Dashboard::$api->get_membership_projects(); | |
| 42 | - $def_pid = 1081723; | |
| 43 | - | |
| 44 | - if ( | |
| 45 | - ( 'unit' === $type && in_array( $def_pid, $projects, true ) ) | |
| 46 | - || ( 'single' === $type && $def_pid === $projects ) | |
| 47 | - ) { | |
| 48 | - return true; | |
| 49 | - } | |
| 50 | - | |
| 51 | - if ( function_exists( 'is_wpmudev_member' ) ) { | |
| 52 | - return is_wpmudev_member(); | |
| 53 | - } | |
| 54 | - | |
| 55 | - return false; | |
| 36 | + return WPMUDEV_Dashboard::$upgrader->user_can_install( | |
| 37 | + Config_Hub_Helper::WDP_ID, | |
| 38 | + true | |
| 39 | + ); | |
| 56 | 40 | } |
| 57 | 41 | |
| 58 | 42 | return false; |
| 59 | 43 | } |
| @@ -60,20 +44,18 @@ | ||
| 60 | 44 | |
| 61 | 45 | /** |
| 62 | 46 | * Check if user is a WPMU DEV admin. |
| 63 | 47 | * |
| 48 | + * @return bool | |
| 64 | 49 | * @since 2.6.3 |
| 65 | - * | |
| 66 | - * @return bool | |
| 67 | 50 | */ |
| 68 | - public function is_wpmu_dev_admin() { | |
| 51 | + public function is_wpmu_dev_admin(): bool { | |
| 69 | 52 | if ( |
| 70 | - class_exists( 'WPMUDEV_Dashboard' ) | |
| 71 | - && method_exists( 'WPMUDEV_Dashboard_Site', 'allowed_user' ) | |
| 53 | + 'dashboard' === $this->resolve_connection_mode() && | |
| 54 | + is_object( WPMUDEV_Dashboard::$site ) && | |
| 55 | + method_exists( WPMUDEV_Dashboard::$site, 'allowed_user' ) | |
| 72 | 56 | ) { |
| 73 | - $user_id = get_current_user_id(); | |
| 74 | - | |
| 75 | - return \WPMUDEV_Dashboard::$site->allowed_user( $user_id ); | |
| 57 | + return WPMUDEV_Dashboard::$site->allowed_user( get_current_user_id() ); | |
| 76 | 58 | } |
| 77 | 59 | |
| 78 | 60 | return false; |
| 79 | 61 | } |
| @@ -78,25 +60,46 @@ | ||
| 78 | 60 | return false; |
| 79 | 61 | } |
| 80 | 62 | |
| 81 | 63 | /** |
| 82 | - * Bring the plugin menu title. | |
| 64 | + * Returns whether the current install is a Pro version. | |
| 83 | 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 | + * | |
| 84 | 90 | * @return string Menu title. |
| 85 | 91 | */ |
| 86 | - public function get_menu_title() { | |
| 87 | - if ( $this->is_pro() ) { | |
| 88 | - $menu_title = esc_html__( 'Defender Pro', 'wpdef' ); | |
| 89 | - } else { | |
| 90 | - // Check if it's Pro but user logged the WPMU DEV Dashboard out. | |
| 91 | - require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 92 | - $menu_title = file_exists( WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . WP_DEFENDER_PRO_PATH ) | |
| 93 | - && is_plugin_active( WP_DEFENDER_PRO_PATH ) | |
| 94 | - ? esc_html__( 'Defender Pro', 'wpdef' ) | |
| 95 | - : esc_html__( 'Defender', 'wpdef' ); | |
| 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 | + ); | |
| 96 | 99 | } |
| 97 | 100 | |
| 98 | - return $menu_title; | |
| 101 | + return esc_html__( 'Defender', 'defender-security' ); | |
| 99 | 102 | } |
| 100 | 103 | |
| 101 | 104 | /** |
| 102 | 105 | * Return icon svg image. |
| @@ -102,16 +105,189 @@ | ||
| 102 | 105 | * Return icon svg image. |
| 103 | 106 | * |
| 104 | 107 | * @return string |
| 105 | 108 | */ |
| 106 | - public function get_menu_icon() { | |
| 109 | + public function get_menu_icon(): string { | |
| 107 | 110 | ob_start(); |
| 108 | 111 | ?> |
| 109 | - <svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg"> | |
| 110 | - <path fill-rule="evenodd" clip-rule="evenodd" 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" fill="#F0F6FC" fill-opacity="0.6"/> | |
| 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"/> | |
| 111 | 114 | </svg> |
| 112 | 115 | <?php |
| 113 | 116 | $svg = ob_get_clean(); |
| 114 | 117 | |
| 115 | - return 'data:image/svg+xml;base64,' . base64_encode( $svg ); | |
| 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(); | |
| 116 | 292 | } |
| 117 | 293 | } |