This repository has been archived by the owner on Jul 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbn-auto-join-group.php
251 lines (212 loc) · 12 KB
/
bn-auto-join-group.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
<?php
/**
* Plugin Name: Auto Group Join
* Plugin Description: Members automatically are joined to groups based on a profile field
* Author: Brent Layman
* Author URI: http://buglenotes.com
* Plugin URI: http://buglenotes.com
* Version: 1.0
*/
/*
Place this folder in your plugins folder. In the admin panel, configure under "Site Admin -> Profile->Group Links"
If you know the profile field name and the group name are not exact matches, use the group_pre_regex and group_post_regex fields.
a. For example, say you have a profile field for "Graduation Year" where an option is "1987". The corresponding group is "USMA 1987". So, in the
group_pre_regex field put "USMA%". (yes ... put the % into the db field).
b. Another example, say you have a profile field for "Type of Fishing" where an option is "Fly". The corresponding group is "Fly Fishing". So, in the
group_post_regex field put "%Fishing".
c. If the profile options exactly match the group names, leave group_pre_regex and group_post_regex blank.
*/
function addHeaderCode() {
echo '<link type="text/css" rel="stylesheet" href="' . plugins_url('css/bn-auto-join-group.css', __FILE__).'" />' . "\n";
}
function auto_join_group_check_installed() {
global $wpdb, $bp;
if ( is_site_admin() ) {
/* Need to check db tables exist, activate hook no-worky in mu-plugins folder. */
if ( !$wpdb->get_var("SHOW COLUMNS FROM {$bp->profile->table_name_fields} LIKE 'group_link'") ) {
$mysql = "ALTER TABLE {$bp->profile->table_name_fields} ADD `group_link` BINARY NOT NULL DEFAULT '0'";
$wpdb->query($mysql);
}
if ( !$wpdb->get_var("SHOW COLUMNS FROM {$bp->profile->table_name_fields} LIKE 'group_pre_regex'") ) {
$mysql = "ALTER TABLE {$bp->profile->table_name_fields} ADD `group_pre_regex` varchar(100) NOT NULL";
$wpdb->query($mysql);
}
if ( !$wpdb->get_var("SHOW COLUMNS FROM {$bp->profile->table_name_fields} LIKE 'group_post_regex'") ) {
$mysql = "ALTER TABLE {$bp->profile->table_name_fields} ADD `group_post_regex` varchar(100) NOT NULL";
$wpdb->query($mysql);
}
}
}
add_action('admin_head', 'addHeaderCode');
add_action( 'admin_menu', 'auto_join_group_check_installed');
// Now that we have the fields we need, let's do the admin menu
function auto_join_group_plugin_menu() {
add_submenu_page( 'bp-general-settings', __("Group Auto Join", 'buddypress'), __("Group Auto Join", 'buddypress'), 'manage_options', 'auto_join_group_plugin_options', "auto_join_group_plugin_options" );
}
// Here's the actual admin menu
function auto_join_group_plugin_options() {
global $wpdb, $bp;
if ( isset($_GET['pre_regex']) && isset($_GET['post_regex']) && isset($_GET['mode']) && isset($_GET['field_id']) && 'save' == $_GET['mode'] ) {
// save the changes to the database!
if (isset($_GET['link'])) { $group_link = 1; } else { $group_link = 0; }
$sql = $wpdb->prepare("UPDATE {$bp->profile->table_name_fields} SET group_link = %d, group_pre_regex = %s, group_post_regex = %s WHERE id = %d", $group_link, $_GET['pre_regex'], $_GET['post_regex'], $_GET['field_id']);
if ( !$wpdb->query($sql) ) {
echo "<strong>Update Failed (or no fields were changed)</strong><hr />";
} else {
echo "<strong>Update Successful!</strong><hr />";
// Now ... let's link existing users to the group!!!!
// get list of users
if (isset($_GET['link'])) {
$users = $wpdb->get_results("SELECT user_id FROM {$bp->profile->table_name_data} WHERE field_id = " . $_GET['field_id']);
foreach ($users as $user) {
bn_auto_group_join($user->user_id, 'myid');
}
}
}
}
if ( isset($_GET['mode']) && isset($_GET['all_users_group']) && 'save_allusers' == $_GET['mode'] ) {
update_option('gaj_all_users_group ', $_GET['all_users_group']);
$all_users_message = "All users group updated";
}
else {
$all_users_message = '';
}
if ( isset($_GET['mode']) && isset($_GET['field_id']) && 'edit' == $_GET['mode'] ) {
// edit one of the profile linking options
$profiles = $wpdb->get_results("SELECT name, id, group_link, group_pre_regex, group_post_regex FROM {$bp->profile->table_name_fields} WHERE id = " . $_GET['field_id']);
?>
<form method="get" name="auto-join" action="admin.php">
<input type="hidden" name="field_id" value="<?php echo $_GET['field_id']; ?>">
<input type="hidden" name="mode" value="save">
<input type="hidden" name="page" value="auto_join_group_plugin_options">
<table class="form-table"><?php
foreach ($profiles as $profile) { ?>
<tr><td><strong>Profile Field Name:</strong></td><td align="left"><strong><?php echo $profile->name; ?></strong></td></tr>
<tr><td>Link to Groups:</td><td align="left"><input type="checkbox" name="link" <?php if ($profile->group_link == 0) { echo "No"; } else { echo "checked"; } ?>></td></tr>
<tr><td>Pre Matching:</td><td align="left"><input type="text" name="pre_regex" maxlength="100" size="15" value="<?php echo $profile->group_pre_regex; ?>"></td></tr>
<tr><td>Post Matching:</td><td align="left"><input type="text" name="post_regex" maxlength="100" size="15" value="<?php echo $profile->group_post_regex; ?>"></td></tr>
<tr><td colspan="2" align="left"><input type="submit" value="Save"></td></tr>
<tr><td colspan="2" align="left"><strong>DIRECTIONS</strong><br />
<strong>1.</strong> Check the "Link to Groups" box if you want to try to automatically join members to groups based on this profile field.<br /><br />
<strong>2.</strong> If you know the profile field name and the group name are not exact matches, use the group_pre_regex and group_post_regex fields.<br />
<strong>a.</strong> For example, say you have a profile field for "Graduation Year" where an option is "1987". The corresponding group is "USMA 1987". So, in the
"Pre Matching" field put "USMA%". (yes ... put the % into the db field).<br />
<strong>b.</strong> Another example, say you have a profile field for "Type of Fishing" where an option is "Fly". The corresponding group is "Fly Fishing". So, in the
"Post Matching" field put "%Fishing".<br />
<strong>c.</strong> If the profile options exactly match the group names, leave "Pre Matching" and "Post Matching" blank.<br /><br />
<strong>3.</strong> Once you save your changes, if the "Link to Groups" box is checked, all existing wpmu members will be added to the matching groups!
</td></tr>
<?php
}
?></table><?php
} else {
// get profile fields with group linking
$profiles = $wpdb->get_results("SELECT name, id, group_link, group_pre_regex, group_post_regex FROM {$bp->profile->table_name_fields} WHERE parent_id = 0 AND (type = 'selectbox' OR type = 'textbox')");
echo "<table class=\"widefat\"><thead><tr><td colspan='4''><strong>Click edit</strong> on the Profile field you'd like to link to a group.<br /><br />
<strong>NOTE</strong> - currently only 'selectbox' and 'textbox' type profile fields are supported!</td></tr>
<tr><td colspan='5'> </td></tr>
<tr><th>Profile Field Name</th><th>Linked?</th><th>Pre Matching</th><th>Post Matching</th><th> </th></tr>";
foreach ($profiles as $profile) { ?>
<tr>
<td><?php echo $profile->name; ?></td>
<td style="text-align:center;"><?php echo ($profile->group_link == 1) ? '<strong>YES</strong>' : 'No'; ?></td>
<td><?php echo $profile->group_pre_regex; ?></td>
<td><?php echo $profile->group_post_regex; ?></td>
<td><a href="admin.php?page=auto_join_group_plugin_options&field_id=<?php echo $profile->id; ?>&mode=edit"><?php _e( 'Edit', 'buddypress' ); ?></a></td>
</tr>
<?php
}
?>
</table>
<h3>All Users</h3>
<p>If you wish to add all users who register to a particular group, select it here</p>
<?php
if(!empty($all_users_message)) {
echo "<p><strong>$all_users_message</strong></p>";
}
?>
<form method="get" name="auto-join" action="admin.php">
<label for="all_users_group">Group:</label>
<select id="all_users_group" name="all_users_group">
<option value="0">None</option>
<?php
$current_setting = get_option('gaj_all_users_group');
$groupmatches = $wpdb->get_results("SELECT * FROM {$bp->groups->table_name}");
foreach ($groupmatches as $groupmatch) {
?><option value="<?php echo $groupmatch->id; ?>"<?php if($groupmatch->id == $current_setting) echo " selected"; ?>><?php echo $groupmatch->name; ?></option>
<?php
}
?>
</select>
<p class="submit">
<input type="submit" name="Submit" class="button-primary" value="Save All Users Changes">
<input type="hidden" name="mode" value="save_allusers">
<input type="hidden" name="page" value="auto_join_group_plugin_options">
</p>
</form>
<?php
}
}
add_action('admin_menu', 'auto_join_group_plugin_menu', 50);
function check_and_add_to_group($userid, $group_id)
{
// see if the user is already in the group
if ( !BP_Groups_Member::check_is_member( $userid, $group_id ) ) {
// make sure the user isn't banned from the group!
if ( !groups_is_user_banned( $userid, $group_id ) ) {
// add the group already!
$user_id = $userid;
if ( groups_check_user_has_invite( $user_id, $group_id ) ) {
groups_delete_invite( $user_id, $group_id );
}
$new_member = new bp_groups_member;
$new_member->group_id = $group_id;
$new_member->inviter_id = 0;
$new_member->user_id = $user_id;
$new_member->is_admin = 0;
$new_member->user_title = '';
$new_member->date_modified = time();
$new_member->is_confirmed = 1;
if ( !$new_member->save() ) {
return false;
}
// Should I add this to the activity stream? left off for now
/* Modify group meta */
groups_update_groupmeta( $group_id, 'total_member_count', (int) groups_get_groupmeta( $group_id, 'total_member_count') + 1 );
groups_update_groupmeta( $group_id, 'last_activity', time() );
}
}
return false;
}
// automatically join users to groups based on profile fields when the user modifies his profile or activates his account
function bn_auto_group_join($userid, $x = 0, $y = 0){
global $wpdb, $bp, $user_id;
// get the user id
if ($bp->loggedin_user->id && $x !== 'myid') {
$userid = $bp->loggedin_user->id ; // current user if logged in. On activation, userid sent with do_action
}
// All users
if($all_users_group = get_option('gaj_all_users_group')) {
check_and_add_to_group($userid, $all_users_group);
}
// get profile fields with group linking
$profiles = $wpdb->get_results("SELECT * FROM {$bp->profile->table_name_fields} WHERE group_link = 1");
foreach ($profiles as $profile) {
// see what the person has for that field
$profileinfo = $wpdb->get_results("SELECT value FROM {$bp->profile->table_name_data} WHERE user_id = $userid AND field_id = $profile->id");
foreach ($profileinfo as $profilevalue ) {
// see if we can match the group
$groupmatches = $wpdb->get_results("SELECT * FROM {$bp->groups->table_name} WHERE name like '$profile->group_pre_regex$profilevalue->value$profile->group_post_regex'");
foreach ($groupmatches as $groupmatch) {
if(!check_and_add_to_group($userid, $groupmatch->id)) {
return false;
}
}
}
}
}
// below hook is located in bp-xprofile.php. Add user to group when profile is edited
add_action( 'xprofile_updated_profile', 'bn_auto_group_join', 1, 1 );
// below hook is located in wpmu-functions.php. Add user to group when account is activated
add_action( 'bp_core_activated_user', 'bn_auto_group_join', 12, 3 );
?>