-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbp-process.php
More file actions
411 lines (342 loc) · 10.4 KB
/
Copy pathbp-process.php
File metadata and controls
411 lines (342 loc) · 10.4 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
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
<?php
/**
* BuddyPress Demo Data Import Process
*
* @package BuddyX_Theme_Demo_Importer
* @since 3.0.0
*/
// Prevent direct access
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Import demo users into BuddyPress
*
* @since 3.0.0
* @return array Array of created user IDs
*/
function buddyx_bp_import_users() {
$users = array();
$users_data = require __DIR__ . '/demos/demo-bp-data/users.php';
foreach ( $users_data as $user ) {
// Use example.com domain for demo emails
$email = str_replace( '@gmail.com', '@example.com', $user['email'] );
$user_id = wp_insert_user(
array(
'user_login' => $user['login'],
'user_pass' => $user['pass'],
'display_name' => $user['display_name'],
'user_email' => $email,
'user_registered' => buddyx_bp_get_random_date( 45, 1 ),
)
);
if ( is_wp_error( $user_id ) ) {
continue;
}
// Set xProfile name field
if ( bp_is_active( 'xprofile' ) ) {
xprofile_set_field_data( 1, $user_id, $user['display_name'] );
}
// Set user meta
$name = explode( ' ', $user['display_name'] );
update_user_meta( $user_id, 'first_name', $name[0] );
update_user_meta( $user_id, 'last_name', isset( $name[1] ) ? $name[1] : '' );
update_user_meta( $user_id, 'buddyx_bp_user', 1 );
// Update user activity
bp_update_user_last_activity( $user_id, buddyx_bp_get_random_date( 5 ) );
// Set notification preferences
bp_update_user_meta( $user_id, 'notification_messages_new_message', 'no' );
bp_update_user_meta( $user_id, 'notification_friends_friendship_request', 'no' );
bp_update_user_meta( $user_id, 'notification_friends_friendship_accepted', 'no' );
$users[] = $user_id;
}
// Save imported user IDs
if ( ! empty( $users ) ) {
bp_update_option( 'buddyx_bp_imported_user_ids', $users );
}
return $users;
}
/**
* Import extended profile fields and data
*
* @since 3.0.0
* @return int Number of profile fields populated
*/
function buddyx_bp_import_users_profile() {
$count = 0;
if ( ! bp_is_active( 'xprofile' ) ) {
return $count;
}
$data = array();
$groups = array();
$xprofile_structure = require __DIR__ . '/demos/demo-bp-data/xprofile_structure.php';
// Import profile groups
foreach ( $xprofile_structure as $group_type => $group_data ) {
$group_id = xprofile_insert_field_group(
array(
'name' => $group_data['name'],
'description' => $group_data['desc'],
)
);
$groups[] = $group_id;
// Import fields
foreach ( $group_data['fields'] as $field_type => $field_data ) {
$field_id = xprofile_insert_field(
array(
'field_group_id' => $group_id,
'parent_id' => 0,
'type' => $field_type,
'name' => $field_data['name'],
'description' => $field_data['desc'],
'is_required' => $field_data['required'],
'order_by' => 'custom',
)
);
if ( $field_id ) {
// Set field meta
bp_xprofile_update_field_meta( $field_id, 'default_visibility', $field_data['default-visibility'] );
bp_xprofile_update_field_meta( $field_id, 'allow_custom_visibility', $field_data['allow-custom-visibility'] );
$data[ $field_id ]['type'] = $field_type;
// Import field options
if ( ! empty( $field_data['options'] ) ) {
foreach ( $field_data['options'] as $option ) {
$option_id = xprofile_insert_field(
array(
'field_group_id' => $group_id,
'parent_id' => $field_id,
'type' => 'option',
'name' => $option['name'],
'can_delete' => true,
'is_default_option' => $option['is_default_option'],
'option_order' => $option['option_order'],
)
);
$data[ $field_id ]['options'][ $option_id ] = $option['name'];
}
} else {
$data[ $field_id ]['options'] = array();
}
}
}
}
$xprofile_data = require __DIR__ . '/demos/demo-bp-data/xprofile_data.php';
$users = buddyx_bp_get_random_users_ids( 0 );
// Import profile field data for each user
foreach ( $users as $user_id ) {
foreach ( $data as $field_id => $field_data ) {
switch ( $field_data['type'] ) {
case 'datebox':
case 'textarea':
case 'number':
case 'textbox':
case 'url':
case 'selectbox':
case 'radio':
if ( xprofile_set_field_data( $field_id, $user_id, $xprofile_data[ $field_data['type'] ][ array_rand( $xprofile_data[ $field_data['type'] ] ) ] ) ) {
$count++;
}
break;
case 'checkbox':
case 'multiselectbox':
if ( xprofile_set_field_data( $field_id, $user_id, explode( ',', $xprofile_data[ $field_data['type'] ][ array_rand( $xprofile_data[ $field_data['type'] ] ) ] ) ) ) {
$count++;
}
break;
}
}
}
// Save imported group IDs
if ( ! empty( $groups ) ) {
bp_update_option( 'buddyx_bp_imported_user_xprofile_ids', $groups );
}
return $count;
}
/**
* Import user activity posts
*
* @since 3.0.0
* @return int Number of activity items created
*/
function buddyx_bp_import_users_activity() {
$count = 0;
if ( ! bp_is_active( 'activity' ) ) {
return $count;
}
$users = buddyx_bp_get_random_users_ids( 0 );
$activity = require __DIR__ . '/demos/demo-bp-data/activity.php';
for ( $i = 0; $i < 75; $i++ ) {
$user = $users[ array_rand( $users ) ];
$content = $activity[ array_rand( $activity ) ];
$bp_activity_id = bp_activity_post_update( array(
'user_id' => $user,
'content' => $content
) );
if ( $bp_activity_id ) {
$bp_activity = new BP_Activity_Activity( $bp_activity_id );
$bp_activity->date_recorded = buddyx_bp_get_random_date( 44 );
if ( $bp_activity->save() ) {
$count++;
}
}
}
return $count;
}
/**
* Import friend connections between users
*
* @since 3.0.0
* @return int Number of friend connections created
*/
function buddyx_bp_import_users_friends() {
$count = 0;
if ( ! bp_is_active( 'friends' ) ) {
return $count;
}
$users = buddyx_bp_get_random_users_ids( 50 );
// Add filter to fix friend connection dates
add_filter( 'bp_core_current_time', 'buddyx_bp_friends_add_friend_date_fix' );
for ( $i = 0; $i < 100; $i++ ) {
$user_one = $users[ array_rand( $users ) ];
$user_two = $users[ array_rand( $users ) ];
// Make them friends if possible
if ( $user_one !== $user_two && friends_add_friend( $user_one, $user_two, true ) ) {
$count++;
}
}
// Remove filter
remove_filter( 'bp_core_current_time', 'buddyx_bp_friends_add_friend_date_fix' );
return $count;
}
/**
* Import BuddyPress groups
*
* @since 3.0.0
* @param array|bool $users Optional. User list to work with. Get random if empty.
* @return array Array of created group IDs
*/
function buddyx_bp_import_groups( $users = false ) {
$groups = array();
$group_ids = array();
if ( ! bp_is_active( 'groups' ) ) {
return $group_ids;
}
// Use currently available users from DB if no default were specified
if ( empty( $users ) ) {
$users = get_users();
}
require __DIR__ . '/demos/demo-bp-data/groups.php';
foreach ( $groups as $group ) {
$creator_id = is_object( $users[ array_rand( $users ) ] ) ? $users[ array_rand( $users ) ]->ID : $users[ array_rand( $users ) ];
$cur = groups_create_group(
array(
'creator_id' => $creator_id,
'name' => $group['name'],
'description' => $group['description'],
'slug' => groups_check_slug( sanitize_title( esc_attr( $group['name'] ) ) ),
'status' => $group['status'],
'date_created' => buddyx_bp_get_random_date( 30, 5 ),
'enable_forum' => $group['enable_forum'],
)
);
if ( ! $cur ) {
continue;
}
// Update group last activity
groups_update_groupmeta( $cur, 'last_activity', buddyx_bp_get_random_date( 10 ) );
// Create forums if Forum Component is active
if (
bp_is_active( 'forums' ) &&
function_exists( 'bp_forums_is_installed_correctly' ) &&
bp_forums_is_installed_correctly() &&
function_exists( 'groups_new_group_forum' )
) {
groups_new_group_forum( $cur, $group['name'], $group['description'] );
}
$group_ids[] = $cur;
}
// Save imported group IDs
if ( ! empty( $group_ids ) ) {
bp_update_option( 'buddyx_bp_imported_group_ids', $group_ids );
}
return $group_ids;
}
/**
* Import group activity posts
*
* @since 3.0.0
* @return int Number of group activity items created
*/
function buddyx_bp_import_groups_activity() {
$count = 0;
if ( ! bp_is_active( 'groups' ) || ! bp_is_active( 'activity' ) ) {
return $count;
}
$users = buddyx_bp_get_random_users_ids( 0 );
$groups = buddyx_bp_get_random_groups_ids( 0 );
$activity = require __DIR__ . '/demos/demo-bp-data/activity.php';
for ( $i = 0; $i < 150; $i++ ) {
$user_id = $users[ array_rand( $users ) ];
$group_id = $groups[ array_rand( $groups ) ];
$content = $activity[ array_rand( $activity ) ];
// Only post if user is a member of the group
if ( ! groups_is_user_member( $user_id, $group_id ) ) {
continue;
}
$bp_activity_id = groups_post_update(
array(
'user_id' => $user_id,
'group_id' => $group_id,
'content' => $content,
)
);
if ( $bp_activity_id ) {
$bp_activity = new BP_Activity_Activity( $bp_activity_id );
$bp_activity->date_recorded = buddyx_bp_get_random_date( 29 );
if ( $bp_activity->save() ) {
$count++;
}
}
}
return $count;
}
/**
* Import group members
*
* @since 3.0.0
* @param array $groups Optional. Predefined group list or import random groups
* @return array Array of group IDs that had members added
*/
function buddyx_bp_import_groups_members( $groups = array() ) {
$members = array();
if ( ! bp_is_active( 'groups' ) ) {
return $members;
}
// Use random groups if none provided
if ( empty( $groups ) ) {
$groups = buddyx_bp_get_random_groups_ids( 0 );
}
// Add filter to fix join date
add_filter( 'bp_after_activity_add_parse_args', 'buddyx_bp_groups_join_group_date_fix' );
foreach ( $groups as $group_id ) {
$user_ids = buddyx_bp_get_random_users_ids( wp_rand( 2, 15 ) );
foreach ( $user_ids as $user_id ) {
if ( groups_join_group( $group_id, $user_id ) ) {
$members[] = $group_id;
}
}
}
// Remove filter
remove_filter( 'bp_after_activity_add_parse_args', 'buddyx_bp_groups_join_group_date_fix' );
return $members;
}
/**
* Import forums and topics for groups (placeholder)
*
* @since 3.0.0
* @param array $groups Group IDs to create forums for
* @return bool Always returns true
*/
function buddyx_bp_import_groups_forums( $groups ) {
// Placeholder for future forum import functionality
return true;
}