-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhelpers.php
347 lines (281 loc) · 7.9 KB
/
helpers.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
<?php
/**
* Get plugin admin area root page: settings.php for WPMS and tool.php for WP.
*
* @return string
*/
function bpdd_get_root_admin_page() {
return is_multisite() ? 'settings.php' : 'tools.php';
}
/**
* Delete all imported information.
*/
function bpdd_clear_db() {
global $wpdb;
$bp = buddypress();
/*
* Groups
*/
$groups = bp_get_option( 'bpdd_imported_group_ids' );
if ( ! empty( $groups ) ) {
foreach ( (array) $groups as $group_id ) {
groups_delete_group( $group_id );
}
}
/*
* Users and all their data.
*/
$users = bp_get_option( 'bpdd_imported_user_ids' );
if ( ! empty( $users ) ) {
$users_str = implode( ',', (array) $users );
foreach ( (array) $users as $user_id ) {
bp_core_delete_account( $user_id );
}
}
/*
* PRIVATE MESSAGES are not deleted by BuddyPress when user is deleted, for some reason.
* This is a BuddyPress bug, and the fix below will stay here for some time.
*/
$thread_ids = bp_get_option( 'bpdd_imported_user_messages_ids' );
if ( ! empty( $thread_ids ) && ! empty( $users_str ) ) {
// Finally, remove from the DB completely.
foreach ( $thread_ids as $thread_id ) {
// Get the message ids in order to delete their metas.
$message_ids = $wpdb->get_col(
$wpdb->prepare(
"SELECT id FROM {$bp->messages->table_name_messages}
WHERE thread_id = %d",
$thread_id
)
);
// Delete all the messages.
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$bp->messages->table_name_messages}
WHERE thread_id = %d",
$thread_id
)
);
// Delete message meta.
foreach ( $message_ids as $message_id ) {
bp_messages_delete_meta( $message_id );
}
// Delete all the recipients.
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$bp->messages->table_name_recipients}
WHERE thread_id = %d",
$thread_id
)
);
}
}
/*
* xProfile groups and fields, metas are not deleted - it's a BuddyPress bug.
*/
$xprofile_ids = bp_get_option( 'bpdd_imported_user_xprofile_ids' );
foreach ( (array) $xprofile_ids as $xprofile_id ) {
$group = new BP_XProfile_Group( $xprofile_id );
$group->delete();
}
bpdd_delete_import_records();
}
/**
* Fix the date issue, when all joined_group events took place at the same time.
*
* @param array $args Arguments that are passed to bp_activity_add().
*
* @return array
* @throws \Exception
*/
function bpdd_groups_join_group_date_fix( $args ) {
if (
$args['type'] === 'joined_group' &&
$args['component'] === 'groups'
) {
$args['recorded_time'] = bpdd_get_random_date( 25, 1 );
}
return $args;
}
/**
* Fix the date issue, when all friends connections are done at the same time.
*
* @param string $current_time Default BuddyPress current timestamp.
*
* @return string
* @throws \Exception
*/
function bpdd_friends_add_friend_date_fix( $current_time ) {
return strtotime( bpdd_get_random_date( 43 ) );
}
/**
* Get the array (or a string) of group IDs.
*
* @param int $count If you need all, use 0.
* @param string $output What to return: 'array' or 'string'. If string - comma separated.
*
* @return array|string Default is array.
*/
function bpdd_get_random_groups_ids( $count = 1, $output = 'array' ) {
$groups_arr = (array) bp_get_option( 'bpdd_imported_group_ids' );
if ( ! empty( $groups_arr ) ) {
$total_groups = count( $groups_arr );
if ( $count <= 0 || $count > $total_groups ) {
$count = $total_groups;
}
// Get random groups.
$random_keys = (array) array_rand( $groups_arr, $count );
$groups = [];
foreach ( $groups_arr as $key => $value ) {
if ( in_array( $key, $random_keys, true ) ) {
$groups[] = $value;
}
}
} else {
global $wpdb;
$bp = buddypress();
$limit = '';
if ( $count > 0 ) {
$limit = 'LIMIT ' . (int) $count;
}
$groups = $wpdb->get_col( "SELECT id FROM {$bp->groups->table_name} ORDER BY rand() {$limit}" );
}
/*
* Convert to integers, because get_col() returns array of strings.
*/
$groups = array_map( 'intval', $groups );
if ( $output === 'string' ) {
return implode( ',', $groups );
}
return $groups;
}
/**
* Get the array (or a string) of user IDs.
*
* @param int $count If you need all, use 0.
* @param string $output What to return: 'array' or 'string'. If string - comma separated.
*
* @return array|string Default is array.
*/
function bpdd_get_random_users_ids( $count = 1, $output = 'array' ) {
$users_arr = (array) bp_get_option( 'bpdd_imported_user_ids' );
if ( ! empty( $users_arr ) ) {
$total_members = count( $users_arr );
if ( $count <= 0 || $count > $total_members ) {
$count = $total_members;
}
// Get random users.
$random_keys = (array) array_rand( $users_arr, $count );
$users = [];
foreach ( $users_arr as $key => $value ) {
if ( in_array( $key, $random_keys, true ) ) {
$users[] = $value;
}
}
} else {
// Get by default (if no users were imported) all currently registered users.
$users = get_users(
[
'fields' => 'ID',
]
);
}
/*
* Convert to integers, because get_col() and get_users() return array of strings.
*/
$users = array_map( 'intval', $users );
if ( $output === 'string' ) {
return implode( ',', $users );
}
return $users;
}
/**
* Get a random date between some days in the past.
* If [30;5] is specified - that means a random date between 30 and 5 days from now.
*
* @param int $days_from
* @param int $days_to
*
* @return string Random time in 'Y-m-d h:i:s' format.
*/
function bpdd_get_random_date( $days_from = 30, $days_to = 0 ) {
// $days_from should always be less than $days_to
if ( $days_to > $days_from ) {
$days_to = $days_from - 1;
}
try {
$date_from = new DateTime( 'now - ' . $days_from . ' days' );
$date_to = new DateTime( 'now - ' . $days_to . ' days' );
$date = date( 'Y-m-d H:i:s', wp_rand( $date_from->getTimestamp(), $date_to->getTimestamp() ) );
} catch ( Exception $e ) {
$date = date( 'Y-m-d H:i:s' );
}
return $date;
}
/**
* Get the current timestamp, using current blog time settings.
*
* @return int
*/
function bpdd_get_time() {
return (int) current_time( 'timestamp' );
}
/**
* Check whether something was imported or not.
*
* @param string $group Possible values: users, groups
* @param string $import What exactly was imported
*
* @return bool
*/
function bpdd_is_imported( $group, $import ) {
$group = sanitize_key( $group );
$import = sanitize_key( $import );
if ( ! in_array( $group, [ 'users', 'groups' ] ) ) {
return false;
}
return array_key_exists( $import, (array) bp_get_option( 'bpdd_import_' . $group ) );
}
/**
* Display a disabled attribute for inputs of the particular value was already imported.
*
* @param string $group
* @param string $import
*/
function bpdd_imported_disabled( $group, $import ) {
$group = sanitize_key( $group );
$import = sanitize_key( $import );
if ( ! in_array( $group, [ 'users', 'groups' ], true ) ) {
echo '';
}
echo bpdd_is_imported( $group, $import ) ? 'disabled="disabled" checked="checked"' : '';
}
/**
* Save when the importing was done.
*
* @param string $group
* @param string $import
*
* @return bool
*/
function bpdd_update_import( $group, $import ) {
$group = sanitize_key( $group );
$import = sanitize_key( $import );
if ( ! in_array( $group, [ 'users', 'groups' ], true ) ) {
return false;
}
$values = bp_get_option( 'bpdd_import_' . $group, [] );
$values[ $import ] = bpdd_get_time();
return bp_update_option( 'bpdd_import_' . $group, $values );
}
/**
* Remove all imported ids and the indication, that importing was done.
*/
function bpdd_delete_import_records() {
bp_delete_option( 'bpdd_import_users' );
bp_delete_option( 'bpdd_import_groups' );
bp_delete_option( 'bpdd_imported_user_ids' );
bp_delete_option( 'bpdd_imported_group_ids' );
bp_delete_option( 'bpdd_imported_user_messages_ids' );
bp_delete_option( 'bpdd_imported_user_xprofile_ids' );
}