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 / users.php

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

281 lines 8.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Users Functions
4 *
5 * @package GamiPress\Users_Functions
6 * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]>
7 * @since 1.0.0
8 */
9 // Exit if accessed directly
10 if( !defined( 'ABSPATH' ) ) exit;
11
12 /**
13 * Register user's meta
14 *
15 * @since 1.8.0
16 */
17 function gamipress_register_users_metas() {
18
19 // Points types
20 foreach( gamipress_get_points_types() as $points_type => $data ) {
21 $meta_args = array(
22 'type' => 'integer',
23 'description' => sprintf( __( '%s balance', 'gamipress' ), $data['plural_name'] ),
24 'single' => true,
25 'show_in_rest' => true,
26 'sanitize_callback' => 'absint',
27 'auth_callback' => '__return_true'
28 );
29
30 /**
31 * Filters the points type meta arguments
32 *
33 * @since 1.8.0
34 *
35 * @param array $meta_args Meta arguments that will be passed to register_meta() function
36 * @param string $points_type Points type's slug
37 * @param array $data Points type's data
38 */
39 apply_filters( "gamipress_register_{$points_type}_meta_args", $meta_args, $points_type, $data );
40
41 register_meta( 'user', "_gamipress_{$points_type}_points", $meta_args );
42 }
43
44 // Rank types
45 foreach( gamipress_get_rank_types() as $rank_type => $data ) {
46 $meta_args = array(
47 'type' => 'integer',
48 'description' => sprintf( __( 'Current %s', 'gamipress' ), $data['singular_name'] ),
49 'single' => true,
50 'show_in_rest' => true,
51 'sanitize_callback' => 'absint',
52 'auth_callback' => '__return_true'
53 );
54
55 /**
56 * Filters the rank type meta arguments
57 *
58 * @since 1.8.0
59 *
60 * @param array $meta_args Meta arguments that will be passed to register_meta() function
61 * @param string $rank_type Rank type's slug
62 * @param array $data Rank type's data
63 */
64 apply_filters( "gamipress_register_{$rank_type}_meta_args", $meta_args, $rank_type, $data );
65
66 register_meta( 'user', "_gamipress_{$rank_type}_rank", $meta_args );
67 }
68
69 }
70 add_action( 'init', 'gamipress_register_users_metas' );
71
72 /**
73 * Remove all user logs and earnings when is deleted from the database.
74 *
75 * @since 1.8.0
76 *
77 * @param int $id ID of the user to delete.
78 * @param int|null $reassign ID of the user to reassign posts and links to (Default null, for no reassignment).
79 */
80 function gamipress_on_delete_user( $id, $reassign ) {
81
82 global $wpdb;
83
84 $user_earnings = GamiPress()->db->user_earnings;
85 $user_earnings_meta = GamiPress()->db->user_earnings_meta;
86 $logs = GamiPress()->db->logs;
87 $logs_meta = GamiPress()->db->logs_meta;
88
89 // Delete all user's earnings
90 $wpdb->query( "DELETE ue FROM {$user_earnings} AS ue WHERE ue.user_id = {$id}" );
91 // Delete orphaned user earnings metas
92 $wpdb->query( "DELETE uem FROM {$user_earnings_meta} uem INNER JOIN {$user_earnings} ue ON ue.user_earning_id = uem.user_earning_id WHERE ue.user_earning_id IS NULL" );
93
94 // Delete all user's logs
95 $wpdb->query( "DELETE l FROM {$logs} AS l WHERE l.user_id = {$id}" );
96 // Delete orphaned logs metas
97 $wpdb->query( "DELETE lm FROM {$logs_meta} lm INNER JOIN {$logs} l ON l.log_id = lm.log_id WHERE l.log_id IS NULL" );
98
99 }
100 add_action( 'delete_user', 'gamipress_on_delete_user', 10, 2 );
101
102 /**
103 * Get user's achievements
104 *
105 * @since 1.0.0
106 * @updated 1.6.3 Return an empty array if not user provided or current user is not logged in
107 *
108 * @param array $args An array of all our relevant arguments
109 *
110 * @return array An array of all the achievement objects that matched our parameters, or empty if none
111 */
112 function gamipress_get_user_achievements( $args = array() ) {
113
114 // If not properly upgrade to required version fallback to compatibility function
115 if( ! is_gamipress_upgraded_to( '1.2.8' ) ) {
116 return gamipress_get_user_achievements_old( $args );
117 }
118
119 // Setup our default args
120 $defaults = array(
121 'user_id' => 0, // The given user's ID
122 'site_id' => get_current_blog_id(), // The given site's ID
123 'achievement_id' => false, // A specific achievement's post ID
124 'achievement_type' => false, // A specific achievement type
125 'since' => 0, // A specific timestamp to use in place of $limit_in_days
126 'limit' => -1, // Limit of achievements to return
127 'groupby' => false, // Group by clause, setting it to 'post_id' or 'achievement_id' will prevent duplicated achievements
128 );
129
130 $args = wp_parse_args( $args, $defaults );
131
132 // Use current user's ID if none specified
133 if ( ! $args['user_id'] ) {
134 $args['user_id'] = get_current_user_id();
135 }
136
137 // Bail if not user provided or current user is not logged in
138 if( absint( $args['user_id'] ) === 0 ) {
139 return array();
140 }
141
142 // Setup CT object
143 $ct_table = ct_setup_table( 'gamipress_user_earnings' );
144
145 // Bail if table not yet created
146 if( ! is_object( $ct_table ) ) {
147 return array();
148 }
149
150 // Setup query args
151 $query_args = array(
152 'user_id' => $args['user_id'],
153 'items_per_page' => $args['limit'],
154 );
155
156 if( $args['limit'] === -1 ) {
157 $query_args['nopaging'] = true;
158 }
159
160 if( $args['achievement_id'] !== false ) {
161 $query_args['post_id'] = $args['achievement_id'];
162 }
163
164 if( $args['achievement_type'] !== false ) {
165 $query_args['post_type'] = $args['achievement_type'];
166 }
167
168 if( $args['groupby'] !== false ) {
169 $query_args['groupby'] = $args['groupby'];
170
171 // achievement_id is allowed
172 if( $args['groupby'] === 'achievement_id' ) {
173 $query_args['groupby'] = 'post_id';
174 }
175 }
176
177 if( $args['since'] !== 0 ) {
178 $query_args['since'] = $args['since'];
179 }
180
181 $ct_query = new CT_Query( $query_args );
182
183 $results = $ct_query->get_results();
184
185 $achievements = array();
186
187 foreach ( $results as $key => $achievement ) {
188
189 // Unset if achievement is NULL
190 if( is_null( $achievement ) ) {
191 unset( $results[$key] );
192 continue;
193 }
194
195 // Update object for backward compatibility for usages previously to 1.2.7
196 $achievement->ID = $achievement->post_id;
197 $achievement->date_earned = strtotime( $achievement->date );
198
199 // If achievements earned will be displayed, then need to pass some filters
200 if( isset( $args['display'] ) && $args['display'] ) {
201
202 // If not existent achievements
203 if( ! gamipress_post_exists( $achievement->post_id ) ) {
204 continue;
205 }
206
207 // If not published achievements
208 if( gamipress_get_post_field( 'post_status', $achievement->post_id ) !== 'publish' ) {
209 continue;
210 }
211
212 // If hidden achievements on display context
213 if( gamipress_is_achievement_hidden( $achievement->post_id ) ) {
214 continue;
215 }
216
217 }
218
219 $achievements[$key] = $achievement;
220
221 }
222
223 ct_reset_setup_table();
224
225 return $achievements;
226
227 }
228
229 /**
230 * Updates the user's earned achievements
231 *
232 * @since 1.0.0
233 *
234 * @param array $args An array containing all our relevant arguments
235 *
236 * @return bool The updated umeta ID on success, false on failure
237 */
238 function gamipress_update_user_achievements( $args = array() ) {
239
240 // If not properly upgrade to required version fallback to compatibility function
241 if( ! is_gamipress_upgraded_to( '1.2.8' ) ) {
242 return gamipress_update_user_achievements_old( $args );
243 }
244
245 // Setup our default args
246 $defaults = array(
247 'user_id' => 0, // The given user's ID
248 'site_id' => get_current_blog_id(), // The given site's ID
249 'new_achievements' => false, // An array of NEW achievements earned by the user
250 );
251
252 $args = wp_parse_args( $args, $defaults );
253
254 // Use current user's ID if none specified
255 if ( ! $args['user_id'] )
256 $args['user_id'] = get_current_user_id();
257
258 // Lets to append the new achievements array
259 if ( is_array( $args['new_achievements'] ) && ! empty( $args['new_achievements'] ) ) {
260
261 foreach( $args['new_achievements'] as $new_achievement ) {
262
263 $user_earning_data = array(
264 'title' => gamipress_get_post_field( 'post_title', $new_achievement->ID ),
265 'post_id' => $new_achievement->ID,
266 'post_type' => $new_achievement->post_type,
267 'points' => absint( $new_achievement->points ),
268 'points_type' => $new_achievement->points_type,
269 'date' => date( 'Y-m-d H:i:s', $new_achievement->date_earned )
270 );
271
272 gamipress_insert_user_earning( absint( $args['user_id'] ), $user_earning_data );
273
274 }
275
276 }
277
278 return true;
279
280 }
281