helper.php
18 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
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
<?php
class ACUI_Helper{
private static $instance;
function __construct(){
}
private static function is_instantiated() {
if ( ! empty( self::$instance ) && ( self::$instance instanceof ACUI_Helper ) ) {
return true;
}
return false;
}
private static function setup_instance() {
self::$instance = new ACUI_Helper;
}
static function instance() {
if ( self::is_instantiated() ) {
return self::$instance;
}
self::setup_instance();
return self::$instance;
}
function detect_delimiter( $file ) {
$delimiters = array( ';' => 0, ',' => 0, "\t" => 0, "|" => 0 );
$handle = @fopen( $file, "r" );
$firstLine = fgets( $handle );
fclose( $handle );
foreach( $delimiters as $delimiter => &$count ) {
$count = count( str_getcsv( $firstLine, $delimiter ) );
}
return array_search( max( $delimiters ), $delimiters );
}
function user_id_exists( $user_id ){
if ( get_userdata( $user_id ) === false )
return false;
else
return true;
}
function get_roles_by_user_id( $user_id ){
$roles = array();
$user = new WP_User( $user_id );
if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
foreach ( $user->roles as $role )
$roles[] = $role;
}
return $roles;
}
static function get_editable_roles( $include_no_role = true ){
global $wp_roles;
$all_roles = $wp_roles->roles;
$editable_roles = apply_filters('editable_roles', $all_roles);
$list_editable_roles = array();
foreach ($editable_roles as $key => $editable_role)
$list_editable_roles[$key] = translate_user_role( $editable_role["name"] );
if( $include_no_role )
$list_editable_roles['no_role'] = __( 'No role', 'import-users-from-csv-with-meta' );
return $list_editable_roles;
}
static function get_csv_delimiters_titles(){
return array(
'COMMA' => __( 'Comma', 'import-users-from-csv-with-meta' ),
'COLON' => __( 'Colon', 'import-users-from-csv-with-meta' ),
'SEMICOLON' => __( 'Semicolon', 'import-users-from-csv-with-meta' ),
'TAB' => __( 'Tab', 'import-users-from-csv-with-meta' ),
);
}
static function get_list_users_with_display_name(){
$blogusers = get_users( array( 'fields' => array( 'ID', 'display_name' ) ) );
$result = array();
foreach ( $blogusers as $bloguser )
$result[ $bloguser->ID ] = $bloguser->display_name;
return $result;
}
static function get_loaded_periods(){
$loaded_periods = wp_get_schedules();
$result = array();
foreach ( $loaded_periods as $key => $value )
$result[ $key ] = $value['display'];
return $result;
}
function get_seconds_by_period( $period ){
$loaded_periods = wp_get_schedules();
return isset( $loaded_periods[ $period ] ) ? $loaded_periods[ $period ]['interval'] : 0;
}
static function get_errors_by_row( $errors, $row, $type = 'error' ){
$errors_found = array();
foreach( $errors as $error ){
if( $error['row'] == $row && ( $error['type'] == $type || 'any' == $type ) ){
$errors_found[] = $error['message'];
}
}
return $errors_found;
}
function string_conversion( $string ){
return mb_convert_encoding( $string, 'UTF-8', mb_detect_encoding( $string ) );
}
function get_wp_users_fields(){
return array( "id", "user_email", "user_nicename", "user_url", "display_name", "nickname", "first_name", "last_name", "description", "jabber", "aim", "yim", "user_registered", "password", "user_pass", "locale", "show_admin_bar_front", "user_login" );
}
function get_restricted_fields(){
$wp_users_fields = $this->get_wp_users_fields();
$wp_min_fields = array( "Username", "Email", "role" );
$acui_restricted_fields = array_merge( $wp_users_fields, $wp_min_fields );
return apply_filters( 'acui_restricted_fields', $acui_restricted_fields );
}
function get_not_meta_fields(){
return apply_filters( 'acui_not_meta_fields', array() );
}
function get_random_unique_username( $prefix = '' ){
do {
$rnd_str = sprintf("%06d", mt_rand(1, 999999));
} while( username_exists( $prefix . $rnd_str ) );
return $prefix . $rnd_str;
}
function array_one_dimension($array) {
$one_dimension = true;
array_walk_recursive($array, function ($value) use (&$one_dimension) {
if( is_array( $value ) ) {
$one_dimension = false;
}
});
return $one_dimension;
}
function array_correlative_index( $array ) {
if( !is_array( $array ) ) {
return false;
}
$expectedKey = 0;
foreach( $array as $key => $value ) {
if ($key !== $expectedKey) {
return false;
}
$expectedKey++;
}
return true;
}
function array_contains_wp_error( $array ) {
foreach( $array as $key => $value ){
if( is_wp_error( $value ) )
return true;
}
return false;
}
function new_error( $row, $message = '', $type = 'error' ){
global $errors_totals;
switch( $type ){
case 'error':
$errors_totals['errors']++;
break;
case 'warning':
$errors_totals['warnings']++;
break;
case 'notice':
$errors_totals['notices']++;
break;
}
return array( 'row' => $row, 'message' => $message, 'type' => $type );
}
function maybe_update_email( $user_id, $email, $password, $update_emails_existing_users, $original_email ){
$user_object = get_user_by( 'id', $user_id );
if( $user_object->user_email == $email || ( apply_filters( 'acui_allow_no_email', false ) && empty( $original_email ) ) )
return $user_id;
switch( $update_emails_existing_users ){
case 'yes':
$user_id = wp_update_user( array( 'ID' => $user_id, 'user_email' => $email ) );
break;
case 'no':
$user_id = 0;
break;
case 'create':
$user_id = wp_insert_user( array(
'user_login' => $this->get_random_unique_username( 'duplicated_username_' ),
'user_email' => $email,
'user_pass' => $password
) );
break;
}
return $user_id;
}
static function get_attachment_id_by_url( $url ) {
$wp_upload_dir = wp_upload_dir();
$dir = set_url_scheme( trailingslashit( $wp_upload_dir['baseurl'] ), 'relative' );
if ( false !== strpos( $url, $dir ) ) {
$file = basename( $url );
$query_args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'fields' => 'ids',
'meta_query' => array(
array(
'key' => '_wp_attachment_metadata',
'compare' => 'LIKE',
'value' => $file,
),
),
);
$query = new WP_Query( $query_args );
if ( $query->have_posts() ) {
foreach ( $query->posts as $attachment_id ) {
$meta = wp_get_attachment_metadata( $attachment_id );
$original_file = basename( $meta['file'] );
$cropped_files = wp_list_pluck( $meta['sizes'], 'file' );
if ( $original_file === $file || in_array( $file, $cropped_files ) ) {
return (int) $attachment_id;
}
}
}
}
return false;
}
static function get_post_id_by_slug( $slug ){
global $wpdb;
$page_path = rawurlencode( urldecode( $slug ) );
$page_path = str_replace( '%2F', '/', $page_path );
$page_path = str_replace( '%20', ' ', $page_path );
$parts = explode( '/', trim( $page_path, '/' ) );
$parts = array_map( 'sanitize_title_for_query', $parts );
$escaped_parts = esc_sql( $parts );
$in_string = "'" . implode( "','", $escaped_parts ) . "'";
$pages = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent, post_type FROM $wpdb->posts WHERE post_name IN (%s)", $in_string ), OBJECT_K );
$revparts = array_reverse( $parts );
$foundid = 0;
foreach ( (array) $pages as $page ) {
if ( $page->post_name == $revparts[0] ) {
$count = 0;
$p = $page;
while ( 0 != $p->post_parent && isset( $pages[ $p->post_parent ] ) ) {
$count++;
$parent = $pages[ $p->post_parent ];
if ( ! isset( $revparts[ $count ] ) || $parent->post_name != $revparts[ $count ] ) {
break;
}
$p = $parent;
}
if ( 0 == $p->post_parent && count( $revparts ) == $count + 1 && $p->post_name == $revparts[ $count ] ) {
$foundid = $page->ID;
if ( $page->post_type == $p->post_type ) {
break;
}
}
}
}
return $foundid;
}
function print_table_header_footer( $headers ){
?>
<h3><?php echo apply_filters( 'acui_log_inserting_updating_data_title', __( 'Inserting and updating data', 'import-users-from-csv-with-meta' ) ); ?></h3>
<table id="acui_results">
<thead>
<tr>
<th><?php _e( 'Row', 'import-users-from-csv-with-meta' ); ?></th>
<?php foreach( $headers as $element ):
echo "<th>" . esc_html( $element ) . "</th>";
endforeach; ?>
<?php do_action( 'acui_header_table_extra_rows' ); ?>
</tr>
</thead>
<tfoot>
<tr>
<th><?php _e( 'Row', 'import-users-from-csv-with-meta' ); ?></th>
<?php foreach( $headers as $element ):
echo "<th>" . esc_html( $element ) . "</th>";
endforeach; ?>
<?php do_action( 'acui_header_table_extra_rows' ); ?>
</tr>
</tfoot>
<tbody>
<?php
}
function print_table_end(){
?>
</tbody>
</table>
<?php
}
function print_row_imported( $row, $data, $errors ){
$styles = "";
if( !empty( ACUI_Helper::get_errors_by_row( $errors, $row, 'any' ) ) )
$styles = "background-color:red; color:white;";
echo "<tr style='$styles' ><td>" . ($row - 1) . "</td>";
foreach ( $data as $element ){
if( is_wp_error( $element ) )
$element = $element->get_error_message();
elseif( is_object( $element ) ){
$element = serialize( $element );
}
elseif( is_array( $element ) ){
$element_string = '';
$i = 0;
foreach( $element as $it => $el ){
if( is_wp_error( $el ) )
$element_string .= $el->get_error_message();
elseif( is_array( $el ) || is_object( $el ) )
$element_string .= serialize( $el );
elseif( !is_int( $it ) )
$element_string .= $it . "=>" . $el;
else
$element_string .= $el;
if(++$i !== count( $element ) ){
$element_string .= ',';
}
}
$element = $element_string;
}
$element = esc_html( $element );
echo "<td>$element</td>";
}
echo "</tr>\n";
flush();
}
function print_errors( $errors ){
if( empty( $errors ) )
return;
?>
<h3><?php _e( 'Errors, warnings and notices', 'import-users-from-csv-with-meta' ); ?></h3>
<table id="acui_errors">
<thead>
<tr>
<th><?php _e( 'Row', 'import-users-from-csv-with-meta' ); ?></th>
<th><?php _e( 'Details', 'import-users-from-csv-with-meta' ); ?></th>
<th><?php _e( 'Type', 'import-users-from-csv-with-meta' ); ?></th>
</tr>
</thead>
<tfoot>
<tr>
<th><?php _e( 'Row', 'import-users-from-csv-with-meta' ); ?></th>
<th><?php _e( 'Details', 'import-users-from-csv-with-meta' ); ?></th>
<th><?php _e( 'Type', 'import-users-from-csv-with-meta' ); ?></th>
</tr>
</tfoot>
<tbody>
<?php foreach( $errors as $error ): ?>
<tr>
<td><?php echo $error['row']; ?></td>
<td><?php echo esc_html( $error['message'] ); ?></td>
<td><?php echo $error['type']; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php
}
function print_results( $results, $errors ){
?>
<h3><?php _e( 'Results', 'import-users-from-csv-with-meta' ); ?></h3>
<table id="acui_results">
<tbody>
<tr>
<th><?php _e( 'Users processed', 'import-users-from-csv-with-meta' ); ?></th>
<td><?php echo $results['created'] + $results['updated']; ?></td>
</tr>
<tr>
<th><?php _e( 'Users created', 'import-users-from-csv-with-meta' ); ?></th>
<td><?php echo $results['created']; ?></td>
</tr>
<tr>
<th><?php _e( 'Users updated', 'import-users-from-csv-with-meta' ); ?></th>
<td><?php echo $results['updated']; ?></td>
</tr>
<tr>
<th><?php _e( 'Users deleted', 'import-users-from-csv-with-meta' ); ?></th>
<td><?php echo $results['deleted']; ?></td>
</tr>
<tr>
<th><?php _e( 'Errors, warnings and notices found', 'import-users-from-csv-with-meta' ); ?></td>
<td><?php echo count( $errors ); ?></td>
</tr>
</tbody>
</table>
<?php
}
function print_end_of_process(){
?>
<br/>
<p><?php printf( __( 'Process finished you can go <a href="%s">here to see results</a> or you can do <a href="%s">a new import</a>.', 'import-users-from-csv-with-meta' ), get_admin_url( null, 'users.php' ), get_admin_url( null, 'tools.php?page=acui&tab=homepage' ) ); ?></p>
<?php
}
function execute_datatable(){
?>
<script>
jQuery( document ).ready( function( $ ){
$( '#acui_results,#acui_errors' ).DataTable({
"scrollX": true,
});
} )
</script>
<?php
}
function basic_css(){
?>
<style type="text/css">
.wrap{
overflow-x:auto!important;
}
.wrap table{
min-width:800px!important;
}
.wrap table th,
.wrap table td{
width:200px!important;
}
</style>
<?php
}
static function get_array_from_cell( $value ){
if( strpos( $value, "=>" ) === false )
return explode( "::", $value );
$array_prepared = array();
foreach( explode( "::", $value ) as $data ){
$key_value = explode( "=>", $data );
$array_prepared[ $key_value[0] ] = $key_value[1];
}
return $array_prepared;
}
static function get_value_from_row( $key, $headers, $row, $user_id = 0 ){
$pos = array_search( $key, $headers );
if( $pos === false ){
return ( $user_id == 0 ) ? false : get_user_meta( $user_id, $key, true );
}
return $row[ $pos ];
}
static function show_meta( $user_id, $meta_key ){
$user_meta = get_user_meta( $user_id, $meta_key, true );
return is_array( $user_meta ) ? var_export( $user_meta, true ) : $user_meta;
}
function maybe_disable_wordpress_core_emails(){
if( !get_option('acui_automatic_wordpress_email') ){
add_filter( 'send_email_change_email', function() { return false; }, PHP_INT_MAX );
add_filter( 'send_password_change_email', function() { return false; }, PHP_INT_MAX );
}
}
function maybe_enable_wordpress_core_emails(){
if( !get_option('acui_automatic_wordpress_email') ){
remove_filter( 'send_email_change_email', function() { return false; }, 999 );
remove_filter( 'send_password_change_email', function() { return false; }, 999 );
}
}
// notices
static function get_notices(){
$notices = get_transient( 'acui_notices' );
delete_transient( 'acui_notices' );
return is_array( $notices ) ? $notices : array();
}
static function add_notice( $notice ){
$notices = self::get_notices();
$notices[] = $notice;
set_transient( 'acui_notices', $notices, 120 );
}
function get_notice(){
$notices = self::get_notices();
if( count( $notices ) == 0 )
return false;
$return = '';
foreach( $notices as $key => $notice ){
$return = $notice;
unset( $notices[ $key ] );
set_transient( 'acui_notices', $notices );
return $return;
}
return false;
}
}
function ACUIHelper(){
return ACUI_Helper::instance();
}