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