class-buddypress.php
5.36 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
<?php
defined('ABSPATH') or exit;
/**
* Class MC4WP_BuddyPress_Integration
*
* @ignore
*/
class MC4WP_BuddyPress_Integration extends MC4WP_User_Integration
{
/**
* @var string
*/
public $name = 'BuddyPress';
/**
* @var string
*/
public $description = 'Subscribes users from BuddyPress registration forms.';
/**
* Add hooks
*/
public function add_hooks()
{
if (! $this->options['implicit']) {
add_action('bp_before_registration_submit_buttons', array( $this, 'output_checkbox' ), 20);
}
if (is_multisite()) {
/**
* Multisite signups are a two-stage process - the data is first added to
* the 'signups' table and then converted into an actual user during the
* activation process.
*
* To avoid all signups being subscribed to the Mailchimp list until they
* have responded to the activation email, a value is stored in the signup
* usermeta data which is retrieved on activation and acted upon.
*/
add_filter('bp_signup_usermeta', array( $this, 'store_usermeta' ), 10, 1);
add_action('bp_core_activated_user', array( $this, 'subscribe_from_usermeta' ), 10, 3);
} else {
add_action('bp_core_signup_user', array( $this, 'subscribe_from_form' ), 10, 4);
}
/**
* There is one further issue to consider, which is that many BuddyPress
* installs have a user moderation plugin (e.g. BP Registration Options)
* installed. This is because email activation on itself is sometimes not enough to ensure
* that user signups are not spammers. There should therefore be a way for
* plugins to delay the Mailchimp signup process.
*
* Plugins can hook into the 'mc4wp_integration_buddypress_should_subscribe' filter to prevent
* subscriptions from taking place:
*
* add_filter( 'mc4wp_integration_buddypress_should_subscribe', '__return_false' );
*
* The plugin would then then call:
*
* do_action( 'mc4wp_integration_buddypress_subscribe_user', $user_id );
*
* to perform the subscription at a later point.
*/
add_action('mc4wp_integration_buddypress_subscribe_user', array( $this, 'subscribe_buddypress_user' ), 10, 1);
}
/**
* Subscribes from BuddyPress Registration Form.
*
* @param int $user_id
* @param string $user_login
* @param string $user_password
* @param string $user_email
* @return bool
*/
public function subscribe_from_form($user_id, $user_login, $user_password, $user_email)
{
if (! $this->triggered()) {
return false;
}
$subscribe = true;
/**
* Allow other plugins to prevent the Mailchimp sign-up.
*
* @param bool $subscribe False does not subscribe the user.
* @param int $user_id The user ID to subscribe
*/
$subscribe = apply_filters('mc4wp_integration_buddypress_should_subscribe', $subscribe, $user_id);
if (! $subscribe) {
return false;
}
return $this->subscribe_buddypress_user($user_id);
}
/**
* Stores subscription data from BuddyPress Registration Form.
*
* @param array $usermeta The existing usermeta
* @return array $usermeta The modified usermeta
*/
public function store_usermeta($usermeta)
{
// only add meta if triggered (checked)
if ($this->triggered()) {
$usermeta['mc4wp_subscribe'] = '1';
}
return $usermeta;
}
/**
* Subscribes from BuddyPress Activation.
*
* @param int $user_id The activated user ID
* @param string $key the activation key (not used)
* @param array $userdata An array containing the activated user data
* @return bool
*/
public function subscribe_from_usermeta($user_id, $key, $userdata)
{
// sanity check
if (empty($user_id)) {
return false;
}
// bail if our usermeta key is not switched on
$meta = ( isset($userdata['meta']) ) ? $userdata['meta'] : array();
if (empty($meta['mc4wp_subscribe'])) {
return false;
}
$subscribe = true;
/**
* @ignore Documented elsewhere, see MC4WP_BuddyPress_Integration::subscribe_from_form.
*/
$subscribe = apply_filters('mc4wp_integration_buddypress_should_subscribe', $subscribe, $user_id);
if (! $subscribe) {
return false;
}
return $this->subscribe_buddypress_user($user_id);
}
/**
* Subscribes a user to Mailchimp list(s).
*
* @param int $user_id The user ID to subscribe
* @return bool
*/
public function subscribe_buddypress_user($user_id)
{
$user = get_userdata($user_id);
// was a user found with the given ID?
if (! $user instanceof WP_User) {
return false;
}
// gather email address and name from user
$data = $this->user_merge_vars($user);
return $this->subscribe($data, $user_id);
}
/* End BuddyPress functions */
/**
* @return bool
*/
public function is_installed()
{
return class_exists('BuddyPress');
}
}