PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 8.0.1
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v8.0.1
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 7.8.6 7.8.5 All 44 releases
gamipress / includes / credentials.php

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

419 lines 13.7 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 <contact@gamipress.com>, Ruben Garcia <rubengcdev@gmail.com>
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
228 $user_earning = gamipress_get_last_earning( array( 'post_id' => $post_id, 'user_id' => $user_id ) );
229
230 // Only render credential link if user is viewing its own achievement/rank
231 if( $user_id === $viewing_user_id ) {
232 // Show credential link if user has earned it
233 if( $user_earning )
234 $links .= gamipress_get_credential_link( $user_earning );
235 }
236
237 /**
238 * Available filter to add credentials links when viewing a post (achievement or rank)
239 *
240 * @since 7.9.8
241 *
242 * @param string $html The credential link HTML
243 * @param int $post_id Achievement or Rank ID
244 * @param int $user_id User ID
245 * @param array $args Template args
246 * @param stdClass $credential User earning (the credential) if viewing a credential
247 *
248 * @return string HTML Markup
249 */
250 $links = apply_filters( 'gamipress_credentials_links_on_post', $links, $post_id, $user_id, $args, $credential );
251
252 } else {
253
254 /**
255 * Available filter to add credentials links when viewing a credential
256 *
257 * @since 7.9.8
258 *
259 * @param string $html The credential link HTML
260 * @param int $post_id Achievement or Rank ID
261 * @param int $user_id User ID
262 * @param array $args Template args
263 * @param stdClass $credential User earning (the credential) if viewing a credential
264 *
265 * @return string HTML Markup
266 */
267 $links = apply_filters( 'gamipress_credentials_links_on_credential', $links, $post_id, $user_id, $args, $credential );
268
269 }
270
271 /**
272 * Available filter to add credentials links HTML
273 *
274 * @since 7.9.8
275 *
276 * @param string $html The credential link HTML
277 * @param int $post_id Achievement or Rank ID
278 * @param int $user_id User ID
279 * @param array $args Template args
280 * @param stdClass $credential User earning (the credential) if viewing a credential
281 *
282 * @return string HTML Markup
283 */
284 $links = apply_filters( 'gamipress_credentials_links', $links, $post_id, $user_id, $args, $credential );
285
286 return '<div class="gamipress-credentials-links">' . $links . '</div>';
287
288 }
289
290 /**
291 * Credential text used on single templates
292 *
293 * @since 7.9.8
294 *
295 * @param int $post_id Achievement or Rank ID.
296 * @param int $user_id User ID.
297 *
298 * @return string HTML Markup.
299 */
300 function gamipress_get_credential_text( $post_id = 0, $user_id = 0 ) {
301
302 $html = '';
303 $user_earning = null;
304
305 // Check if function called as gamipress_get_credential_text( $user_earning )
306 if( gettype( $post_id ) === 'object' && property_exists( $post_id, 'user_earning_id' ) ) {
307 $user_earning = $post_id;
308 }
309
310 if( $user_earning === null ) {
311 $user_earning = gamipress_get_last_earning( array( 'post_id' => $post_id, 'user_id' => $user_id ) );
312 } else {
313 $post_id = $user_earning->post_id;
314 $user_id = $user_earning->user_id;
315 }
316
317 $user = get_user_by( 'id', $user_id );
318
319 if ( $user ) {
320
321 // User display
322 $user_url = get_author_posts_url( $user->ID );
323
324 $user_display = '<a href="' . esc_attr( $user_url ) . '">'
325 . get_avatar( $user->ID )
326 . esc_html( $user->display_name )
327 . '</a>';
328
329 /**
330 * Available filter to override the credential text user display
331 *
332 * @since 7.9.8
333 *
334 * @param string $user_display The user display.
335 * @param int $post_id Achievement or Rank ID.
336 * @param int $user_id User ID.
337 * @param stdClass $user_earning User earning (the credential)
338 *
339 * @return string
340 */
341 $user_display = apply_filters( 'gamipress_credential_text_user_display', $user_display, $post_id, $user_id, $user_earning );
342
343 // Type display
344 $post_type = gamipress_get_post_type( $post_id );
345 if( gamipress_is_achievement( $post_type ) ) {
346 $type_display = gamipress_get_achievement_type_singular( $post_type, true );
347 } else {
348 $type_display = gamipress_get_rank_type_singular( $post_type, true );
349 }
350
351 /**
352 * Available filter to override the credential text type display
353 *
354 * @since 7.9.8
355 *
356 * @param string $type_display The type display.
357 * @param int $post_id Achievement or Rank ID.
358 * @param int $user_id User ID.
359 * @param stdClass $user_earning User earning (the credential)
360 *
361 * @return string
362 */
363 $type_display = apply_filters( 'gamipress_credential_text_type_display', $type_display, $post_id, $user_id, $user_earning );
364
365 // Date display
366 $date_display = '<strong>' . date_i18n( get_option( 'date_format' ), strtotime( $user_earning->date ) ) . '</strong>';
367
368 /**
369 * Available filter to override the credential text date display
370 *
371 * @since 7.9.8
372 *
373 * @param string $date_display The date display.
374 * @param int $post_id Achievement or Rank ID.
375 * @param int $user_id User ID.
376 * @param stdClass $user_earning User earning (the credential)
377 *
378 * @return string
379 */
380 $date_display = apply_filters( 'gamipress_credential_text_date_display', $date_display, $post_id, $user_id, $user_earning );
381
382 // translators: %1$s: User display name %2$s: Achievement or rank type (Badge, Quest) %3$s: Earned date
383 $credential_text = sprintf( __( '%1$s earned this %2$s on %3$s', 'gamipress' ),
384 $user_display,
385 $type_display,
386 $date_display
387 );
388
389 /**
390 * Available filter to override the credential text
391 *
392 * @since 7.9.8
393 *
394 * @param string $text The credential text.
395 * @param int $post_id Achievement or Rank ID.
396 * @param int $user_id User ID.
397 * @param stdClass $user_earning User earning (the credential)
398 *
399 * @return string
400 */
401 $credential_text = apply_filters( 'gamipress_credential_text', $credential_text, $post_id, $user_id, $user_earning );
402
403 $html .= '<div class="gamipress-credential-text"><p>' . $credential_text . '</p></div>';
404 }
405
406 /**
407 * Available filter to override the credential text HTML
408 *
409 * @since 7.9.8
410 *
411 * @param string $html The credential text HTML markup.
412 * @param int $post_id Achievement or Rank ID.
413 * @param int $user_id User ID.
414 * @param stdClass $user_earning User earning (the credential)
415 *
416 * @return string HTML Markup.
417 */
418 return apply_filters( 'gamipress_credential_text_html', $html, $post_id, $user_id, $user_earning );
419 }