-
Notifications
You must be signed in to change notification settings - Fork 7
/
process.php
455 lines (366 loc) · 11.3 KB
/
process.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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<?php
/**
* Importer engine - USERS
*/
function bpdd_import_users() {
$users = array();
$users_data = require __DIR__ . '/data/users.php';
foreach ( $users_data as $user ) {
$user_id = wp_insert_user(
array(
'user_login' => $user['login'],
'user_pass' => $user['pass'],
'display_name' => $user['display_name'],
'user_email' => $user['email'],
'user_registered' => bpdd_get_random_date( 45, 1 ),
)
);
if ( is_wp_error( $user_id ) ) {
continue;
}
if ( bp_is_active( 'xprofile' ) ) {
xprofile_set_field_data( 1, $user_id, $user['display_name'] );
}
$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] : '' );
bp_update_user_last_activity( $user_id, bpdd_get_random_date( 5 ) );
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;
}
if ( ! empty( $users ) ) {
bp_update_option( 'bpdd_imported_user_ids', $users );
}
return $users;
}
/**
* Import extended profile fields.
*
* @return int
*/
function bpdd_import_users_profile() {
$count = 0;
if ( ! bp_is_active( 'xprofile' ) ) {
return $count;
}
$data = array();
$xprofile_structure = require __DIR__ . '/data/xprofile_structure.php';
// Firstly, 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;
// Then 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 ) {
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;
// finally import 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__ . '/data/xprofile_data.php';
$users = bpdd_get_random_users_ids( 0 );
// Now import profile fields data for all fields 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;
}
}
}
if ( ! empty( $groups ) ) {
bp_update_option( 'bpdd_imported_user_xprofile_ids', $groups );
}
return $count;
}
/**
* Import private messages between users.
*
* @return array
*/
function bpdd_import_users_messages() {
$messages = array();
if ( ! bp_is_active( 'messages' ) ) {
return $messages;
}
/** @var $messages_subjects array */
/** @var $messages_content array */
require __DIR__ . '/data/messages.php';
// first level messages
for ( $i = 0; $i < 33; $i ++ ) {
$messages[] = messages_new_message(
array(
'sender_id' => bpdd_get_random_users_ids( 1, 'string' ),
'recipients' => bpdd_get_random_users_ids( 1, 'array' ),
'subject' => $messages_subjects[ array_rand( $messages_subjects ) ],
'content' => $messages_content[ array_rand( $messages_content ) ],
'date_sent' => bpdd_get_random_date( 15, 5 ),
)
);
}
for ( $i = 0; $i < 33; $i ++ ) {
$messages[] = messages_new_message(
array(
'sender_id' => bpdd_get_random_users_ids( 1, 'string' ),
'recipients' => bpdd_get_random_users_ids( 2, 'array' ),
'subject' => $messages_subjects[ array_rand( $messages_subjects ) ],
'content' => $messages_content[ array_rand( $messages_content ) ],
'date_sent' => bpdd_get_random_date( 13, 3 ),
)
);
}
for ( $i = 0; $i < 33; $i ++ ) {
$messages[] = messages_new_message(
array(
'sender_id' => bpdd_get_random_users_ids( 1, 'string' ),
'recipients' => bpdd_get_random_users_ids( 3, 'array' ),
'subject' => $messages_subjects[ array_rand( $messages_subjects ) ],
'content' => $messages_content[ array_rand( $messages_content ) ],
'date_sent' => bpdd_get_random_date( 10 ),
)
);
}
$messages[] = messages_new_message(
array(
'sender_id' => bpdd_get_random_users_ids( 1, 'string' ),
'recipients' => bpdd_get_random_users_ids( 5, 'array' ),
'subject' => $messages_subjects[ array_rand( $messages_subjects ) ],
'content' => $messages_content[ array_rand( $messages_content ) ],
'date_sent' => bpdd_get_random_date( 5 ),
)
);
// Get rid of empty values, like failed attempts to import messages.
$messages = array_filter( $messages );
if ( ! empty( $messages ) ) {
bp_update_option( 'bpdd_imported_user_messages_ids', $messages );
}
return $messages;
}
/**
* Import Activity - aka "status updates".
*
* @return int Number of activity records that were inserted into the database.
*/
function bpdd_import_users_activity() {
$count = 0;
if ( ! bp_is_active( 'activity' ) ) {
return $count;
}
$users = bpdd_get_random_users_ids( 0 );
/** @var $activity array */
require __DIR__ . '/data/activity.php';
for ( $i = 0; $i < 75; $i ++ ) {
$user = $users[ array_rand( $users ) ];
$content = $activity[ array_rand( $activity ) ];
if ( $bp_activity_id = bp_activity_post_update( array( 'user_id' => $user, 'content' => $content ) ) ) {
$bp_activity = new BP_Activity_Activity( $bp_activity_id );
$bp_activity->date_recorded = bpdd_get_random_date( 44 );
if ( $bp_activity->save() ) {
$count ++;
}
}
}
return $count;
}
/**
* Get random users from the DB and generate friends connections.
*
* @return int
*/
function bpdd_import_users_friends() {
$count = 0;
if ( ! bp_is_active( 'friends' ) ) {
return $count;
}
$users = bpdd_get_random_users_ids( 50 );
add_filter( 'bp_core_current_time', 'bpdd_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 ( friends_add_friend( $user_one, $user_two, true ) ) {
$count ++;
}
}
remove_filter( 'bp_core_current_time', 'bpdd_friends_add_friend_date_fix' );
return $count;
}
/**
* Importer engine - GROUPS
*
* @param bool|array $users Users list we want to work with. Get random if empty.
*
* @return array
*/
function bpdd_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__ . '/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' => bpdd_get_random_date( 30, 5 ),
'enable_forum' => $group['enable_forum'],
)
);
if ( ! $cur ) {
continue;
}
groups_update_groupmeta( $cur, 'last_activity', bpdd_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;
}
if ( ! empty( $group_ids ) ) {
bp_update_option( 'bpdd_imported_group_ids', $group_ids );
}
return $group_ids;
}
/**
* Import groups activity - aka "status updates".
*
* @return int
*/
function bpdd_import_groups_activity() {
$count = 0;
if ( ! bp_is_active( 'groups' ) || ! bp_is_active( 'activity' ) ) {
return $count;
}
$users = bpdd_get_random_users_ids( 0 );
$groups = bpdd_get_random_groups_ids( 0 );
/** @var $activity array */
require __DIR__ . '/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 ) ];
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 = bpdd_get_random_date( 29 );
if ( $bp_activity->save() ) {
$count ++;
}
}
}
return $count;
}
/**
* Import groups members.
*
* @param array $groups We can import random groups or work with a predefined list.
*
* @return array
*/
function bpdd_import_groups_members( $groups = array() ) {
$members = array();
if ( ! bp_is_active( 'groups' ) ) {
return $members;
}
if ( empty( $groups ) ) {
$groups = bpdd_get_random_groups_ids( 0 );
}
add_filter( 'bp_after_activity_add_parse_args', 'bpdd_groups_join_group_date_fix' );
foreach ( $groups as $group_id ) {
$user_ids = bpdd_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( 'bp_after_activity_add_parse_args', 'bpdd_groups_join_group_date_fix' );
return $members;
}
/**
* Give ability to import forums and topics for groups.
* Not used currently.
*
* @param array $groups
*
* @return bool
*/
function bpdd_import_groups_forums( $groups ) {
return true;
}