PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 7.9.8
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v7.9.8
8.0.4 8.0.3 8.0.1 8.0.2 8.0.0 7.9.9.7 7.9.9.6 7.9.9.5 7.9.9.4 7.9.9.3 7.9.9.2 7.9.9.1 7.9.9 7.9.8 7.9.7 7.9.6 7.9.5 7.9.4 7.9.3 7.9.2 7.9.1 7.9.0 7.8.9 7.8.8 7.8.7 All 46 releases
gamipress / includes / functions / user-earnings.php

user-earnings.php in GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI 7.9.8, at includes/functions/user-earnings.php

516 lines 14.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * User Earnings Functions
4 *
5 * @package GamiPress\User_Earnings_Functions
6 * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]>
7 * @since 1.4.3
8 */
9 // Exit if accessed directly
10 if( !defined( 'ABSPATH' ) ) exit;
11
12 /**
13 * Create a user earning
14 *
15 * @since 1.4.3
16 *
17 * @param int $user_id The user ID
18 * @param array $data User earning data
19 * @param array $meta User earning meta data
20 *
21 * @return int The user earning ID of the newly created user earning entry
22 */
23 function gamipress_insert_user_earning( $user_id = 0, $data = array(), $meta = array() ) {
24
25 global $wpdb;
26
27 // Setup table
28 $ct_table = ct_setup_table( 'gamipress_user_earnings' );
29
30 $user_id = absint( $user_id );
31
32 if( $user_id === 0 ) {
33 $user_id = get_current_user_id();
34 }
35
36 // Post data
37 $data = wp_parse_args( $data, array(
38 'title' => '',
39 'user_id' => $user_id,
40 'post_id' => 0,
41 'post_type' => '',
42 'points' => 0,
43 'points_type' => '',
44 'date' => date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ),
45 ) );
46
47 // If title is empty, try to get the title from post assigned
48 if( empty( $data['title'] ) && absint( $data['post_id'] ) !== 0 ) {
49 $data['title'] = gamipress_get_post_field( 'post_title', $data['post_id'] );
50 }
51
52 /**
53 * Filter available to override the user earning data
54 *
55 * @since 6.9.8
56 *
57 * @param array $data User earning data
58 * @param array $meta User earning meta data
59 * @param int $user_id The user ID
60 *
61 * @return array
62 */
63 $data = apply_filters( 'gamipress_insert_user_earning_data', $data, $meta, $user_id );
64
65 /**
66 * Filter available to override the user earning metadata
67 *
68 * @since 6.9.8
69 *
70 * @param array $meta User earning meta data
71 * @param array $data User earning data
72 * @param int $user_id The user ID
73 *
74 * @return array
75 */
76 $meta = apply_filters( 'gamipress_insert_user_earning_meta', $meta, $data, $user_id );
77
78 /**
79 * Filter to allow insert earnings to user
80 *
81 * @since 7.0.5
82 *
83 * @param bool
84 * @param array $data User earning data
85 * @param array $meta User earning meta data
86 * @param int $user_id The user ID
87 *
88 * @return bool
89 */
90 $allow = apply_filters( 'gamipress_allow_insert_user_earning_to_user', true, $user_id, $data, $meta );
91
92 if ( ! $allow ) {
93 return 0;
94 }
95
96 // Store user earning entry
97 $user_earning_id = $ct_table->db->insert( $data );
98
99 // parent_post_type and parent_id meta to meet on requirements the parent information
100 if( $user_earning_id && in_array( $data['post_type'], gamipress_get_requirement_types_slugs() ) ) {
101 $parent_id = absint( gamipress_get_post_field( 'post_parent', $data['post_id'] ) );
102
103 $meta['parent_post_type'] = '';
104
105 if( $parent_id !== 0 ) {
106 $meta['parent_post_type'] = gamipress_get_post_field( 'post_type', $parent_id );
107
108 if( $meta['parent_post_type'] === 'points-type' ) {
109 $meta['parent_post_type'] = gamipress_get_post_field( 'post_name', $parent_id );
110 }
111 }
112
113 if( empty( $meta['parent_post_type'] ) ) {
114 $meta['parent_post_type'] = $data['post_type'];
115 }
116 }
117
118 // Store user earning meta data
119 if ( $user_earning_id && ! empty( $meta ) ) {
120
121 $metas = array();
122
123 foreach ( (array) $meta as $key => $value ) {
124 // Sanitize vars
125 $meta_key = '_gamipress_' . sanitize_key( $key );
126 $meta_key = wp_unslash( $meta_key );
127 $meta_value = wp_unslash( $value );
128 $meta_value = esc_sql( $meta_value );
129 $meta_value = sanitize_meta( $meta_key, $meta_value, $ct_table->name );
130 $meta_value = maybe_serialize( $meta_value );
131
132 // Setup the insert value
133 $metas[] = $wpdb->prepare( '%d, %s, %s', array( $user_earning_id, $meta_key, $meta_value ) );
134 }
135
136 $user_earnings_meta = GamiPress()->db->user_earnings_meta;
137 $metas = implode( '), (', $metas );
138
139 // Since the user earning is recently inserted, is faster to run a single query to insert all metas instead of insert them one-by-one
140 $wpdb->query( "INSERT INTO {$user_earnings_meta} (user_earning_id, meta_key, meta_value) VALUES ({$metas})" );
141
142 }
143
144 /**
145 * Action triggered after insert a user earning
146 *
147 * @since 1.4.3
148 *
149 * @param int $user_earning_id The user earning ID
150 * @param array $data User earning data
151 * @param array $meta User earning meta data
152 * @param int $user_id The user ID
153 */
154 do_action( 'gamipress_insert_user_earning', $user_earning_id, $data, $meta, $user_id );
155
156 ct_reset_setup_table();
157
158 return $user_earning_id;
159
160 }
161
162 /**
163 * Get a specific earning count
164 *
165 * @since 1.8.6
166 *
167 * @param array $query User earning query parameters
168 *
169 * @return int The number of user earnings found
170 */
171 function gamipress_get_earnings_count( $query = array() ) {
172
173 global $wpdb;
174
175 $where = gamipress_get_earnings_where( $query );
176
177 // Merge all wheres
178 $where = implode( ' AND ', $where );
179
180 $user_earnings = GamiPress()->db->user_earnings;
181
182 return absint( $wpdb->get_var( "SELECT COUNT(*) FROM {$user_earnings} AS ue WHERE {$where}" ) );
183
184 }
185
186 /**
187 * Get a specific earning points sum
188 *
189 * @since 1.8.6
190 *
191 * @param array $query User earning query parameters
192 *
193 * @return int The sum of points of user earnings found
194 */
195 function gamipress_get_earnings_points_sum( $query = array() ) {
196
197 global $wpdb;
198
199 $where = gamipress_get_earnings_where( $query );
200
201 // Merge all wheres
202 $where = implode( ' AND ', $where );
203
204 $user_earnings = GamiPress()->db->user_earnings;
205
206 return absint( $wpdb->get_var( "SELECT SUM(ue.points) FROM {$user_earnings} AS ue WHERE {$where}" ) );
207
208 }
209
210 /**
211 * Get the last earning
212 *
213 * @since 2.1.2
214 *
215 * @param array $query User earning query parameters
216 *
217 * @return stdClass|null The last earning object
218 */
219 function gamipress_get_last_earning( $query = array() ) {
220
221 $query['limit'] = 1;
222
223 return gamipress_get_last_earnings( $query );
224
225 }
226
227 /**
228 * Get the last earnings
229 *
230 * @since 2.2.0
231 *
232 * @param array $query User earning query parameters
233 *
234 * @return array|stdClass|null Array of the last earnings registered
235 */
236 function gamipress_get_last_earnings( $query = array() ) {
237
238 global $wpdb;
239
240 $where = gamipress_get_earnings_where( $query );
241
242 // Merge all wheres
243 $where = implode( ' AND ', $where );
244
245 $user_earnings = GamiPress()->db->user_earnings;
246
247 $limit = ( isset( $query['limit'] ) ? absint( $query['limit'] ) : 1 );
248
249 return $wpdb->get_results( "SELECT ue.* FROM {$user_earnings} AS ue WHERE {$where} ORDER BY ue.date DESC LIMIT {$limit}" );
250
251 }
252
253 /**
254 * Get the last earning ID
255 *
256 * @since 2.1.2
257 *
258 * @param array $query User earning query parameters
259 *
260 * @return int The last earning ID
261 */
262 function gamipress_get_last_earning_id( $query = array() ) {
263
264 global $wpdb;
265
266 $where = gamipress_get_earnings_where( $query );
267
268 // Merge all wheres
269 $where = implode( ' AND ', $where );
270
271 $user_earnings = GamiPress()->db->user_earnings;
272
273 return $wpdb->get_var( $wpdb->prepare( "SELECT ue.user_earning_id FROM {$user_earnings} AS ue WHERE {$where} ORDER BY ue.date DESC LIMIT 1" ) );
274
275 }
276
277 /**
278 * Get the last earning post ID
279 *
280 * @since 2.1.2
281 *
282 * @param array $query User earning query parameters
283 *
284 * @return int The last earning post ID
285 */
286 function gamipress_get_last_earning_post_id( $query = array() ) {
287
288 global $wpdb;
289
290 $where = gamipress_get_earnings_where( $query );
291
292 // Merge all wheres
293 $where = implode( ' AND ', $where );
294
295 $user_earnings = GamiPress()->db->user_earnings;
296
297 return $wpdb->get_var( "SELECT ue.post_id FROM {$user_earnings} AS ue WHERE {$where} ORDER BY ue.date DESC LIMIT 1" );
298
299 }
300
301 /**
302 * Get the last earning date
303 *
304 * @since 1.8.7
305 *
306 * @param array $query User earning query parameters
307 *
308 * @return string The last earning date
309 */
310 function gamipress_get_last_earning_date( $query = array() ) {
311
312 global $wpdb;
313
314 $where = gamipress_get_earnings_where( $query );
315
316 // Merge all wheres
317 $where = implode( ' AND ', $where );
318
319 $user_earnings = GamiPress()->db->user_earnings;
320
321 return $wpdb->get_var( "SELECT ue.date FROM {$user_earnings} AS ue WHERE {$where} ORDER BY ue.date DESC LIMIT 1" );
322
323 }
324
325 /**
326 * Get the last earning datetime
327 *
328 * @since 1.8.7
329 *
330 * @param array $query User earning query parameters
331 *
332 * @return int The last earning datetime
333 */
334 function gamipress_get_last_earning_datetime( $query = array() ) {
335
336 $date = gamipress_get_last_earning_date( $query );
337
338 return ! empty( $date ) ? strtotime( $date ) : 0;
339
340 }
341
342 /**
343 * Setup a common where conditions for the user earnings queries
344 *
345 * @since 1.8.7
346 *
347 * @param array $query User earning query parameters
348 *
349 * @return array Array of where clauses
350 */
351 function gamipress_get_earnings_where( $query = array() ) {
352
353 // Post data
354 $query = wp_parse_args( $query, array(
355 'user_id' => 0,
356 'post_id' => 0,
357 'achievement_id' => 0,
358 'post_type' => '',
359 'points_type' => '',
360 'since' => 0,
361 ) );
362
363 // Parse mapped keys
364 if( $query['achievement_id'] !== 0 && $query['post_id'] === 0 ) {
365 $query['post_id'] = $query['achievement_id'];
366 }
367
368 $where = array(
369 '1 = 1'
370 );
371
372 // User ID
373 if( is_array( $query['user_id'] ) ) {
374 $where[] = 'ue.user_id IN ( ' . implode( ', ', $query['user_id'] ) . ' )';
375 } else if ( absint( $query['user_id'] ) !== 0 ) {
376 $where[] = 'ue.user_id = ' . absint( $query['user_id'] );
377 }
378
379 // Post ID
380 if( is_array( $query['post_id'] ) ) {
381 $where[] = 'ue.post_id IN ( ' . implode( ', ', $query['post_id'] ) . ' )';
382 } else if ( absint( $query['post_id'] ) !== 0 ) {
383 $where[] = 'ue.post_id = ' . absint( $query['post_id'] );
384 }
385
386 // Post type
387 if( is_array( $query['post_type'] ) ) {
388 $where[] = 'ue.post_type IN ( "' . implode( '", "', $query['post_type'] ) . '" )';
389 } else if ( ! empty( $query['post_type'] ) ) {
390 $where[] = 'ue.post_type = "' . $query['post_type'] . '"';
391 }
392
393 // Points type
394 if( is_array( $query['points_type'] ) ) {
395 $where[] = 'ue.points_type IN ( "' . implode( '", "', $query['points_type'] ) . '" )';
396 } else if ( ! empty( $query['points_type'] ) ) {
397 $where[] = 'ue.points_type = "' . $query['points_type'] . '"';
398 }
399
400 // Points
401 if( isset( $query['points'] ) ) {
402
403 $compare = '=';
404
405 if( isset( $query['points_compare'] ) ) {
406 $allowed_operators = array( '=', '<', '>', '<=', '>=' );
407 if( in_array( $query['points_compare'], $allowed_operators ) ) {
408 $compare = $query['points_compare'];
409 }
410 }
411
412 $where[] = 'ue.points ' . $compare . ' ' . $query['points'];
413 }
414
415 // Since
416 if( ! empty( $query['since'] ) ) {
417
418 $since = $query['since'];
419
420 // Turn a string date into time
421 if( gettype( $query['since'] ) === 'string' ) {
422 $since = strtotime( $query['since'] );
423 }
424
425 if( $since > 0 ) {
426 $since = date( 'Y-m-d H:i:s', $since );
427 $where[] = " ue.date > '{$since}'";
428 }
429 }
430
431 // Before
432 if( isset( $query['before'] ) ) {
433
434 $before = $query['before'];
435
436 // Turn a string date into time
437 if( gettype( $query['before'] ) === 'string' ) {
438 $before = strtotime( $query['before'] );
439 }
440
441 if( $before > 0 ) {
442 $before = date( 'Y-m-d H:i:s', $before );
443 $where[] = " ue.date < '{$before}'";
444 }
445
446 }
447
448 // After
449 if( isset( $query['after'] ) ) {
450
451 $after = $query['after'];
452
453 // Turn a string date into time
454 if( gettype( $query['after'] ) === 'string' ) {
455 $after = strtotime( $query['after'] );
456 }
457
458 if( $after > 0 ) {
459 $after = date( 'Y-m-d H:i:s', $after );
460 $where[] = " ue.date > '{$after}'";
461 }
462
463 }
464
465 return $where;
466
467 }
468
469 /**
470 * Get the user earning object data
471 *
472 * @since 2.1.2
473 *
474 * @param int $user_earning_id The user earning ID
475 * @param string $meta_key The meta key to retrieve. By default, returns
476 * data for all keys. Default empty.
477 * @param bool $single Optional. Whether to return a single value. Default false.
478 *
479 * @return mixed Will be an array if $single is false. Will be value of meta data field if $single is true.
480 */
481 function gamipress_get_user_earning_meta( $user_earning_id, $meta_key = '', $single = false ) {
482
483 ct_setup_table( 'gamipress_user_earnings' );
484
485 $meta_value = ct_get_object_meta( $user_earning_id, $meta_key, $single );
486
487 ct_reset_setup_table();
488
489 return $meta_value;
490
491 }
492
493 /**
494 * Update the user earning object data
495 *
496 * @since 2.1.2
497 *
498 * @param int $user_earning_id The user earning ID
499 * @param string $meta_key Metadata key.
500 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
501 * @param mixed $prev_value Optional. Previous value to check before removing.
502 * Default empty.
503 *
504 * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure.
505 */
506 function gamipress_update_user_earning_meta( $user_earning_id, $meta_key, $meta_value, $prev_value = '' ) {
507
508 ct_setup_table( 'gamipress_user_earnings' );
509
510 $meta_id = ct_update_object_meta( $user_earning_id, $meta_key, $meta_value, $prev_value );
511
512 ct_reset_setup_table();
513
514 return $meta_id;
515
516 }