PlanEntity.php
231 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\PlanRepository; |
| 10 | use ProfilePress\Core\Membership\Services\Calculator; |
| 11 | use ProfilePress\Core\Membership\Services\OrderService; |
| 12 | |
| 13 | /** |
| 14 | * @property int $id |
| 15 | * @property string $name |
| 16 | * @property string $description |
| 17 | * @property string $price |
| 18 | * @property string $billing_frequency |
| 19 | * @property string $subscription_length |
| 20 | * @property int $total_payments |
| 21 | * @property string $signup_fee |
| 22 | * @property string $free_trial |
| 23 | */ |
| 24 | class PlanEntity extends AbstractModel implements ModelInterface |
| 25 | { |
| 26 | protected $id = 0; |
| 27 | |
| 28 | protected $name = ''; |
| 29 | |
| 30 | protected $description = ''; |
| 31 | |
| 32 | protected $price = '0'; |
| 33 | |
| 34 | protected $billing_frequency = SubscriptionBillingFrequency::MONTHLY; |
| 35 | |
| 36 | protected $subscription_length = 'renew_indefinitely'; |
| 37 | |
| 38 | // 0 indicates renew indefinitely. |
| 39 | protected $total_payments = 0; |
| 40 | |
| 41 | protected $signup_fee = '0'; |
| 42 | |
| 43 | protected $free_trial = SubscriptionTrialPeriod::DISABLED; |
| 44 | |
| 45 | protected $status = 'false'; |
| 46 | |
| 47 | protected $meta_data = []; |
| 48 | |
| 49 | public function __construct($data = []) |
| 50 | { |
| 51 | if (is_array($data) && ! empty($data)) { |
| 52 | |
| 53 | foreach ($data as $key => $value) { |
| 54 | $this->$key = $value; |
| 55 | |
| 56 | if ($key == 'meta_data') { |
| 57 | $this->meta_data = ! empty($value) && ppress_is_json($value) ? \json_decode($value, true) : []; |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @return bool |
| 65 | */ |
| 66 | public function exists() |
| 67 | { |
| 68 | return ! empty($this->id); |
| 69 | } |
| 70 | |
| 71 | public function get_id() |
| 72 | { |
| 73 | return absint($this->id); |
| 74 | } |
| 75 | |
| 76 | public function get_name() |
| 77 | { |
| 78 | return $this->name; |
| 79 | } |
| 80 | |
| 81 | public function is_active() |
| 82 | { |
| 83 | return $this->status == 'true'; |
| 84 | } |
| 85 | |
| 86 | public function is_recurring() |
| 87 | { |
| 88 | return ! empty($this->billing_frequency) && $this->billing_frequency != 'lifetime'; |
| 89 | } |
| 90 | |
| 91 | public function has_free_trial() |
| 92 | { |
| 93 | return $this->is_recurring() && |
| 94 | $this->free_trial != SubscriptionTrialPeriod::DISABLED && |
| 95 | ! OrderService::init()->customer_has_trialled($this->id); |
| 96 | } |
| 97 | |
| 98 | public function has_signup_fee() |
| 99 | { |
| 100 | return ! Calculator::init($this->signup_fee)->isNegativeOrZero(); |
| 101 | } |
| 102 | |
| 103 | public function get_description() |
| 104 | { |
| 105 | return apply_filters('ppress_subscription_plan_description', wpautop($this->description), $this->get_id()); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @return string |
| 110 | */ |
| 111 | public function get_price() |
| 112 | { |
| 113 | return ppress_sanitize_amount($this->price); |
| 114 | } |
| 115 | |
| 116 | public function get_billing_frequency() |
| 117 | { |
| 118 | return $this->billing_frequency; |
| 119 | } |
| 120 | |
| 121 | public function get_subscription_length() |
| 122 | { |
| 123 | return $this->subscription_length; |
| 124 | } |
| 125 | |
| 126 | public function get_total_payments() |
| 127 | { |
| 128 | return absint($this->total_payments); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @return string |
| 133 | */ |
| 134 | public function get_signup_fee() |
| 135 | { |
| 136 | return ppress_sanitize_amount($this->signup_fee); |
| 137 | } |
| 138 | |
| 139 | public function get_free_trial() |
| 140 | { |
| 141 | return sanitize_text_field($this->free_trial); |
| 142 | } |
| 143 | |
| 144 | public function get_edit_plan_url() |
| 145 | { |
| 146 | return add_query_arg(['ppress_subp_action' => 'edit', 'id' => $this->id], PPRESS_MEMBERSHIP_SUBSCRIPTION_PLANS_SETTINGS_PAGE); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * @return false|int |
| 151 | */ |
| 152 | public function save() |
| 153 | { |
| 154 | if ($this->id > 0) { |
| 155 | |
| 156 | $result = PlanRepository::init()->update($this); |
| 157 | |
| 158 | do_action('ppress_membership_update_plan', $result, $this); |
| 159 | |
| 160 | return $result; |
| 161 | } |
| 162 | |
| 163 | $result = PlanRepository::init()->add($this); |
| 164 | |
| 165 | do_action('ppress_membership_add_plan', $result, $this); |
| 166 | |
| 167 | return $result; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * @return false|string |
| 172 | */ |
| 173 | public function get_checkout_url() |
| 174 | { |
| 175 | return ppress_plan_checkout_url($this->get_id()); |
| 176 | } |
| 177 | |
| 178 | public function update_meta($meta_key, $meta_value) |
| 179 | { |
| 180 | $this->meta_data[$meta_key] = $meta_value; |
| 181 | |
| 182 | return PlanRepository::init()->updateColumn( |
| 183 | $this->get_id(), |
| 184 | 'meta_data', |
| 185 | \wp_json_encode($this->meta_data) |
| 186 | ); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * @param $meta_key |
| 191 | * |
| 192 | * @return false|mixed |
| 193 | */ |
| 194 | public function get_meta($meta_key) |
| 195 | { |
| 196 | return ppress_var($this->meta_data, $meta_key); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * @param $meta_key |
| 201 | * |
| 202 | * @return false|int |
| 203 | */ |
| 204 | public function delete_meta($meta_key) |
| 205 | { |
| 206 | unset($this->meta_data[$meta_key]); |
| 207 | |
| 208 | return PlanRepository::init()->updateColumn( |
| 209 | $this->get_id(), |
| 210 | 'meta_data', |
| 211 | \wp_json_encode($this->meta_data) |
| 212 | ); |
| 213 | |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * @return false|int |
| 218 | */ |
| 219 | public function activate() |
| 220 | { |
| 221 | return PlanRepository::init()->updateColumn($this->id, 'status', 'true'); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * @return false|int |
| 226 | */ |
| 227 | public function deactivate() |
| 228 | { |
| 229 | return PlanRepository::init()->updateColumn($this->id, 'status', 'false'); |
| 230 | } |
| 231 | } |