PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 8.0.6
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v8.0.6
8.0.6 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 All 48 releases
gamipress / includes / users.php

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

286 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' => 'gamipress_user_metas_auth_callback'
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' => 'gamipress_user_metas_auth_callback'
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 * User's meta auth callback
74 *
75 * @since 8.0.6
76 *
77 * @param bool $allowed
78 * @param string $meta_key
79 * @param int $user_id
80 *
81 * @return bool
82 */
83 function gamipress_user_metas_auth_callback( $allowed, $meta_key, $user_id ) {
84 return current_user_can( gamipress_get_manager_capability() );
85 }
86
87 /**
88 * Remove all user logs and earnings when is deleted from the database.
89 *
90 * @since 1.8.0
91 *
92 * @param int $id ID of the user to delete.
93 * @param int|null $reassign ID of the user to reassign posts and links to (Default null, for no reassignment).
94 */
95 function gamipress_on_delete_user( $id, $reassign ) {
96
97 global $wpdb;
98
99 $user_earnings = GamiPress()->db->user_earnings;
100 $user_earnings_meta = GamiPress()->db->user_earnings_meta;
101 $logs = GamiPress()->db->logs;
102 $logs_meta = GamiPress()->db->logs_meta;
103
104 // Delete all user's earnings
105 $wpdb->query( "DELETE ue FROM {$user_earnings} AS ue WHERE ue.user_id = {$id}" );
106 // Delete orphaned user earnings metas
107 $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" );
108
109 // Delete all user's logs
110 $wpdb->query( "DELETE l FROM {$logs} AS l WHERE l.user_id = {$id}" );
111 // Delete orphaned logs metas
112 $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" );
113
114 }
115 add_action( 'delete_user', 'gamipress_on_delete_user', 10, 2 );
116
117 /**
118 * Get user's achievements
119 *
120 * @since 1.0.0
121 * @updated 1.6.3 Return an empty array if not user provided or current user is not logged in
122 *
123 * @param array $args An array of all our relevant arguments
124 *
125 * @return array An array of all the achievement objects that matched our parameters, or empty if none
126 */
127 function gamipress_get_user_achievements( $args = array() ) {
128
129 // Setup our default args
130 $defaults = array(
131 'user_id' => 0, // The given user's ID
132 'site_id' => get_current_blog_id(), // The given site's ID
133 'achievement_id' => false, // A specific achievement's post ID
134 'achievement_type' => false, // A specific achievement type
135 'since' => 0, // A specific timestamp to use in place of $limit_in_days
136 'limit' => -1, // Limit of achievements to return
137 'groupby' => false, // Group by clause, setting it to 'post_id' or 'achievement_id' will prevent duplicated achievements
138 );
139
140 $args = wp_parse_args( $args, $defaults );
141
142 // Use current user's ID if none specified
143 if ( ! $args['user_id'] ) {
144 $args['user_id'] = get_current_user_id();
145 }
146
147 // Bail if not user provided or current user is not logged in
148 if( absint( $args['user_id'] ) === 0 ) {
149 return array();
150 }
151
152 // Setup CT object
153 $ct_table = ct_setup_table( 'gamipress_user_earnings' );
154
155 // Bail if table not yet created
156 if( ! is_object( $ct_table ) ) {
157 return array();
158 }
159
160 // Setup query args
161 $query_args = array(
162 'user_id' => $args['user_id'],
163 'items_per_page' => $args['limit'],
164 );
165
166 if( $args['limit'] === -1 ) {
167 $query_args['nopaging'] = true;
168 }
169
170 if( $args['achievement_id'] !== false ) {
171 $query_args['post_id'] = $args['achievement_id'];
172 }
173
174 if( $args['achievement_type'] !== false ) {
175 $query_args['post_type'] = $args['achievement_type'];
176 }
177
178 if( $args['groupby'] !== false ) {
179 $query_args['groupby'] = $args['groupby'];
180
181 // achievement_id is allowed
182 if( $args['groupby'] === 'achievement_id' ) {
183 $query_args['groupby'] = 'post_id';
184 }
185 }
186
187 if( $args['since'] !== 0 ) {
188 $query_args['since'] = $args['since'];
189 }
190
191 $ct_query = new CT_Query( $query_args );
192
193 $results = $ct_query->get_results();
194
195 $achievements = array();
196
197 foreach ( $results as $key => $achievement ) {
198
199 // Unset if achievement is NULL
200 if( is_null( $achievement ) ) {
201 unset( $results[$key] );
202 continue;
203 }
204
205 // Update object for backward compatibility for usages previously to 1.2.7
206 $achievement->ID = $achievement->post_id;
207 $achievement->date_earned = strtotime( $achievement->date );
208
209 // If achievements earned will be displayed, then need to pass some filters
210 if( isset( $args['display'] ) && $args['display'] ) {
211
212 // If not existent achievements
213 if( ! gamipress_post_exists( $achievement->post_id ) ) {
214 continue;
215 }
216
217 // If not published achievements
218 if( gamipress_get_post_field( 'post_status', $achievement->post_id ) !== 'publish' ) {
219 continue;
220 }
221
222 // If hidden achievements on display context
223 if( gamipress_is_achievement_hidden( $achievement->post_id ) ) {
224 continue;
225 }
226
227 }
228
229 $achievements[$key] = $achievement;
230
231 }
232
233 ct_reset_setup_table();
234
235 return $achievements;
236
237 }
238
239 /**
240 * Updates the user's earned achievements
241 *
242 * @since 1.0.0
243 *
244 * @param array $args An array containing all our relevant arguments
245 *
246 * @return bool The updated umeta ID on success, false on failure
247 */
248 function gamipress_update_user_achievements( $args = array() ) {
249
250 // Setup our default args
251 $defaults = array(
252 'user_id' => 0, // The given user's ID
253 'site_id' => get_current_blog_id(), // The given site's ID
254 'new_achievements' => false, // An array of NEW achievements earned by the user
255 );
256
257 $args = wp_parse_args( $args, $defaults );
258
259 // Use current user's ID if none specified
260 if ( ! $args['user_id'] )
261 $args['user_id'] = get_current_user_id();
262
263 // Lets to append the new achievements array
264 if ( is_array( $args['new_achievements'] ) && ! empty( $args['new_achievements'] ) ) {
265
266 foreach( $args['new_achievements'] as $new_achievement ) {
267
268 $user_earning_data = array(
269 'title' => gamipress_get_post_field( 'post_title', $new_achievement->ID ),
270 'post_id' => $new_achievement->ID,
271 'post_type' => $new_achievement->post_type,
272 'points' => absint( $new_achievement->points ),
273 'points_type' => $new_achievement->points_type,
274 'date' => date( 'Y-m-d H:i:s', $new_achievement->date_earned )
275 );
276
277 gamipress_insert_user_earning( absint( $args['user_id'] ), $user_earning_data );
278
279 }
280
281 }
282
283 return true;
284
285 }
286