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