PluginProbe ʕ •ᴥ•ʔ
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress / trunk
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress vtrunk
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 / Plan / PlanEntity.php
wp-user-avatar / src / Membership / Models / Plan Last commit date
PlanEntity.php 1 month ago PlanFactory.php 3 years ago index.php 3 years ago
PlanEntity.php
346 lines
1 <?php
2
3 namespace ProfilePress\Core\Membership\Models\Plan;
4
5 use ProfilePress\Core\Membership\Models\AbstractModel;
6 use ProfilePress\Core\Membership\Models\ModelInterface;
7 use ProfilePress\Core\Membership\Models\Subscription\SubscriptionBillingFrequency;
8 use ProfilePress\Core\Membership\Models\Subscription\SubscriptionTrialPeriod;
9 use ProfilePress\Core\Membership\Repositories\GroupRepository;
10 use ProfilePress\Core\Membership\Repositories\PlanRepository;
11 use ProfilePress\Core\Membership\Services\Calculator;
12 use ProfilePress\Core\Membership\Services\OrderService;
13
14 /**
15 * @property int $id
16 * @property string $name
17 * @property string $user_role
18 * @property string $order_note
19 * @property string $description
20 * @property string $price
21 * @property string $billing_frequency
22 * @property string $subscription_length
23 * @property int $total_payments
24 * @property string $signup_fee
25 * @property string $free_trial
26 */
27 class PlanEntity extends AbstractModel implements ModelInterface
28 {
29 const PLAN_EXTRAS = 'plan_extras';
30
31 protected $id = 0;
32
33 protected $name = '';
34
35 protected $description = '';
36
37 protected $user_role = '';
38
39 protected $order_note = '';
40
41 protected $price = '0';
42
43 protected $billing_frequency = SubscriptionBillingFrequency::MONTHLY;
44
45 protected $subscription_length = 'renew_indefinitely';
46
47 // 0 indicates renew indefinitely.
48 protected $total_payments = 0;
49
50 protected $signup_fee = '0';
51
52 protected $free_trial = SubscriptionTrialPeriod::DISABLED;
53
54 protected $status = 'false';
55
56 protected $meta_data = [];
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 == 'meta_data') {
66 $this->meta_data = ! 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 get_id()
81 {
82 return absint($this->id);
83 }
84
85 public function get_name()
86 {
87 return $this->name;
88 }
89
90 public function is_active()
91 {
92 return $this->status == 'true';
93 }
94
95 public function is_recurring()
96 {
97 return ! empty($this->billing_frequency) && $this->billing_frequency != SubscriptionBillingFrequency::LIFETIME;
98 }
99
100 /**
101 * If subscription plan, do we want to setup payment gateway subscription for automatic renewal / recurring payments?
102 *
103 * @return bool
104 */
105 public function is_auto_renew(): bool
106 {
107 return ppress_cache_transform('plan_entity_is_auto_renew', function () {
108
109 $result = $this->is_recurring() && ppress_settings_by_key('disable_auto_renew') != 'true';
110
111 return apply_filters('ppress_subscription_is_auto_renew', $result, $this);
112 });
113 }
114
115 /**
116 * Check if a membership plan is of a one-time payment, not a subscription
117 *
118 * @return bool
119 */
120 public function is_lifetime()
121 {
122 return ! empty($this->billing_frequency) && $this->billing_frequency == SubscriptionBillingFrequency::LIFETIME;
123 }
124
125 public function has_free_trial()
126 {
127 return $this->is_recurring() &&
128 $this->free_trial != SubscriptionTrialPeriod::DISABLED &&
129 ! OrderService::init()->customer_has_trialled($this->id);
130 }
131
132 public function has_signup_fee()
133 {
134 return ! Calculator::init($this->signup_fee)->isNegativeOrZero();
135 }
136
137 public function get_description()
138 {
139 return apply_filters('ppress_subscription_plan_description', wpautop($this->description), $this->get_id());
140 }
141
142 /**
143 * @return string
144 */
145 public function get_price()
146 {
147 return apply_filters('ppress_membership_plan_price', ppress_sanitize_amount($this->price), $this);
148 }
149
150 public function get_billing_frequency()
151 {
152 return $this->billing_frequency;
153 }
154
155 public function get_subscription_length()
156 {
157 return $this->subscription_length;
158 }
159
160 public function get_total_payments()
161 {
162 return absint($this->total_payments);
163 }
164
165 /**
166 * @return string
167 */
168 public function get_signup_fee()
169 {
170 return ppress_sanitize_amount($this->signup_fee);
171 }
172
173 public function get_free_trial()
174 {
175 return sanitize_text_field($this->free_trial);
176 }
177
178 public function get_edit_plan_url()
179 {
180 return add_query_arg([
181 'ppress_subp_action' => 'edit',
182 'id' => $this->id
183 ], PPRESS_MEMBERSHIP_SUBSCRIPTION_PLANS_SETTINGS_PAGE);
184 }
185
186 /**
187 * @return false|int
188 */
189 public function save()
190 {
191 if ($this->id > 0) {
192
193 $result = PlanRepository::init()->update($this);
194
195 do_action('ppress_membership_update_plan', $result, $this);
196
197 return $result;
198 }
199
200 $result = PlanRepository::init()->add($this);
201
202 do_action('ppress_membership_add_plan', $result, $this);
203
204 return $result;
205 }
206
207 /**
208 * @return false|string
209 */
210 public function get_checkout_url()
211 {
212 return ppress_plan_checkout_url($this->get_id());
213 }
214
215 /**
216 * @return bool
217 */
218 public function has_downloads()
219 {
220 $val = $this->get_downloads();
221
222 return isset($val['files']) && is_array($val['files']) && ! empty($val['files']);
223 }
224
225 public function get_downloads()
226 {
227 $cache_key = sprintf('ppress_plan_%d_downloads', $this->get_id());
228
229 $ret = wp_cache_get($cache_key);
230
231 if (false === $ret) {
232
233 $ret = [];
234
235 $extras = $this->get_plan_extras();
236
237 $file_names = ppress_var($extras, 'df_names');
238 $file_urls = ppress_var($extras, 'df_urls');
239
240 if ( ! is_array($file_urls) || empty($file_urls)) return false;
241
242 foreach ($file_urls as $index => $file_url) {
243 if ( ! empty($file_url)) {
244 $ret['files'][$file_url] = ppress_var($file_names, $index, pathinfo($file_url)['filename']);
245 }
246 }
247
248 $ret['download_limit'] = ppress_is_boolean($extras['df_download_limit']) || ! empty($extras['df_download_limit']) ?
249 absint($extras['df_download_limit']) :
250 absint(ppress_get_file_downloads_setting('download_limit', 0, true));
251
252 $ret['download_expiry'] = ppress_is_boolean($extras['df_download_expiry']) || ! empty($extras['df_download_expiry']) ?
253 absint($extras['df_download_expiry']) :
254 absint(ppress_get_file_downloads_setting('download_expiry', 0, true));
255
256 wp_cache_set($cache_key, $ret, '', MINUTE_IN_SECONDS);
257 }
258
259 return $ret;
260 }
261
262 /**
263 * @param string $extra_key
264 *
265 * @return false|mixed
266 */
267 public function get_plan_extras($extra_key = '')
268 {
269 $extras = $this->get_meta(self::PLAN_EXTRAS);
270
271 if ( ! empty($extra_key)) {
272 return ppress_var($extras, $extra_key, '');
273 }
274
275 return $extras;
276 }
277
278 public function update_meta($meta_key, $meta_value)
279 {
280 $this->meta_data[$meta_key] = $meta_value;
281
282 return PlanRepository::init()->updateColumn(
283 $this->get_id(),
284 'meta_data',
285 \wp_json_encode($this->meta_data)
286 );
287 }
288
289 /**
290 * @param $meta_key
291 *
292 * @return false|mixed
293 */
294 public function get_meta($meta_key)
295 {
296 return ppress_var($this->meta_data, $meta_key);
297 }
298
299 /**
300 * @param $meta_key
301 *
302 * @return false|int
303 */
304 public function delete_meta($meta_key)
305 {
306 unset($this->meta_data[$meta_key]);
307
308 return PlanRepository::init()->updateColumn(
309 $this->get_id(),
310 'meta_data',
311 \wp_json_encode($this->meta_data)
312 );
313 }
314
315 /**
316 * @return false|int
317 */
318 public function activate()
319 {
320 return PlanRepository::init()->updateColumn($this->id, 'status', 'true');
321 }
322
323 /**
324 * @return false|int
325 */
326 public function deactivate()
327 {
328 return PlanRepository::init()->updateColumn($this->id, 'status', 'false');
329 }
330
331 /**
332 * @return int|false
333 */
334 public function get_group_id()
335 {
336 $groups = GroupRepository::init()->retrieveAll(0, 1, 'ASC');
337
338 foreach ($groups as $group) {
339 if (in_array($this->get_id(), $group->get_plan_ids(), true)) {
340 return $group->get_id();
341 }
342 }
343
344 return false;
345 }
346 }