| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Freemius |
| 4 |
* @copyright Copyright (c) 2015, Freemius, Inc. |
| 5 |
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3 |
| 6 |
* @since 1.0.5 |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Class FS_Plugin_License |
| 15 |
*/ |
| 16 |
class FS_Plugin_License extends FS_Entity { |
| 17 |
|
| 18 |
#region Properties |
| 19 |
|
| 20 |
/** |
| 21 |
* @var number |
| 22 |
*/ |
| 23 |
public $plugin_id; |
| 24 |
/** |
| 25 |
* @var number |
| 26 |
*/ |
| 27 |
public $user_id; |
| 28 |
/** |
| 29 |
* @var number |
| 30 |
*/ |
| 31 |
public $plan_id; |
| 32 |
/** |
| 33 |
* @author Leo Fajardo (@leorw) |
| 34 |
* @since 2.3.0 |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
public $parent_plan_name; |
| 39 |
/** |
| 40 |
* @author Leo Fajardo (@leorw) |
| 41 |
* @since 2.3.0 |
| 42 |
* |
| 43 |
* @var string |
| 44 |
*/ |
| 45 |
public $parent_plan_title; |
| 46 |
/** |
| 47 |
* @author Leo Fajardo (@leorw) |
| 48 |
* @since 2.3.0 |
| 49 |
* |
| 50 |
* @var number |
| 51 |
*/ |
| 52 |
public $parent_license_id; |
| 53 |
/** |
| 54 |
* @author Leo Fajardo (@leorw) |
| 55 |
* @since 2.4.0 |
| 56 |
* |
| 57 |
* @var array |
| 58 |
*/ |
| 59 |
public $products; |
| 60 |
/** |
| 61 |
* @var number |
| 62 |
*/ |
| 63 |
public $pricing_id; |
| 64 |
/** |
| 65 |
* @var int|null |
| 66 |
*/ |
| 67 |
public $quota; |
| 68 |
/** |
| 69 |
* @var int |
| 70 |
*/ |
| 71 |
public $activated; |
| 72 |
/** |
| 73 |
* @var int |
| 74 |
*/ |
| 75 |
public $activated_local; |
| 76 |
/** |
| 77 |
* @var string |
| 78 |
*/ |
| 79 |
public $expiration; |
| 80 |
/** |
| 81 |
* @var string |
| 82 |
*/ |
| 83 |
public $secret_key; |
| 84 |
/** |
| 85 |
* @var bool |
| 86 |
*/ |
| 87 |
public $is_whitelabeled; |
| 88 |
/** |
| 89 |
* @var bool $is_free_localhost Defaults to true. If true, allow unlimited localhost installs with the same |
| 90 |
* license. |
| 91 |
*/ |
| 92 |
public $is_free_localhost; |
| 93 |
/** |
| 94 |
* @var bool $is_block_features Defaults to true. If false, don't block features after license expiry - only |
| 95 |
* block updates and support. |
| 96 |
*/ |
| 97 |
public $is_block_features; |
| 98 |
/** |
| 99 |
* @var bool |
| 100 |
*/ |
| 101 |
public $is_cancelled; |
| 102 |
|
| 103 |
#endregion Properties |
| 104 |
|
| 105 |
/** |
| 106 |
* @param stdClass|bool $license |
| 107 |
*/ |
| 108 |
function __construct( $license = false ) { |
| 109 |
parent::__construct( $license ); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Get entity type. |
| 114 |
* |
| 115 |
* @return string |
| 116 |
*/ |
| 117 |
static function get_type() { |
| 118 |
return 'license'; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Check how many site activations left. |
| 123 |
* |
| 124 |
* @author Vova Feldman (@svovaf) |
| 125 |
* @since 1.0.5 |
| 126 |
* |
| 127 |
* @return int |
| 128 |
*/ |
| 129 |
function left() { |
| 130 |
if ( ! $this->is_features_enabled() ) { |
| 131 |
return 0; |
| 132 |
} |
| 133 |
|
| 134 |
if ( $this->is_unlimited() ) { |
| 135 |
return 999; |
| 136 |
} |
| 137 |
|
| 138 |
return ( $this->quota - $this->activated - ( $this->is_free_localhost ? 0 : $this->activated_local ) ); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Check if single site license. |
| 143 |
* |
| 144 |
* @author Vova Feldman (@svovaf) |
| 145 |
* @since 1.1.8.1 |
| 146 |
* |
| 147 |
* @return bool |
| 148 |
*/ |
| 149 |
function is_single_site() { |
| 150 |
return ( is_numeric( $this->quota ) && 1 == $this->quota ); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* @author Vova Feldman (@svovaf) |
| 155 |
* @since 1.0.5 |
| 156 |
* |
| 157 |
* @return bool |
| 158 |
*/ |
| 159 |
function is_expired() { |
| 160 |
return ! $this->is_lifetime() && ( strtotime( $this->expiration ) < WP_FS__SCRIPT_START_TIME ); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Check if license is not expired. |
| 165 |
* |
| 166 |
* @author Vova Feldman (@svovaf) |
| 167 |
* @since 1.2.1 |
| 168 |
* |
| 169 |
* @return bool |
| 170 |
*/ |
| 171 |
function is_valid() { |
| 172 |
return ! $this->is_expired(); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* @author Vova Feldman (@svovaf) |
| 177 |
* @since 1.0.6 |
| 178 |
* |
| 179 |
* @return bool |
| 180 |
*/ |
| 181 |
function is_lifetime() { |
| 182 |
return is_null( $this->expiration ); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* @author Vova Feldman (@svovaf) |
| 187 |
* @since 1.2.0 |
| 188 |
* |
| 189 |
* @return bool |
| 190 |
*/ |
| 191 |
function is_unlimited() { |
| 192 |
return is_null( $this->quota ); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Check if license is fully utilized. |
| 197 |
* |
| 198 |
* @author Vova Feldman (@svovaf) |
| 199 |
* @since 1.0.6 |
| 200 |
* |
| 201 |
* @param bool|null $is_localhost |
| 202 |
* |
| 203 |
* @return bool |
| 204 |
*/ |
| 205 |
function is_utilized( $is_localhost = null ) { |
| 206 |
if ( is_null( $is_localhost ) ) { |
| 207 |
$is_localhost = WP_FS__IS_LOCALHOST_FOR_SERVER; |
| 208 |
} |
| 209 |
|
| 210 |
if ( $this->is_unlimited() ) { |
| 211 |
return false; |
| 212 |
} |
| 213 |
|
| 214 |
return ! ( $this->is_free_localhost && $is_localhost ) && |
| 215 |
( $this->quota <= $this->activated + ( $this->is_free_localhost ? 0 : $this->activated_local ) ); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Check if license can be activated. |
| 220 |
* |
| 221 |
* @author Vova Feldman (@svovaf) |
| 222 |
* @since 2.0.0 |
| 223 |
* |
| 224 |
* @param bool|null $is_localhost |
| 225 |
* |
| 226 |
* @return bool |
| 227 |
*/ |
| 228 |
function can_activate( $is_localhost = null ) { |
| 229 |
return ! $this->is_utilized( $is_localhost ) && $this->is_features_enabled(); |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Check if license can be activated on a given number of production and localhost sites. |
| 234 |
* |
| 235 |
* @author Vova Feldman (@svovaf) |
| 236 |
* @since 2.0.0 |
| 237 |
* |
| 238 |
* @param int $production_count |
| 239 |
* @param int $localhost_count |
| 240 |
* |
| 241 |
* @return bool |
| 242 |
*/ |
| 243 |
function can_activate_bulk( $production_count, $localhost_count ) { |
| 244 |
if ( $this->is_unlimited() ) { |
| 245 |
return true; |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* For simplicity, the logic will work as following: when given X sites to activate the license on, if it's |
| 250 |
* possible to activate on ALL of them, do the activation. If it's not possible to activate on ALL of them, |
| 251 |
* do NOT activate on any of them. |
| 252 |
*/ |
| 253 |
return ( $this->quota >= $this->activated + $production_count + ( $this->is_free_localhost ? 0 : $this->activated_local + $localhost_count ) ); |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* @author Vova Feldman (@svovaf) |
| 258 |
* @since 1.2.1 |
| 259 |
* |
| 260 |
* @return bool |
| 261 |
*/ |
| 262 |
function is_active() { |
| 263 |
return ( ! $this->is_cancelled ); |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Check if license's plan features are enabled. |
| 268 |
* |
| 269 |
* - Either if plan not expired |
| 270 |
* - If expired, based on the configuration to block features or not. |
| 271 |
* |
| 272 |
* @author Vova Feldman (@svovaf) |
| 273 |
* @since 1.0.6 |
| 274 |
* |
| 275 |
* @return bool |
| 276 |
*/ |
| 277 |
function is_features_enabled() { |
| 278 |
return $this->is_active() && ( ! $this->is_block_features || ! $this->is_expired() ); |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Subscription considered to be new without any payments |
| 283 |
* if the license expires in less than 24 hours |
| 284 |
* from the license creation. |
| 285 |
* |
| 286 |
* @author Vova Feldman (@svovaf) |
| 287 |
* @since 1.0.9 |
| 288 |
* |
| 289 |
* @return bool |
| 290 |
*/ |
| 291 |
function is_first_payment_pending() { |
| 292 |
if ( $this->is_lifetime() ) { |
| 293 |
return false; |
| 294 |
} |
| 295 |
|
| 296 |
return ( WP_FS__TIME_24_HOURS_IN_SEC >= strtotime( $this->expiration ) - strtotime( $this->created ) ); |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* @return int |
| 301 |
*/ |
| 302 |
function total_activations() { |
| 303 |
return ( $this->activated + $this->activated_local ); |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* @author Vova Feldman (@svovaf) |
| 308 |
* @since 2.3.1 |
| 309 |
* |
| 310 |
* @return string |
| 311 |
*/ |
| 312 |
function get_html_escaped_masked_secret_key() { |
| 313 |
return self::mask_secret_key_for_html( $this->secret_key ); |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* @author Vova Feldman (@svovaf) |
| 318 |
* @since 2.3.1 |
| 319 |
* |
| 320 |
* @param string $secret_key |
| 321 |
* |
| 322 |
* @return string |
| 323 |
*/ |
| 324 |
static function mask_secret_key_for_html( $secret_key ) { |
| 325 |
return ( |
| 326 |
// Initial 6 chars - sk_ABC |
| 327 |
htmlspecialchars( substr( $secret_key, 0, 6 ) ) . |
| 328 |
// Masking |
| 329 |
str_pad( '', ( strlen( $secret_key ) - 9 ) * 6, '•' ) . |
| 330 |
// Last 3 chars. |
| 331 |
htmlspecialchars( substr( $secret_key, - 3 ) ) |
| 332 |
); |
| 333 |
} |
| 334 |
} |
| 335 |
|