| 1 |
<?php |
| 2 |
/** |
| 3 |
* The data class. |
| 4 |
* |
| 5 |
* @link http://wpmudev.com |
| 6 |
* @since 1.0.0 |
| 7 |
* @author Joel James <[email protected]> |
| 8 |
* @package WPMUDEV\Hub\Connector |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace WPMUDEV\Hub\Connector; |
| 12 |
|
| 13 |
/** |
| 14 |
* Class Data |
| 15 |
*/ |
| 16 |
class Data { |
| 17 |
|
| 18 |
use Singleton; |
| 19 |
|
| 20 |
/** |
| 21 |
* Returns the URL with wpmudev.com base URL prefixed. |
| 22 |
* |
| 23 |
* If a custom server is configured, it will be used as base url. |
| 24 |
* |
| 25 |
* @since 1.0.0 |
| 26 |
* |
| 27 |
* @param string $path Path to suffix. |
| 28 |
* |
| 29 |
* @return string The full URL. |
| 30 |
*/ |
| 31 |
public function server_url( $path = '' ) { |
| 32 |
$base_url = 'https://wpmudev.com/'; |
| 33 |
// If custom api server is set. |
| 34 |
if ( defined( '\WPMUDEV_CUSTOM_API_SERVER' ) && ! empty( \WPMUDEV_CUSTOM_API_SERVER ) ) { |
| 35 |
$base_url = trailingslashit( \WPMUDEV_CUSTOM_API_SERVER ); |
| 36 |
} |
| 37 |
|
| 38 |
return $base_url . $path; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Returns current full URL. |
| 43 |
* |
| 44 |
* @since 1.0.0 |
| 45 |
* |
| 46 |
* @return string The full URL. |
| 47 |
*/ |
| 48 |
public function current_url() { |
| 49 |
$parts = wp_parse_url( home_url() ); |
| 50 |
|
| 51 |
// Attempt to get from parsed data. |
| 52 |
if ( ! empty( $parts['scheme'] ) && ! empty( $parts['host'] ) ) { |
| 53 |
return "{$parts['scheme']}://{$parts['host']}" . add_query_arg( array() ); |
| 54 |
} |
| 55 |
|
| 56 |
return add_query_arg( array() ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Returns the canonical site_url that should be used for the site in the hub. |
| 61 |
* |
| 62 |
* Define WPMUDEV_HUB_SITE_URL to override or make static the url it should show as |
| 63 |
* in the hub. Defaults to network_site_url() which may be dynamically filtered |
| 64 |
* by some plugins and hosting providers. |
| 65 |
* |
| 66 |
* @since 1.0.0 |
| 67 |
* |
| 68 |
* @return string |
| 69 |
*/ |
| 70 |
public function network_site_url() { |
| 71 |
return defined( '\WPMUDEV_HUB_SITE_URL' ) ? \WPMUDEV_HUB_SITE_URL : network_site_url(); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Returns the canonical home_url that should be used for the site in the hub. |
| 76 |
* |
| 77 |
* Define WPMUDEV_HUB_HOME_URL to override or make static the url it should show as |
| 78 |
* in the hub. Defaults to WPMUDEV_HUB_SITE_URL if set, or network_home_url() which |
| 79 |
* may be dynamically filtered by some plugins and hosting providers. |
| 80 |
* |
| 81 |
* @since 1.0.0 |
| 82 |
* |
| 83 |
* @return string |
| 84 |
*/ |
| 85 |
public function network_home_url() { |
| 86 |
if ( defined( '\WPMUDEV_HUB_HOME_URL' ) ) { |
| 87 |
return \WPMUDEV_HUB_HOME_URL; |
| 88 |
} elseif ( defined( '\WPMUDEV_HUB_SITE_URL' ) ) { |
| 89 |
return \WPMUDEV_HUB_SITE_URL; |
| 90 |
} else { |
| 91 |
return network_home_url(); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Returns the canonical home_url that should be used for the site in the hub. |
| 97 |
* |
| 98 |
* Define WPMUDEV_HUB_ADMIN_URL to override or make static the url it should show as |
| 99 |
* in the hub. Defaults to deriving from WPMUDEV_HUB_SITE_URL if set, or network_admin_url() |
| 100 |
* which may be dynamically filtered by some plugins and hosting providers. |
| 101 |
* |
| 102 |
* @since 1.0.0 |
| 103 |
* |
| 104 |
* @return string |
| 105 |
*/ |
| 106 |
public function network_admin_url() { |
| 107 |
if ( defined( '\WPMUDEV_HUB_ADMIN_URL' ) ) { |
| 108 |
return \WPMUDEV_HUB_ADMIN_URL; |
| 109 |
} elseif ( defined( '\WPMUDEV_HUB_SITE_URL' ) ) { |
| 110 |
return is_multisite() ? trailingslashit( \WPMUDEV_HUB_SITE_URL ) . 'wp-admin/network/' : trailingslashit( \WPMUDEV_HUB_SITE_URL ) . 'wp-admin/'; |
| 111 |
} else { |
| 112 |
return network_admin_url(); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* The proper way to get details about the current membership. |
| 118 |
* |
| 119 |
* @since 1.0.0 |
| 120 |
* |
| 121 |
* @return array |
| 122 |
*/ |
| 123 |
public function membership_data() { |
| 124 |
$data = Options::get( 'membership_data', array() ); |
| 125 |
// Basic sanitation, to avoid incompatible return values. |
| 126 |
if ( ! is_array( $data ) ) { |
| 127 |
$data = array(); |
| 128 |
} |
| 129 |
|
| 130 |
// Make sure it's in correct structure. |
| 131 |
$data = wp_parse_args( |
| 132 |
$data, |
| 133 |
array( |
| 134 |
'membership' => '', |
| 135 |
'membership_full_level' => '', |
| 136 |
'membership_projects' => array(), |
| 137 |
'membership_excluded_projects' => array(), |
| 138 |
'membership_access' => array(), |
| 139 |
) |
| 140 |
); |
| 141 |
|
| 142 |
/** |
| 143 |
* Filter to modify raw membership data. |
| 144 |
* |
| 145 |
* @since 1.0.0 |
| 146 |
* |
| 147 |
* @param array $data Membership data. |
| 148 |
*/ |
| 149 |
return apply_filters( 'wpmudev_hub_connector_get_membership_data', $data ); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Get the current Hub site ID. |
| 154 |
* |
| 155 |
* @since 1.0.0 |
| 156 |
* |
| 157 |
* @return int |
| 158 |
*/ |
| 159 |
public function hub_site_id() { |
| 160 |
$membership = $this->membership_data(); |
| 161 |
|
| 162 |
return $membership['hub_site_id'] ?? 0; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Get current membership type. |
| 167 |
* |
| 168 |
* Possible return values: |
| 169 |
* 'free' - Free hub membership. |
| 170 |
* 'single' - Single membership (i.e. only 1 project is licensed) |
| 171 |
* 'unit' - One or more projects licensed |
| 172 |
* 'full' - Full membership, no restrictions. |
| 173 |
* 'paused' - Membership access is paused. |
| 174 |
* 'expired' - Expired membership. |
| 175 |
* '' - (empty string) If user is not logged in or with an unknown type. |
| 176 |
* |
| 177 |
* @since 1.0.0 |
| 178 |
* |
| 179 |
* @return string The membership type. |
| 180 |
*/ |
| 181 |
public function membership_type() { |
| 182 |
$data = $this->membership_data(); |
| 183 |
|
| 184 |
// Default type is empty. |
| 185 |
$type = ''; |
| 186 |
|
| 187 |
// Available membership types. |
| 188 |
$types = array( |
| 189 |
'full', |
| 190 |
'unit', |
| 191 |
'free', |
| 192 |
'paused', |
| 193 |
'expired', |
| 194 |
); |
| 195 |
|
| 196 |
// All possible string values. |
| 197 |
if ( is_string( $data['membership'] ) && in_array( $data['membership'], $types, true ) ) { |
| 198 |
$type = $data['membership']; |
| 199 |
} elseif ( |
| 200 |
is_numeric( $data['membership'] ) |
| 201 |
|| ( |
| 202 |
is_bool( $data['membership'] ) |
| 203 |
&& isset( $data['membership_full_level'] ) |
| 204 |
&& is_numeric( $data['membership_full_level'] ) |
| 205 |
) |
| 206 |
) { |
| 207 |
$type = 'single'; |
| 208 |
} |
| 209 |
|
| 210 |
return $type; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Get the list of installed plugins. |
| 215 |
* |
| 216 |
* We will exclude WPMUDEV plugins from the list. |
| 217 |
* |
| 218 |
* @since 1.0.0 |
| 219 |
* |
| 220 |
* @return array |
| 221 |
*/ |
| 222 |
public function plugins() { |
| 223 |
// Make sure required functions are ready. |
| 224 |
if ( ! function_exists( 'get_plugins' ) ) { |
| 225 |
require_once \ABSPATH . 'wp-admin/includes/plugin.php'; |
| 226 |
} |
| 227 |
|
| 228 |
$plugins = array(); |
| 229 |
$installed = get_plugins(); |
| 230 |
|
| 231 |
// Extract and collect details we need. |
| 232 |
foreach ( $installed as $slug => $data ) { |
| 233 |
// Skip WPMUDEV plugins because we get them in different list. |
| 234 |
if ( ! empty( $data['WDP ID'] ) ) { |
| 235 |
continue; |
| 236 |
} |
| 237 |
|
| 238 |
// Only network active plugin should be considered as active. |
| 239 |
$active = is_multisite() ? is_plugin_active_for_network( $slug ) : is_plugin_active( $slug ); |
| 240 |
|
| 241 |
$plugins[ $slug ] = array( |
| 242 |
'name' => $data['Name'], |
| 243 |
'version' => $data['Version'], |
| 244 |
'plugin_url' => $data['PluginURI'], |
| 245 |
'author' => $data['Author'], |
| 246 |
'author_url' => $data['AuthorURI'], |
| 247 |
'network' => $data['Network'], |
| 248 |
'active' => $active, |
| 249 |
); |
| 250 |
} |
| 251 |
|
| 252 |
return $plugins; |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Get the list of installed themes. |
| 257 |
* |
| 258 |
* @since 1.0.0 |
| 259 |
* |
| 260 |
* @return array |
| 261 |
*/ |
| 262 |
public function themes() { |
| 263 |
$themes = array(); |
| 264 |
$installed = wp_get_themes(); |
| 265 |
$stylesheet = get_stylesheet(); |
| 266 |
|
| 267 |
foreach ( $installed as $slug => $theme ) { |
| 268 |
if ( is_multisite() ) { |
| 269 |
$active = $theme->is_allowed() || $stylesheet === $slug; // network enabled or on main site. |
| 270 |
} else { |
| 271 |
// If the theme is available on main site it's "active". |
| 272 |
$active = $stylesheet === $slug; |
| 273 |
} |
| 274 |
|
| 275 |
$themes[ $slug ] = array( |
| 276 |
'name' => $theme->display( 'Name', false ), |
| 277 |
'version' => $theme->display( 'Version', false ), |
| 278 |
'author' => $theme->display( 'Author', false ), |
| 279 |
'author_url' => $theme->display( 'AuthorURI', false ), |
| 280 |
'screenshot' => $theme->get_screenshot(), |
| 281 |
'parent' => $theme->parent() ? $theme->get_template() : false, |
| 282 |
'active' => $active, |
| 283 |
); |
| 284 |
} |
| 285 |
|
| 286 |
return $themes; |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* Get the list of WPMUDEV projects. |
| 291 |
* |
| 292 |
* Only plugins are in this list as of now. |
| 293 |
* |
| 294 |
* @since 1.0.0 |
| 295 |
* |
| 296 |
* @return array |
| 297 |
*/ |
| 298 |
public function wpmudev_projects() { |
| 299 |
if ( ! function_exists( 'get_plugins' ) ) { |
| 300 |
require_once \ABSPATH . 'wp-admin/includes/plugin.php'; |
| 301 |
} |
| 302 |
|
| 303 |
$projects = array(); |
| 304 |
$installed = get_plugins(); |
| 305 |
|
| 306 |
// Extract and collect details we need. |
| 307 |
foreach ( $installed as $slug => $data ) { |
| 308 |
// Not a WPMUDEV Pro plugin. |
| 309 |
if ( empty( $data['WDP ID'] ) ) { |
| 310 |
continue; |
| 311 |
} |
| 312 |
|
| 313 |
// Project ID. |
| 314 |
$project_id = $data['WDP ID']; |
| 315 |
|
| 316 |
// On multisite, only consider network active plugins as active. |
| 317 |
$active = is_multisite() ? is_plugin_active_for_network( $slug ) : is_plugin_active( $slug ); |
| 318 |
|
| 319 |
/** |
| 320 |
* Collect extra data from individual plugins. |
| 321 |
* |
| 322 |
* @since 1.0.0 |
| 323 |
* |
| 324 |
* @param string $extra Default extra data is an empty string. |
| 325 |
* @param int $pid Project ID. |
| 326 |
*/ |
| 327 |
$extra = apply_filters( 'wpmudev_api_project_extra_data', '', $project_id ); |
| 328 |
|
| 329 |
$projects[ $project_id ] = array( |
| 330 |
'version' => $data['Version'], |
| 331 |
'active' => $active, |
| 332 |
'extra' => $extra, |
| 333 |
); |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* Allows modification of the plugin data that is sent to the server. |
| 338 |
* |
| 339 |
* @since 1.0.0 |
| 340 |
* |
| 341 |
* @param array $projects The whole array of project details. |
| 342 |
*/ |
| 343 |
return apply_filters( 'wpmudev_api_project_data', $projects ); |
| 344 |
} |
| 345 |
|
| 346 |
/** |
| 347 |
* Get currently logged in member user data. |
| 348 |
* |
| 349 |
* @since 1.0.0 |
| 350 |
* @since 1.0.7 Internally cached by default. Introduce optional `$force` param |
| 351 |
* |
| 352 |
* @param bool $force Force data from API. |
| 353 |
* |
| 354 |
* @return array |
| 355 |
*/ |
| 356 |
public function profile_data( bool $force = false ) { |
| 357 |
// Get profile data. |
| 358 |
$profile = Options::get_transient( 'profile' ); |
| 359 |
|
| 360 |
if ( empty( $profile ) || $force ) { |
| 361 |
$profile = API::get()->get_profile(); |
| 362 |
|
| 363 |
// Eagerly store to transient to avoid API abuse, even if API is error. |
| 364 |
Options::set_transient( 'profile', is_array( $profile ) ? $profile : true, HOUR_IN_SECONDS ); |
| 365 |
$profile = Options::get_transient( 'profile' ); |
| 366 |
} |
| 367 |
|
| 368 |
if ( is_wp_error( $profile ) || empty( $profile ) || ! is_array( $profile ) ) { |
| 369 |
$profile = array(); |
| 370 |
} |
| 371 |
|
| 372 |
// Make sure the structure is correct. |
| 373 |
$profile = wp_parse_args( |
| 374 |
$profile, |
| 375 |
array( |
| 376 |
'avatar' => '', |
| 377 |
'member_since' => '', |
| 378 |
'name' => '', |
| 379 |
'title' => '', |
| 380 |
'user_name' => '', |
| 381 |
) |
| 382 |
); |
| 383 |
|
| 384 |
/** |
| 385 |
* Allows modification of the profile data. |
| 386 |
* |
| 387 |
* @since 1.0.0 |
| 388 |
* |
| 389 |
* @param array $profile Profile data. |
| 390 |
*/ |
| 391 |
return apply_filters( 'wpmudev_api_profile_data', $profile ); |
| 392 |
} |
| 393 |
|
| 394 |
/** |
| 395 |
* Returns an array of projects id available on plan / membership. |
| 396 |
* |
| 397 |
* @since 1.0.7 |
| 398 |
* @return array |
| 399 |
*/ |
| 400 |
public function membership_projects(): array { |
| 401 |
$type = $this->membership_type(); |
| 402 |
|
| 403 |
if ( 'full' === $type ) { |
| 404 |
return array(); |
| 405 |
} |
| 406 |
$data = $this->membership_data(); |
| 407 |
// For free and unit memberships. |
| 408 |
if ( in_array( $type, array( 'free', 'unit' ), true ) ) { |
| 409 |
$projects = is_array( $data['membership_projects'] ) ? $data['membership_projects'] : array(); |
| 410 |
foreach ( $projects as $i => $p ) { |
| 411 |
$projects[ $i ] = intval( $p ); |
| 412 |
} |
| 413 |
|
| 414 |
return $projects; |
| 415 |
} |
| 416 |
if ( is_numeric( $data['membership'] ) ) { |
| 417 |
return array( intval( $data['membership'] ) ); |
| 418 |
} |
| 419 |
if ( is_bool( $data['membership'] ) && is_numeric( $data['membership_full_level'] ) ) { |
| 420 |
return array( intval( $data['membership_full_level'] ) ); |
| 421 |
} |
| 422 |
|
| 423 |
return array(); |
| 424 |
} |
| 425 |
|
| 426 |
/** |
| 427 |
* Get projects that are strictly forbidden to be installed or updated for |
| 428 |
* current membership level. |
| 429 |
* |
| 430 |
* @since 1.0.7 |
| 431 |
* @return array |
| 432 |
*/ |
| 433 |
public function membership_excluded_projects(): array { |
| 434 |
$excluded = array(); |
| 435 |
$data = $this->membership_data(); |
| 436 |
if ( false === empty( $data['membership_excluded_projects'] ) && is_array( $data['membership_excluded_projects'] ) ) { |
| 437 |
foreach ( $data['membership_excluded_projects'] as $pid ) { |
| 438 |
$excluded[] = intval( $pid ); |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
return $excluded; |
| 443 |
} |
| 444 |
|
| 445 |
/** |
| 446 |
* Checks if access is allowed for membership plan by access id string. |
| 447 |
* |
| 448 |
* @since 1.0.7 |
| 449 |
* |
| 450 |
* @param string $access Access ID. |
| 451 |
* |
| 452 |
* @return bool |
| 453 |
*/ |
| 454 |
public function membership_has_access( string $access ): bool { |
| 455 |
$data = $this->membership_data(); |
| 456 |
$accesses = $data['membership_access']; |
| 457 |
|
| 458 |
// The membership_access can be boolean true for full access, or array with allowed features strings. |
| 459 |
if ( true === $accesses ) { |
| 460 |
return true; |
| 461 |
} |
| 462 |
|
| 463 |
if ( false === is_array( $accesses ) ) { |
| 464 |
return false; |
| 465 |
} |
| 466 |
|
| 467 |
return in_array( $access, $accesses, true ); |
| 468 |
} |
| 469 |
|
| 470 |
/** |
| 471 |
* Get full WPMU DEV Hosting ID. |
| 472 |
* |
| 473 |
* @since 1.1.0 |
| 474 |
* |
| 475 |
* @return string |
| 476 |
*/ |
| 477 |
public function get_full_wpmu_dev_hosting_id(): string { |
| 478 |
if ( ! defined( 'WPMUDEV_HOSTING_SITE_ID' ) && ! isset( $_SERVER['WPMUDEV_HOSTED'] ) ) { |
| 479 |
return ''; |
| 480 |
} |
| 481 |
|
| 482 |
$server_id = defined( 'WPMUDEV_HOSTING_SITE_ID' ) ? WPMUDEV_HOSTING_SITE_ID : gethostname(); |
| 483 |
$website_id = ( defined( 'WPMUDEV_HOSTING_WEBSITE_ID' ) && ! empty( WPMUDEV_HOSTING_WEBSITE_ID ) ) ? WPMUDEV_HOSTING_WEBSITE_ID : ''; |
| 484 |
|
| 485 |
if ( empty( $website_id ) ) { // non shared sub-hosting. |
| 486 |
return $server_id; // return server id. |
| 487 |
} |
| 488 |
|
| 489 |
return implode( '-', array( $website_id, $server_id ) ); |
| 490 |
} |
| 491 |
} |
| 492 |
|