| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); |
| 3 |
|
| 4 |
/** |
| 5 |
* Imagify User class. |
| 6 |
* |
| 7 |
* @since 1.0 |
| 8 |
*/ |
| 9 |
class Imagify_User { |
| 10 |
/** |
| 11 |
* The Imagify user ID. |
| 12 |
* |
| 13 |
* @since 1.0 |
| 14 |
* |
| 15 |
* @var string |
| 16 |
* @access public |
| 17 |
*/ |
| 18 |
public $id; |
| 19 |
|
| 20 |
/** |
| 21 |
* The user email. |
| 22 |
* |
| 23 |
* @since 1.0 |
| 24 |
* |
| 25 |
* @var string |
| 26 |
* @access public |
| 27 |
*/ |
| 28 |
public $email; |
| 29 |
|
| 30 |
/** |
| 31 |
* The plan ID. |
| 32 |
* |
| 33 |
* @since 1.0 |
| 34 |
* |
| 35 |
* @var int |
| 36 |
* @access public |
| 37 |
*/ |
| 38 |
public $plan_id; |
| 39 |
|
| 40 |
/** |
| 41 |
* The plan label. |
| 42 |
* |
| 43 |
* @since 1.2 |
| 44 |
* |
| 45 |
* @var string |
| 46 |
* @access public |
| 47 |
*/ |
| 48 |
public $plan_label; |
| 49 |
|
| 50 |
/** |
| 51 |
* The total quota. |
| 52 |
* |
| 53 |
* @since 1.0 |
| 54 |
* |
| 55 |
* @var int |
| 56 |
* @access public |
| 57 |
*/ |
| 58 |
public $quota; |
| 59 |
|
| 60 |
/** |
| 61 |
* The total extra quota (Imagify Pack). |
| 62 |
* |
| 63 |
* @since 1.0 |
| 64 |
* |
| 65 |
* @var int |
| 66 |
* @access public |
| 67 |
*/ |
| 68 |
public $extra_quota; |
| 69 |
|
| 70 |
/** |
| 71 |
* The extra quota consumed. |
| 72 |
* |
| 73 |
* @since 1.0 |
| 74 |
* |
| 75 |
* @var int |
| 76 |
* @access public |
| 77 |
*/ |
| 78 |
public $extra_quota_consumed; |
| 79 |
|
| 80 |
/** |
| 81 |
* The current month consumed quota. |
| 82 |
* |
| 83 |
* @since 1.0 |
| 84 |
* |
| 85 |
* @var int |
| 86 |
* @access public |
| 87 |
*/ |
| 88 |
public $consumed_current_month_quota; |
| 89 |
|
| 90 |
/** |
| 91 |
* The next month date to credit the account. |
| 92 |
* |
| 93 |
* @since 1.1.1 |
| 94 |
* |
| 95 |
* @var date |
| 96 |
* @access public |
| 97 |
*/ |
| 98 |
public $next_date_update; |
| 99 |
|
| 100 |
/** |
| 101 |
* If the account is activate or not. |
| 102 |
* |
| 103 |
* @since 1.0.1 |
| 104 |
* |
| 105 |
* @var bool |
| 106 |
* @access public |
| 107 |
*/ |
| 108 |
public $is_active; |
| 109 |
|
| 110 |
/** |
| 111 |
* Store a \WP_Error object if the request to fetch the user data failed. |
| 112 |
* False overwise. |
| 113 |
* |
| 114 |
* @var bool|\WP_Error |
| 115 |
* @since 1.9.9 |
| 116 |
* @access private |
| 117 |
* @author Grégory Viguier |
| 118 |
*/ |
| 119 |
private $error; |
| 120 |
|
| 121 |
/** |
| 122 |
* The constructor. |
| 123 |
* |
| 124 |
* @since 1.0 |
| 125 |
* |
| 126 |
* @return void |
| 127 |
*/ |
| 128 |
public function __construct() { |
| 129 |
$user = get_imagify_user(); |
| 130 |
|
| 131 |
if ( is_wp_error( $user ) ) { |
| 132 |
$this->error = $user; |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
$this->id = $user->id; |
| 137 |
$this->email = $user->email; |
| 138 |
$this->plan_id = (int) $user->plan_id; |
| 139 |
$this->plan_label = ucfirst( $user->plan_label ); |
| 140 |
$this->quota = $user->quota; |
| 141 |
$this->extra_quota = $user->extra_quota; |
| 142 |
$this->extra_quota_consumed = $user->extra_quota_consumed; |
| 143 |
$this->consumed_current_month_quota = $user->consumed_current_month_quota; |
| 144 |
$this->next_date_update = $user->next_date_update; |
| 145 |
$this->is_active = $user->is_active; |
| 146 |
$this->error = false; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Get the possible error returned when fetching user data. |
| 151 |
* |
| 152 |
* @since 1.9.9 |
| 153 |
* @access public |
| 154 |
* @author Grégory Viguier |
| 155 |
* |
| 156 |
* @return bool|\WP_Error A \WP_Error object if the request to fetch the user data failed. False overwise. |
| 157 |
*/ |
| 158 |
public function get_error() { |
| 159 |
return $this->error; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Percentage of consumed quota, including extra quota. |
| 164 |
* |
| 165 |
* @since 1.0 |
| 166 |
* |
| 167 |
* @access public |
| 168 |
* @return float|int |
| 169 |
*/ |
| 170 |
public function get_percent_consumed_quota() { |
| 171 |
static $done = false; |
| 172 |
|
| 173 |
if ( $this->get_error() ) { |
| 174 |
return 0; |
| 175 |
} |
| 176 |
|
| 177 |
$quota = $this->quota; |
| 178 |
$consumed_quota = $this->consumed_current_month_quota; |
| 179 |
|
| 180 |
if ( imagify_round_half_five( $this->extra_quota_consumed ) < $this->extra_quota ) { |
| 181 |
$quota += $this->extra_quota; |
| 182 |
$consumed_quota += $this->extra_quota_consumed; |
| 183 |
} |
| 184 |
|
| 185 |
if ( ! $quota || ! $consumed_quota ) { |
| 186 |
$percent = 0; |
| 187 |
} else { |
| 188 |
$percent = 100 * $consumed_quota / $quota; |
| 189 |
$percent = round( $percent, 1 ); |
| 190 |
$percent = min( max( 0, $percent ), 100 ); |
| 191 |
} |
| 192 |
|
| 193 |
if ( $done ) { |
| 194 |
return $percent; |
| 195 |
} |
| 196 |
|
| 197 |
$previous_percent = Imagify_Data::get_instance()->get( 'previous_quota_percent' ); |
| 198 |
|
| 199 |
// Percent is not 100% anymore. |
| 200 |
if ( 100.0 === (float) $previous_percent && $percent < 100 ) { |
| 201 |
/** |
| 202 |
* Triggered when the consumed quota percent decreases below 100%. |
| 203 |
* |
| 204 |
* @since 1.7 |
| 205 |
* @author Grégory Viguier |
| 206 |
* |
| 207 |
* @param float|int $percent The current percentage of consumed quota. |
| 208 |
*/ |
| 209 |
do_action( 'imagify_not_over_quota_anymore', $percent ); |
| 210 |
} |
| 211 |
|
| 212 |
// Percent is not >= 80% anymore. |
| 213 |
if ( (float) $previous_percent >= 80.0 && $percent < 80 ) { |
| 214 |
/** |
| 215 |
* Triggered when the consumed quota percent decreases below 80%. |
| 216 |
* |
| 217 |
* @since 1.7 |
| 218 |
* @author Grégory Viguier |
| 219 |
* |
| 220 |
* @param float|int $percent The current percentage of consumed quota. |
| 221 |
* @param float|int $previous_percent The previous percentage of consumed quota. |
| 222 |
*/ |
| 223 |
do_action( 'imagify_not_almost_over_quota_anymore', $percent, $previous_percent ); |
| 224 |
} |
| 225 |
|
| 226 |
if ( (float) $previous_percent !== (float) $percent ) { |
| 227 |
Imagify_Data::get_instance()->set( 'previous_quota_percent', $percent ); |
| 228 |
} |
| 229 |
|
| 230 |
$done = true; |
| 231 |
|
| 232 |
return $percent; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Count percent of unconsumed quota. |
| 237 |
* |
| 238 |
* @since 1.0 |
| 239 |
* |
| 240 |
* @access public |
| 241 |
* @return float|int |
| 242 |
*/ |
| 243 |
public function get_percent_unconsumed_quota() { |
| 244 |
return 100 - $this->get_percent_consumed_quota(); |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Check if the user has a free account. |
| 249 |
* |
| 250 |
* @since 1.1.1 |
| 251 |
* |
| 252 |
* @access public |
| 253 |
* @return bool |
| 254 |
*/ |
| 255 |
public function is_free() { |
| 256 |
return 1 === $this->plan_id; |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Check if the user has consumed all his/her quota. |
| 261 |
* |
| 262 |
* @since 1.1.1 |
| 263 |
* @since 1.9.9 Return false if the request to fetch the user data failed. |
| 264 |
* |
| 265 |
* @access public |
| 266 |
* @return bool |
| 267 |
*/ |
| 268 |
public function is_over_quota() { |
| 269 |
if ( $this->get_error() ) { |
| 270 |
return false; |
| 271 |
} |
| 272 |
|
| 273 |
if ( $this->is_free() && 100.0 === (float) $this->get_percent_consumed_quota() ) { |
| 274 |
return true; |
| 275 |
} |
| 276 |
|
| 277 |
return false; |
| 278 |
} |
| 279 |
} |
| 280 |
|