| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Freemius |
| 4 |
* @copyright Copyright (c) 2015, Freemius, Inc. |
| 5 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 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 |
* @var number |
| 34 |
*/ |
| 35 |
public $pricing_id; |
| 36 |
/** |
| 37 |
* @var int|null |
| 38 |
*/ |
| 39 |
public $quota; |
| 40 |
/** |
| 41 |
* @var int |
| 42 |
*/ |
| 43 |
public $activated; |
| 44 |
/** |
| 45 |
* @var int |
| 46 |
*/ |
| 47 |
public $activated_local; |
| 48 |
/** |
| 49 |
* @var string |
| 50 |
*/ |
| 51 |
public $expiration; |
| 52 |
/** |
| 53 |
* @var string |
| 54 |
*/ |
| 55 |
public $secret_key; |
| 56 |
/** |
| 57 |
* @var bool $is_free_localhost Defaults to true. If true, allow unlimited localhost installs with the same |
| 58 |
* license. |
| 59 |
*/ |
| 60 |
public $is_free_localhost; |
| 61 |
/** |
| 62 |
* @var bool $is_block_features Defaults to true. If false, don't block features after license expiry - only |
| 63 |
* block updates and support. |
| 64 |
*/ |
| 65 |
public $is_block_features; |
| 66 |
/** |
| 67 |
* @var bool |
| 68 |
*/ |
| 69 |
public $is_cancelled; |
| 70 |
|
| 71 |
#endregion Properties |
| 72 |
|
| 73 |
/** |
| 74 |
* @param stdClass|bool $license |
| 75 |
*/ |
| 76 |
function __construct( $license = false ) { |
| 77 |
parent::__construct( $license ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Get entity type. |
| 82 |
* |
| 83 |
* @return string |
| 84 |
*/ |
| 85 |
static function get_type() { |
| 86 |
return 'license'; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Check how many site activations left. |
| 91 |
* |
| 92 |
* @author Vova Feldman (@svovaf) |
| 93 |
* @since 1.0.5 |
| 94 |
* |
| 95 |
* @return int |
| 96 |
*/ |
| 97 |
function left() { |
| 98 |
if ( ! $this->is_active() || $this->is_expired() ) { |
| 99 |
return 0; |
| 100 |
} |
| 101 |
|
| 102 |
if ( $this->is_unlimited() ) { |
| 103 |
return 999; |
| 104 |
} |
| 105 |
|
| 106 |
return ( $this->quota - $this->activated - ( $this->is_free_localhost ? 0 : $this->activated_local ) ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Check if single site license. |
| 111 |
* |
| 112 |
* @author Vova Feldman (@svovaf) |
| 113 |
* @since 1.1.8.1 |
| 114 |
* |
| 115 |
* @return bool |
| 116 |
*/ |
| 117 |
function is_single_site() { |
| 118 |
return ( is_numeric( $this->quota ) && 1 == $this->quota ); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* @author Vova Feldman (@svovaf) |
| 123 |
* @since 1.0.5 |
| 124 |
* |
| 125 |
* @return bool |
| 126 |
*/ |
| 127 |
function is_expired() { |
| 128 |
return ! $this->is_lifetime() && ( strtotime( $this->expiration ) < WP_FS__SCRIPT_START_TIME ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Check if license is not expired. |
| 133 |
* |
| 134 |
* @author Vova Feldman (@svovaf) |
| 135 |
* @since 1.2.1 |
| 136 |
* |
| 137 |
* @return bool |
| 138 |
*/ |
| 139 |
function is_valid() { |
| 140 |
return ! $this->is_expired(); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* @author Vova Feldman (@svovaf) |
| 145 |
* @since 1.0.6 |
| 146 |
* |
| 147 |
* @return bool |
| 148 |
*/ |
| 149 |
function is_lifetime() { |
| 150 |
return is_null( $this->expiration ); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* @author Vova Feldman (@svovaf) |
| 155 |
* @since 1.2.0 |
| 156 |
* |
| 157 |
* @return bool |
| 158 |
*/ |
| 159 |
function is_unlimited() { |
| 160 |
return is_null( $this->quota ); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Check if license is fully utilized. |
| 165 |
* |
| 166 |
* @author Vova Feldman (@svovaf) |
| 167 |
* @since 1.0.6 |
| 168 |
* |
| 169 |
* @param bool $is_localhost |
| 170 |
* |
| 171 |
* @return bool |
| 172 |
*/ |
| 173 |
function is_utilized( $is_localhost = null ) { |
| 174 |
if ( is_null( $is_localhost ) ) { |
| 175 |
$is_localhost = WP_FS__IS_LOCALHOST_FOR_SERVER; |
| 176 |
} |
| 177 |
|
| 178 |
if ( $this->is_unlimited() ) { |
| 179 |
return false; |
| 180 |
} |
| 181 |
|
| 182 |
return ! ( $this->is_free_localhost && $is_localhost ) && |
| 183 |
( $this->quota <= $this->activated + ( $this->is_free_localhost ? 0 : $this->activated_local ) ); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* @author Vova Feldman (@svovaf) |
| 188 |
* @since 1.2.1 |
| 189 |
* |
| 190 |
* @return bool |
| 191 |
*/ |
| 192 |
function is_active() { |
| 193 |
return ( ! $this->is_cancelled ); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Check if license's plan features are enabled. |
| 198 |
* |
| 199 |
* - Either if plan not expired |
| 200 |
* - If expired, based on the configuration to block features or not. |
| 201 |
* |
| 202 |
* @author Vova Feldman (@svovaf) |
| 203 |
* @since 1.0.6 |
| 204 |
* |
| 205 |
* @return bool |
| 206 |
*/ |
| 207 |
function is_features_enabled() { |
| 208 |
return $this->is_active() && ( ! $this->is_block_features || ! $this->is_expired() ); |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Subscription considered to be new without any payments |
| 213 |
* if the license expires in less than 24 hours |
| 214 |
* from the license creation. |
| 215 |
* |
| 216 |
* @author Vova Feldman (@svovaf) |
| 217 |
* @since 1.0.9 |
| 218 |
* |
| 219 |
* @return bool |
| 220 |
*/ |
| 221 |
function is_first_payment_pending() { |
| 222 |
return ( WP_FS__TIME_24_HOURS_IN_SEC >= strtotime( $this->expiration ) - strtotime( $this->created ) ); |
| 223 |
} |
| 224 |
} |
| 225 |
|