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 | } |