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
OrderRepository.php
508 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Membership\Repositories; |
| 4 | |
| 5 | use ProfilePress\Core\Base; |
| 6 | use ProfilePress\Core\Membership\Models\Order\OrderFactory; |
| 7 | use ProfilePress\Core\Membership\Models\ModelInterface; |
| 8 | use ProfilePress\Core\Membership\Models\Order\OrderEntity; |
| 9 | use ProfilePress\Core\Membership\Models\Order\OrderMode; |
| 10 | use ProfilePress\Core\Membership\Models\Order\OrderStatus; |
| 11 | use ProfilePress\Core\Membership\Models\Order\OrderType; |
| 12 | use ProfilePress\Core\Membership\Services\OrderService; |
| 13 | |
| 14 | class OrderRepository extends BaseRepository |
| 15 | { |
| 16 | protected $table; |
| 17 | |
| 18 | public function __construct() |
| 19 | { |
| 20 | $this->table = Base::orders_db_table(); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * @param OrderEntity $data |
| 25 | * |
| 26 | * @return false|int |
| 27 | */ |
| 28 | public function add(ModelInterface $data) |
| 29 | { |
| 30 | $result = $this->wpdb()->insert( |
| 31 | $this->table, |
| 32 | [ |
| 33 | 'order_key' => ! empty($data->order_key) ? $data->order_key : OrderService::init()->generate_order_key(), |
| 34 | 'plan_id' => $data->plan_id, |
| 35 | 'customer_id' => $data->customer_id, |
| 36 | 'subscription_id' => $data->subscription_id, |
| 37 | 'order_type' => $data->order_type, |
| 38 | 'transaction_id' => $data->transaction_id, |
| 39 | 'payment_method' => $data->payment_method, |
| 40 | 'status' => $data->status, |
| 41 | 'coupon_code' => $data->coupon_code, |
| 42 | 'subtotal' => $data->subtotal, |
| 43 | 'tax' => $data->tax, |
| 44 | 'tax_rate' => $data->tax_rate, |
| 45 | 'discount' => $data->discount, |
| 46 | 'total' => $data->total, |
| 47 | 'billing_address' => $data->billing_address, |
| 48 | 'billing_city' => $data->billing_city, |
| 49 | 'billing_state' => $data->billing_state, |
| 50 | 'billing_country' => $data->billing_country, |
| 51 | 'billing_postcode' => $data->billing_postcode, |
| 52 | 'billing_phone' => $data->billing_phone, |
| 53 | 'mode' => $data->mode, |
| 54 | 'currency' => $data->currency, |
| 55 | 'ip_address' => $data->ip_address, |
| 56 | 'date_created' => empty($data->date_created) ? current_time('mysql', true) : $data->date_created, |
| 57 | 'date_completed' => $data->date_completed |
| 58 | ], |
| 59 | [ |
| 60 | '%s', |
| 61 | '%d', |
| 62 | '%d', |
| 63 | '%d', |
| 64 | '%s', |
| 65 | '%s', |
| 66 | '%s', |
| 67 | '%s', |
| 68 | '%s', |
| 69 | '%s', |
| 70 | '%s', |
| 71 | '%s', |
| 72 | '%s', |
| 73 | '%s', |
| 74 | '%s', |
| 75 | '%s', |
| 76 | '%s', |
| 77 | '%s', |
| 78 | '%s', |
| 79 | '%s', |
| 80 | '%s', |
| 81 | '%s', |
| 82 | '%s', |
| 83 | '%s', |
| 84 | '%s' |
| 85 | ] |
| 86 | ); |
| 87 | |
| 88 | return ! $result ? false : $this->wpdb()->insert_id; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @param OrderEntity $data |
| 93 | * |
| 94 | * @return false|int |
| 95 | */ |
| 96 | public function update(ModelInterface $data) |
| 97 | { |
| 98 | $result = $this->wpdb()->update( |
| 99 | $this->table, |
| 100 | [ |
| 101 | 'order_key' => ! empty($data->order_key) ? $data->order_key : OrderService::init()->generate_order_key(), |
| 102 | 'plan_id' => $data->plan_id, |
| 103 | 'customer_id' => $data->customer_id, |
| 104 | 'subscription_id' => $data->subscription_id, |
| 105 | 'order_type' => $data->order_type, |
| 106 | 'transaction_id' => $data->transaction_id, |
| 107 | 'payment_method' => $data->payment_method, |
| 108 | 'status' => $data->status, |
| 109 | 'coupon_code' => $data->coupon_code, |
| 110 | 'subtotal' => $data->subtotal, |
| 111 | 'tax' => $data->tax, |
| 112 | 'tax_rate' => $data->tax_rate, |
| 113 | 'discount' => $data->discount, |
| 114 | 'total' => $data->total, |
| 115 | 'billing_address' => $data->billing_address, |
| 116 | 'billing_city' => $data->billing_city, |
| 117 | 'billing_state' => $data->billing_state, |
| 118 | 'billing_country' => $data->billing_country, |
| 119 | 'billing_postcode' => $data->billing_postcode, |
| 120 | 'billing_phone' => $data->billing_phone, |
| 121 | 'mode' => $data->mode, |
| 122 | 'currency' => $data->currency, |
| 123 | 'ip_address' => $data->ip_address, |
| 124 | 'date_completed' => $data->date_completed |
| 125 | ], |
| 126 | ['id' => $data->id], |
| 127 | [ |
| 128 | '%s', |
| 129 | '%d', |
| 130 | '%d', |
| 131 | '%d', |
| 132 | '%s', |
| 133 | '%s', |
| 134 | '%s', |
| 135 | '%s', |
| 136 | '%s', |
| 137 | '%s', |
| 138 | '%s', |
| 139 | '%s', |
| 140 | '%s', |
| 141 | '%s', |
| 142 | '%s', |
| 143 | '%s', |
| 144 | '%s', |
| 145 | '%s', |
| 146 | '%s', |
| 147 | '%s', |
| 148 | '%s', |
| 149 | '%s', |
| 150 | '%s', |
| 151 | '%s' |
| 152 | ], |
| 153 | ['%d'] |
| 154 | ); |
| 155 | |
| 156 | return $result === false ? false : $data->id; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @param $id |
| 161 | * |
| 162 | * @return int|false |
| 163 | */ |
| 164 | public function delete($id) |
| 165 | { |
| 166 | return $this->wpdb()->delete($this->table, ['id' => $id], ['%d']); |
| 167 | } |
| 168 | |
| 169 | public function delete_pending_orders($customer_id, $plan_id) |
| 170 | { |
| 171 | return $this->wpdb()->delete( |
| 172 | $this->table, |
| 173 | [ |
| 174 | 'customer_id' => $customer_id, |
| 175 | 'plan_id' => $plan_id, |
| 176 | 'status' => OrderStatus::PENDING |
| 177 | ], |
| 178 | ['%d', '%d', '%s'] |
| 179 | ); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * @param $id |
| 184 | * |
| 185 | * @return OrderEntity |
| 186 | */ |
| 187 | public function retrieve($id) |
| 188 | { |
| 189 | $result = $this->wpdb()->get_row( |
| 190 | $this->wpdb()->prepare( |
| 191 | "SELECT * FROM $this->table WHERE id = %d", |
| 192 | $id |
| 193 | ), |
| 194 | ARRAY_A |
| 195 | ); |
| 196 | |
| 197 | if ( ! $result) $result = []; |
| 198 | |
| 199 | return OrderFactory::make($result); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * @param $order_key |
| 204 | * |
| 205 | * @return OrderEntity |
| 206 | */ |
| 207 | public function retrieveByOrderKey($order_key) |
| 208 | { |
| 209 | $result = $this->wpdb()->get_row( |
| 210 | $this->wpdb()->prepare( |
| 211 | "SELECT * FROM $this->table WHERE order_key = %s", |
| 212 | $order_key |
| 213 | ), |
| 214 | ARRAY_A |
| 215 | ); |
| 216 | |
| 217 | if ( ! $result) $result = []; |
| 218 | |
| 219 | return OrderFactory::make($result); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * @param $args |
| 224 | * @param $count |
| 225 | * |
| 226 | * @return int|OrderEntity[] |
| 227 | */ |
| 228 | public function retrieveBy($args = array(), $count = false) |
| 229 | { |
| 230 | $defaults = [ |
| 231 | 'search' => '', |
| 232 | 'number' => 10, |
| 233 | 'offset' => 0, |
| 234 | 'order_id' => 0, |
| 235 | 'plan_id' => 0, |
| 236 | 'customer_id' => 0, |
| 237 | 'subscription_id' => 0, |
| 238 | 'transaction_id' => 0, |
| 239 | 'order_type' => '', |
| 240 | 'payment_method' => '', |
| 241 | 'status' => [], |
| 242 | 'coupon_code' => '', |
| 243 | 'mode' => '', |
| 244 | 'currency' => '', |
| 245 | 'date_created' => '', |
| 246 | 'date_completed' => '', |
| 247 | 'start_date' => '', |
| 248 | 'end_date' => '', |
| 249 | 'date_compare' => '=', |
| 250 | 'date_column' => 'date_created', |
| 251 | 'order' => 'DESC', |
| 252 | 'orderby' => 'id' |
| 253 | ]; |
| 254 | |
| 255 | $args = wp_parse_args($args, $defaults); |
| 256 | |
| 257 | $limit = absint($args['number']); |
| 258 | |
| 259 | $offset = $args['offset']; |
| 260 | $search = $args['search']; |
| 261 | |
| 262 | $sql = "SELECT * FROM $this->table"; |
| 263 | |
| 264 | if ($count === true) { |
| 265 | $sql = "SELECT COUNT(id) FROM $this->table"; |
| 266 | } |
| 267 | |
| 268 | $user_table = $this->wpdb()->users; |
| 269 | $customer_table = Base::customers_db_table(); |
| 270 | |
| 271 | $date_compare = ! empty($args['date_compare']) ? esc_sql($args['date_compare']) : '='; |
| 272 | |
| 273 | $replacement = [1]; |
| 274 | $sql .= " WHERE 1=%d"; // fixes Notice: wpdb::prepare was called incorrectly. The query argument of wpdb::prepare() must have a placeholder |
| 275 | |
| 276 | if ($args['order_id'] > 0) { |
| 277 | $sql .= " AND id = %d"; |
| 278 | $replacement[] = (int)$args['order_id']; |
| 279 | } |
| 280 | |
| 281 | if ($args['plan_id'] > 0) { |
| 282 | $sql .= " AND plan_id = %d"; |
| 283 | $replacement[] = (int)$args['plan_id']; |
| 284 | } |
| 285 | |
| 286 | if ($args['customer_id'] > 0) { |
| 287 | $sql .= " AND customer_id = %d"; |
| 288 | $replacement[] = (int)$args['customer_id']; |
| 289 | } |
| 290 | |
| 291 | if ($args['subscription_id'] > 0) { |
| 292 | $sql .= " AND subscription_id = %d"; |
| 293 | $replacement[] = (int)$args['subscription_id']; |
| 294 | } |
| 295 | |
| 296 | if ( ! empty($args['transaction_id'])) { |
| 297 | $sql .= " AND transaction_id = %s"; |
| 298 | $replacement[] = sanitize_text_field($args['transaction_id']); |
| 299 | } |
| 300 | |
| 301 | $order_type = $args['order_type']; |
| 302 | if ( ! empty($order_type) && in_array($order_type, array_keys(OrderType::get_all()))) { |
| 303 | $sql .= " AND order_type = %s"; |
| 304 | $replacement[] = $order_type; |
| 305 | } |
| 306 | |
| 307 | if ( ! empty($args['payment_method'])) { |
| 308 | $sql .= " AND payment_method = %s"; |
| 309 | $replacement[] = $args['payment_method']; |
| 310 | } |
| 311 | |
| 312 | $args['status'] = ! empty($args['status']) && is_string($args['status']) ? [$args['status']] : $args['status']; |
| 313 | |
| 314 | if ( |
| 315 | ! empty($args['status']) && |
| 316 | count(array_intersect($args['status'], array_keys(OrderStatus::get_all()))) == count($args['status']) |
| 317 | ) { |
| 318 | $sql .= " AND status IN (" . implode(',', array_fill(0, count($args['status']), '%s')) . ") "; |
| 319 | $replacement = array_merge($replacement, $args['status']); |
| 320 | } |
| 321 | |
| 322 | if ( ! empty($args['coupon_code'])) { |
| 323 | $sql .= " AND coupon_code = %s"; |
| 324 | $replacement[] = sanitize_text_field($args['coupon_code']); |
| 325 | } |
| 326 | |
| 327 | if ( ! empty($args['currency'])) { |
| 328 | $sql .= " AND currency = %s"; |
| 329 | $replacement[] = sanitize_text_field($args['currency']); |
| 330 | } |
| 331 | |
| 332 | if ( ! empty($args['mode']) && in_array($args['mode'], array_keys(OrderMode::get_all()))) { |
| 333 | $sql .= " AND mode = %s"; |
| 334 | $replacement[] = $args['mode']; |
| 335 | } |
| 336 | |
| 337 | if ( ! empty($args['date_created'])) { |
| 338 | $sql .= " AND date_created $date_compare %s"; |
| 339 | $replacement[] = gmdate('Y-m-d H:i:s', ppress_strtotime_utc($args['date_created'])); |
| 340 | } |
| 341 | |
| 342 | if ( ! empty($args['date_completed'])) { |
| 343 | $sql .= " AND date_created = %s"; |
| 344 | $replacement[] = gmdate('Y-m-d H:i:s', ppress_strtotime_utc($args['date_completed'])); |
| 345 | } |
| 346 | |
| 347 | $start_date = $args['start_date']; |
| 348 | $end_date = $args['end_date']; |
| 349 | $date_column = esc_sql($args['date_column']); |
| 350 | |
| 351 | if ( ! empty($start_date)) { |
| 352 | $sql .= " AND $date_column >= %s"; |
| 353 | $replacement[] = gmdate('Y-m-d H:i:s', ppress_strtotime_utc($start_date)); |
| 354 | } |
| 355 | |
| 356 | if ( ! empty($end_date)) { |
| 357 | $sql .= " AND $date_column <= %s"; |
| 358 | $replacement[] = gmdate('Y-m-d H:i:s', ppress_strtotime_utc($end_date)); |
| 359 | } |
| 360 | |
| 361 | if ( ! empty($search)) { |
| 362 | |
| 363 | if (is_numeric($search)) { |
| 364 | $sql .= " AND (id = %d"; |
| 365 | $sql .= " OR plan_id = %d"; |
| 366 | $sql .= " OR customer_id = %d"; |
| 367 | $sql .= " OR subscription_id = %d)"; |
| 368 | |
| 369 | $replacement[] = $search; |
| 370 | $replacement[] = $search; |
| 371 | $replacement[] = $search; |
| 372 | $replacement[] = $search; |
| 373 | } elseif (filter_var($search, FILTER_VALIDATE_EMAIL)) { |
| 374 | $sql .= " AND customer_id = (SELECT id FROM $customer_table WHERE user_id = (SELECT ID FROM $user_table WHERE user_email = %s))"; |
| 375 | $replacement[] = $search; |
| 376 | } else { |
| 377 | $sql .= " AND (order_key LIKE %s"; |
| 378 | $sql .= " OR transaction_id = %s"; |
| 379 | $sql .= " OR payment_method = %s"; |
| 380 | $sql .= " OR coupon_code = %s"; |
| 381 | $sql .= " OR ip_address = %s)"; |
| 382 | $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))"; |
| 383 | |
| 384 | $search_like = '%' . parent::wpdb()->esc_like(sanitize_text_field($search)) . '%'; |
| 385 | |
| 386 | $replacement[] = parent::wpdb()->esc_like(sanitize_text_field($search)) . '%'; |
| 387 | $replacement[] = $search; |
| 388 | $replacement[] = $search; |
| 389 | $replacement[] = $search; |
| 390 | $replacement[] = $search; |
| 391 | $replacement[] = $search_like; |
| 392 | $replacement[] = $search_like; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | $sql .= sprintf(" ORDER BY %s %s", esc_sql($args['orderby']), esc_sql($args['order'])); |
| 397 | |
| 398 | if ($count === false) { |
| 399 | if ($limit > 0) { |
| 400 | $sql .= " LIMIT %d"; |
| 401 | $replacement[] = $limit; |
| 402 | } |
| 403 | |
| 404 | if ($offset > 0) { |
| 405 | $sql .= " OFFSET %d"; |
| 406 | $replacement[] = $offset; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | |
| 411 | if ($count === true) { |
| 412 | return (int)$this->wpdb()->get_var($this->wpdb()->prepare($sql, $replacement)); |
| 413 | } |
| 414 | |
| 415 | $result = $this->wpdb()->get_results($this->wpdb()->prepare($sql, $replacement), 'ARRAY_A'); |
| 416 | |
| 417 | if (is_array($result) && ! empty($result)) { |
| 418 | return array_map([OrderFactory::class, 'make'], $result); |
| 419 | } |
| 420 | |
| 421 | return []; |
| 422 | } |
| 423 | |
| 424 | public function get_customer_total_spend($customer_id) |
| 425 | { |
| 426 | return $this->wpdb()->get_var( |
| 427 | $this->wpdb()->prepare( |
| 428 | "SELECT SUM(total) FROM $this->table WHERE customer_id = %d AND status = %s", |
| 429 | $customer_id, |
| 430 | OrderStatus::COMPLETED |
| 431 | ) |
| 432 | ); |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * @param int $order_id |
| 437 | * @param string $meta_key |
| 438 | * @param string $meta_value |
| 439 | * @param bool $unique |
| 440 | * |
| 441 | * @return int|false Meta ID on success, false on failure. |
| 442 | */ |
| 443 | public function add_meta_data($order_id, $meta_key, $meta_value, $unique = false) |
| 444 | { |
| 445 | return add_metadata('ppress_order', $order_id, $meta_key, $meta_value, $unique); |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * @param int $order_id |
| 450 | * @param string $meta_key |
| 451 | * @param string $meta_value |
| 452 | * @param string $prev_value |
| 453 | * |
| 454 | * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure. |
| 455 | */ |
| 456 | public function update_meta_data($order_id, $meta_key, $meta_value, $prev_value = '') |
| 457 | { |
| 458 | return update_metadata('ppress_order', $order_id, $meta_key, $meta_value, $prev_value); |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * @param int $order_id |
| 463 | * @param string $meta_key |
| 464 | * @param string $meta_value |
| 465 | * @param bool $delete_all |
| 466 | * |
| 467 | * @return bool True on success, false on failure. |
| 468 | */ |
| 469 | public function delete_meta_data($order_id, $meta_key, $meta_value = '', $delete_all = false) |
| 470 | { |
| 471 | return delete_metadata('ppress_order', $order_id, $meta_key, $meta_value, $delete_all); |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * @param $order_id |
| 476 | * |
| 477 | * @return bool |
| 478 | */ |
| 479 | public function delete_all_meta_data($order_id) |
| 480 | { |
| 481 | return $this->wpdb()->delete( |
| 482 | Base::order_meta_db_table(), |
| 483 | array('ppress_order_id' => $order_id) |
| 484 | ); |
| 485 | } |
| 486 | |
| 487 | /** |
| 488 | * @param $order_id |
| 489 | * @param string $meta_key |
| 490 | * @param bool $single |
| 491 | * |
| 492 | * @return array|false|mixed |
| 493 | */ |
| 494 | public function get_meta_data($order_id, $meta_key = '', $single = true) |
| 495 | { |
| 496 | return get_metadata('ppress_order', $order_id, $meta_key, $single); |
| 497 | } |
| 498 | |
| 499 | public function get_count_by_status($status) |
| 500 | { |
| 501 | return $this->wpdb()->get_var( |
| 502 | $this->wpdb()->prepare( |
| 503 | "SELECT COUNT(id) FROM $this->table WHERE status = %s", |
| 504 | $status |
| 505 | ) |
| 506 | ); |
| 507 | } |
| 508 | } |