This repository has been archived by the owner on Jan 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
achievements-core.php
executable file
·2127 lines (1787 loc) · 77 KB
/
achievements-core.php
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* This file is included when BuddyPress is active, and after the plugins_loaded action.
*
* @author Paul Gibbs <paul@byotos.com>
* @package Achievements for BuddyPress
* @subpackage core
*
* $Id: achievements-core.php 1024 2012-01-02 13:53:50Z DJPaul $
*/
/**
* Constant for third-party plugins to check if Achievements is active
*/
define ( 'ACHIEVEMENTS_IS_INSTALLED', 1 );
/**
* Version number
*/
define ( 'ACHIEVEMENTS_VERSION', '2.0.6' );
load_plugin_textdomain( 'dpa', false, '/achievements/includes/languages/' );
// The classes file holds all database access classes and functions
require ( dirname( __FILE__ ) . '/achievements-classes.php' );
// The ajax file holds all functions used in AJAX queries
require ( dirname( __FILE__ ) . '/achievements-ajax.php' );
// The cssjs file sets up and enqueue all CSS and JS files
require ( dirname( __FILE__ ) . '/achievements-cssjs.php' );
// The templatetags file contains classes and functions designed for use in template files
require ( dirname( __FILE__ ) . '/achievements-templatetags.php' );
// The widgets file contains code to create and register widgets
require ( dirname( __FILE__ ) . '/achievements-widgets.php' );
// The notifications file contains functions to send email notifications on specific user actions
require ( dirname( __FILE__ ) . '/achievements-notifications.php' );
// The filters file creates and apply filters to component output functions
require ( dirname( __FILE__ ) . '/achievements-filters.php' );
/**
* Set up global variables
*
* @global object $bp BuddyPress global settings
* @since 2.0
*/
function dpa_setup_globals() {
global $bp;
$bp->achievements = new stdClass;
$bp->achievements->id = 'achievements';
$bp->achievements->slug = DPA_SLUG;
$bp->achievements->table_achievements = $bp->table_prefix . 'achievements';
$bp->achievements->table_unlocked = $bp->table_prefix . 'achievements_unlocked';
$bp->achievements->table_actions = $bp->table_prefix . 'achievements_actions';
$bp->active_components[$bp->achievements->slug] = $bp->achievements->id;
// For BuddyPress 1.5
$bp->achievements->root_slug = isset( $bp->pages->achievements->slug ) ? $bp->pages->achievements->slug : DPA_SLUG;
$bp->achievements->name = __( 'Achievements', 'dpa' );
$bp->achievements->notification_callback = 'dpa_format_notifications';
}
add_action( 'bp_setup_globals', 'dpa_setup_globals' );
/**
* Registers as a root component (example.com/achievements/)
*
* @since 2.0
*/
function dpa_setup_root_component() {
bp_core_add_root_component( DPA_SLUG );
}
add_action( 'bp_setup_root_components', 'dpa_setup_root_component' );
/**
* Set up navigation and register pages
*
* @global object $bp BuddyPress global settings
* @global bool $is_member_page If we are under anything with a members slug
* @since 2.0
* @uses DPA_Achievement
*/
function dpa_setup_nav() {
global $bp, $is_member_page;
$url = dpa_get_achievements_permalink() . '/';
if ( bp_is_current_component( $bp->achievements->slug ) && $bp->achievements->current_achievement = new DPA_Achievement( array( 'type' => 'single', 'slug' => apply_filters( 'dpa_get_achievement_slug', $bp->current_action ) ) ) ) {
if ( isset( $bp->achievements->current_achievement->is_active ) && !$bp->achievements->current_achievement->is_active && !dpa_permission_can_user_edit() ) {
// Require edit permission to view an inactive Achievement's pages
bp_core_redirect( dpa_get_achievements_permalink() );
return;
} else {
if ( $bp->achievements->current_achievement->id )
$bp->is_single_item = true;
$bp->is_item_admin = 0;
if ( $bp->loggedin_user->is_super_admin )
$bp->is_item_admin = 1; // Possibly redundant since BP 1.1, but left for compatibility just in case
}
}
// Add 'Achievements' to the main navigation
bp_core_new_nav_item( array( 'name' => sprintf( __( 'Achievements <span>%s</span>', 'dpa' ), dpa_get_total_achievement_count_for_user() ), 'slug' => $bp->achievements->slug, 'position' => 80, 'screen_function' => 'dpa_screen_my_achievements', 'default_subnav_slug' => DPA_SLUG_MY_ACHIEVEMENTS, 'item_css_id' => $bp->achievements->id ) );
$subnav_url = $bp->loggedin_user->domain . DPA_SLUG . '/';
bp_core_new_subnav_item( array( 'name' => sprintf( __( 'My Achievements <span>%s</span>', 'dpa' ), dpa_get_total_achievement_count_for_user() ), 'slug' => DPA_SLUG_MY_ACHIEVEMENTS, 'parent_url' => $subnav_url, 'parent_slug' => $bp->achievements->slug, 'screen_function' => 'dpa_screen_my_achievements', 'position' => 10, 'item_css_id' => 'achievements_my' ) );
if ( bp_is_current_component( $bp->achievements->slug ) ) {
if ( bp_is_my_profile() && !$bp->is_single_item ) {
$bp->bp_options_title = __( 'My Achievements', 'dpa' );
} elseif ( !bp_is_my_profile() && !$bp->is_single_item ) {
$bp->bp_options_title = $bp->displayed_user->fullname;
} elseif ( $bp->is_single_item ) {
// When in a single listing, the first action is bumped down one because of the listing ID, so we need to adjust this and set the listing name to current_item.
$bp->current_item = $bp->current_action;
if ( isset( $bp->action_variables[0] ) )
$bp->current_action = $bp->action_variables[0];
else
$bp->current_action = '';
array_shift( $bp->action_variables );
$bp->bp_options_title = apply_filters( 'dpa_get_achievement_name', $bp->achievements->current_achievement->name );
$achievement_link = $url . $bp->achievements->current_achievement->slug . '/';
$parent_slug = $bp->achievements->current_achievement->slug;
// Add to the main navigation
$main_nav = array(
'name' => __( 'Home', 'dpa' ),
'slug' => $bp->achievements->current_achievement->slug,
'position' => -1, // Do not show in BuddyBar
'screen_function' => 'dpa_screen_achievement_activity',
'default_subnav_slug' => DPA_SLUG_ACHIEVEMENT_ACTIVITY,
'item_css_id' => $bp->achievements->id
);
bp_core_new_nav_item( $main_nav );
/**
* Setup the subnav items
*/
// Add the "Home" subnav item, as this will always be present
$sub_nav[] = array(
'name' => __( 'Home', 'dpa' ),
'slug' => DPA_SLUG_ACHIEVEMENT_ACTIVITY,
'parent_url' => $achievement_link,
'parent_slug' => $parent_slug,
'screen_function' => 'dpa_screen_achievement_activity',
'position' => 20,
'item_css_id' => 'achievements_activity'
);
$sub_nav[] = array(
'name' => sprintf( __( 'Unlocked By <span>%s</span>', 'dpa' ), dpa_get_achievement_unlocked_count( $bp->achievements->current_achievement->id ) ),
'slug' => DPA_SLUG_ACHIEVEMENT_UNLOCKED_BY,
'parent_url' => $achievement_link,
'parent_slug' => $parent_slug,
'screen_function' => 'dpa_screen_achievement_unlocked_by',
'position' => 40,
'item_css_id' => 'achievements_unlockedby'
);
if ( dpa_permission_can_user_change_picture() ) {
$sub_nav[] = array(
'name' => __( 'Change Picture', 'dpa' ),
'slug' => DPA_SLUG_ACHIEVEMENT_CHANGE_PICTURE,
'parent_url' => $achievement_link,
'parent_slug' => $parent_slug,
'screen_function' => 'dpa_screen_achievement_change_picture',
'position' => 50,
'item_css_id' => 'achievements_changepicture'
);
}
if ( dpa_permission_can_user_delete() ) {
$sub_nav[] = array(
'name' => __( 'Delete', 'dpa' ),
'slug' => DPA_SLUG_ACHIEVEMENT_DELETE,
'parent_url' => $achievement_link,
'parent_slug' => $parent_slug,
'screen_function' => 'dpa_screen_achievement_delete',
'position' => 50,
'user_has_access' => dpa_permission_can_user_delete(),
'item_css_id' => 'achievements_delete'
);
}
if ( dpa_permission_can_user_delete() ) {
$sub_nav[] = array(
'name' => __( 'Edit', 'dpa' ),
'slug' => DPA_SLUG_ACHIEVEMENT_EDIT,
'parent_url' => $achievement_link,
'parent_slug' => $parent_slug,
'screen_function' => 'dpa_screen_achievement_edit',
'position' => 60,
'user_has_access' => dpa_permission_can_user_edit(),
'item_css_id' => 'achievements_edit'
);
}
if ( dpa_permission_can_user_grant() ) {
$sub_nav[] = array(
'name' => __( 'Grant', 'dpa' ),
'slug' => DPA_SLUG_ACHIEVEMENT_GRANT,
'parent_url' => $achievement_link,
'parent_slug' => $parent_slug,
'screen_function' => 'dpa_screen_achievement_grant',
'position' => 70,
'user_has_access' => dpa_permission_can_user_grant(),
'item_css_id' => 'achievements_grant'
);
}
// initialize the subnav items
foreach( $sub_nav as $nav )
bp_core_new_subnav_item( $nav );
}
}
}
add_action( 'bp_setup_nav', 'dpa_setup_nav' );
/**
* Add WP Admin Bar support
*
* @global object $bp BuddyPress global settings
* @global object $wp_admin_bar WP Admin Bar object
* @since 2.1
*/
function dpa_setup_admin_bar() {
global $bp, $wp_admin_bar;
// Prevent debug notices
$wp_admin_nav = array();
// Menus for logged in user
if ( is_user_logged_in() ) {
// Setup the logged in user variables
$user_domain = $bp->loggedin_user->domain;
$link = trailingslashit( $user_domain . $bp->achievements->slug );
// Add the "Achievements" sub menu
$wp_admin_nav[] = array(
'parent' => $bp->my_account_menu_id,
'id' => 'my-account-' . $bp->achievements->id,
'title' => __( 'Achievements', 'dpa' ),
'href' => trailingslashit( $link )
);
// My Achievements
$wp_admin_nav[] = array(
'id' => 'dpa-my-achievements',
'parent' => 'my-account-' . $bp->achievements->id,
'title' => __( 'My Achievements', 'dpa' ),
'href' => trailingslashit( $link . DPA_SLUG_MY_ACHIEVEMENTS )
);
foreach( $wp_admin_nav as $admin_menu )
$wp_admin_bar->add_menu( $admin_menu );
}
}
add_action( 'bp_setup_admin_bar', 'dpa_setup_admin_bar' );
/**
* Add "show achievement unlocks" filter to sitewide and member activity streams
*
* @since 2.0
*/
function dpa_add_sitewide_activity_filter() {
?>
<option value="new_achievement"><?php _e( 'Achievements', 'dpa' ); ?></option>
<?php
}
add_action( 'bp_activity_filter_options', 'dpa_add_sitewide_activity_filter' );
add_action( 'bp_member_activity_filter_options', 'dpa_add_sitewide_activity_filter' );
/**
* Adds RSS links to page <head> section
*
* @global object $bp BuddyPress global settings
* @since 2.0
*/
function dpa_add_rss() {
global $bp;
if ( dpa_is_achievement_single() ) :
?>
<link rel="alternate" type="application/rss+xml" title="<?php bloginfo( 'name' ) ?> | <?php echo apply_filters( 'dpa_get_achievement_name', $bp->achievements->current_achievement->name ) ?> | <?php _e( 'Achievement RSS Feed', 'dpa' ) ?>" href="<?php dpa_achievement_activity_feed_link() ?>" />
<?php
endif;
}
add_action( 'bp_head', 'dpa_add_rss' );
/**
* This function adds a wp-admin menu item under "BuddyPress."
*
* @global object $bp BuddyPress global settings
* @since 2.0
*/
function dpa_add_admin_menu() {
global $bp;
if ( !$bp->loggedin_user->is_super_admin )
return false;
// This file holds everything that goes into /wp-admin/
require ( dirname( __FILE__ ) . '/achievements-admin.php' );
wp_enqueue_style( 'achievements-wpadmin', plugins_url( '/css/wpadmin.css', __FILE__ ), array(), ACHIEVEMENTS_VERSION );
add_submenu_page( 'bp-general-settings', __( 'Achievements', 'dpa' ), __( 'Achievements', 'dpa' ), 'manage_options', $bp->achievements->id, 'dpa_admin_screen' );
add_action( 'load-buddypress_page_achievements', 'dpa_admin_screen_on_load' );
add_action( 'admin_init', 'dpa_register_admin_settings' );
}
add_action( is_multisite() ? 'network_admin_menu' : 'admin_menu', 'dpa_add_admin_menu' );
/**
* Load template filter; allows site/super admin to override Achievements' templates in
* their active theme and replace the ones that are stored in the plugin directory.
*
* @global object $bp BuddyPress global settings
* @param string $found_template Contains any matches found for the requested template(s)
* @param array $templates Page template(s) we want to load
* @return string Absolute directory path to the requested template
* @since 2.0
*/
function dpa_load_template_filter( $found_template, $templates ) {
global $bp;
if ( !bp_is_current_component( $bp->achievements->slug ) || $found_template )
return $found_template;
$filtered_templates = array();
$filtered_templates = locate_template( $templates, false );
if ( is_array( $filtered_templates ) && isset( $filtered_templates[0] ) && $filtered_templates[0] )
return apply_filters( 'dpa_load_template_filter', $filtered_templates[0] );
$filtered_templates = array();
foreach ( (array) $templates as $template ) {
$url = dirname( __FILE__ ) . '/templates/' . $template;
if ( file_exists( $url ) )
$filtered_templates[] = $url;
}
if ( !isset( $filtered_templates[0] ) )
$filtered_templates[0] = '';
return apply_filters( 'dpa_load_template_filter', $filtered_templates[0] );
}
add_filter( 'bp_located_template', 'dpa_load_template_filter', 10, 2 );
/**
* Includes, into page output, a template file. To be used within a template included by bp_core_load_template().
*
* If the requested template follows the BuddyPress core theme file structure, for example "[groups]/[single]/file.php"
* i.e. being a file of a registered root component underneath its "single" folder, we'll also try to match against a version
* of that template path suffixed with the identifier of the object of the current component.
*
* For example, on pages of a group named "snugglewugglepants", we'd look for "groups/single/file-snugglewugglepants.php"
* as well as plain-old "groups/single/file.php".
*
* Underneath the member page hierarchy, the user's login name is used, e.g. "members/single/page-admin.php" for the admin user.
*
* @global object $bp BuddyPress global settings
* @param array $templates Requested templates, in priority order
* @see bp_core_add_root_component()
* @see bp_core_load_template()
* @since 2.0
*/
function dpa_load_template( $templates ) {
global $bp;
// Until BuddyPress gets better template filtering, support GenesisConnect.
if ( function_exists( 'gconnect_locate_template' ) && $templates == array( 'members/single/member-header.php' ) ) {
gconnect_locate_template( $templates, true );
return;
}
$alternative_templates = array();
foreach ( $templates as $template ) {
$uri = explode( '/', $template );
$uri_size = count( $uri );
if ( !is_array( $uri ) || $uri_size < 2 )
continue;
if ( '/' == $uri[0] ) {
unset( $uri[0] );
$uri = array_merge( $uri, array() ); // Reset the keys by merging with an empty array
}
if ( !bp_is_current_component( $uri[0] ) || 'single' != $uri[1] )
continue;
if ( $bp->current_item )
$template_slug = $bp->current_item;
elseif ( BP_MEMBERS_SLUG == $uri[0] && isset( $bp->displayed_user ) )
$template_slug = $bp->displayed_user->userdata->user_login;
else
continue;
$extension = strpos( $uri[$uri_size - 1], '.php' );
if ( false === $extension )
continue;
$template_slug = apply_filters( 'dpa_load_template_slug', $template_slug, $template, $templates );
if ( $template_slug ) {
// Reconstruct the URL with the filename suffix
$uri[$uri_size - 1] = substr( $uri[$uri_size - 1], 0, $extension ) . "-{$template_slug}.php";
$alternative_templates[] = implode( '/', $uri );
}
}
$templates = array_merge( apply_filters( 'dpa_load_template_alternatives', $alternative_templates, $templates ), $templates );
if ( $located_template = apply_filters( 'bp_located_template', locate_template( $templates, false ), $templates, $alternative_templates ) )
load_template( apply_filters( 'bp_load_template', $located_template ) );
}
/**
* Loads the Achievements meta from the settings database
*
* @return array
* @since 2.0
*/
function dpa_get_achievements_meta() {
return apply_filters( 'dpa_get_achievements_meta', get_site_option( 'achievements_meta' ) );
}
/**
* Loads active actions from the database
*
* @global object $bp BuddyPress global settings
* @global wpdb $wpdb WordPress database object
* @return array
* @since 2.0
*/
function dpa_get_active_actions() {
global $bp, $wpdb;
if ( !$actions = wp_cache_get( 'dpa_active_actions', 'dpa' ) ) {
$actions = $wpdb->get_results( "SELECT DISTINCT action.name FROM {$bp->achievements->table_achievements} as achievement, {$bp->achievements->table_actions} as action WHERE achievement.action_id = action.id AND achievement.action_id != -1 AND (achievement.is_active = 1 OR achievement.is_active = 2)" );
wp_cache_set( 'dpa_active_actions', $actions, 'dpa' );
}
return apply_filters( 'dpa_get_active_actions', (array)$actions );
}
/**
* Loads the actions from the database.
*
* @global object $bp BuddyPress global settings
* @global wpdb $wpdb WordPress database object
* @return string
* @since 2.0
*/
function dpa_get_actions() {
global $bp, $wpdb;
if ( !$actions = wp_cache_get( 'dpa_actions', 'dpa' ) ) {
$actions = $wpdb->get_results( "SELECT * FROM {$bp->achievements->table_actions} ORDER BY category, description" );
wp_cache_set( 'dpa_actions', $actions, 'dpa' );
}
return apply_filters( 'dpa_get_actions', (array)$actions );
}
/**
* On Achievement creation, create/update all the relevant metas.
*
* @param DPA_Achievement $achievement
* @since 2.0
*/
function dpa_achievement_creation_update_meta( $achievement ) {
// Create site Achievement meta...
$meta = dpa_get_achievements_meta();
$meta[$achievement->id] = array( 'no_of_unlocks' => 0 );
update_site_option( 'achievements_meta', apply_filters( 'dpa_achievement_creation_update_meta', $meta, $achievement ) );
// Update other metas...
wp_cache_delete( 'dpa_achievements_meta', 'dpa' );
wp_cache_delete( 'dpa_active_actions', 'dpa' );
}
add_action( 'dpa_screen_achievement_create_success', 'dpa_achievement_creation_update_meta', 10, 1 );
/**
* On Achievement edit, update all the relevant metas.
*
* @global object $bp BuddyPress global settings
* @global wpdb $wpdb WordPress database object
* @param DPA_Achievement $new_achievement New version of the Achievement
* @param DPA_Achievement $old_achievement Previous version of the Achievement
* @since 2.0
*/
function dpa_achievement_modification_update_meta( $new_achievement, $old_achievement ) {
global $bp, $wpdb;
// Achievement picture has changed, clear picture url cache.
if ( $new_achievement->picture_id != $old_achievement->picture_id )
wp_cache_delete( 'dpa_achievement_picture_urls', 'dpa' );
// Achievement type has changed, clear user count meta.
if ( $new_achievement->action_id != $old_achievement->action_id ) {
$users = $wpdb->query( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = 'achievements_counters'" );
foreach ( (array)$users as $user_id ) {
$counters = get_user_meta( $user_id, 'achievements_counters', true );
if ( is_array( $counters ) && isset( $counters[$new_achievement->id] ) ) {
unset( $counters[$new_achievement->id] );
update_user_meta( $user_id, 'achievements_counters', $counters );
}
}
}
}
add_action( 'dpa_screen_achievement_edit_success', 'dpa_achievement_modification_update_meta', 10, 2 );
/**
* On Achievement deletion, remove/update all the relevant metas.
*
* @global wpdb $wpdb WordPress database object
* @param int $id Achievement ID
* @since 2.0
*/
function dpa_achievement_deletion_update_meta( $id ) {
global $wpdb;
// Update site Achievement meta...
$meta = dpa_get_achievements_meta();
unset( $meta[$id] );
update_site_option( 'achievements_meta', apply_filters( 'dpa_achievement_deletion_update_meta', $meta, $id ) );
// Update other metas...
$users = $wpdb->query( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = 'achievements_counters'" );
foreach ( (array)$users as $user_id ) {
$counters = get_user_meta( $user_id, 'achievements_counters', true );
if ( is_array( $counters ) && isset( $counters[$id] ) ) {
unset( $counters[$id] );
update_user_meta( $user_id, 'achievements_counters', $counters );
}
}
wp_cache_delete( 'dpa_achievements_meta', 'dpa' );
wp_cache_delete( 'dpa_active_actions', 'dpa' );
dpa_delete_highscore_cache();
}
add_action( 'dpa_achievement_deleted', 'dpa_achievement_deletion_update_meta', 10, 1 );
/**
* Clean up after a user is deleted.
*
* @global object $bp BuddyPress global settings
* @param integer $user_id
* @since 2.0
*/
function dpa_remove_user_data( $user_id ) {
global $bp;
delete_user_meta( $user_id, 'achievements_points' );
delete_user_meta( $user_id, 'achievements_counters' );
dpa_delete_highscore_cache();
bp_core_delete_notifications_from_user( $user_id, $bp->achievements->id, 'new_achievement' );
// Delete achievement unlocks
dpa_delete_achievement_unlocks_for_user( $user_id );
// Delete unlocked achievements count cache
wp_cache_delete( 'dpa_get_total_achievements_count_for_user_' . $user_id, 'dpa' );
// Update site achievement meta
$meta = dpa_get_achievements_meta();
$achievements = DPA_Achievement::get( array( 'type' => 'all' ) );
$achievements = $achievements['achievements'];
foreach ( (array)$achievements as $achievement )
$meta[$achievement->id] = array( 'no_of_unlocks' => dpa_get_total_achievement_unlocked_count( $achievement->id ) );
update_site_option( 'achievements_meta', apply_filters( 'dpa_remove_user_data_update_achievement_meta', $meta, $user_id ) );
do_action( 'dpa_remove_user_data', $user_id );
}
add_action( 'wpmu_delete_user', 'dpa_remove_user_data', 1, 1 );
add_action( 'delete_user', 'dpa_remove_user_data', 1, 1 );
add_action( 'make_spam_user', 'dpa_remove_user_data', 1, 1 );
/**
* This function works around a bug present in BuddyPress 1.2.6 and earlier where the 'make_spam_user' action was
* not firing on non-multisite installs when the user was marked as a spammer from the buddybar.
*
* @param int $user_id Spammer's user ID
* @param bool $is_spam
* @see bp_core_action_set_spammer_status()
* @since 2.0.2
*/
function dpa_user_marked_as_spammer( $user_id, $is_spam ) {
if ( $is_spam )
dpa_remove_user_data( $user_id );
}
add_action( 'bp_core_action_set_spammer_status', 'dpa_user_marked_as_spammer', 10, 2 );
/**
* Deletes highscore cache for all widgets.
*
* @since 2.0
*/
function dpa_delete_highscore_cache() {
$sitewide_widget_options = get_option( 'widget_achievements-sitewide' );
foreach ( (array) $sitewide_widget_options as $widget ) {
if ( empty( $widget ) )
continue;
if ( !empty( $widget->limit ) )
wp_cache_delete( 'dpa_high_scorers_' . apply_filters( 'dpa_widget_limit', $widget->limit ), 'dpa' );
}
}
/**
* When someone unlocks an Achievement, create/update the relevant metas.
*
* @param int $achievement_id
* @param int $user_id
* @since 2.0
*/
function dpa_achievement_unlocked_update_meta( $achievement_id, $user_id ) {
$meta = dpa_get_achievements_meta();
$meta[$achievement_id]['no_of_unlocks'] += apply_filters( 'dpa_achievement_unlocked_update_meta', 1 );
update_site_option( 'achievements_meta', $meta );
wp_cache_delete( 'dpa_achievements_meta', 'dpa' );
dpa_delete_highscore_cache();
}
add_action( 'dpa_achievement_unlocked', 'dpa_achievement_unlocked_update_meta', 5, 2 );
/**
* Restores BuddyPress Groups global settings after the activity stream loop on the Achievement's page.
* Or, in other words, tidies up after a hack. Ahem.
*
* @deprecated 2.1 Not used any more
* @global object $bp BuddyPress global settings
* @see dpa_achievement_activity_filter
* @see dpa_screen_achievement_activity
* @since 2.0
*/
function dpa_activity_screen_restore_settings() {
global $bp;
_deprecated_function( __FUNCTION__, '2.1' );
$bp->groups->id = $bp->achievements->old_groups_id;
if ( bp_is_active( 'groups' ) && isset( $bp->achievements->old_current_group_id ) ) {
$bp->groups->current_group->id = $bp->achievements->old_current_group_id;
$bp->groups->current_group->status = $bp->achievements->old_current_group_status;
} elseif ( bp_is_active( 'groups' ) ) {
$bp->groups->current_group = null;
} else {
$bp->groups = null;
}
}
/**
* Replaces a string in the internationalisation table with a custom value.
*
* @global object $l10n List of domain translated string (gettext_reader) objects
* @param string $find Text to find in the table
* @param string $replace Replacement text
* @since 2.0
*/
function dpa_override_i18n( $find, $replace ) {
global $l10n;
if ( isset( $l10n['buddypress'] ) && isset( $l10n['buddypress']->entries[$find] ) ) {
$l10n['buddypress']->entries[$find]->translations[0] = $replace;
} else {
$mo = new MO();
$mo->add_entry( array( 'singular' => $find, 'translations' => array( $replace ) ) );
if ( isset( $l10n['buddypress'] ) )
$mo->merge_with( $l10n['buddypress'] );
$l10n['buddypress'] = $mo;
}
}
/**
* Changes the 'no activity' message on an achievement's activity page.
*
* @since 2.0
*/
function dpa_achievement_activity_il8n_filter() {
$find = __( 'Sorry, there was no activity found. Please try a different filter.', 'buddypress' ); // Intentionally uses BuddyPress' text domain.
$replace = __( 'Sorry, there was no activity found.', 'dpa' );
dpa_override_i18n( $find, $replace );
}
/**
* Hijacks the activity stream on an achievement's activity page so only achievement items are displayed.
*
* @global object $bp BuddyPress global settings
* @since 2.1
*/
function dpa_achievement_activity_filter( $query_string ) {
global $bp;
if ( bp_is_current_component( $bp->achievements->slug ) && !empty( $bp->current_action ) )
$query_string = 'object=achievements&primary_id=' . $bp->achievements->current_achievement->id;
return $query_string;
}
add_filter( 'bp_dtheme_ajax_querystring', 'dpa_achievement_activity_filter' );
/********************************************************************************
* Achievements
*
* These functions implement the main logic.
*/
/**
* We have to (always) register some actions early due to the BuddyPress load order.
* This won't add extra queries to each page, it'll only add action hooks; the queries are only made
* if the action is called.
*
* The other difference between this and dpa_setup_achievements() is that this one doesn't abort if
* the user is not logged in. If you find a reason to want to hook into this function, then we're
* probably doing something wrong. Let me know what you're trying to do and we'll see if core can be improved.
*
* @since 2.0
*/
function dpa_setup_achievements_early() {
// v2.2 - bp_core_activated_user doesn't work because the user is not logged in when we get to the handling functions
$actions = apply_filters( 'dpa_setup_achievements_early', array( 'bp_core_activated_user', 'pending_to_publish' ) );
foreach ( $actions as $action )
add_action( $action, 'dpa_handle_action_' . $action, 10, 10 );
}
add_action( 'dpa_init', 'dpa_setup_achievements_early' );
/**
* Hooks into the relevant WordPress actions which Achievements need to make its magic work.
*
* @global object $bp BuddyPress global settings
* @since 2.0
*/
function dpa_setup_achievements() {
global $bp;
$actions = apply_filters( 'dpa_setup_achievements', dpa_get_active_actions() );
foreach ( $actions as $action )
add_action( $action->name, 'dpa_handle_action_' . $action->name, 10, 10 );
}
add_action( 'bp_init', 'dpa_setup_achievements', 9 );
/**
* Implements the Achievement actions, and unlocks if criteria met.
*
* @global DPA_Achievement_Template $achievements_template Achievements template tag object
* @global int $blog_id Site ID (variable is from WordPress and hasn't been updated for 3.0; confusing name is confusing)
* @global object $bp BuddyPress global settings
* @param string $name Action name
* @param array $func_args Optional; action's arguments, from func_get_args().
* @param string $type Optional; if the Achievement's action is called from within the loop in this function (i.e. points awarded), set this to "latent" to avoid a single Achievement being awarded mulitple times.
* @see dpa_setup_achievements()
* @since 2.0
* @uses DPA_Achievement
*/
function dpa_handle_action( $name, $func_args=null, $type='' ) {
global $achievements_template, $blog_id, $bp;
do_action( 'dpa_before_handle_action', $name, $func_args, $type );
if ( !$name = apply_filters( 'dpa_handle_action', $name, $func_args, $type ) )
return;
$user_id = apply_filters( 'dpa_handle_action_user_id', $bp->loggedin_user->id, $name, $func_args, $type );
if ( false === $user_id || empty( $user_id ) )
return;
if ( 'latent' == $type ) {
$latent_achievements = DPA_Achievement::get( array( 'user_id' => $user_id, 'type' => 'active_by_action', 'action' => $name ) );
$achievements_template->achievements = array_merge( $achievements_template->achievements, $latent_achievements['achievements'] );
$achievements_template->achievement_count += count( $latent_achievements['achievements'] );
do_action( 'dpa_handle_latent_action', $name, $func_args, $is_latent, $latent_achievements );
// Avoid duplicate invocations of latent Achievement actions
if ( 'dpa_points_incremented' == $name )
remove_action( 'dpa_points_incremented', 'dpa_handle_action_dpa_points_incremented', 10, 10 );
if ( 'dpa_achievement_unlocked' == $name )
remove_action( 'dpa_achievement_unlocked', 'dpa_handle_action_dpa_achievement_unlocked', 10, 10 );
return;
}
// This is to support plugins which use the 'dpa_handle_action_user_id' filter to return an array of user IDs.
if ( is_array( $user_id ) )
$user_ids = $user_id;
else
$user_ids = array( $user_id );
foreach ( $user_ids as $user_id ) {
if ( dpa_has_achievements( array( 'user_id' => $user_id, 'type' => 'active_by_action', 'action' => $name ) ) ) {
while ( dpa_achievements() ) {
dpa_the_achievement();
$site_id = apply_filters( 'dpa_handle_action_site_id', dpa_get_achievement_site_id(), $name, $func_args, $type, $user_id );
if ( false === $site_id )
continue;
$site_is_valid = false;
if ( !is_multisite() || $site_id < 1 || $blog_id == $site_id )
$site_is_valid = true;
$group_is_valid = false;
if ( dpa_get_achievement_group_id() < 1 || dpa_is_group_achievement_valid( $name, $func_args, $user_id ) )
$group_is_valid = true;
$site_is_valid = apply_filters( 'dpa_handle_action_site_is_valid', $site_is_valid, $name, $func_args, $type, $user_id );
$group_is_valid = apply_filters( 'dpa_handle_action_group_is_valid', $group_is_valid, $name, $func_args, $type, $user_id );
if ( $site_is_valid && $group_is_valid )
dpa_maybe_unlock_achievement( $user_id );
}
}
}
do_action( 'dpa_after_handle_action', $name, $func_args, $type );
$achievements_template = null;
}
/**
* Checks if an Achievement's criteria has been met, and if it has, unlock the Achievement.
* Achievements with an action count of 0 are, effectively, unlocked each time but only
* the points are added to user's total.
*
* @global object $bp BuddyPress global settings
* @param int $user_id
* @param string $skip_validation Set to 'force' to skip Achievement validation, e.g. the Achievement is unlocked regardless of its criteria.
* @since 2.0
*/
function dpa_maybe_unlock_achievement( $user_id, $skip_validation='' ) {
global $bp;
$action_count = dpa_get_achievement_action_count();
if ( dpa_is_achievement_unlocked() && $action_count > 0 )
return;
$counters = get_user_meta( $user_id, 'achievements_counters', true );
$achievement_id = dpa_get_achievement_id();
$skip_validation = ( 'force' == $skip_validation );
// No point saving a count of 1 if the action_count is 1.
$unlocked = false;
if ( 0 === $action_count || 1 == $action_count ) {
$unlocked = true;
} elseif ( !$skip_validation ) {
if ( !$counters && !is_array( $counters ) )
$counters = array();
$counters[$achievement_id] += apply_filters( 'dpa_achievement_counter_increment_by', 1 );
update_user_meta( $user_id, 'achievements_counters', $counters );
do_action( 'dpa_achievement_counter_incremented' );
}
if ( !$unlocked && ( $skip_validation || $counters[$achievement_id] >= $action_count ) ) {
if ( isset( $counters[$achievement_id] ) ) {
unset( $counters[$achievement_id] );
update_user_meta( $user_id, 'achievements_counters', $counters );
}
$unlocked = true;
}
// Update points, insert unlocked record into DB and send notifications.
if ( $unlocked ) {
dpa_points_increment( dpa_get_achievement_points(), $user_id );
// Let Achievements with action_count == 0, which have already been unlocked, only increment points.
if ( dpa_is_achievement_unlocked() && 0 === $action_count )
return;
dpa_unlock_achievement( $user_id );
if ( apply_filters( 'dpa_achievement_unlocked_tell_user', true, $achievement_id, $user_id ) ) {
bp_core_add_notification( $achievement_id, $user_id, $bp->achievements->id, 'new_achievement', $user_id );
dpa_record_activity( $user_id, dpa_format_activity( $user_id, $achievement_id ), $achievement_id );
}
do_action( 'dpa_achievement_unlocked', $achievement_id, $user_id );
}
}
/**
* Use this to unlock an Achievement for the specified user, ignoring the Achievement's criteria.
*
* @global DPA_Achievement_Template $achievements_template Achievements template tag object
* @global object $bp BuddyPress global settings
* @param int $user_id Optional
* @param string $achievement_slug Optional; defaults to the Achievement currently being viewed
* @since 2.0
* @uses DPA_Achievement
*/
function dpa_force_unlock_achievement( $user_id=0, $achievement_slug='' ) {
global $achievements_template, $bp;
if ( !$user_id )
$user_id = $bp->loggedin_user->id;
if ( !$achievement_slug )
$achievement_slug = apply_filters( 'dpa_get_achievement_slug', $bp->current_item );
if ( !empty( $achievements_template->achievement ) )
$original_achievement = $achievements_template->achievement;
if ( !empty( $achievements_template->achievements ) )
$original_achievements = $achievements_template->achievements;
// Can't use $bp->achievements_current_achievement as that achievement's populate_extras meta is for the logged-in user (or doesn't exist).
$achievements_template->achievement = new DPA_Achievement( array( 'type' => 'single', 'slug' => $achievement_slug, 'user_id' => $user_id ) );
$achievements_template->achievements = array( $achievements_template->achievement );
dpa_maybe_unlock_achievement( $user_id, 'force' );
if ( isset( $original_achievements ) )
$achievements_template->achievements = $original_achievements;
if ( isset( $original_achievement ) )
$achievements_template->achievement = $original_achievement;
}
/**
* Implements the comment_post action for WordPress
*
* @since 2.0
*/
function dpa_handle_action_comment_post() { $func_get_args = func_get_args(); dpa_handle_action( 'comment_post', $func_get_args ); }
/**
* Implements the draft_to_publish (posts, pages) action for WordPress
*
* @since 2.0
*/
function dpa_handle_action_draft_to_publish() { $func_get_args = func_get_args(); dpa_handle_action( 'draft_to_publish', $func_get_args ); }
/**
* Implements the pending_to_publish (posts, pages) action for WordPress. Bit of a kludge that works in conjunction with dpa_handle_action_draft_to_publish().
*
* @see dpa_handle_action_draft_to_publish()
* @since 2.0
*/
function dpa_handle_action_pending_to_publish() { $func_get_args = func_get_args(); dpa_handle_action( 'draft_to_publish', $func_get_args ); }
/**
* Implements the friends_friendship_requested action for BuddyPress
*
* @since 2.0
*/
function dpa_handle_action_friends_friendship_requested() { $func_get_args = func_get_args(); dpa_handle_action( 'friends_friendship_requested', $func_get_args ); }
/**
* Implements the groups_invite_user action for BuddyPress
*
* @since 2.0
*/
function dpa_handle_action_groups_invite_user() { $func_get_args = func_get_args(); dpa_handle_action( 'groups_invite_user', $func_get_args ); }
/**
* Implements the groups_join_group action for BuddyPress
*
* @since 2.0
*/
function dpa_handle_action_groups_join_group() { $func_get_args = func_get_args(); dpa_handle_action( 'groups_join_group', $func_get_args ); }
/**
* Implements the groups_promoted_member action for BuddyPress
*
* @since 2.0
*/
function dpa_handle_action_groups_promoted_member() { $func_get_args = func_get_args(); dpa_handle_action( 'groups_promoted_member', $func_get_args ); }
/**
* Implements the groups_demoted_member action for BuddyPress
*
* @since 2.0
*/
function dpa_handle_action_groups_demoted_member() { $func_get_args = func_get_args(); dpa_handle_action( 'groups_demoted_member', $func_get_args ); }