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