PluginProbe ʕ •ᴥ•ʔ
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress / trunk
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress vtrunk
4.16.18 4.16.17 4.16.16 trunk 1.0 1.0.1 1.0.2 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.5a 1.1.6 1.1.7 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.4 1.4.1 1.4.2 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.7 1.7.1 1.7.2 1.8 1.8.1 1.8.10 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9 1.9.1 1.9.10 1.9.11 1.9.12 1.9.13 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.1.9 2.2.10 2.2.11 2.2.12 2.2.13 2.2.14 2.2.15 2.2.16 2.2.2 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 3.0 3.1 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 3.1.19 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.10 3.2.11 3.2.12 3.2.13 3.2.14 3.2.15 3.2.16 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 4.0.0 4.0.1 4.0.2 4.0.3 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.10.0 4.10.1 4.10.2 4.10.3 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.13.3 4.13.4 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.15.0 4.15.1 4.15.10 4.15.11 4.15.12 4.15.13 4.15.14 4.15.15 4.15.16 4.15.17 4.15.18 4.15.19 4.15.2 4.15.20 4.15.20.1 4.15.21 4.15.22 4.15.23 4.15.24 4.15.25 4.15.3 4.15.4 4.15.5 4.15.6 4.15.7 4.15.8 4.15.9 4.16.0 4.16.1 4.16.10 4.16.11 4.16.12 4.16.13 4.16.14 4.16.15 4.16.2 4.16.3 4.16.4 4.16.5 4.16.6 4.16.7 4.16.8 4.16.9 4.2.0 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.5.3 4.5.4 4.5.5 4.6.0 4.7.0 4.8.0 4.9.0
wp-user-avatar / src / Membership / Repositories / CustomerRepository.php
wp-user-avatar / src / Membership / Repositories Last commit date
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 }