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
PlanRepository.php
184 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Membership\Repositories; |
| 4 | |
| 5 | use ProfilePress\Core\Base; |
| 6 | use ProfilePress\Core\Membership\Models\Plan\PlanFactory; |
| 7 | use ProfilePress\Core\Membership\Models\ModelInterface; |
| 8 | use ProfilePress\Core\Membership\Models\Plan\PlanEntity; |
| 9 | |
| 10 | class PlanRepository extends BaseRepository |
| 11 | { |
| 12 | protected $table; |
| 13 | |
| 14 | public function __construct() |
| 15 | { |
| 16 | $this->table = Base::subscription_plans_db_table(); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * @param PlanEntity $data |
| 21 | * |
| 22 | * @return false|int |
| 23 | */ |
| 24 | public function add(ModelInterface $data) |
| 25 | { |
| 26 | global $wpdb; |
| 27 | |
| 28 | $result = $wpdb->insert( |
| 29 | $this->table, |
| 30 | array( |
| 31 | 'name' => $data->name, |
| 32 | 'description' => $data->description, |
| 33 | 'user_role' => $data->user_role, |
| 34 | 'order_note' => $data->order_note, |
| 35 | 'price' => $data->price, |
| 36 | 'billing_frequency' => $data->billing_frequency, |
| 37 | 'subscription_length' => $data->subscription_length, |
| 38 | 'total_payments' => $data->total_payments, |
| 39 | 'signup_fee' => $data->signup_fee, |
| 40 | 'free_trial' => $data->free_trial |
| 41 | ), |
| 42 | array( |
| 43 | '%s', |
| 44 | '%s', |
| 45 | '%s', |
| 46 | '%s', |
| 47 | '%s', |
| 48 | '%s', |
| 49 | '%s', |
| 50 | '%s', |
| 51 | '%d', |
| 52 | '%s', |
| 53 | '%d', |
| 54 | ) |
| 55 | ); |
| 56 | |
| 57 | return ! $result ? false : $wpdb->insert_id; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @param PlanEntity $data |
| 62 | * |
| 63 | * @return false|int |
| 64 | */ |
| 65 | public function update(ModelInterface $data) |
| 66 | { |
| 67 | global $wpdb; |
| 68 | |
| 69 | $result = $wpdb->update( |
| 70 | $this->table, |
| 71 | [ |
| 72 | 'name' => $data->name, |
| 73 | 'description' => $data->description, |
| 74 | 'user_role' => $data->user_role, |
| 75 | 'order_note' => $data->order_note, |
| 76 | 'price' => $data->price, |
| 77 | 'billing_frequency' => $data->billing_frequency, |
| 78 | 'subscription_length' => $data->subscription_length, |
| 79 | 'total_payments' => $data->total_payments, |
| 80 | 'signup_fee' => $data->signup_fee, |
| 81 | 'free_trial' => $data->free_trial |
| 82 | ], |
| 83 | ['id' => $data->id], |
| 84 | [ |
| 85 | '%s', |
| 86 | '%s', |
| 87 | '%s', |
| 88 | '%s', |
| 89 | '%s', |
| 90 | '%s', |
| 91 | '%s', |
| 92 | '%d', |
| 93 | '%s', |
| 94 | '%s', |
| 95 | ], |
| 96 | ['%d'] |
| 97 | ); |
| 98 | |
| 99 | return $result === false ? false : $data->id; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @param $id |
| 104 | * |
| 105 | * @return int|false |
| 106 | */ |
| 107 | public function delete($id) |
| 108 | { |
| 109 | global $wpdb; |
| 110 | |
| 111 | return $wpdb->delete($this->table, ['id' => $id], ['%d']); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @param $id |
| 116 | * |
| 117 | * @return PlanEntity |
| 118 | */ |
| 119 | public function retrieve($id) |
| 120 | { |
| 121 | global $wpdb; |
| 122 | |
| 123 | $result = $wpdb->get_row( |
| 124 | $wpdb->prepare( |
| 125 | "SELECT * FROM $this->table WHERE id = %d", |
| 126 | $id |
| 127 | ), |
| 128 | ARRAY_A |
| 129 | ); |
| 130 | |
| 131 | if ( ! $result) $result = []; |
| 132 | |
| 133 | return PlanFactory::make($result); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @param int $limit |
| 138 | * @param int $current_page |
| 139 | * |
| 140 | * @return PlanEntity[]|array |
| 141 | */ |
| 142 | public function retrieveAll($limit = 0, $current_page = 1, $status = '') |
| 143 | { |
| 144 | global $wpdb; |
| 145 | |
| 146 | $replacement = [1]; |
| 147 | $sql = "SELECT * FROM $this->table"; |
| 148 | $sql .= " WHERE 1=%d"; // fixes Notice: wpdb::prepare was called incorrectly. The query argument of wpdb::prepare() must have a placeholder |
| 149 | |
| 150 | if ( ! empty($status)) { |
| 151 | $sql .= " AND status = %s"; |
| 152 | $replacement[] = $status; |
| 153 | } |
| 154 | |
| 155 | $sql .= " ORDER BY id DESC"; |
| 156 | if ($limit > 0) { |
| 157 | $sql .= " LIMIT %d"; |
| 158 | $replacement[] = $limit; |
| 159 | } |
| 160 | |
| 161 | if ($current_page > 1) { |
| 162 | $sql .= " OFFSET %d"; |
| 163 | $replacement[] = ($current_page - 1) * $limit; |
| 164 | } |
| 165 | |
| 166 | $result = $wpdb->get_results($wpdb->prepare($sql, $replacement), 'ARRAY_A'); |
| 167 | |
| 168 | if (is_array($result) && ! empty($result)) { |
| 169 | return array_map([PlanFactory::class, 'make'], $result); |
| 170 | } |
| 171 | |
| 172 | return []; |
| 173 | } |
| 174 | |
| 175 | public function get_count_by_status($status) |
| 176 | { |
| 177 | return $this->wpdb()->get_var( |
| 178 | $this->wpdb()->prepare( |
| 179 | "SELECT COUNT(id) FROM $this->table WHERE status = %s", |
| 180 | $status |
| 181 | ) |
| 182 | ); |
| 183 | } |
| 184 | } |