| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('UD_CENTRAL_DIR')) die('Security check'); |
| 4 |
|
| 5 |
/* |
| 6 |
Handles entitlements. |
| 7 |
|
| 8 |
This default class handles entitlements in conjunction with the Simba Plugins Updates + Licensing Manager - https://wordpress.org/plugins/simba-plugin-updates-manager/ . Thus, you will need that plugin to be installed and active, if you want to handle licensing - otherwise, all usage is unlimited (for users who can get to the dashboard). |
| 9 |
|
| 10 |
If it is installed, then admins get unlimited access, and everyone else requires valid licenses. You can modify this with the updraftcentral_licencemanager_nolicencesrequired filter to remove all licensing; or, various other filters to replace things piecemeal. |
| 11 |
|
| 12 |
*/ |
| 13 |
|
| 14 |
if (!class_exists('UpdraftCentral_Licence_Manager')) : |
| 15 |
|
| 16 |
class UpdraftCentral_Licence_Manager { |
| 17 |
|
| 18 |
private $user = null; |
| 19 |
|
| 20 |
private $rc = null; |
| 21 |
|
| 22 |
private $no_licences_required = false; |
| 23 |
|
| 24 |
/** |
| 25 |
* Pass in a UpdraftCentral_User |
| 26 |
* |
| 27 |
* @param string $user |
| 28 |
* @param array $central_object |
| 29 |
*/ |
| 30 |
public function __construct($user, $central_object) { |
| 31 |
|
| 32 |
/* |
| 33 |
Filter: updraftcentral_licencemanager_nolicencesrequired |
| 34 |
Use this filter to de-activate licensing for specified users - allow unlimited access to everyone. By default, we limit it to admins. |
| 35 |
(You then also need to hook into various other filters in order to actually do something useful - the default values allow everyone unlimited licences) |
| 36 |
e.g. to optionally integrate with the Simba updates/licensing manager |
| 37 |
$simba_manager_installed = class_exists('Updraft_Manager_Premium'); |
| 38 |
$no_licences_required = ($no_licences_required || !$simba_manager_installed); |
| 39 |
$require_spm_version = '1.5.15'; |
| 40 |
if (!$no_licences_required && (!$simba_manager_installed || !defined('UDMANAGER_VERSION') || !version_compare(UDMANAGER_VERSION, $require_spm_version, '>='))) { |
| 41 |
throw new Exception(sprintf('The Simba updates + licensing manager (version %s or later) is required - please install and activate it.', $require_spm_version)); |
| 42 |
} |
| 43 |
*/ |
| 44 |
|
| 45 |
$this->no_licences_required = apply_filters('updraftcentral_licencemanager_nolicencesrequired', true, $user); |
| 46 |
$this->user = $user; |
| 47 |
$this->rc = $central_object; |
| 48 |
|
| 49 |
add_shortcode('updraftcentral_licences_in_use', array($this, 'how_many_licences_in_use')); |
| 50 |
add_shortcode('updraftcentral_licences_total', array($this, 'how_many_licences_available')); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Indicate whether a licence slot is available or not |
| 55 |
* |
| 56 |
* @used-by UpdraftCentral_User::add_site() |
| 57 |
* @param array $params any parameters that one need to filter. |
| 58 |
* |
| 59 |
* @return Boolean |
| 60 |
*/ |
| 61 |
public function is_slot_available($params = array()) { |
| 62 |
|
| 63 |
$licences_in_use = $this->how_many_licences_in_use(); |
| 64 |
$licences_available = $this->how_many_licences_available(); |
| 65 |
|
| 66 |
if ($this->no_licences_required) { |
| 67 |
$is_available = true; |
| 68 |
} else { |
| 69 |
$is_available = ($licences_available < 0 || $licences_in_use < $licences_available); |
| 70 |
} |
| 71 |
|
| 72 |
return apply_filters('updraftcentral_licencemanager_is_slot_available', $is_available, $this, $this->user, $licences_in_use, $licences_available, $params); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Used both internally, and as a shortcode |
| 77 |
* |
| 78 |
* @return Integer |
| 79 |
*/ |
| 80 |
public function how_many_licences_in_use() { |
| 81 |
|
| 82 |
if (!is_array($this->user->sites)) { |
| 83 |
$in_use = 0; |
| 84 |
} else { |
| 85 |
$in_use = count($this->user->sites); |
| 86 |
} |
| 87 |
|
| 88 |
return apply_filters('updraftcentral_licencemanager_how_many_licences_in_use', $in_use, $this, $this->user); |
| 89 |
} |
| 90 |
|
| 91 |
public function get_entitlements_expiring_in($expire_window_begin, $expire_window_end) { |
| 92 |
|
| 93 |
/* |
| 94 |
Example if integrating with Simba Updates/Licensing manager: |
| 95 |
return count(Updraft_Manager_Premium::db_get_all_entitlements($user->user_id, $expire_window_begin, $expire_window_end, array('updraft-central'), 'updraft-central')) |
| 96 |
No format for the returned array is specified or needed. The only important thing is that there's 1 entry for each entitlement. You can just push a value of 'true' on for each licence if there's no info needed. |
| 97 |
Returns -1 for 'unlimited' |
| 98 |
*/ |
| 99 |
|
| 100 |
return apply_filters('updraftcentral_licencemanager_get_entitlements_expiring_in', 0, $expire_window_begin, $expire_window_end, $this, $this->user); |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* How many licences recently expired |
| 106 |
* |
| 107 |
* @param integer $how_long_ago Default: 4 weeks |
| 108 |
* @return array |
| 109 |
*/ |
| 110 |
public function how_many_licences_recently_expired($how_long_ago = 2419200) { |
| 111 |
if ($this->no_licences_required) { |
| 112 |
$how_many = 0; |
| 113 |
} else { |
| 114 |
$time_now = time(); |
| 115 |
$how_many = $this->get_entitlements_expiring_in($time_now, ($time_now - $how_long_ago)); |
| 116 |
} |
| 117 |
|
| 118 |
return apply_filters('updraftcentral_how_many_licences_recently_expired', $how_many, $how_long_ago, $this, $this->user); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* How many licences soon expiring |
| 123 |
* |
| 124 |
* @param integer $in_next Default: 4 weeks |
| 125 |
* @return array |
| 126 |
*/ |
| 127 |
public function how_many_licences_soon_expiring($in_next = 2419200) { |
| 128 |
if ($this->no_licences_required) { |
| 129 |
$how_many = 0; |
| 130 |
} else { |
| 131 |
$time_now = time(); |
| 132 |
$how_many = $this->get_entitlements_expiring_in($time_now, ($time_now + $in_next)); |
| 133 |
} |
| 134 |
|
| 135 |
return apply_filters('updraftcentral_how_many_licences_soon_expiring', $how_many, $in_next, $this, $this->user); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Used both internally, and as a shortcode |
| 140 |
* |
| 141 |
* @return array |
| 142 |
*/ |
| 143 |
public function how_many_licences_available() { |
| 144 |
if ($this->no_licences_required) { |
| 145 |
$entitlements_available = -1; |
| 146 |
} else { |
| 147 |
$entitlements_available = $this->get_entitlements_expiring_in(time(), PHP_INT_MAX); |
| 148 |
} |
| 149 |
|
| 150 |
return apply_filters('updraftcentral_how_many_licences_available', $entitlements_available, $this, $this->user, $this->no_licences_required); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
endif; |
| 155 |
|