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 / integrations / buddypress / includes / bp-activity.php

bp-activity.php in GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI 7.9.8, at integrations/buddypress/includes/bp-activity.php

500 lines 18.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * BuddyPress Activity
4 *
5 * @package GamiPress\BuddyPress\BuddyPress_Activity
6 * @since 1.0.1
7 */
8
9 // Exit if accessed directly
10 if( !defined( 'ABSPATH' ) ) exit;
11
12 /**
13 * Register custom meta boxes
14 *
15 * @since 1.0.1
16 * @updated 1.2.6
17 */
18 function gamipress_bp_activity_meta_boxes() {
19
20 // Start with an underscore to hide fields from custom fields list
21 $prefix = '_gamipress_bp_';
22
23 if ( ! gamipress_bp_is_active( 'activity' ) ) {
24 return;
25 }
26
27
28 // Points Type
29 gamipress_add_meta_box(
30 'bp-activity-points-type-data',
31 __( 'BuddyPress Member Activity', 'gamipress' ),
32 array( 'points-type' ),
33 array(
34 $prefix . 'create_points_award_activity' => array(
35 'name' => __( 'Awards activity entries', 'gamipress' ),
36 'tooltip' => __( 'Create an activity entry on user\'s profile when they get awarded by points award of this type.', 'gamipress' ),
37 'label_cb' => 'cmb_tooltip_label_cb',
38 'type' => 'checkbox',
39 'classes' => 'gamipress-switch'
40 ),
41 $prefix . 'create_points_deduct_activity' => array(
42 'name' => __( 'Deducts activity entries', 'gamipress' ),
43 'tooltip' => __( 'Create an activity entry on user\'s profile when they get deducted by a points deduct of this type.', 'gamipress' ),
44 'label_cb' => 'cmb_tooltip_label_cb',
45 'type' => 'checkbox',
46 'classes' => 'gamipress-switch'
47 ),
48 )
49 );
50
51 // Achievement Type
52 gamipress_add_meta_box(
53 'bp-activity-achievement-type-data',
54 __( 'BuddyPress Member Activity', 'gamipress' ),
55 array( 'achievement-type' ),
56 array(
57 $prefix . 'create_achievement_activity' => array(
58 'name' => __( 'Achievement activity entries', 'gamipress' ),
59 'tooltip' => __( 'Create an activity entry on user\'s profile when they earn an achievement of this type.', 'gamipress' ),
60 'label_cb' => 'cmb_tooltip_label_cb',
61 'type' => 'checkbox',
62 'classes' => 'gamipress-switch'
63 ),
64 $prefix . 'create_step_activity' => array(
65 'name' => __( 'Step activity entries', 'gamipress' ),
66 'tooltip' => __( 'Create an activity entry on user\'s profile when they complete a step of an achievement of this type.', 'gamipress' ),
67 'label_cb' => 'cmb_tooltip_label_cb',
68 'type' => 'checkbox',
69 'classes' => 'gamipress-switch'
70 ),
71 )
72 );
73
74 // Rank Type
75 gamipress_add_meta_box(
76 'bp-activity-rank-type-data',
77 __( 'BuddyPress Member Activity', 'gamipress' ),
78 array( 'rank-type' ),
79 array(
80 $prefix . 'create_rank_activity' => array(
81 'name' => __( 'Rank activity entries', 'gamipress' ),
82 'tooltip' => __( 'Create an activity entry on user\'s profile when they reach a new rank of this type.', 'gamipress' ),
83 'label_cb' => 'cmb_tooltip_label_cb',
84 'type' => 'checkbox',
85 'classes' => 'gamipress-switch'
86 ),
87 $prefix . 'create_rank_requirement_activity' => array(
88 'name' => __( 'Rank requirement activity entries', 'gamipress' ),
89 'tooltip' => __( 'Create an activity entry on user\'s profile when they complete a rank requirement of a rank of this type.', 'gamipress' ),
90 'label_cb' => 'cmb_tooltip_label_cb',
91 'type' => 'checkbox',
92 'classes' => 'gamipress-switch'
93 ),
94 )
95 );
96
97 }
98 add_action( 'cmb2_admin_init', 'gamipress_bp_activity_meta_boxes' );
99
100 /**
101 * Create BuddyPress Activity when a user earns an achievement.
102 *
103 * @since 1.0.1
104 * @updated 1.2.6 Make function more flexible, now logic is handled by 'gamipress_bp_activity_details' filter
105 *
106 * @param int $user_id
107 * @param int $post_id
108 * @param string $trigger_type
109 * @param int $site_id
110 * @param array $args
111 */
112 function gamipress_award_achievement_bp_activity( $user_id, $post_id, $trigger_type, $site_id, $args ) {
113
114 if ( ! gamipress_bp_is_active( 'activity' ) ) {
115 return;
116 }
117
118 $activity = array();
119
120 $activity = apply_filters( 'gamipress_bp_activity_details', $activity, $user_id, $post_id, $trigger_type, $site_id, $args );
121
122 // Insert the activity
123 if( ! empty( $activity ) )
124 bp_activity_add( $activity );
125
126 }
127 add_action( 'gamipress_award_achievement', 'gamipress_award_achievement_bp_activity', 10, 5 );
128
129 /**
130 * Create BuddyPress Activity when a user earns an achievement.
131 *
132 * @since 1.3.2
133 *
134 * @param int $user_id
135 * @param WP_Post $new_rank
136 * @param WP_Post $old_rank
137 * @param int $admin_id
138 * @param int $achievement_id
139 */
140 function gamipress_update_user_rank_bp_activity( $user_id, $new_rank, $old_rank, $admin_id, $achievement_id ) {
141
142 if ( ! gamipress_bp_is_active( 'activity' ) ) {
143 return;
144 }
145
146 $activity = array();
147
148 $activity = apply_filters( 'gamipress_bp_activity_details', $activity, $user_id, $new_rank->ID, '', get_current_blog_id(), array() );
149
150 // Insert the activity
151 if( ! empty( $activity ) )
152 bp_activity_add( $activity );
153
154 }
155 add_action( 'gamipress_update_user_rank', 'gamipress_update_user_rank_bp_activity', 10, 5 );
156
157 /**
158 * Points award and deduct activity entry details
159 *
160 * @since 1.2.6
161 *
162 * @param array $activity
163 * @param int $user_id
164 * @param int $post_id
165 * @param string $trigger_type
166 * @param int $site_id
167 * @param array $args
168 *
169 * @return array
170 */
171 function gamipress_bp_points_activity_details( $activity, $user_id, $post_id, $trigger_type, $site_id, $args ) {
172
173 // Setup vars
174 $prefix = '_gamipress_bp_';
175 $post = gamipress_get_post( $post_id );
176 $post_type = $post->post_type;
177 $points_types = gamipress_get_points_types();
178
179 // Bail if isn't a points award or deduct
180 if( ! in_array( $post_type, array( 'points-award', 'points-deduct' ) ) )
181 return $activity;
182
183 $points_type = gamipress_get_points_award_points_type( $post_id );
184
185 // Bail if not correctly assigned to a points type
186 if( ! $points_type )
187 return $activity;
188
189 // Bail if create activity option isn't enabled
190 if( ! (bool) gamipress_get_post_meta( $points_type->ID, $prefix . 'create_' . str_replace( '-', '_', $post_type ) . '_activity', true ) )
191 return $activity;
192
193 $points = absint( gamipress_get_post_meta( $post_id, '_gamipress_points' ) );
194 $formatted_points = gamipress_format_points( $points, $points_type->post_name );
195
196 // Setup our entry content
197 $content = '<div id="gamipress-' . $post_type . '-' . $post_id . '" class="gamipress-' . $post_type . ' user-has-earned">';
198 $content .= '<div class="gamipress-points-type-image">' . gamipress_get_points_type_thumbnail( $points_type->post_name ) . '</div>';
199 $content .= '<div class="gamipress-points-type-description">' . $post->post_title . '</div>';
200 $content .= '</div>';
201
202 // Bypass checking our activity items from moderation, as we know we are legit.
203 add_filter( 'bp_bypass_check_for_moderation', '__return_true' );
204
205 $action_pattern = __( '%1$s earned %2$s', 'gamipress' );
206
207 if( $post_type === 'points-deduct' ) {
208 $action_pattern = __( '%1$s lost %2$s', 'gamipress' );
209 }
210
211
212 // Setup the activity details
213 $activity = array(
214 'action' => sprintf( $action_pattern, bp_core_get_userlink( $user_id ), $formatted_points ),
215 'content' => $content,
216 'component' => 'gamipress',
217 'type' => 'activity_update',
218 'primary_link' => get_permalink( $post_id ),
219 'user_id' => $user_id,
220 'item_id' => $post_id,
221 );
222
223 return $activity;
224
225 }
226 add_filter( 'gamipress_bp_activity_details', 'gamipress_bp_points_activity_details', 10, 6 );
227
228 /**
229 * Achievement activity entry details
230 *
231 * @since 1.2.6
232 *
233 * @param array $activity
234 * @param int $user_id
235 * @param int $post_id
236 * @param string $trigger_type
237 * @param int $site_id
238 * @param array $args
239 *
240 * @return array
241 */
242 function gamipress_bp_achievement_activity_details( $activity, $user_id, $post_id, $trigger_type, $site_id, $args ) {
243
244 // Setup vars
245 $prefix = '_gamipress_bp_';
246 $post = gamipress_get_post( $post_id );
247 $post_type = $post->post_type;
248 $achievement_types = gamipress_get_achievement_types();
249
250 // Bail if isn't an achievement
251 if( ! in_array( $post_type, gamipress_get_achievement_types_slugs() ) )
252 return $activity;
253
254 // Bail if create activity option isn't enabled
255 if( ! (bool) gamipress_get_post_meta( $achievement_types[$post_type]['ID'], $prefix . 'create_achievement_activity', true ) )
256 return $activity;
257
258 // Grab the singular name for our achievement type
259 $singular_name = strtolower( $achievement_types[$post_type]['singular_name'] );
260
261 // Setup our entry content
262 $content = '<div id="gamipress-achievement-' . $post_id . '" class="gamipress-achievement user-has-earned">';
263 $content .= '<div class="gamipress-achievement-image"><a href="'. get_permalink( $post_id ) . '">' . gamipress_get_achievement_post_thumbnail( $post_id ) . '</a></div>';
264 $content .= '<div class="gamipress-achievement-description">' . gamipress_bp_activity_get_post_excerpt( $post_id ) . '</div>';
265 $content .= '</div>';
266
267 // Bypass checking our activity items from moderation, as we know we are legit.
268 add_filter( 'bp_bypass_check_for_moderation', '__return_true' );
269
270 // Setup the activity details
271 $activity = array(
272 'action' => sprintf( __( '%1$s earned the %2$s %3$s', 'gamipress' ), bp_core_get_userlink( $user_id ), $singular_name, '<a href="' . get_permalink( $post_id ) . '">' . $post->post_title . '</a>' ),
273 'content' => $content,
274 'component' => 'gamipress',
275 'type' => 'activity_update',
276 'primary_link' => get_permalink( $post_id ),
277 'user_id' => $user_id,
278 'item_id' => $post_id,
279 );
280
281 return $activity;
282
283 }
284 add_filter( 'gamipress_bp_activity_details', 'gamipress_bp_achievement_activity_details', 10, 6 );
285
286 /**
287 * Step activity entry details
288 *
289 * @since 1.2.6
290 *
291 * @param array $activity
292 * @param int $user_id
293 * @param int $post_id
294 * @param string $trigger_type
295 * @param int $site_id
296 * @param array $args
297 *
298 * @return array
299 */
300 function gamipress_bp_step_activity_details( $activity, $user_id, $post_id, $trigger_type, $site_id, $args ) {
301
302 // Setup vars
303 $prefix = '_gamipress_bp_';
304 $post = gamipress_get_post( $post_id );
305 $post_type = $post->post_type;
306 $achievement_types = gamipress_get_achievement_types();
307
308 // Bail if isn't a step
309 if( $post_type !== 'step' )
310 return $activity;
311
312 $achievement = gamipress_get_step_achievement( $post_id );
313
314 // Bail if create activity option isn't enabled
315 if( ! (bool) gamipress_get_post_meta( $achievement_types[$achievement->post_type]['ID'], $prefix . 'create_step_activity', true ) )
316 return $activity;
317
318 // Grab the singular name for our achievement type
319 $singular_name = strtolower( $achievement_types[$achievement->post_type]['singular_name'] );
320
321 // Setup our entry content
322 $content = '<div id="gamipress-achievement-' . $achievement->ID . '" class="gamipress-achievement user-has-earned">';
323 $content .= '<div class="gamipress-achievement-image"><a href="'. get_permalink( $achievement->ID ) . '">' . gamipress_get_achievement_post_thumbnail( $achievement->ID ) . '</a></div>';
324 $content .= '<div class="gamipress-achievement-description">' . gamipress_bp_activity_get_post_excerpt( $achievement->ID ) . '</div>';
325 $content .= '</div>';
326
327 // Bypass checking our activity items from moderation, as we know we are legit.
328 add_filter( 'bp_bypass_check_for_moderation', '__return_true' );
329
330 // Setup the activity details
331 $activity = array(
332 'action' => sprintf( __( '%1$s completed the step "%2$s" of the %3$s %4$s', 'gamipress' ), bp_core_get_userlink( $user_id ), $post->post_title, $singular_name, '<a href="' . get_permalink( $achievement->ID ) . '">' . $achievement->post_title . '</a>' ),
333 'content' => $content,
334 'component' => 'gamipress',
335 'type' => 'activity_update',
336 'primary_link' => get_permalink( $achievement->ID ),
337 'user_id' => $user_id,
338 'item_id' => $post_id,
339 );
340
341 return $activity;
342
343 }
344 add_filter( 'gamipress_bp_activity_details', 'gamipress_bp_step_activity_details', 10, 6 );
345
346 /**
347 * Rank activity entry details
348 *
349 * @since 1.2.6
350 *
351 * @param array $activity
352 * @param int $user_id
353 * @param int $post_id
354 * @param string $trigger_type
355 * @param int $site_id
356 * @param array $args
357 *
358 * @return array
359 */
360 function gamipress_bp_rank_activity_details( $activity, $user_id, $post_id, $trigger_type, $site_id, $args ) {
361
362 // Setup vars
363 $prefix = '_gamipress_bp_';
364 $post = gamipress_get_post( $post_id );
365 $post_type = $post->post_type;
366 $rank_types = gamipress_get_rank_types();
367
368 // Bail if isn't a rank
369 if( ! in_array( $post_type, gamipress_get_rank_types_slugs() ) )
370 return $activity;
371
372 // Bail if create activity option isn't enabled
373 if( ! (bool) gamipress_get_post_meta( $rank_types[$post_type]['ID'], $prefix . 'create_rank_activity', true ) )
374 return $activity;
375
376 // Grab the singular name for our rank type
377 $singular_name = strtolower( $rank_types[$post_type]['singular_name'] );
378
379 // Setup our entry content
380 $content = '<div id="gamipress-rank-' . $post_id . '" class="gamipress-rank user-has-earned">';
381 $content .= '<div class="gamipress-rank-image"><a href="'. get_permalink( $post_id ) . '">' . gamipress_get_rank_post_thumbnail( $post_id ) . '</a></div>';
382 $content .= '<div class="gamipress-rank-description">' . gamipress_bp_activity_get_post_excerpt( $post_id ) . '</div>';
383 $content .= '</div>';
384
385 // Bypass checking our activity items from moderation, as we know we are legit.
386 add_filter( 'bp_bypass_check_for_moderation', '__return_true' );
387
388 // Setup the activity details
389 $activity = array(
390 'action' => sprintf( __( '%1$s reached the %2$s %3$s', 'gamipress' ), bp_core_get_userlink( $user_id ), $singular_name, '<a href="' . get_permalink( $post_id ) . '">' . $post->post_title . '</a>' ),
391 'content' => $content,
392 'component' => 'gamipress',
393 'type' => 'activity_update',
394 'primary_link' => get_permalink( $post_id ),
395 'user_id' => $user_id,
396 'item_id' => $post_id,
397 );
398
399 return $activity;
400
401 }
402 add_filter( 'gamipress_bp_activity_details', 'gamipress_bp_rank_activity_details', 10, 6 );
403
404 /**
405 * Rank requirement activity entry details
406 *
407 * @since 1.2.6
408 *
409 * @param array $activity
410 * @param int $user_id
411 * @param int $post_id
412 * @param string $trigger_type
413 * @param int $site_id
414 * @param array $args
415 *
416 * @return array
417 */
418 function gamipress_bp_rank_requirement_activity_details( $activity, $user_id, $post_id, $trigger_type, $site_id, $args ) {
419
420 // Setup vars
421 $prefix = '_gamipress_bp_';
422 $post = gamipress_get_post( $post_id );
423 $post_type = $post->post_type;
424 $rank_types = gamipress_get_rank_types();
425
426 // Bail if isn't a rank requirement
427 if( $post_type !== 'rank-requirement' )
428 return $activity;
429
430 $rank = gamipress_get_rank_requirement_rank( $post_id );
431
432 // Bail if create activity option isn't enabled
433 if( ! (bool) gamipress_get_post_meta( $rank_types[$rank->post_type]['ID'], $prefix . 'create_rank_requirement_activity', true ) )
434 return $activity;
435
436 // Grab the singular name for our rank type
437 $singular_name = strtolower( $rank_types[$rank->post_type]['singular_name'] );
438
439 // Setup our entry content
440 $content = '<div id="gamipress-rank-' . $rank->ID . '" class="gamipress-rank user-has-earned">';
441 $content .= '<div class="gamipress-rank-image"><a href="'. get_permalink( $rank->ID ) . '">' . gamipress_get_rank_post_thumbnail( $rank->ID ) . '</a></div>';
442 $content .= '<div class="gamipress-rank-description">' . gamipress_bp_activity_get_post_excerpt( $rank->ID ) . '</div>';
443 $content .= '</div>';
444
445 // Bypass checking our activity items from moderation, as we know we are legit.
446 add_filter( 'bp_bypass_check_for_moderation', '__return_true' );
447
448 // Setup the activity details
449 $activity = array(
450 'action' => sprintf( __( '%1$s completed the requirement "%2$s" of the %3$s %4$s', 'gamipress' ), bp_core_get_userlink( $user_id ), $post->post_title, $singular_name, '<a href="' . get_permalink( $rank->ID ) . '">' . $rank->post_title . '</a>' ),
451 'content' => $content,
452 'component' => 'gamipress',
453 'type' => 'activity_update',
454 'primary_link' => get_permalink( $rank->ID ),
455 'user_id' => $user_id,
456 'item_id' => $post_id,
457 );
458
459 return $activity;
460
461 }
462 add_filter( 'gamipress_bp_activity_details', 'gamipress_bp_rank_requirement_activity_details', 10, 6 );
463
464 /**
465 * Filter activity allowed html tags to allow divs with classes and ids.
466 *
467 * @since 1.0.1
468 *
469 * @param array $activity_allowed_tags
470 *
471 * @return array
472 */
473 function gamipress_bp_activity_allowed_tags( $activity_allowed_tags ) {
474
475 $activity_allowed_tags['div'] = array();
476 $activity_allowed_tags['div']['id'] = array();
477 $activity_allowed_tags['div']['class'] = array();
478
479 return $activity_allowed_tags;
480
481 }
482 add_filter( 'bp_activity_allowed_tags', 'gamipress_bp_activity_allowed_tags' );
483
484 /**
485 * Helper function to get a post excerpt
486 *
487 * @since 1.0.1
488 *
489 * @param int $post_id
490 *
491 * @return string
492 */
493 function gamipress_bp_activity_get_post_excerpt( $post_id ) {
494
495 $excerpt = has_excerpt( $post_id ) ? gamipress_get_post_field( 'post_excerpt', $post_id ) : gamipress_get_post_field( 'post_content', $post_id );
496
497 return wpautop( do_blocks( apply_filters( 'get_the_excerpt', $excerpt, get_post( $post_id ) ) ) );
498
499 }
500