class-buddypress.php
3.33 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
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if(class_exists( 'BuddyPress' )) {
class WPF_BuddyPress extends WPF_Integrations_Base {
/**
* Gets things started
*
* @access public
* @return void
*/
public function init() {
add_filter( 'wpf_redirect_post_id', array( $this, 'get_bb_page_id' ) );
add_filter( 'wpf_meta_fields', array( $this, 'set_contact_field_names' ), 30 );
add_filter( 'wpf_pulled_user_meta', array( $this, 'pulled_user_meta' ), 10, 2 );
add_action( 'xprofile_updated_profile', array( $this, 'push_profile_data'), 10, 3);
add_filter( 'wpf_user_register', array( $this, 'user_register' ), 10, 2 );
}
/**
* Gets page ID for BBP core pages
*
* @access public
* @return int Post ID
*/
public function get_bb_page_id( $post_id ) {
if($post_id != 0)
return $post_id;
global $bp;
$post = get_page_by_path( $bp->unfiltered_uri[0] );
if(!empty($post))
return $post->ID;
return $post_id;
}
/**
* Loads XProfile fields for inclusion in Contact Fields table
*
* @access public
* @return array Meta Fields
*/
public function set_contact_field_names( $meta_fields ) {
if(!class_exists('BP_XProfile_Data_Template'))
return $meta_fields;
$r = array(
'user_id' => get_current_user_id(),
'member_type' => 'any',
'fetch_fields' => true,
'fetch_field_data' => true,
);
$profile_template = new BP_XProfile_Data_Template( $r );
foreach($profile_template->groups as $group) {
if(!empty($group->fields)) {
foreach($group->fields as $field) {
$meta_fields['bbp_field_' . $field->id] = array('label' => $field->name, 'type' => 'text');
}
}
}
return $meta_fields;
}
/**
* Triggered when BBP profile is updated
*
* @access public
* @return void
*/
public function push_profile_data( $user_id, $posted_field_ids, $errors ) {
$update_data = array();
foreach($_POST as $field_id => $field_data) {
$update_data['bbp_' . $field_id] = $field_data;
}
wp_fusion()->user->push_user_meta( $user_id, $update_data );
}
/**
* Triggered when new user registered through BBP
*
* @access public
* @return array Post Data
*/
public function user_register( $post_data, $user_id ) {
$post_data['user_login'] = $post_data['signup_username'];
$post_data['user_pass'] = $post_data['signup_password'];
$post_data['user_email'] = $post_data['signup_email'];
// Xprofile fields
foreach($post_data as $field_id => $value) {
if (strpos($field_id, 'field_') !== false)
$post_data['bbp_' . $field_id] = $value;
}
return $post_data;
}
/**
* Triggered when user meta is loaded from the CRM
*
* @access public
* @return array User Meta
*/
public function pulled_user_meta( $user_meta, $user_id ) {
if(!class_exists('BP_XProfile_ProfileData'))
return $user_meta;
foreach($user_meta as $key => $value) {
if (strpos($key, 'bbp_field_') !== false) {
$field_id = str_replace('bbp_field_', '', $key);
$field = new BP_XProfile_ProfileData( $field_id, $user_id );
$field->value = $value;
$field->save();
unset($user_meta[$key]);
}
}
return $user_meta;
}
}
new WPF_BuddyPress;
}