PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 7.9.9
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v7.9.9
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
gamipress / includes / credentials.php

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

414 lines 13.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Credentials
4 *
5 * @package GamiPress\Credentials
6 * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]>
7 * @since 7.9.8
8 */
9 // Exit if accessed directly
10 if( !defined( 'ABSPATH' ) ) exit;
11
12 /**
13 * Encode credential ID
14 *
15 * @since 7.9.8
16 *
17 * @param int $id
18 *
19 * @return string
20 */
21 function gamipress_encode_credential_id( $id = 0 ) {
22
23 $number = (int) $id ^ (int) GAMIPRESS_CRED_KEY;
24
25 if ( $number === 0 ) return '0';
26
27 $result = '';
28 while ($number > 0) {
29 $result = GAMIPRESS_CRED_SALT[$number % 62] . $result;
30 $number = intdiv( $number, 62 );
31 }
32
33 return $result;
34 }
35
36 /**
37 * Decode credential ID
38 *
39 * @since 7.9.8
40 *
41 * @param string $encoded
42 *
43 * @return int
44 */
45 function gamipress_decode_credential_id( $encoded = '' ) {
46
47 $number = 0;
48
49 for ( $i = 0; $i < strlen( $encoded ); $i++ )
50 $number = $number * 62 + strpos( GAMIPRESS_CRED_SALT, $encoded[$i] );
51
52 return (int) $number ^ (int) GAMIPRESS_CRED_KEY;
53 }
54
55 /**
56 * Get the credential from the URL.
57 *
58 * @since 7.9.8
59 *
60 * @return bool|stdClass False or Queried user earning
61 */
62 function gamipress_get_the_credential() {
63 global $gamipress_current_credential;
64
65 $credential_id = isset( $_GET[GAMIPRESS_CRED_VAR] ) ? $_GET[GAMIPRESS_CRED_VAR] : '';
66 $credential_id = gamipress_decode_credential_id( $credential_id );
67
68 if( $credential_id === 0 ) return false;
69
70 if( $gamipress_current_credential ) {
71 // Global credential already setup
72 return $gamipress_current_credential;
73 }
74
75 // Query the credential
76 ct_setup_table( 'gamipress_user_earnings' );
77 $user_earning = ct_get_object( $credential_id );
78 ct_reset_setup_table();
79
80 if( ! $user_earning ) return false;
81
82 // Bail if credential post does not match with the current post
83 if( absint( $user_earning->post_id ) !== absint( get_the_ID() ) ) return false;
84
85 $gamipress_current_credential = $user_earning;
86
87 return $user_earning;
88 }
89
90
91 /**
92 * Build a credential URL.
93 *
94 * @since 7.9.8
95 *
96 * @param int $post_id Achievement or Rank ID, also accepts a User Earning object.
97 * @param int $user_id User ID.
98 *
99 * @return string Credential URL or post permalink.
100 */
101 function gamipress_get_credential_url( $post_id = 0, $user_id = 0 ) {
102
103 $user_earning = null;
104
105 // Check if function called as gamipress_get_credential_url( $user_earning )
106 if( gettype( $post_id ) === 'object' && property_exists( $post_id, 'user_earning_id' ) ) {
107 $user_earning = $post_id;
108 }
109
110 if( $user_earning === null ) {
111 $user_earning = gamipress_get_last_earning( array( 'post_id' => $post_id, 'user_id' => $user_id ) );
112 } else {
113 $post_id = $user_earning->post_id;
114 $user_id = $user_earning->user_id;
115 }
116
117 $url = get_the_permalink( $post_id );
118
119 if( $user_earning ) {
120 $value = gamipress_encode_credential_id( absint( $user_earning->user_earning_id ) );
121
122 $url = add_query_arg( array( GAMIPRESS_CRED_VAR => $value ), $url );
123 }
124
125 /**
126 * Available filter to override the credential URL
127 *
128 * @since 7.9.8
129 *
130 * @param string $html The credential URL
131 * @param int $post_id Achievement or Rank ID
132 * @param int $user_id User ID
133 * @param stdClass $user_earning User earning (the credential)
134 *
135 * @return string Credential URL
136 */
137 return apply_filters( 'gamipress_credential_url', $url, $post_id, $user_id, $user_earning );
138
139 }
140
141 /**
142 * Credential link
143 *
144 * @since 7.9.8
145 *
146 * @param int $post_id Achievement or Rank ID.
147 * @param int $user_id User ID.
148 *
149 * @return string HTML Markup.
150 */
151 function gamipress_get_credential_link( $post_id = 0, $user_id = 0 ) {
152
153 $user_earning = null;
154
155 // Check if function called as gamipress_get_credential_link( $user_earning )
156 if( gettype( $post_id ) === 'object' && property_exists( $post_id, 'user_earning_id' ) ) {
157 $user_earning = $post_id;
158 }
159
160 if( $user_earning === null ) {
161 $user_earning = gamipress_get_last_earning( array( 'post_id' => $post_id, 'user_id' => $user_id ) );
162 } else {
163 $post_id = $user_earning->post_id;
164 $user_id = $user_earning->user_id;
165 }
166
167 $url = gamipress_get_credential_url( $user_earning );
168
169 /**
170 * Available filter to override the credential link label
171 *
172 * @since 7.9.8
173 *
174 * @param string $label The credential link label
175 * @param int $post_id Achievement or Rank ID
176 * @param int $user_id User ID
177 * @param stdClass $user_earning User earning (the credential)
178 *
179 * @return string The credential link label
180 */
181 $label = apply_filters( 'gamipress_credential_link_label', __( 'View Credential', 'gamipress' ), $post_id, $user_id, $user_earning );
182
183 $html = '<a href="' . esc_attr( $url ) . '" class="gamipress-credential-link">'
184 . $label
185 . '</a>';
186
187 /**
188 * Available filter to override the credential link HTML
189 *
190 * @since 7.9.8
191 *
192 * @param string $html The credential link HTML
193 * @param int $post_id Achievement or Rank ID
194 * @param int $user_id User ID
195 * @param stdClass $user_earning User earning (the credential)
196 *
197 * @return string HTML Markup
198 */
199 return apply_filters( 'gamipress_credential_link', $html, $post_id, $user_id, $user_earning );
200
201 }
202
203 /**
204 * Credentials links HTML (for single templates)
205 *
206 * @since 7.9.8
207 *
208 * @param int $post_id Achievement or Rank ID.
209 * @param array $args Template args
210 *
211 * @return string HTML Markup.
212 */
213 function gamipress_get_credentials_links_html( $post_id = 0, $args = array() ) {
214
215 $credential = gamipress_get_the_credential();
216 $user_id = get_current_user_id();
217
218 if( ! isset( $args['user_id'] ) ) {
219 $args['user_id'] = get_current_user_id();
220 }
221 $viewing_user_id = absint( $args['user_id'] );
222
223 $links = '';
224
225 if( ! $credential ) {
226 // Links when not viewing a credential
227 if( $user_id === $viewing_user_id ) {
228 // Only render credential link if user is viewing its own achievement/rank
229 $links .= gamipress_get_credential_link( $post_id, $user_id );
230 }
231
232 /**
233 * Available filter to add credentials links when viewing a post (achievement or rank)
234 *
235 * @since 7.9.8
236 *
237 * @param string $html The credential link HTML
238 * @param int $post_id Achievement or Rank ID
239 * @param int $user_id User ID
240 * @param array $args Template args
241 * @param stdClass $credential User earning (the credential) if viewing a credential
242 *
243 * @return string HTML Markup
244 */
245 $links = apply_filters( 'gamipress_credentials_links_on_post', $links, $post_id, $user_id, $args, $credential );
246
247 } else {
248
249 /**
250 * Available filter to add credentials links when viewing a credential
251 *
252 * @since 7.9.8
253 *
254 * @param string $html The credential link HTML
255 * @param int $post_id Achievement or Rank ID
256 * @param int $user_id User ID
257 * @param array $args Template args
258 * @param stdClass $credential User earning (the credential) if viewing a credential
259 *
260 * @return string HTML Markup
261 */
262 $links = apply_filters( 'gamipress_credentials_links_on_credential', $links, $post_id, $user_id, $args, $credential );
263
264 }
265
266 /**
267 * Available filter to add credentials links HTML
268 *
269 * @since 7.9.8
270 *
271 * @param string $html The credential link HTML
272 * @param int $post_id Achievement or Rank ID
273 * @param int $user_id User ID
274 * @param array $args Template args
275 * @param stdClass $credential User earning (the credential) if viewing a credential
276 *
277 * @return string HTML Markup
278 */
279 $links = apply_filters( 'gamipress_credentials_links', $links, $post_id, $user_id, $args, $credential );
280
281 return '<div class="gamipress-credentials-links">' . $links . '</div>';
282
283 }
284
285 /**
286 * Credential text used on single templates
287 *
288 * @since 7.9.8
289 *
290 * @param int $post_id Achievement or Rank ID.
291 * @param int $user_id User ID.
292 *
293 * @return string HTML Markup.
294 */
295 function gamipress_get_credential_text( $post_id = 0, $user_id = 0 ) {
296
297 $html = '';
298 $user_earning = null;
299
300 // Check if function called as gamipress_get_credential_text( $user_earning )
301 if( gettype( $post_id ) === 'object' && property_exists( $post_id, 'user_earning_id' ) ) {
302 $user_earning = $post_id;
303 }
304
305 if( $user_earning === null ) {
306 $user_earning = gamipress_get_last_earning( array( 'post_id' => $post_id, 'user_id' => $user_id ) );
307 } else {
308 $post_id = $user_earning->post_id;
309 $user_id = $user_earning->user_id;
310 }
311
312 $user = get_user_by( 'id', $user_id );
313
314 if ( $user ) {
315
316 // User display
317 $user_url = get_author_posts_url( $user->ID );
318
319 $user_display = '<a href="' . esc_attr( $user_url ) . '">'
320 . get_avatar( $user->ID )
321 . esc_html( $user->display_name )
322 . '</a>';
323
324 /**
325 * Available filter to override the credential text user display
326 *
327 * @since 7.9.8
328 *
329 * @param string $user_display The user display.
330 * @param int $post_id Achievement or Rank ID.
331 * @param int $user_id User ID.
332 * @param stdClass $user_earning User earning (the credential)
333 *
334 * @return string
335 */
336 $user_display = apply_filters( 'gamipress_credential_text_user_display', $user_display, $post_id, $user_id, $user_earning );
337
338 // Type display
339 $post_type = gamipress_get_post_type( $post_id );
340 if( gamipress_is_achievement( $post_type ) ) {
341 $type_display = gamipress_get_achievement_type_singular( $post_type, true );
342 } else {
343 $type_display = gamipress_get_rank_type_singular( $post_type, true );
344 }
345
346 /**
347 * Available filter to override the credential text type display
348 *
349 * @since 7.9.8
350 *
351 * @param string $type_display The type display.
352 * @param int $post_id Achievement or Rank ID.
353 * @param int $user_id User ID.
354 * @param stdClass $user_earning User earning (the credential)
355 *
356 * @return string
357 */
358 $type_display = apply_filters( 'gamipress_credential_text_type_display', $type_display, $post_id, $user_id, $user_earning );
359
360 // Date display
361 $date_display = '<strong>' . date_i18n( get_option( 'date_format' ), strtotime( $user_earning->date ) ) . '</strong>';
362
363 /**
364 * Available filter to override the credential text date display
365 *
366 * @since 7.9.8
367 *
368 * @param string $date_display The date display.
369 * @param int $post_id Achievement or Rank ID.
370 * @param int $user_id User ID.
371 * @param stdClass $user_earning User earning (the credential)
372 *
373 * @return string
374 */
375 $date_display = apply_filters( 'gamipress_credential_text_date_display', $date_display, $post_id, $user_id, $user_earning );
376
377 // translators: %1$s: User display name %2$s: Achievement or rank type (Badge, Quest) %3$s: Earned date
378 $credential_text = sprintf( __( '%1$s earned this %2$s on %3$s', 'gamipress' ),
379 $user_display,
380 $type_display,
381 $date_display
382 );
383
384 /**
385 * Available filter to override the credential text
386 *
387 * @since 7.9.8
388 *
389 * @param string $text The credential text.
390 * @param int $post_id Achievement or Rank ID.
391 * @param int $user_id User ID.
392 * @param stdClass $user_earning User earning (the credential)
393 *
394 * @return string
395 */
396 $credential_text = apply_filters( 'gamipress_credential_text', $credential_text, $post_id, $user_id, $user_earning );
397
398 $html .= '<div class="gamipress-credential-text"><p>' . $credential_text . '</p></div>';
399 }
400
401 /**
402 * Available filter to override the credential text HTML
403 *
404 * @since 7.9.8
405 *
406 * @param string $html The credential text HTML markup.
407 * @param int $post_id Achievement or Rank ID.
408 * @param int $user_id User ID.
409 * @param stdClass $user_earning User earning (the credential)
410 *
411 * @return string HTML Markup.
412 */
413 return apply_filters( 'gamipress_credential_text_html', $html, $post_id, $user_id, $user_earning );
414 }