PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.7.0
Defender Security – Malware Scanner, Login Security & Firewall v5.7.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / traits / defender-dashboard-client.php

defender-dashboard-client.php in Defender Security – Malware Scanner, Login Security & Firewall 5.7.0, at src/traits/defender-dashboard-client.php

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