BaseRepository.php
3 years ago
CouponRepository.php
2 years ago
CustomerRepository.php
1 year ago
GroupRepository.php
3 years ago
OrderRepository.php
11 months ago
PlanRepository.php
2 months ago
RepositoryInterface.php
3 years ago
SubscriptionRepository.php
3 months ago
index.php
3 years ago
SubscriptionRepository.php
345 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Membership\Repositories; |
| 4 | |
| 5 | use ProfilePress\Core\Base; |
| 6 | use ProfilePress\Core\Membership\Models\Subscription\SubscriptionFactory; |
| 7 | use ProfilePress\Core\Membership\Models\ModelInterface; |
| 8 | use ProfilePress\Core\Membership\Models\Subscription\SubscriptionEntity; |
| 9 | use ProfilePress\Core\Membership\Models\Subscription\SubscriptionStatus; |
| 10 | use ProfilePress\Core\Membership\Services\SubscriptionService; |
| 11 | |
| 12 | class SubscriptionRepository extends BaseRepository |
| 13 | { |
| 14 | protected $table; |
| 15 | |
| 16 | public function __construct() |
| 17 | { |
| 18 | $this->table = Base::subscriptions_db_table(); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * @param SubscriptionEntity $data |
| 23 | * |
| 24 | * @return false|int |
| 25 | */ |
| 26 | public function add(ModelInterface $data) |
| 27 | { |
| 28 | $result = $this->wpdb()->insert( |
| 29 | $this->table, |
| 30 | array( |
| 31 | 'parent_order_id' => $data->parent_order_id, |
| 32 | 'plan_id' => $data->plan_id, |
| 33 | 'customer_id' => $data->customer_id, |
| 34 | 'billing_frequency' => $data->billing_frequency, |
| 35 | 'initial_amount' => $data->initial_amount, |
| 36 | 'initial_tax' => $data->initial_tax, |
| 37 | 'initial_tax_rate' => $data->initial_tax_rate, |
| 38 | 'recurring_amount' => $data->recurring_amount, |
| 39 | 'recurring_tax' => $data->recurring_tax, |
| 40 | 'recurring_tax_rate' => $data->recurring_tax_rate, |
| 41 | 'total_payments' => $data->total_payments, |
| 42 | 'trial_period' => $data->trial_period, |
| 43 | 'status' => $data->status, |
| 44 | 'profile_id' => $data->profile_id, |
| 45 | 'created_date' => empty($data->created_date) ? current_time('mysql', true) : $data->created_date, |
| 46 | 'expiration_date' => $data->expiration_date |
| 47 | ), |
| 48 | array( |
| 49 | '%d', |
| 50 | '%d', |
| 51 | '%d', |
| 52 | '%s', |
| 53 | '%s', |
| 54 | '%s', |
| 55 | '%s', |
| 56 | '%s', |
| 57 | '%s', |
| 58 | '%s', |
| 59 | '%d', |
| 60 | '%s', |
| 61 | '%s', |
| 62 | '%s', |
| 63 | '%s', |
| 64 | '%s', |
| 65 | ) |
| 66 | ); |
| 67 | |
| 68 | return ! $result ? false : $this->wpdb()->insert_id; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @param SubscriptionEntity $data |
| 73 | * |
| 74 | * @return false|int |
| 75 | */ |
| 76 | public function update(ModelInterface $data) |
| 77 | { |
| 78 | $result = $this->wpdb()->update( |
| 79 | $this->table, |
| 80 | [ |
| 81 | 'parent_order_id' => $data->parent_order_id, |
| 82 | 'plan_id' => $data->plan_id, |
| 83 | 'customer_id' => $data->customer_id, |
| 84 | 'billing_frequency' => $data->billing_frequency, |
| 85 | 'initial_amount' => $data->initial_amount, |
| 86 | 'initial_tax' => $data->initial_tax, |
| 87 | 'initial_tax_rate' => $data->initial_tax_rate, |
| 88 | 'recurring_amount' => $data->recurring_amount, |
| 89 | 'recurring_tax' => $data->recurring_tax, |
| 90 | 'recurring_tax_rate' => $data->recurring_tax_rate, |
| 91 | 'total_payments' => $data->total_payments, |
| 92 | 'trial_period' => $data->trial_period, |
| 93 | 'status' => $data->status, |
| 94 | 'profile_id' => $data->profile_id, |
| 95 | 'created_date' => empty($data->created_date) ? current_time('mysql', true) : $data->created_date, |
| 96 | 'expiration_date' => $data->expiration_date |
| 97 | ], |
| 98 | ['id' => $data->id], |
| 99 | [ |
| 100 | '%d', |
| 101 | '%d', |
| 102 | '%d', |
| 103 | '%s', |
| 104 | '%s', |
| 105 | '%s', |
| 106 | '%s', |
| 107 | '%s', |
| 108 | '%s', |
| 109 | '%s', |
| 110 | '%d', |
| 111 | '%s', |
| 112 | '%s', |
| 113 | '%s', |
| 114 | '%s', |
| 115 | '%s', |
| 116 | ], |
| 117 | ['%d'] |
| 118 | ); |
| 119 | |
| 120 | return $result === false ? false : $data->id; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * @param $id |
| 125 | * |
| 126 | * @return int|false |
| 127 | */ |
| 128 | public function delete($id) |
| 129 | { |
| 130 | return $this->wpdb()->delete($this->table, ['id' => $id], ['%d']); |
| 131 | } |
| 132 | |
| 133 | public function delete_pending_subs($customer_id, $plan_id) |
| 134 | { |
| 135 | $subs = $this->retrieveBy([ |
| 136 | 'customer_id' => $customer_id, |
| 137 | 'plan_id' => $plan_id, |
| 138 | 'status' => [SubscriptionStatus::PENDING] |
| 139 | ]); |
| 140 | |
| 141 | if ( ! empty($subs)) { |
| 142 | foreach ($subs as $sub) { |
| 143 | SubscriptionService::init()->delete_subscription($sub->id); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @param $id |
| 150 | * |
| 151 | * @return SubscriptionEntity |
| 152 | */ |
| 153 | public function retrieve($id) |
| 154 | { |
| 155 | $result = $this->wpdb()->get_row( |
| 156 | $this->wpdb()->prepare( |
| 157 | "SELECT * FROM $this->table WHERE id = %d", |
| 158 | $id |
| 159 | ), |
| 160 | ARRAY_A |
| 161 | ); |
| 162 | |
| 163 | if ( ! $result) $result = []; |
| 164 | |
| 165 | return SubscriptionFactory::make($result); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * @param $args |
| 170 | * @param $count |
| 171 | * |
| 172 | * @return array|SubscriptionEntity[] |
| 173 | */ |
| 174 | public function retrieveBy($args = [], $count = false) |
| 175 | { |
| 176 | $defaults = [ |
| 177 | 'fields' => '*', |
| 178 | 'subscription_id' => 0, |
| 179 | 'search' => '', |
| 180 | 'number' => 10, |
| 181 | 'offset' => 0, |
| 182 | 'parent_order_id' => 0, |
| 183 | 'plan_id' => 0, |
| 184 | 'customer_id' => 0, |
| 185 | 'profile_id' => 0, |
| 186 | 'status' => [], |
| 187 | 'created_date' => '', |
| 188 | 'expiration_date' => '', |
| 189 | 'start_date' => '', |
| 190 | 'end_date' => '', |
| 191 | 'date_compare' => '=', |
| 192 | 'date_column' => 'created_date', |
| 193 | 'order' => 'DESC', |
| 194 | 'orderby' => 'id', |
| 195 | 'raw_response' => false |
| 196 | ]; |
| 197 | |
| 198 | $args = wp_parse_args($args, $defaults); |
| 199 | |
| 200 | $limit = absint($args['number']); |
| 201 | |
| 202 | $offset = $args['offset']; |
| 203 | $search = $args['search']; |
| 204 | |
| 205 | $sql = sprintf("SELECT %s FROM $this->table", esc_sql(sanitize_text_field($args['fields']))); |
| 206 | |
| 207 | if ($count === true) { |
| 208 | $sql = "SELECT COUNT(id) FROM $this->table"; |
| 209 | } |
| 210 | |
| 211 | $user_table = $this->wpdb()->users; |
| 212 | $customer_table = Base::customers_db_table(); |
| 213 | |
| 214 | $date_compare = ! empty($args['date_compare']) ? esc_sql($args['date_compare']) : '='; |
| 215 | |
| 216 | $replacement = [1]; |
| 217 | $sql .= " WHERE 1=%d"; // fixes Notice: wpdb::prepare was called incorrectly. The query argument of wpdb::prepare() must have a placeholder |
| 218 | |
| 219 | if ($args['subscription_id'] > 0) { |
| 220 | $sql .= " AND id = %d"; |
| 221 | $replacement[] = (int)$args['subscription_id']; |
| 222 | } |
| 223 | |
| 224 | if ($args['plan_id'] > 0) { |
| 225 | $sql .= " AND plan_id = %d"; |
| 226 | $replacement[] = (int)$args['plan_id']; |
| 227 | } |
| 228 | |
| 229 | if ($args['customer_id'] > 0) { |
| 230 | $sql .= " AND customer_id = %d"; |
| 231 | $replacement[] = (int)$args['customer_id']; |
| 232 | } |
| 233 | |
| 234 | if ($args['parent_order_id'] > 0) { |
| 235 | $sql .= " AND parent_order_id = %d"; |
| 236 | $replacement[] = (int)$args['parent_order_id']; |
| 237 | } |
| 238 | |
| 239 | if ( ! empty($args['profile_id'])) { |
| 240 | if ($args['profile_id'] === 'NOT_EMPTY') { |
| 241 | $sql .= " AND profile_id IS NOT NULL AND profile_id <> ''"; |
| 242 | } elseif ($args['profile_id'] === 'EMPTY') { |
| 243 | $sql .= " AND (profile_id IS NULL OR profile_id = '')"; |
| 244 | } else { |
| 245 | $sql .= " AND profile_id = %s"; |
| 246 | $replacement[] = sanitize_text_field($args['profile_id']); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | if ( |
| 251 | ! empty($args['status']) && |
| 252 | count(array_intersect($args['status'], array_keys(SubscriptionStatus::get_all()))) == count($args['status']) |
| 253 | ) { |
| 254 | $sql .= " AND status IN (" . implode(',', array_fill(0, count($args['status']), '%s')) . ") "; |
| 255 | $replacement = array_merge($replacement, $args['status']); |
| 256 | } |
| 257 | |
| 258 | if ( ! empty($args['created_date'])) { |
| 259 | $sql .= " AND created_date $date_compare %s"; |
| 260 | $replacement[] = gmdate('Y-m-d H:i:s', ppress_strtotime_utc($args['created_date'])); |
| 261 | } |
| 262 | |
| 263 | if ( ! empty($args['expiration_date'])) { |
| 264 | $sql .= " AND expiration_date $date_compare %s"; |
| 265 | $replacement[] = gmdate('Y-m-d H:i:s', ppress_strtotime_utc($args['expiration_date'])); |
| 266 | } |
| 267 | |
| 268 | $start_date = $args['start_date']; |
| 269 | $end_date = $args['end_date']; |
| 270 | $date_column = esc_sql($args['date_column']); |
| 271 | |
| 272 | if ( ! empty($start_date)) { |
| 273 | $sql .= " AND $date_column >= %s"; |
| 274 | $replacement[] = gmdate('Y-m-d H:i:s', ppress_strtotime_utc($start_date)); |
| 275 | } |
| 276 | |
| 277 | if ( ! empty($end_date)) { |
| 278 | $sql .= " AND $date_column <= %s"; |
| 279 | $replacement[] = gmdate('Y-m-d H:i:s', ppress_strtotime_utc($end_date)); |
| 280 | } |
| 281 | |
| 282 | if ( ! empty($search)) { |
| 283 | |
| 284 | if (is_numeric($search)) { |
| 285 | $sql .= " AND (id = %d"; |
| 286 | $sql .= " OR plan_id = %d"; |
| 287 | $sql .= " OR customer_id = %d)"; |
| 288 | |
| 289 | $replacement[] = $search; |
| 290 | $replacement[] = $search; |
| 291 | $replacement[] = $search; |
| 292 | } elseif (filter_var($search, FILTER_VALIDATE_EMAIL)) { |
| 293 | $sql .= " AND customer_id = (SELECT id FROM $customer_table WHERE user_id = (SELECT ID FROM $user_table WHERE user_email = %s))"; |
| 294 | $replacement[] = $search; |
| 295 | } else { |
| 296 | $sql .= " AND profile_id = %s"; |
| 297 | $sql .= " OR customer_id IN (SELECT id FROM $customer_table WHERE user_id IN (SELECT ID FROM $user_table WHERE user_nicename LIKE %s OR display_name LIKE %s))"; |
| 298 | |
| 299 | $search_like = '%' . parent::wpdb()->esc_like(sanitize_text_field($search)) . '%'; |
| 300 | |
| 301 | $replacement[] = $search; |
| 302 | $replacement[] = $search_like; |
| 303 | $replacement[] = $search_like; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | if ( ! empty($args['orderby'])) { |
| 308 | $sql .= sprintf(" ORDER BY %s %s", esc_sql($args['orderby']), esc_sql($args['order'])); |
| 309 | } |
| 310 | |
| 311 | if ($count === false) { |
| 312 | if ($limit > 0) { |
| 313 | $sql .= " LIMIT %d"; |
| 314 | $replacement[] = $limit; |
| 315 | } |
| 316 | |
| 317 | if ($offset > 0) { |
| 318 | $sql .= " OFFSET %d"; |
| 319 | $replacement[] = $offset; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | if ($count === true) { |
| 324 | return $this->wpdb()->get_var($this->wpdb()->prepare($sql, $replacement)); |
| 325 | } |
| 326 | |
| 327 | $result = $this->wpdb()->get_results($this->wpdb()->prepare($sql, $replacement), 'ARRAY_A'); |
| 328 | |
| 329 | if (is_array($result) && ! empty($result)) { |
| 330 | return $args['raw_response'] ? $result : array_map([SubscriptionFactory::class, 'make'], $result); |
| 331 | } |
| 332 | |
| 333 | return []; |
| 334 | } |
| 335 | |
| 336 | public function get_count_by_status($status) |
| 337 | { |
| 338 | return $this->wpdb()->get_var( |
| 339 | $this->wpdb()->prepare( |
| 340 | "SELECT COUNT(id) FROM $this->table WHERE status = %s", |
| 341 | $status |
| 342 | ) |
| 343 | ); |
| 344 | } |
| 345 | } |