PluginProbe ʕ •ᴥ•ʔ
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress / 4.16.19
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress v4.16.19
4.16.19 4.16.18 4.16.17 4.16.16 trunk 1.0 1.0.1 1.0.2 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.5a 1.1.6 1.1.7 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.4 1.4.1 1.4.2 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.7 1.7.1 1.7.2 1.8 1.8.1 1.8.10 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9 1.9.1 1.9.10 1.9.11 1.9.12 1.9.13 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.1.9 2.2.10 2.2.11 2.2.12 2.2.13 2.2.14 2.2.15 2.2.16 2.2.2 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 3.0 3.1 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 3.1.19 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.10 3.2.11 3.2.12 3.2.13 3.2.14 3.2.15 3.2.16 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 4.0.0 4.0.1 4.0.2 4.0.3 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.10.0 4.10.1 4.10.2 4.10.3 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.13.3 4.13.4 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.15.0 4.15.1 4.15.10 4.15.11 4.15.12 4.15.13 4.15.14 4.15.15 4.15.16 4.15.17 4.15.18 4.15.19 4.15.2 4.15.20 4.15.20.1 4.15.21 4.15.22 4.15.23 4.15.24 4.15.25 4.15.3 4.15.4 4.15.5 4.15.6 4.15.7 4.15.8 4.15.9 4.16.0 4.16.1 4.16.10 4.16.11 4.16.12 4.16.13 4.16.14 4.16.15 4.16.2 4.16.3 4.16.4 4.16.5 4.16.6 4.16.7 4.16.8 4.16.9 4.2.0 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.5.3 4.5.4 4.5.5 4.6.0 4.7.0 4.8.0 4.9.0
wp-user-avatar / src / Membership / Models / Coupon / CouponEntity.php
wp-user-avatar / src / Membership / Models / Coupon Last commit date
CouponApplication.php 3 years ago CouponEntity.php 1 year ago CouponFactory.php 3 years ago CouponType.php 2 years ago CouponUnit.php 3 years ago index.php 3 years ago
CouponEntity.php
311 lines
1 <?php
2
3 namespace ProfilePress\Core\Membership\Models\Coupon;
4
5 use ProfilePress\Core\Membership\Models\AbstractModel;
6 use ProfilePress\Core\Membership\Models\Customer\CustomerFactory;
7 use ProfilePress\Core\Membership\Models\ModelInterface;
8 use ProfilePress\Core\Membership\Models\Order\OrderStatus;
9 use ProfilePress\Core\Membership\Models\Order\OrderType;
10 use ProfilePress\Core\Membership\Repositories\CouponRepository;
11 use ProfilePress\Core\Membership\Repositories\OrderRepository;
12 use ProfilePressVendor\Carbon\Carbon;
13 use ProfilePressVendor\Carbon\CarbonImmutable;
14
15 /**
16 * @property int $id
17 * @property string $code
18 * @property string $description
19 * @property string $coupon_type
20 * @property string $coupon_application
21 * @property string $is_onetime_use
22 * @property string $amount
23 * @property string $unit
24 * @property array $plan_ids
25 * @property int $usage_limit
26 * @property string $status
27 * @property string $start_date
28 * @property string $end_date
29 */
30 class CouponEntity extends AbstractModel implements ModelInterface
31 {
32 protected $id = 0;
33
34 protected $code = '';
35
36 protected $description = '';
37
38 protected $coupon_type = CouponType::RECURRING;
39
40 protected $coupon_application = CouponApplication::NEW_PURCHASE;
41
42 protected $amount = 0;
43
44 protected $unit = CouponUnit::PERCENTAGE;
45
46 protected $plan_ids = [];
47
48 protected $usage_limit = '';
49
50 protected $status = 'true';
51
52 protected $is_onetime_use = 'false';
53
54 protected $start_date = '';
55
56 protected $end_date = '';
57
58 public function __construct($data = [])
59 {
60 if (is_array($data) && ! empty($data)) {
61
62 foreach ($data as $key => $value) {
63 $this->$key = $value;
64
65 if ($key == 'plan_ids') {
66 $this->plan_ids = ! empty($value) && ppress_is_json($value) ? \json_decode($value, true) : [];
67 }
68 }
69 }
70 }
71
72 /**
73 * @return bool
74 */
75 public function exists()
76 {
77 return ! empty($this->id);
78 }
79
80 public function is_active()
81 {
82 return $this->status == 'true';
83 }
84
85 public function is_onetime_use()
86 {
87 return $this->is_onetime_use == 'true';
88 }
89
90 public function is_recurring()
91 {
92 return $this->get_coupon_type() == CouponType::RECURRING;
93 }
94
95 public function is_expired()
96 {
97 $end_date = $this->get_end_date();
98
99 if ( ! empty($end_date) && time() > CarbonImmutable::parse($end_date, 'UTC')->endOfDay()->getTimestamp()) {
100 return true;
101 }
102
103 return false;
104 }
105
106 protected function set_coupon_application($val)
107 {
108 $valid_options = (new \ReflectionClass(CouponApplication::class))->getConstants();
109
110 if (in_array($val, $valid_options)) {
111 $this->coupon_application = $val;
112 }
113 }
114
115 public function get_coupon_type()
116 {
117 return ! empty($this->coupon_type) ? $this->coupon_type : CouponType::RECURRING;
118 }
119
120 public function get_coupon_application()
121 {
122 return ! empty($this->coupon_application) ? $this->coupon_application : CouponApplication::NEW_PURCHASE;
123 }
124
125 public function get_id()
126 {
127 return absint($this->id);
128 }
129
130 /**
131 * @return string
132 */
133 public function get_description()
134 {
135 return sanitize_textarea_field($this->description);
136 }
137
138 /**
139 * @return string
140 */
141 public function get_amount()
142 {
143 return ! empty($this->amount) ? (float)$this->amount : '';
144 }
145
146 protected function set_plan_ids($value)
147 {
148 $this->plan_ids = is_array($value) ? \wp_json_encode($value) : $value;
149 }
150
151 public function get_plan_ids()
152 {
153 return $this->plan_ids;
154 }
155
156 public function get_usage_limit()
157 {
158 return ! empty($this->usage_limit) ? absint($this->usage_limit) : '';
159 }
160
161 public function get_start_date()
162 {
163 return ! empty($this->start_date) && (string)$this->start_date != '0000-00-00' ? (string)$this->start_date : '';
164 }
165
166 public function get_end_date()
167 {
168 return ! empty($this->end_date) && (string)$this->end_date != '0000-00-00' ? (string)$this->end_date : '';
169 }
170
171 /**
172 * Check if a coupon is valid
173 *
174 * @param int $plan_id
175 * @param string $order_type
176 *
177 * @return bool
178 */
179 public function is_valid($plan_id = 0, $order_type = OrderType::NEW_ORDER)
180 {
181 if ( ! $this->is_active()) return false;
182
183 if (
184 $order_type == OrderType::NEW_ORDER &&
185 $this->get_coupon_application() == CouponApplication::EXISTING_PURCHASE
186 ) {
187 return false;
188 }
189
190 if (
191 $order_type != OrderType::NEW_ORDER &&
192 $this->get_coupon_application() == CouponApplication::NEW_PURCHASE
193 ) {
194 return false;
195 }
196
197 if (
198 in_array($order_type, [OrderType::UPGRADE, OrderType::DOWNGRADE]) &&
199 $this->get_coupon_application() == CouponApplication::NEW_PURCHASE
200 ) {
201 return false;
202 }
203
204 if (is_user_logged_in()) {
205
206 $current_user_id = get_current_user_id();
207
208 $customer = CustomerFactory::fromUserId($current_user_id);
209
210 if ($this->is_onetime_use() && $customer->exists()) {
211
212 $count = OrderRepository::init()->retrieveBy([
213 'customer_id' => $customer->get_id(),
214 'coupon_code' => $this->code
215 ], true);
216
217 if ($count > 0) return false;
218 }
219 }
220
221 $start_date = ! empty($this->get_start_date()) ? $this->get_start_date() . ' 00:00:00' : '';
222 $end_date = ! empty($this->get_end_date()) ? $this->get_end_date() . ' 23:59:59' : '';
223
224 if ( ! empty($start_date) && empty($end_date)) {
225 $result = (new CarbonImmutable($start_date, wp_timezone()))
226 ->lessThanOrEqualTo(CarbonImmutable::now(wp_timezone()));
227
228 if ( ! $result) return false;
229 }
230
231 if ( ! empty($end_date) && empty($start_date)) {
232 $result = (new CarbonImmutable($end_date, wp_timezone()))
233 ->greaterThanOrEqualTo(CarbonImmutable::now(wp_timezone()));
234
235 if ( ! $result) return false;
236 }
237
238 if ( ! empty($start_date) && ! empty($end_date)) {
239 $result = CarbonImmutable::now(wp_timezone())->isBetween(
240 new CarbonImmutable($start_date, wp_timezone()),
241 new CarbonImmutable($end_date, wp_timezone())
242 );
243
244 if ( ! $result) return false;
245 }
246
247 if ( ! empty($plan_id) && ! empty($this->get_plan_ids()) && ! in_array($plan_id, $this->get_plan_ids())) {
248 return false;
249 }
250
251 $usage_limit = absint($this->get_usage_limit());
252 $usage_count = absint($this->get_usage_count());
253
254 if ($usage_limit > 0 && $usage_count >= $usage_limit) {
255 return false;
256 }
257
258 return true;
259 }
260
261 /**
262 * Return how many times coupon has been used. update this after every successful order
263 *
264 * @return int
265 */
266 public function get_usage_count()
267 {
268 return OrderRepository::init()->retrieveBy([
269 'status' => [OrderStatus::COMPLETED, OrderStatus::REFUNDED],
270 'coupon_code' => $this->code,
271 'number' => 0
272 ], true);
273 }
274
275 /**
276 * @return false|int
277 */
278 public function save()
279 {
280 if ($this->exists()) {
281
282 $result = CouponRepository::init()->update($this);
283
284 do_action('ppress_membership_update_coupon', $result, $this);
285
286 return $result;
287 }
288
289 $result = CouponRepository::init()->add($this);
290
291 do_action('ppress_membership_add_coupon', $result, $this);
292
293 return $result;
294 }
295
296 /**
297 * @return false|int
298 */
299 public function activate()
300 {
301 return CouponRepository::init()->updateColumn($this->id, 'status', 'true');
302 }
303
304 /**
305 * @return false|int
306 */
307 public function deactivate()
308 {
309 return CouponRepository::init()->updateColumn($this->id, 'status', 'false');
310 }
311 }