PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 8.0.5
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v8.0.5
8.0.5 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 All 47 releases
← All changes | includes/functions/user-earnings.php +518 -0 7.9.0 → 8.0.5 View file →
@@ -1,0 +1,518 @@
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 + $results = gamipress_get_last_earnings( $query );
224 +
225 + return count( $results ) === 1 ? $results[0] : $results;
226 +
227 +}
228 +
229 +/**
230 + * Get the last earnings
231 + *
232 + * @since 2.2.0
233 + *
234 + * @param array $query User earning query parameters
235 + *
236 + * @return array|stdClass|null Array of the last earnings registered
237 + */
238 +function gamipress_get_last_earnings( $query = array() ) {
239 +
240 + global $wpdb;
241 +
242 + $where = gamipress_get_earnings_where( $query );
243 +
244 + // Merge all wheres
245 + $where = implode( ' AND ', $where );
246 +
247 + $user_earnings = GamiPress()->db->user_earnings;
248 +
249 + $limit = ( isset( $query['limit'] ) ? absint( $query['limit'] ) : 1 );
250 +
251 + return $wpdb->get_results( "SELECT ue.* FROM {$user_earnings} AS ue WHERE {$where} ORDER BY ue.date DESC LIMIT {$limit}" );
252 +
253 +}
254 +
255 +/**
256 + * Get the last earning ID
257 + *
258 + * @since 2.1.2
259 + *
260 + * @param array $query User earning query parameters
261 + *
262 + * @return int The last earning ID
263 + */
264 +function gamipress_get_last_earning_id( $query = array() ) {
265 +
266 + global $wpdb;
267 +
268 + $where = gamipress_get_earnings_where( $query );
269 +
270 + // Merge all wheres
271 + $where = implode( ' AND ', $where );
272 +
273 + $user_earnings = GamiPress()->db->user_earnings;
274 +
275 + 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" ) );
276 +
277 +}
278 +
279 +/**
280 + * Get the last earning post ID
281 + *
282 + * @since 2.1.2
283 + *
284 + * @param array $query User earning query parameters
285 + *
286 + * @return int The last earning post ID
287 + */
288 +function gamipress_get_last_earning_post_id( $query = array() ) {
289 +
290 + global $wpdb;
291 +
292 + $where = gamipress_get_earnings_where( $query );
293 +
294 + // Merge all wheres
295 + $where = implode( ' AND ', $where );
296 +
297 + $user_earnings = GamiPress()->db->user_earnings;
298 +
299 + return $wpdb->get_var( "SELECT ue.post_id FROM {$user_earnings} AS ue WHERE {$where} ORDER BY ue.date DESC LIMIT 1" );
300 +
301 +}
302 +
303 +/**
304 + * Get the last earning date
305 + *
306 + * @since 1.8.7
307 + *
308 + * @param array $query User earning query parameters
309 + *
310 + * @return string The last earning date
311 + */
312 +function gamipress_get_last_earning_date( $query = array() ) {
313 +
314 + global $wpdb;
315 +
316 + $where = gamipress_get_earnings_where( $query );
317 +
318 + // Merge all wheres
319 + $where = implode( ' AND ', $where );
320 +
321 + $user_earnings = GamiPress()->db->user_earnings;
322 +
323 + return $wpdb->get_var( "SELECT ue.date FROM {$user_earnings} AS ue WHERE {$where} ORDER BY ue.date DESC LIMIT 1" );
324 +
325 +}
326 +
327 +/**
328 + * Get the last earning datetime
329 + *
330 + * @since 1.8.7
331 + *
332 + * @param array $query User earning query parameters
333 + *
334 + * @return int The last earning datetime
335 + */
336 +function gamipress_get_last_earning_datetime( $query = array() ) {
337 +
338 + $date = gamipress_get_last_earning_date( $query );
339 +
340 + return ! empty( $date ) ? strtotime( $date ) : 0;
341 +
342 +}
343 +
344 +/**
345 + * Setup a common where conditions for the user earnings queries
346 + *
347 + * @since 1.8.7
348 + *
349 + * @param array $query User earning query parameters
350 + *
351 + * @return array Array of where clauses
352 + */
353 +function gamipress_get_earnings_where( $query = array() ) {
354 +
355 + // Post data
356 + $query = wp_parse_args( $query, array(
357 + 'user_id' => 0,
358 + 'post_id' => 0,
359 + 'achievement_id' => 0,
360 + 'post_type' => '',
361 + 'points_type' => '',
362 + 'since' => 0,
363 + ) );
364 +
365 + // Parse mapped keys
366 + if( $query['achievement_id'] !== 0 && $query['post_id'] === 0 ) {
367 + $query['post_id'] = $query['achievement_id'];
368 + }
369 +
370 + $where = array(
371 + '1 = 1'
372 + );
373 +
374 + // User ID
375 + if( is_array( $query['user_id'] ) ) {
376 + $where[] = 'ue.user_id IN ( ' . implode( ', ', $query['user_id'] ) . ' )';
377 + } else if ( absint( $query['user_id'] ) !== 0 ) {
378 + $where[] = 'ue.user_id = ' . absint( $query['user_id'] );
379 + }
380 +
381 + // Post ID
382 + if( is_array( $query['post_id'] ) ) {
383 + $where[] = 'ue.post_id IN ( ' . implode( ', ', $query['post_id'] ) . ' )';
384 + } else if ( absint( $query['post_id'] ) !== 0 ) {
385 + $where[] = 'ue.post_id = ' . absint( $query['post_id'] );
386 + }
387 +
388 + // Post type
389 + if( is_array( $query['post_type'] ) ) {
390 + $where[] = 'ue.post_type IN ( "' . implode( '", "', $query['post_type'] ) . '" )';
391 + } else if ( ! empty( $query['post_type'] ) ) {
392 + $where[] = 'ue.post_type = "' . $query['post_type'] . '"';
393 + }
394 +
395 + // Points type
396 + if( is_array( $query['points_type'] ) ) {
397 + $where[] = 'ue.points_type IN ( "' . implode( '", "', $query['points_type'] ) . '" )';
398 + } else if ( ! empty( $query['points_type'] ) ) {
399 + $where[] = 'ue.points_type = "' . $query['points_type'] . '"';
400 + }
401 +
402 + // Points
403 + if( isset( $query['points'] ) ) {
404 +
405 + $compare = '=';
406 +
407 + if( isset( $query['points_compare'] ) ) {
408 + $allowed_operators = array( '=', '<', '>', '<=', '>=' );
409 + if( in_array( $query['points_compare'], $allowed_operators ) ) {
410 + $compare = $query['points_compare'];
411 + }
412 + }
413 +
414 + $where[] = 'ue.points ' . $compare . ' ' . $query['points'];
415 + }
416 +
417 + // Since
418 + if( ! empty( $query['since'] ) ) {
419 +
420 + $since = $query['since'];
421 +
422 + // Turn a string date into time
423 + if( gettype( $query['since'] ) === 'string' ) {
424 + $since = strtotime( $query['since'] );
425 + }
426 +
427 + if( $since > 0 ) {
428 + $since = date( 'Y-m-d H:i:s', $since );
429 + $where[] = " ue.date > '{$since}'";
430 + }
431 + }
432 +
433 + // Before
434 + if( isset( $query['before'] ) ) {
435 +
436 + $before = $query['before'];
437 +
438 + // Turn a string date into time
439 + if( gettype( $query['before'] ) === 'string' ) {
440 + $before = strtotime( $query['before'] );
441 + }
442 +
443 + if( $before > 0 ) {
444 + $before = date( 'Y-m-d H:i:s', $before );
445 + $where[] = " ue.date < '{$before}'";
446 + }
447 +
448 + }
449 +
450 + // After
451 + if( isset( $query['after'] ) ) {
452 +
453 + $after = $query['after'];
454 +
455 + // Turn a string date into time
456 + if( gettype( $query['after'] ) === 'string' ) {
457 + $after = strtotime( $query['after'] );
458 + }
459 +
460 + if( $after > 0 ) {
461 + $after = date( 'Y-m-d H:i:s', $after );
462 + $where[] = " ue.date > '{$after}'";
463 + }
464 +
465 + }
466 +
467 + return $where;
468 +
469 +}
470 +
471 +/**
472 + * Get the user earning object data
473 + *
474 + * @since 2.1.2
475 + *
476 + * @param int $user_earning_id The user earning ID
477 + * @param string $meta_key The meta key to retrieve. By default, returns
478 + * data for all keys. Default empty.
479 + * @param bool $single Optional. Whether to return a single value. Default false.
480 + *
481 + * @return mixed Will be an array if $single is false. Will be value of meta data field if $single is true.
482 + */
483 +function gamipress_get_user_earning_meta( $user_earning_id, $meta_key = '', $single = false ) {
484 +
485 + ct_setup_table( 'gamipress_user_earnings' );
486 +
487 + $meta_value = ct_get_object_meta( $user_earning_id, $meta_key, $single );
488 +
489 + ct_reset_setup_table();
490 +
491 + return $meta_value;
492 +
493 +}
494 +
495 +/**
496 + * Update the user earning object data
497 + *
498 + * @since 2.1.2
499 + *
500 + * @param int $user_earning_id The user earning ID
501 + * @param string $meta_key Metadata key.
502 + * @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
503 + * @param mixed $prev_value Optional. Previous value to check before removing.
504 + * Default empty.
505 + *
506 + * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure.
507 + */
508 +function gamipress_update_user_earning_meta( $user_earning_id, $meta_key, $meta_value, $prev_value = '' ) {
509 +
510 + ct_setup_table( 'gamipress_user_earnings' );
511 +
512 + $meta_id = ct_update_object_meta( $user_earning_id, $meta_key, $meta_value, $prev_value );
513 +
514 + ct_reset_setup_table();
515 +
516 + return $meta_id;
517 +
518 +}