1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * Clear the xprofile_group cache when a new field is created, or when an existing field is deleted |
---|
5 | * |
---|
6 | * See: http://trac.buddypress.org/ticket/1332 |
---|
7 | */ |
---|
8 | function nm_xprofile_clear_profile_group_cache($field) { |
---|
9 | $group_obj = (object) array( 'id' => $field->group_id ); |
---|
10 | xprofile_clear_profile_groups_object_cache( $group_obj ); |
---|
11 | wp_cache_delete( 'xprofile_group_' . $field->group_id, 'bp' ); |
---|
12 | } |
---|
13 | add_action('xprofile_fields_saved_field', 'nm_xprofile_clear_profile_group_cache', 10, 1); |
---|
14 | add_action('xprofile_fields_deleted_field', 'nm_xprofile_clear_profile_group_cache', 10, 1); |
---|
15 | |
---|
16 | /** |
---|
17 | * Check the profile data cache is correct for a user, update it when necessary |
---|
18 | * |
---|
19 | * See: http://trac.buddypress.org/ticket/1332 |
---|
20 | */ |
---|
21 | function nm_xprofile_group_fields($fields, $group_id) { |
---|
22 | global $bp, $profile_template; |
---|
23 | |
---|
24 | if ( is_a($profile_template, 'BP_XProfile_Data_Template') && isset($profile_template->group) ) { |
---|
25 | |
---|
26 | // Is there a difference? |
---|
27 | $diff = array_udiff($profile_template->group->fields, $fields, 'nm_profile_cache_diff'); |
---|
28 | |
---|
29 | if ( ! empty($diff) ) { |
---|
30 | |
---|
31 | // The cached profile data is out-of-date, so replace it, and return the correct data |
---|
32 | // The fields defined by $profile_template->group->fields should be correct |
---|
33 | |
---|
34 | $fields = array(); |
---|
35 | |
---|
36 | // Don't try and fetch any existing profile data if we are using this loop on the registration page |
---|
37 | $get_data = ( !bp_is_register_page() ) ? true : false; |
---|
38 | |
---|
39 | // Update the cache for the displayed user |
---|
40 | $user_id = $bp->displayed_user->id; |
---|
41 | |
---|
42 | for ( $i = 0; $i < count($profile_template->group->fields); $i++ ) { |
---|
43 | |
---|
44 | $field = new BP_XProfile_Field( $profile_template->group->fields[$i]->id, $user_id, $get_data ); |
---|
45 | $fields[$i] = $field; |
---|
46 | |
---|
47 | } |
---|
48 | |
---|
49 | wp_cache_replace( 'xprofile_fields_' . $group_id . '_' . $user_id, $fields, 'bp' ); |
---|
50 | |
---|
51 | } |
---|
52 | |
---|
53 | } |
---|
54 | |
---|
55 | return $fields; |
---|
56 | } |
---|
57 | add_filter('xprofile_group_fields', 'nm_xprofile_group_fields', 10, 2); |
---|
58 | |
---|
59 | /** |
---|
60 | * Calculates the diff between the profile field cache and the profile data cache |
---|
61 | * used by nm_xprofile_group_fields to determine whether we should update the |
---|
62 | * profile data cache |
---|
63 | */ |
---|
64 | function nm_profile_cache_diff($a, $b) { |
---|
65 | $a = (object)$a; |
---|
66 | $b = (object)$b; |
---|
67 | if ($a->id == $b->id && $a->type == $b->type) { |
---|
68 | return 0; |
---|
69 | } elseif ($a->id > $b->id || $a->type > $b->type) { |
---|
70 | return 1; |
---|
71 | } else { |
---|
72 | return -1; |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * Expire the profile data cache for a user after the profile is updated |
---|
78 | * |
---|
79 | * See: http://trac.buddypress.org/ticket/1332 |
---|
80 | */ |
---|
81 | function nm_xprofile_data_after_save(&$profile_data) { |
---|
82 | global $bp, $wpdb; |
---|
83 | $sql = $wpdb->prepare("SELECT group_id FROM `{$bp->profile->table_name_fields}` WHERE id = %d", $profile_data->field_id); |
---|
84 | $group_id = $wpdb->get_var($sql); |
---|
85 | // Expire the cache |
---|
86 | wp_cache_delete( 'xprofile_fields_' . $group_id . '_' . $profile_data->user_id, 'bp' ); |
---|
87 | return true; |
---|
88 | } |
---|
89 | add_action('xprofile_data_after_save', 'nm_xprofile_data_after_save'); |
---|
90 | |
---|
91 | ?> |
---|