triggers.php
19.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
<?php
/**
* Activity Triggers, used for triggering point earning
*
* @package badgeos
* @subpackage points
* @author wooninja
* @link https://wooninja.com
*/
/**
* Helper function for returning our available actvity triggers
*
* @return mixed|void
*/
function get_badgeos_points_award_activity_triggers() {
$GLOBALS['badgeos']->award_points_activity_triggers = apply_filters(
'badgeos_award_points_activity_triggers',
array(
'badgeos_wp_login' => esc_html__( 'Log in to Website', 'badgeos' ),
'badgeos_new_comment' => esc_html__( 'Comment on a post', 'badgeos' ),
'badgeos_specific_new_comment' => esc_html__( 'Comment on a specific post', 'badgeos' ),
'badgeos_new_post' => esc_html__( 'Publish a new post', 'badgeos' ),
'badgeos_visit_a_post' => esc_html__( 'Visit a Post', 'badgeos' ),
'badgeos_award_author_on_visit_post' => esc_html__( 'Award author when a user visits post', 'badgeos' ),
'badgeos_new_page' => esc_html__( 'Publish a new page', 'badgeos' ),
'badgeos_visit_a_page' => esc_html__( 'Visit a Page', 'badgeos' ),
'badgeos_award_author_on_visit_page' => esc_html__( 'Award author when a user visits page', 'badgeos' ),
'user_register' => esc_html__( 'Register to the website', 'badgeos' ),
'badgeos_daily_visit' => esc_html__( 'Daily visit website', 'badgeos' ),
'badgeos_points_on_birthday' => esc_html__( 'On User Birthday', 'badgeos' ),
'badgeos_on_completing_num_of_year' => esc_html__( 'After completing the number of years', 'badgeos' ),
'badgeos_on_completing_num_of_month' => esc_html__( 'After completing the number of months', 'badgeos' ),
'badgeos_on_completing_num_of_day' => esc_html__( 'After completing the number of days', 'badgeos' ),
'badgeos_on_the_first_x_users' => esc_html__( 'Points to the first X users', 'badgeos' ),
)
);
return apply_filters( 'badgeos_activity_triggers_for_all', $GLOBALS['badgeos']->award_points_activity_triggers );
}
/**
* Helper function for returning our available actvity triggers
*
* @return array An array of all our activity triggers stored as 'value' => 'Display Name'
*/
function get_badgeos_points_deduct_activity_triggers() {
$GLOBALS['badgeos']->deduct_points_activity_triggers = apply_filters(
'badgeos_deduct_points_activity_triggers',
array(
'badgeos_wp_login' => esc_html__( 'Log in to Website', 'badgeos' ),
'badgeos_wp_not_login' => esc_html__( 'Not Login for X days', 'badgeos' ),
'badgeos_new_comment' => esc_html__( 'Comment on a post', 'badgeos' ),
'badgeos_specific_new_comment' => esc_html__( 'Comment on a specific post', 'badgeos' ),
'badgeos_new_post' => esc_html__( 'Publish a new post', 'badgeos' ),
'badgeos_visit_a_post' => esc_html__( 'Visit a Post', 'badgeos' ),
'badgeos_award_author_on_visit_post' => esc_html__( 'Award author when a user visits post', 'badgeos' ),
'badgeos_new_page' => esc_html__( 'Publish a new page', 'badgeos' ),
'badgeos_visit_a_page' => esc_html__( 'Visit a Page', 'badgeos' ),
'badgeos_award_author_on_visit_page' => esc_html__( 'Award author when a user visits page', 'badgeos' ),
'user_register' => esc_html__( 'Register to the website', 'badgeos' ),
'badgeos_daily_visit' => esc_html__( 'Daily visit website', 'badgeos' ),
'badgeos_on_completing_num_of_year' => esc_html__( 'After completing the number of years', 'badgeos' ),
'badgeos_on_completing_num_of_month' => esc_html__( 'After completing the number of months', 'badgeos' ),
'badgeos_on_completing_num_of_day' => esc_html__( 'After completing the number of days', 'badgeos' ),
'badgeos_remove_achievment_on_point_deduct' => esc_html__( 'Remove an Achievement', 'badgeos' ),
'badgeos_remove_rank_on_point_deduct' => esc_html__( 'Remove a Rank', 'badgeos' ),
)
);
return apply_filters( 'badgeos_activity_triggers_for_all', $GLOBALS['badgeos']->deduct_points_activity_triggers );
}
/**
* Load up our activity triggers so we can add actions to them
*
* @return void
*/
function badgeos_points_load_activity_triggers() {
/**
* Grab our activity triggers
*/
$award_activity_triggers = get_badgeos_points_award_activity_triggers();
$deduct_activity_triggers = get_badgeos_points_deduct_activity_triggers();
/**
* Loop through each trigger and add our trigger event to the hook
*/
foreach ( $award_activity_triggers as $trigger => $trigger_data ) {
if ( is_array( $trigger_data ) ) {
if ( count( $trigger_data['sub_triggers'] ) > 0 ) {
foreach ( $trigger_data['sub_triggers'] as $tdata ) {
add_action( trim( $tdata['trigger'] ), 'badgeos_dynamic_points_award_trigger_event', 10, 20 );
}
}
} else {
add_action( $trigger, 'badgeos_points_award_trigger_event', 10, 20 );
}
}
/**
* Loop through each trigger and add our trigger event to the hook
*/
foreach ( $deduct_activity_triggers as $trigger => $trigger_data ) {
if ( is_array( $trigger_data ) ) {
if ( count( $trigger_data['sub_triggers'] ) > 0 ) {
foreach ( $trigger_data['sub_triggers'] as $tdata ) {
add_action( trim( $tdata['trigger'] ), 'badgeos_dynamic_points_deduct_trigger_event', 10, 20 );
}
}
} else {
add_action( $trigger, 'badgeos_points_deduct_trigger_event', 10, 20 );
}
}
}
add_action( 'init', 'badgeos_points_load_activity_triggers' );
/**
* Handle each of our award point activity triggers
*
* @return mixed
*/
function badgeos_dynamic_points_award_trigger_event() {
/**
* Setup all our globals
*/
global $blog_id, $wpdb;
$site_id = $blog_id;
$args = func_get_args();
/**
* Grab our current trigger
*/
$this_trigger = current_filter();
/**
* Grab the user ID
*/
$user_id = $args[0];
if ( intval( $user_id ) < 1 ) {
$user_id = get_current_user_id();
}
$user_data = get_user_by( 'id', $user_id );
/**
* Sanity check, if we don't have a user object, bail here
*/
if ( ! is_object( $user_data ) ) {
return $args[0];
}
/**
* If the user doesn't satisfy the trigger requirements, bail here\
*/
if ( ! apply_filters( 'user_deserves_point_award_trigger', true, $user_id, $this_trigger, $site_id, $args ) ) {
return $args[0];
}
$triggered_points = $wpdb->get_results(
$wpdb->prepare(
"SELECT p.ID as post_id FROM $wpdb->postmeta AS pm INNER JOIN $wpdb->posts AS p ON ( p.ID = pm.post_id AND pm.meta_key = '_badgeos_paward_subtrigger_value' ) where p.post_status = 'publish' AND pm.meta_value = %s",
$this_trigger
)
);
if ( ! empty( $triggered_points ) ) {
foreach ( $triggered_points as $point ) {
$parent_point_id = badgeos_get_parent_id( $point->post_id );
$step_params = badgeos_utilities::get_post_meta( $point->post_id, '_badgeos_fields_data', true );
$step_params = badgeos_extract_array_from_query_params( $step_params );
/**
* Update hook count for this user
*/
if ( apply_filters( 'badgeos_check_dynamic_trigger_filter', true, 'point_award', $this_trigger, $step_params, $args[1] ) ) {
$new_count = badgeos_points_update_user_trigger_count( $point->post_id, $parent_point_id, $user_id, $this_trigger, $site_id, 'Award', $args );
badgeos_maybe_award_points_to_user( $point->post_id, $parent_point_id, $user_id, $this_trigger, $site_id, $args );
}
}
}
return $args[0];
}
/**
* Handle each of our deduct point activity triggers
*
* @return mixed
*/
function badgeos_dynamic_points_deduct_trigger_event() {
/**
* Setup all our globals
*/
global $blog_id, $wpdb;
$site_id = $blog_id;
$args = func_get_args();
/**
* Grab our current trigger
*/
$this_trigger = current_filter();
/**
* Grab the user ID
*/
$user_id = $args[0];
if ( intval( $user_id ) < 1 ) {
$user_id = get_current_user_id();
}
$user_data = get_user_by( 'id', $user_id );
/**
* Sanity check, if we don't have a user object, bail here
*/
if ( ! is_object( $user_data ) ) {
return $args[0];
}
/**
* If the user doesn't satisfy the trigger requirements, bail here
*/
if ( ! apply_filters( 'user_deserves_point_deduct_trigger', true, $user_id, $this_trigger, $site_id, $args ) ) {
return $args[0];
}
/**
* Now determine if any Achievements are earned based on this trigger event
*/
$triggered_deducts = $wpdb->get_results(
$wpdb->prepare(
"SELECT p.ID as post_id FROM $wpdb->postmeta AS pm INNER JOIN $wpdb->posts AS p ON ( p.ID = pm.post_id AND pm.meta_key = '_badgeos_pdeduct_subtrigger_value' ) where p.post_status = 'publish' AND pm.meta_value = %s",
$this_trigger
)
);
if ( ! empty( $triggered_deducts ) ) {
foreach ( $triggered_deducts as $point ) {
$parent_point_id = badgeos_get_parent_id( $point->post_id );
$step_params = badgeos_utilities::get_post_meta( $point->post_id, '_badgeos_fields_data', true );
$step_params = badgeos_extract_array_from_query_params( $step_params );
/**
* Update hook count for this user
*/
if ( apply_filters( 'badgeos_check_dynamic_trigger_filter', true, 'point_deduct', $this_trigger, $step_params, $args[1] ) ) {
$new_count = badgeos_points_update_user_trigger_count( $point->post_id, $parent_point_id, $user_id, $this_trigger, $site_id, 'Deduct', $args );
badgeos_maybe_deduct_points_to_user( $point->post_id, $parent_point_id, $user_id, $this_trigger, $site_id, $args );
}
}
}
return $args[0];
}
/**
* Handle each of our award point activity triggers
*
* @return mixed
*/
function badgeos_points_award_trigger_event() {
/**
* Setup all our globals
*/
global $blog_id, $wpdb;
$site_id = $blog_id;
$args = func_get_args();
/**
* Grab our current trigger
*/
$this_trigger = current_filter();
/**
* Grab the user ID
*/
$user_id = badgeos_trigger_get_user_id( $this_trigger, $args );
$user_data = get_user_by( 'id', $user_id );
/**
* Sanity check, if we don't have a user object, bail here
*/
if ( ! is_object( $user_data ) ) {
return $args[0];
}
/**
* If the user doesn't satisfy the trigger requirements, bail here\
*/
if ( ! apply_filters( 'user_deserves_point_award_trigger', true, $user_id, $this_trigger, $site_id, $args ) ) {
return $args[0];
}
$triggered_points = '';
if ( 'badgeos_specific_new_comment' == $this_trigger ) {
$trigger_data = $wpdb->get_results( "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE ( meta_key = '_badgeos_trigger_type' or meta_key = '_point_trigger_type' ) AND meta_value = 'badgeos_specific_new_comment'" );
if ( $trigger_data ) {
$comment_post_id = $args[3]['comment_post_ID'];
foreach ( $trigger_data as $data ) {
$post_specific_id = badgeos_utilities::get_post_meta( absint( $data->post_id ), '_badgeos_achievement_post', true );
if ( absint( $post_specific_id ) == absint( $comment_post_id ) ) {
/**
* Now determine if any badges are earned based on this trigger event
*/
$triggered_points = $wpdb->get_results(
$wpdb->prepare(
"SELECT p.ID as post_id
FROM $wpdb->postmeta AS pm
INNER JOIN $wpdb->posts AS p ON ( p.ID = pm.post_id AND pm.meta_key = '_point_trigger_type' ) where p.post_status = 'publish' AND pm.meta_value = %s
",
$this_trigger
)
);
break;
}
}
}
} else {
/**
* Now determine if any badges are earned based on this trigger event
*/
$triggered_points = $wpdb->get_results(
$wpdb->prepare(
"SELECT p.ID as post_id
FROM $wpdb->postmeta AS pm
INNER JOIN $wpdb->posts AS p ON ( p.ID = pm.post_id AND pm.meta_key = '_point_trigger_type' ) where p.post_status = 'publish' AND pm.meta_value = %s
",
$this_trigger
)
);
}
if ( ! empty( $triggered_points ) ) {
foreach ( $triggered_points as $point ) {
$parent_point_id = badgeos_get_parent_id( $point->post_id );
/**
* Update hook count for this user
*/
$new_count = badgeos_points_update_user_trigger_count( $point->post_id, $parent_point_id, $user_id, $this_trigger, $site_id, 'Award', $args );
badgeos_maybe_award_points_to_user( $point->post_id, $parent_point_id, $user_id, $this_trigger, $site_id, $args );
}
}
return $args[0];
}
/**
* Return parent achievement.
*
* @param int $child_id Child achievement.
* @return int $parent_id parent id.
*/
function badgeos_get_parent_id( $child_id = 0 ) {
global $wpdb;
$parent_id = $wpdb->get_var( $wpdb->prepare( "SELECT p2p_to FROM $wpdb->p2p WHERE p2p_from = %d", $child_id ) );
if ( ! $parent_id ) {
return 0;
}
return $parent_id;
}
/**
* Handle each of our point deduct activity triggers
*
* @return mixed
*/
function badgeos_points_deduct_trigger_event() {
/**
* Setup all our globals
*/
global $blog_id, $wpdb;
$site_id = $blog_id;
$args = func_get_args();
/**
* Grab our current trigger
*/
$this_trigger = current_filter();
/**
* Grab the user ID
*/
$user_id = badgeos_trigger_get_user_id( $this_trigger, $args );
$user_data = get_user_by( 'id', $user_id );
/**
* Sanity check, if we don't have a user object, bail here
*/
if ( ! is_object( $user_data ) ) {
return $args[0];
}
/**
* If the user doesn't satisfy the trigger requirements, bail here
*/
if ( ! apply_filters( 'user_deserves_point_deduct_trigger', true, $user_id, $this_trigger, $site_id, $args ) ) {
return $args[0];
}
$triggered_deducts = '';
if ( 'badgeos_specific_new_comment' == $this_trigger ) {
$trigger_data = $wpdb->get_results( "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE ( meta_key = '_badgeos_trigger_type' or meta_key = '_deduct_trigger_type' ) AND meta_value = 'badgeos_specific_new_comment'" );
if ( $trigger_data ) {
$comment_post_id = $args[3]['comment_post_ID'];
foreach ( $trigger_data as $data ) {
$post_specific_id = badgeos_utilities::get_post_meta( absint( $data->post_id ), '_badgeos_achievement_post', true );
if ( absint( $post_specific_id ) == absint( $comment_post_id ) ) {
/**
* Now determine if any Achievements are earned based on this trigger event
*/
$triggered_deducts = $wpdb->get_results(
$wpdb->prepare(
"SELECT p.ID as post_id
FROM $wpdb->postmeta AS pm
INNER JOIN $wpdb->posts AS p ON ( p.ID = pm.post_id AND pm.meta_key = '_deduct_trigger_type' ) where p.post_status = 'publish' AND pm.meta_value = %s
",
$this_trigger
)
);
break;
}
}
}
} else {
/**
* Now determine if any Achievements are earned based on this trigger event
*/
$triggered_deducts = $wpdb->get_results(
$wpdb->prepare(
"SELECT p.ID as post_id
FROM $wpdb->postmeta AS pm
INNER JOIN $wpdb->posts AS p ON ( p.ID = pm.post_id AND pm.meta_key = '_deduct_trigger_type' ) where p.post_status = 'publish' AND pm.meta_value = %s
",
$this_trigger
)
);
}
if ( ! empty( $triggered_deducts ) ) {
foreach ( $triggered_deducts as $point ) {
$parent_point_id = badgeos_get_parent_id( $point->post_id );
/**
* Update hook count for this user
*/
$new_count = badgeos_points_update_user_trigger_count( $point->post_id, $parent_point_id, $user_id, $this_trigger, $site_id, 'Deduct', $args );
badgeos_maybe_deduct_points_to_user( $point->post_id, $parent_point_id, $user_id, $this_trigger, $site_id, $args );
}
}
return $args[0];
}
/**
* Wrapper function for returning a user's array of point triggers
*
* @param integer $user_id The given user's ID
* @param integer $site_id The desired Site ID to check
* @return array An array of the triggers a user has triggered
*/
function badgeos_points_get_user_triggers( $point_step_id, $user_id = 0, $site_id = 0, $type = 'Award' ) {
/**
* Grab all of the users triggers
*/
if ( $type == 'Deduct' ) {
$user_triggers = ( $array_exists = badgeos_utilities::get_user_meta( $user_id, '_point_deduct_triggers', true ) ) ? $array_exists : array();
} else {
$user_triggers = ( $array_exists = badgeos_utilities::get_user_meta( $user_id, '_point_award_triggers', true ) ) ? $array_exists : array();
}
/**
* Use current site ID if site ID is not set, AND not explicitly set to false
*/
if ( ! $site_id && false !== $site_id ) {
$site_id = get_current_blog_id();
}
/**
* Return only the triggers that are relevant to the provided $site_id
*/
if ( $site_id && isset( $user_triggers[ $site_id ] ) ) {
return $user_triggers[ $site_id ];
/**
* Otherwise, return the full array of all triggers across all sites
*/
} else {
return $user_triggers;
}
}
/**
* Get the count for the number of times a user has triggered a particular point trigger
*
* @param integer $user_id The given user's ID
* @param string $trigger The given trigger we're checking
* @param integer $site_id The desired Site ID to check
* @param array $args The triggered args
* @return integer The total number of times a user has triggered the trigger
*/
function points_get_user_trigger_count( $point_step_id, $user_id, $trigger, $site_id = 0, $type = 'Award', $args = array() ) {
/**
* Set to current site id
*/
if ( ! $site_id ) {
$site_id = get_current_blog_id();
}
/**
* Grab the user's logged triggers
*/
$user_triggers = badgeos_points_get_user_triggers( $point_step_id, $user_id, $site_id, $type );
$trigger = apply_filters( 'points_get_user_trigger_name', $trigger, $point_step_id, $user_id, $site_id, $type, $args );
/**
* If we have any triggers, return the current count for the given trigger
*/
if ( ! empty( $user_triggers ) && isset( $user_triggers[ $trigger . '_' . $point_step_id ] ) ) {
return absint( $user_triggers[ $trigger . '_' . $point_step_id ] );
} else {
return 0;
}
}
/**
* Update the user's point trigger count for a given trigger by 1
*
* @param integer $user_id The given user's ID
* @param string $trigger The trigger we're updating
* @param integer $site_id The desired Site ID to update
* @param array $args The triggered args
* @return integer The updated trigger count
*/
function badgeos_points_update_user_trigger_count( $point_step_id = 0, $point_parent_id = 0, $user_id = '', $trigger = '', $site_id = 0, $type = 'Award', $args = array() ) {
/**
* Set to current site id
*/
if ( ! $site_id ) {
$site_id = get_current_blog_id();
}
/**
* Grab the current count and increase it by 1
*/
$trigger_count = absint( points_get_user_trigger_count( $point_step_id, $user_id, $trigger, $site_id, $type, $args ) );
$increment = 1;
if ( $increment = apply_filters( 'point_update_user_trigger_count', $increment, $user_id, $trigger, $site_id, $type, $args ) ) {
$trigger_count += (int) $increment;
} else {
return $increment;
}
/**
* Update the triggers arary with the new count
*/
$user_triggers = badgeos_points_get_user_triggers( $point_step_id, $user_id, false, $type );
$user_triggers[ $site_id ][ $trigger . '_' . $point_step_id ] = $trigger_count;
if ( $type == 'Deduct' ) {
badgeos_utilities::update_user_meta( $user_id, '_point_deduct_triggers', $user_triggers );
} else {
badgeos_utilities::update_user_meta( $user_id, '_point_award_triggers', $user_triggers );
}
/**
* Send back our trigger count for other purposes\
*/
return $trigger_count;
}