Import users from CSV with meta - Version 1.17

Version Description

  • Many code changes, making it simpler to include more features and make it easier to debug in a future
  • Export bug fixed: the plugin exports an empty role column that breaks the CSV
  • You can now test cron task from the "Cron" settings tab
Download this release

Release Info

Developer carazo
Plugin Icon 128x128 Import users from CSV with meta
Version 1.17
Comparing to
See all releases

Code changes from version 1.16.4.1 to 1.17

addons/buddypress.php CHANGED
@@ -7,38 +7,75 @@ if( !is_plugin_active( 'buddypress/bp-loader.php' ) && !function_exists( 'bp_is_
7
  }
8
 
9
  class ACUI_Buddypress{
 
 
 
10
  function __construct(){
 
 
 
 
 
 
 
 
 
11
  add_filter( 'acui_restricted_fields', array( $this, 'restricted_fields' ), 10, 1 );
12
  add_action( 'acui_tab_import_before_import_button', array( $this, 'show_compatibility' ) );
13
  add_action( 'acui_documentation_after_plugins_activated', array( $this, 'documentation' ) );
14
  add_filter( 'acui_export_columns', array( $this, 'export_columns' ), 10, 1 );
15
  add_filter( 'acui_export_data', array( $this, 'export_data' ), 10, 3 );
16
- add_action( 'post_acui_import_single_user', array( $this, 'import_avatar' ), 10, 3 );
 
17
  }
18
 
19
  function restricted_fields( $acui_restricted_fields ){
20
- return array_merge( $acui_restricted_fields, array( 'bp_avatar' ) );
21
  }
22
 
23
- function show_compatibility(){
24
- if( !class_exists( "BP_XProfile_Group" ) ){
25
- require_once( WP_PLUGIN_DIR . "/buddypress/bp-xprofile/classes/class-bp-xprofile-group.php" );
26
- }
27
-
28
  $buddypress_fields = array();
29
- $buddypress_types = array();
30
- $profile_groups = BP_XProfile_Group::get( array( 'fetch_fields' => true ) );
31
-
32
- if ( !empty( $profile_groups ) ) {
33
- foreach ( $profile_groups as $profile_group ) {
34
  if ( !empty( $profile_group->fields ) ) {
35
  foreach ( $profile_group->fields as $field ) {
36
  $buddypress_fields[] = $field->name;
37
- $buddypress_types[] = $field->type;
38
  }
39
  }
40
  }
41
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  ?>
43
  <h2><?php _e( 'BuddyPress & BuddyBoss compatibility', 'import-users-from-csv-with-meta'); ?></h2>
44
 
@@ -48,7 +85,7 @@ class ACUI_Buddypress{
48
  <th scope="row"><label><?php _e( 'BuddyPress/BuddyBoss users', 'import-users-from-csv-with-meta' ); ?></label></th>
49
  <td><?php _e( 'You can insert any profile from BuddyPress using his name as header. Plugin will check, before import, which fields are defined in BuddyPress and will assign it in the update. You can use this fields:', 'import-users-from-csv-with-meta' ); ?>
50
  <ul style="list-style:disc outside none;margin-left:2em;">
51
- <?php foreach ( $buddypress_fields as $buddypress_field ): ?><li><?php echo $buddypress_field; ?></li><?php endforeach; ?>
52
  </ul>
53
  <?php _e( 'Remember that all date fields have to be imported using a format like this: 2016-01-01 00:00:00', 'import-users-from-csv-with-meta' ); ?>
54
 
@@ -87,42 +124,8 @@ class ACUI_Buddypress{
87
  <?php
88
  }
89
 
90
- function get_fields(){
91
- if( !is_plugin_active( 'buddypress/bp-loader.php' ) && !function_exists( 'bp_is_active' ) ){
92
- return array();
93
- }
94
-
95
- if( !class_exists( "BP_XProfile_Group" ) ){
96
- require_once( WP_PLUGIN_DIR . "/buddypress/bp-xprofile/classes/class-bp-xprofile-group.php" );
97
- }
98
-
99
- $buddypress_fields = array();
100
- $profile_groups = BP_XProfile_Group::get( array( 'fetch_fields' => true ) );
101
-
102
- if ( !empty( $profile_groups ) ) {
103
- foreach ( $profile_groups as $profile_group ) {
104
- if ( !empty( $profile_group->fields ) ) {
105
- foreach ( $profile_group->fields as $field ) {
106
- $buddypress_fields[] = $field->name;
107
- }
108
- }
109
- }
110
- }
111
-
112
- return $buddypress_fields;
113
- }
114
-
115
- function get_groups( $user_id ){
116
- $groups = BP_Groups_Member::get_group_ids( $user_id );
117
- return implode( ",", $groups['groups'] );
118
- }
119
-
120
- function get_member_type( $user_id ){
121
- return implode( ",", bp_get_member_type( $user_id, false ) );
122
- }
123
-
124
  function export_columns( $row ){
125
- foreach ( $this->get_fields() as $key ) {
126
  $row[] = $key;
127
  }
128
 
@@ -133,7 +136,7 @@ class ACUI_Buddypress{
133
  }
134
 
135
  function export_data( $row, $user ){
136
- foreach ( $this->get_fields() as $key ) {
137
  $row[] = ACUI_Exporter::prepare( $key, xprofile_get_field_data( $key, $user, 'comma' ), $datetime_format );
138
  }
139
 
@@ -143,6 +146,68 @@ class ACUI_Buddypress{
143
  return $row;
144
  }
145
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
  function import_avatar( $headers, $row, $user_id ){
147
  $pos = array_search( 'bp_avatar', $headers );
148
 
@@ -188,4 +253,5 @@ class ACUI_Buddypress{
188
  }
189
  }
190
  }
191
- new ACUI_Buddypress();
 
7
  }
8
 
9
  class ACUI_Buddypress{
10
+ var $fields;
11
+ var $profile_groups;
12
+
13
  function __construct(){
14
+ if( !class_exists( "BP_XProfile_Group" ) ){
15
+ require_once( WP_PLUGIN_DIR . "/buddypress/bp-xprofile/classes/class-bp-xprofile-group.php" );
16
+ }
17
+
18
+ $this->profile_groups = $this->get_profile_groups();
19
+ $this->fields = $this->get_fields();
20
+ }
21
+
22
+ function hooks(){
23
  add_filter( 'acui_restricted_fields', array( $this, 'restricted_fields' ), 10, 1 );
24
  add_action( 'acui_tab_import_before_import_button', array( $this, 'show_compatibility' ) );
25
  add_action( 'acui_documentation_after_plugins_activated', array( $this, 'documentation' ) );
26
  add_filter( 'acui_export_columns', array( $this, 'export_columns' ), 10, 1 );
27
  add_filter( 'acui_export_data', array( $this, 'export_data' ), 10, 3 );
28
+ add_action( 'post_acui_import_single_user', array( $this, 'import' ), 10, 3 );
29
+ add_action( 'post_acui_import_single_user', array( $this, 'import_avatar' ), 10, 3 );
30
  }
31
 
32
  function restricted_fields( $acui_restricted_fields ){
33
+ return array_merge( $acui_restricted_fields, array( 'bp_group', 'bp_group_role', 'bp_avatar' ), $this->fields );
34
  }
35
 
36
+ function get_profile_groups(){
37
+ return BP_XProfile_Group::get( array( 'fetch_fields' => true ) );
38
+ }
39
+
40
+ public function get_fields(){
41
  $buddypress_fields = array();
42
+
43
+ if ( !empty( $this->profile_groups ) ) {
44
+ foreach ( $this->profile_groups as $profile_group ) {
 
 
45
  if ( !empty( $profile_group->fields ) ) {
46
  foreach ( $profile_group->fields as $field ) {
47
  $buddypress_fields[] = $field->name;
 
48
  }
49
  }
50
  }
51
  }
52
+
53
+ return $buddypress_fields;
54
+ }
55
+
56
+ function get_field_type( $field_name ){
57
+ if ( !empty( $this->profile_groups ) ) {
58
+ foreach ( $this->profile_groups as $profile_group ) {
59
+ if ( !empty( $profile_group->fields ) ) {
60
+ foreach ( $profile_group->fields as $field ) {
61
+ if( $field_name == $field->name )
62
+ return $field->type;
63
+ }
64
+ }
65
+ }
66
+ }
67
+ }
68
+
69
+ function get_groups( $user_id ){
70
+ $groups = BP_Groups_Member::get_group_ids( $user_id );
71
+ return implode( ",", $groups['groups'] );
72
+ }
73
+
74
+ function get_member_type( $user_id ){
75
+ return implode( ",", bp_get_member_type( $user_id, false ) );
76
+ }
77
+
78
+ function show_compatibility(){
79
  ?>
80
  <h2><?php _e( 'BuddyPress & BuddyBoss compatibility', 'import-users-from-csv-with-meta'); ?></h2>
81
 
85
  <th scope="row"><label><?php _e( 'BuddyPress/BuddyBoss users', 'import-users-from-csv-with-meta' ); ?></label></th>
86
  <td><?php _e( 'You can insert any profile from BuddyPress using his name as header. Plugin will check, before import, which fields are defined in BuddyPress and will assign it in the update. You can use this fields:', 'import-users-from-csv-with-meta' ); ?>
87
  <ul style="list-style:disc outside none;margin-left:2em;">
88
+ <?php foreach ( $this->get_fields() as $buddypress_field ): ?><li><?php echo $buddypress_field; ?></li><?php endforeach; ?>
89
  </ul>
90
  <?php _e( 'Remember that all date fields have to be imported using a format like this: 2016-01-01 00:00:00', 'import-users-from-csv-with-meta' ); ?>
91
 
124
  <?php
125
  }
126
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  function export_columns( $row ){
128
+ foreach ( $this->fields as $key ) {
129
  $row[] = $key;
130
  }
131
 
136
  }
137
 
138
  function export_data( $row, $user ){
139
+ foreach ( $this->fields as $key ) {
140
  $row[] = ACUI_Exporter::prepare( $key, xprofile_get_field_data( $key, $user, 'comma' ), $datetime_format );
141
  }
142
 
146
  return $row;
147
  }
148
 
149
+ function import( $headers, $row, $user_id ){
150
+ foreach( $this->fields as $field ){
151
+ $pos = array_search( $field, $headers );
152
+
153
+ if( $pos === FALSE )
154
+ continue;
155
+
156
+ switch( $this->get_field_type( $field ) ){
157
+ case 'datebox':
158
+ $date = $row[$pos];
159
+ switch( true ){
160
+ case is_numeric( $date ):
161
+ $UNIX_DATE = ($date - 25569) * 86400;
162
+ $datebox = gmdate("Y-m-d H:i:s", $UNIX_DATE);break;
163
+ case preg_match('/(\d{1,2})[\/-](\d{1,2})[\/-]([4567890]{1}\d{1})/',$date,$match):
164
+ $match[3]='19'.$match[3];
165
+ case preg_match('/(\d{1,2})[\/-](\d{1,2})[\/-](20[4567890]{1}\d{1})/',$date,$match):
166
+ case preg_match('/(\d{1,2})[\/-](\d{1,2})[\/-](19[4567890]{1}\d{1})/',$date,$match):
167
+ $datebox= ($match[3].'-'.$match[2].'-'.$match[1]);
168
+ break;
169
+
170
+ default:
171
+ $datebox = $date;
172
+ }
173
+
174
+ $datebox = strtotime( $datebox );
175
+ xprofile_set_field_data( $field, $user_id, date( 'Y-m-d H:i:s', $datebox ) );
176
+ unset( $datebox );
177
+ break;
178
+ default:
179
+ xprofile_set_field_data( $field, $user_id, $row[$pos] );
180
+ }
181
+ }
182
+
183
+ $pos_bp_group = array_search( 'bp_group', $headers );
184
+ $pos_bp_group_role = array_search( 'bp_group_role', $headers );
185
+ if( $pos_bp_group !== FALSE ){
186
+ $groups = explode( ',', $row[ $pos_bp_group ] );
187
+ $groups_role = explode( ',', $row[ $pos_bp_group_role ] );
188
+
189
+ for( $j = 0; $j < count( $groups ); $j++ ){
190
+ $group_id = BP_Groups_Group::group_exists( $groups[ $j ] );
191
+
192
+ if( !empty( $group_id ) ){
193
+ groups_join_group( $group_id, $user_id );
194
+
195
+ if( $groups_role[ $j ] == 'Moderator' ){
196
+ groups_promote_member( $user_id, $group_id, 'mod' );
197
+ }
198
+ elseif( $groups_role[ $j ] == 'Administrator' ){
199
+ groups_promote_member( $user_id, $group_id, 'admin' );
200
+ }
201
+ }
202
+ }
203
+ }
204
+
205
+ $pos_member_type = array_search( 'member_type', $headers );
206
+ if( $pos_member_type !== FALSE ){
207
+ bp_set_member_type( $user_id, $row[$pos_member_type] );
208
+ }
209
+ }
210
+
211
  function import_avatar( $headers, $row, $user_id ){
212
  $pos = array_search( 'bp_avatar', $headers );
213
 
253
  }
254
  }
255
  }
256
+ $acui_buddypress = new ACUI_Buddypress();
257
+ $acui_buddypress->hooks();
addons/new-user-approve.php CHANGED
@@ -7,7 +7,6 @@ if( !is_plugin_active( 'new-user-approve/new-user-approve.php' ) ){
7
  }
8
 
9
  add_action( 'acui_tab_import_before_import_button', 'acui_new_user_approve_tab_import_before_import_button' );
10
-
11
  function acui_new_user_approve_tab_import_before_import_button(){
12
  ?>
13
  <h2><?php _e( 'New User Approve compatibility', 'import-users-from-csv-with-meta'); ?></h2>
@@ -28,4 +27,15 @@ function acui_new_user_approve_tab_import_before_import_button(){
28
  </tbody>
29
  </table>
30
  <?php
 
 
 
 
 
 
 
 
 
 
 
31
  }
7
  }
8
 
9
  add_action( 'acui_tab_import_before_import_button', 'acui_new_user_approve_tab_import_before_import_button' );
 
10
  function acui_new_user_approve_tab_import_before_import_button(){
11
  ?>
12
  <h2><?php _e( 'New User Approve compatibility', 'import-users-from-csv-with-meta'); ?></h2>
27
  </tbody>
28
  </table>
29
  <?php
30
+ }
31
+
32
+ add_action( 'post_acui_import_single_user', 'acui_new_user_post_acui_import_single_user', 10, 6 );
33
+ function acui_new_user_post_acui_import_single_user( $headers, $data, $user_id, $role, $positions, $form_data ){
34
+ $approve_users_new_user_approve = ( empty( $form_data["approve_users_new_user_appove"] ) ) ? "no_approve" : sanitize_text_field( $form_data["approve_users_new_user_appove"] );
35
+ if( $approve_users_new_user_approve == "approve" ){
36
+ update_user_meta( $user_id, "pw_user_status", "approved" );
37
+ }
38
+ else{
39
+ update_user_meta( $user_id, "pending", true );
40
+ }
41
  }
addons/woocommerce-custom-fields.php ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ if ( ! defined( 'ABSPATH' ) ) exit;
4
+
5
+ if( !is_plugin_active( 'woocommerce-custom-fields/woocommerce-custom-fields.php' ) ){
6
+ return;
7
+ }
8
+
9
+ class ACUI_WCF{
10
+ function __construct(){
11
+ add_filter( 'acui_restricted_fields', array( $this, 'restricted_fields' ), 10, 1 );
12
+ add_filter( 'acui_not_meta_fields', array( $this, 'restricted_fields' ), 10, 1 );
13
+ add_action( 'acui_documentation_after_plugins_activated', array( $this, 'documentation' ) );
14
+ add_action( 'post_acui_import_single_user', array( $this, 'import' ), 10, 3 );
15
+ }
16
+
17
+ function get_fields(){
18
+ $customer_fields = get_posts( array(
19
+ 'post_type' => 'wccf_user_field',
20
+ 'posts_per_page' => -1,
21
+ ) );
22
+
23
+ $result = array();
24
+
25
+ foreach ( $customer_fields as $custom_field ) {
26
+ $result[ $custom_field->ID ] = get_post_meta( $custom_field->ID, 'key', true );
27
+ }
28
+
29
+ return $result;
30
+ }
31
+
32
+ function documentation(){
33
+ ?>
34
+ <tr valign="top">
35
+ <th scope="row"><?php _e( "WooCommerce Custom Fields is activated", 'import-users-from-csv-with-meta' ); ?></th>
36
+ <td>
37
+ <?php _e( "You can import those fields, look at fields you can import using the column names shown below.", 'import-users-from-csv-with-meta' ); ?>.
38
+ <ul style="list-style:square inside none; margin-left:2em;">
39
+ <?php foreach ( $this->get_fields() as $field => $field_id): ?>
40
+ <li><?php echo $field; ?></li>
41
+ <?php endforeach; ?>
42
+ </ul>
43
+ </td>
44
+ </tr>
45
+ <?php
46
+ }
47
+
48
+ function restricted_fields( $acui_restricted_fields ){
49
+ return array_merge( $acui_restricted_fields, array_keys( $this->get_fields() ) );
50
+ }
51
+
52
+ function import( $headers, $row, $user_id ){
53
+ $context = 'user_field';
54
+ $columns = array();
55
+ $data = array();
56
+
57
+ foreach ( $this->get_fields() as $key => $value ) {
58
+ $pos = array_search( $value, $headers );
59
+
60
+ if( $pos !== FALSE ){
61
+ $columns[ $value ] = $pos;
62
+ $data[ $value ] = $row[ $columns[ $value ] ];
63
+ }
64
+ }
65
+
66
+ $values = array();
67
+ foreach ( $this->get_fields() as $key => $value ) {
68
+ $values[ $key ] = array( 'value' => $data[ $value ], 'data' => array(), 'files' => array() );
69
+ }
70
+
71
+ $item = RightPress_Help::wc_get_customer( $user_id );
72
+
73
+ if( empty( $values ) || !is_array( $values ) ){
74
+ return;
75
+ }
76
+
77
+ foreach( $values as $field_id => $field_value ) {
78
+ $field = WCCF_Field_Controller::get( $field_id, 'wccf_' . $context );
79
+
80
+ if ( !$field ) {
81
+ continue;
82
+ }
83
+
84
+ $field->store_value( $item, $field_value );
85
+ }
86
+
87
+ $item->save();
88
+ }
89
+ }
90
+
91
+ new ACUI_WCF();
addons/wp-access-area.php ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ if ( ! defined( 'ABSPATH' ) ) exit;
4
+
5
+ if( !is_plugin_active( 'wp-access-areas/wp-access-areas.php' ) ){
6
+ return;
7
+ }
8
+
9
+ class ACUI_WP_Access_Area{
10
+ function __construct(){
11
+ }
12
+
13
+ function hooks(){
14
+ add_filter( 'acui_restricted_fields', array( $this, 'restricted_fields' ), 10, 1 );
15
+ add_action( 'post_acui_import_single_user', array( $this, 'import' ), 10, 3 );
16
+ }
17
+
18
+ function restricted_fields( $acui_restricted_fields ){
19
+ return array_merge( $acui_restricted_fields, array( 'wp-access-areas' ) );
20
+ }
21
+
22
+ function import( $headers, $row, $user_id ){
23
+ $pos = array_search( 'wp-access-areas', $headers );
24
+
25
+ if( $pos === FALSE )
26
+ return;
27
+
28
+ $wpaa_labels = WPAA_AccessArea::get_available_userlabels();
29
+ $active_labels = array_map( 'trim', explode( "#", $row[ $pos ] ) );
30
+
31
+ foreach( $wpaa_labels as $wpa_label ){
32
+ if( in_array( $wpa_label->cap_title , $active_labels )){
33
+ $this->set_cap_for_user( $wpa_label->capability , $user_object , true );
34
+ }
35
+ else{
36
+ $this->set_cap_for_user( $wpa_label->capability , $user_object , false );
37
+ }
38
+ }
39
+ }
40
+
41
+ function set_cap_for_user( $capability , &$user , $add ) {
42
+ $has_cap = $user->has_cap( $capability );
43
+ $is_change = ($add && ! $has_cap) || (!$add && $has_cap);
44
+ if ( $is_change ) {
45
+ if ( $add ) {
46
+ $user->add_cap( $capability , true );
47
+ do_action( 'wpaa_grant_access' , $user , $capability );
48
+ do_action( "wpaa_grant_{$capability}" , $user );
49
+ } else if ( ! $add ) {
50
+ $user->remove_cap( $capability );
51
+ do_action( 'wpaa_revoke_access' , $user , $capability );
52
+ do_action( "wpaa_revoke_{$capability}" , $user );
53
+ }
54
+ }
55
+ }
56
+ }
57
+
58
+ $acui_wp_access_area = new ACUI_WP_Access_Area();
59
+ $acui_wp_access_area->hooks();
addons/wp-members.php CHANGED
@@ -7,8 +7,6 @@ if( !is_plugin_active( 'wp-members/wp-members.php' ) ){
7
  }
8
 
9
  add_action( 'acui_tab_import_before_import_button', 'acui_wp_members_tab_import_before_import_button' );
10
- add_action( 'acui_tab_frontend_before_save_button', 'acui_wp_members_tab_frontend_before_save_button' );
11
-
12
  function acui_wp_members_tab_import_before_import_button(){
13
  ?>
14
  <h2><?php _e( 'WP Members compatibility', 'import-users-from-csv-with-meta'); ?></h2>
@@ -32,6 +30,7 @@ function acui_wp_members_tab_import_before_import_button(){
32
  <?php
33
  }
34
 
 
35
  function acui_wp_members_tab_frontend_before_save_button(){
36
  ?>
37
  <h2><?php _e( 'WP Members compatibility', 'import-users-from-csv-with-meta'); ?></h2>
@@ -52,4 +51,12 @@ function acui_wp_members_tab_frontend_before_save_button(){
52
  </tbody>
53
  </table>
54
  <?php
 
 
 
 
 
 
 
 
55
  }
7
  }
8
 
9
  add_action( 'acui_tab_import_before_import_button', 'acui_wp_members_tab_import_before_import_button' );
 
 
10
  function acui_wp_members_tab_import_before_import_button(){
11
  ?>
12
  <h2><?php _e( 'WP Members compatibility', 'import-users-from-csv-with-meta'); ?></h2>
30
  <?php
31
  }
32
 
33
+ add_action( 'acui_tab_frontend_before_save_button', 'acui_wp_members_tab_frontend_before_save_button' );
34
  function acui_wp_members_tab_frontend_before_save_button(){
35
  ?>
36
  <h2><?php _e( 'WP Members compatibility', 'import-users-from-csv-with-meta'); ?></h2>
51
  </tbody>
52
  </table>
53
  <?php
54
+ }
55
+
56
+ add_action( 'post_acui_import_single_user', 'acui_wp_members_post_acui_import_single_user', 10, 6 );
57
+ function acui_wp_members_post_acui_import_single_user( $headers, $data, $user_id, $role, $positions, $form_data ){
58
+ $activate_users_wp_members = ( !isset( $form_data["activate_users_wp_members"] ) || empty( $form_data["activate_users_wp_members"] ) ) ? "no_activate" : sanitize_text_field( $form_data["activate_users_wp_members"] );
59
+ if( $activate_users_wp_members == "activate" ){
60
+ update_user_meta( $user_id, "active", true );
61
+ }
62
  }
classes/columns.php CHANGED
@@ -5,6 +5,8 @@ class ACUI_Columns{
5
  function __construct(){
6
  //add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) );
7
 
 
 
8
  if( get_option( 'acui_show_profile_fields' ) == true ){
9
  add_action( "user_new_form", array( $this, "extra_user_profile_fields" ) );
10
  add_action( "show_user_profile", array( $this, "extra_user_profile_fields" ) );
@@ -162,7 +164,8 @@ class ACUI_Columns{
162
  }
163
 
164
  function extra_user_profile_fields( $user ) {
165
- $acui_restricted_fields = acui_get_restricted_fields();
 
166
  $headers = get_option("acui_columns");
167
 
168
  if( is_array( $headers ) && !empty( $headers ) ):
@@ -186,8 +189,9 @@ class ACUI_Columns{
186
  }
187
 
188
  function save_extra_user_profile_fields( $user_id ){
 
189
  $headers = get_option("acui_columns");
190
- $acui_restricted_fields = acui_get_restricted_fields();
191
 
192
  $post_filtered = filter_input_array( INPUT_POST );
193
 
@@ -201,6 +205,18 @@ class ACUI_Columns{
201
  }
202
  endif;
203
  }
 
 
 
 
 
 
 
 
 
 
 
 
204
  }
205
 
206
  new ACUI_Columns();
5
  function __construct(){
6
  //add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) );
7
 
8
+ add_action( 'acui_columns_save_settings', array( $this, 'save_settings' ), 10, 1 );
9
+
10
  if( get_option( 'acui_show_profile_fields' ) == true ){
11
  add_action( "user_new_form", array( $this, "extra_user_profile_fields" ) );
12
  add_action( "show_user_profile", array( $this, "extra_user_profile_fields" ) );
164
  }
165
 
166
  function extra_user_profile_fields( $user ) {
167
+ $acui_helper = new ACUI_Helper();
168
+ $acui_restricted_fields = $acui_helper->get_restricted_fields();
169
  $headers = get_option("acui_columns");
170
 
171
  if( is_array( $headers ) && !empty( $headers ) ):
189
  }
190
 
191
  function save_extra_user_profile_fields( $user_id ){
192
+ $acui_helper = new ACUI_Helper();
193
  $headers = get_option("acui_columns");
194
+ $acui_restricted_fields = $acui_helper->get_restricted_fields();
195
 
196
  $post_filtered = filter_input_array( INPUT_POST );
197
 
205
  }
206
  endif;
207
  }
208
+
209
+ public static function save_settings( $form_data ){
210
+ if ( !isset( $form_data['security'] ) || !wp_verify_nonce( $form_data['security'], 'codection-security' ) ) {
211
+ wp_die( __( 'Nonce check failed', 'import-users-from-csv-with-meta' ) );
212
+ }
213
+
214
+ if( isset( $form_data['show-profile-fields-action'] ) && $form_data['show-profile-fields-action'] == 'update' )
215
+ update_option( "acui_show_profile_fields", isset( $form_data["show-profile-fields"] ) && $form_data["show-profile-fields"] == "yes" );
216
+
217
+ if( isset( $form_data['reset-profile-fields-action'] ) && $form_data['reset-profile-fields-action'] == 'reset' )
218
+ update_option( "acui_columns", array() );
219
+ }
220
  }
221
 
222
  new ACUI_Columns();
classes/cron.php CHANGED
@@ -6,6 +6,7 @@ class ACUI_Cron{
6
  function __construct(){
7
  add_action( 'acui_cron_save_settings', array( $this, 'save_settings' ), 10, 1 );
8
  add_action( 'acui_cron_process', array( $this, 'process' ), 10 );
 
9
  }
10
 
11
  function save_settings( $form_data ){
@@ -67,7 +68,8 @@ class ACUI_Cron{
67
  $form_data[ "security" ] = wp_create_nonce( "codection-security" );
68
 
69
  ob_start();
70
- acui_fileupload_process( $form_data, true );
 
71
  $message .= "<br/>" . ob_get_contents() . "<br/>";
72
  ob_end_clean();
73
 
@@ -221,7 +223,7 @@ class ACUI_Cron{
221
  else
222
  echo "<option value=''>" . __( 'Disable role assignment in cron import', 'import-users-from-csv-with-meta' ) . "</option>";
223
 
224
- $list_roles = acui_get_editable_roles();
225
  foreach ($list_roles as $key => $value) {
226
  if($key == $role)
227
  echo "<option selected='selected' value='$key'>$value</option>";
@@ -314,7 +316,7 @@ class ACUI_Cron{
314
  <div style="margin-left:25px;">
315
  <select name="cron-change-role-not-present-role" id="cron-change-role-not-present-role">
316
  <?php
317
- $list_roles = acui_get_editable_roles();
318
  foreach ($list_roles as $key => $value):
319
  ?>
320
  <option value='<?php echo $key; ?>' <?php selected( $cron_change_role_not_present_role, $key ); ?> ><?php echo $value; ?></option>
@@ -358,6 +360,7 @@ class ACUI_Cron{
358
  </table>
359
  <?php wp_nonce_field( 'codection-security', 'security' ); ?>
360
  <input class="button-primary" type="submit" value="<?php _e( 'Save schedule options', 'import-users-from-csv-with-meta'); ?>"/>
 
361
  </form>
362
 
363
  <script>
@@ -368,6 +371,26 @@ class ACUI_Cron{
368
  check_delete_users_checked();
369
  });
370
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
371
  function check_delete_users_checked(){
372
  if( $('#cron-delete-users').is(':checked') ){
373
  $( '#cron-change-role-not-present-role' ).prop( 'disabled', true );
@@ -411,6 +434,14 @@ class ACUI_Cron{
411
  </script>
412
  <?php
413
  }
 
 
 
 
 
 
 
 
414
  }
415
 
416
  new ACUI_Cron();
6
  function __construct(){
7
  add_action( 'acui_cron_save_settings', array( $this, 'save_settings' ), 10, 1 );
8
  add_action( 'acui_cron_process', array( $this, 'process' ), 10 );
9
+ add_action( 'wp_ajax_acui_fire_cron', array( $this, 'ajax_fire_cron' ) );
10
  }
11
 
12
  function save_settings( $form_data ){
68
  $form_data[ "security" ] = wp_create_nonce( "codection-security" );
69
 
70
  ob_start();
71
+ $acui_import = new ACUI_Import();
72
+ $acui_import->fileupload_process( $form_data, true );
73
  $message .= "<br/>" . ob_get_contents() . "<br/>";
74
  ob_end_clean();
75
 
223
  else
224
  echo "<option value=''>" . __( 'Disable role assignment in cron import', 'import-users-from-csv-with-meta' ) . "</option>";
225
 
226
+ $list_roles = ACUI_Helper::get_editable_roles();
227
  foreach ($list_roles as $key => $value) {
228
  if($key == $role)
229
  echo "<option selected='selected' value='$key'>$value</option>";
316
  <div style="margin-left:25px;">
317
  <select name="cron-change-role-not-present-role" id="cron-change-role-not-present-role">
318
  <?php
319
+ $list_roles = ACUI_Helper::get_editable_roles();
320
  foreach ($list_roles as $key => $value):
321
  ?>
322
  <option value='<?php echo $key; ?>' <?php selected( $cron_change_role_not_present_role, $key ); ?> ><?php echo $value; ?></option>
360
  </table>
361
  <?php wp_nonce_field( 'codection-security', 'security' ); ?>
362
  <input class="button-primary" type="submit" value="<?php _e( 'Save schedule options', 'import-users-from-csv-with-meta'); ?>"/>
363
+ <input id="cron-execute-cron-task-now" class="button-primary" type="button" value="<?php _e( 'Execute cron task now', 'import-users-from-csv-with-meta'); ?>"/>
364
  </form>
365
 
366
  <script>
371
  check_delete_users_checked();
372
  });
373
 
374
+ $( '#cron-execute-cron-task-now' ).click( function(){
375
+ $( this )
376
+ .prop( 'disabled', true )
377
+ .val( 'Loading...' );
378
+
379
+ var data = {
380
+ 'action': 'acui_fire_cron',
381
+ 'security': '<?php echo wp_create_nonce( "codection-security" ); ?>'
382
+ };
383
+
384
+ $.post( ajaxurl, data, function( response ) {
385
+ if( response != "OK" )
386
+ alert( "<?php _e( 'Problems executing cron task: ', 'import-users-from-csv-with-meta' ); ?>" + response );
387
+ else{
388
+ alert( "<?php _e( 'Cron task successfully executed', 'import-users-from-csv-with-meta' ); ?>" );
389
+ document.location.reload();
390
+ }
391
+ });
392
+ } );
393
+
394
  function check_delete_users_checked(){
395
  if( $('#cron-delete-users').is(':checked') ){
396
  $( '#cron-change-role-not-present-role' ).prop( 'disabled', true );
434
  </script>
435
  <?php
436
  }
437
+
438
+ function ajax_fire_cron(){
439
+ check_ajax_referer( 'codection-security', 'security' );
440
+
441
+ do_action( 'acui_cron_process' );
442
+ echo "OK";
443
+ wp_die();
444
+ }
445
  }
446
 
447
  new ACUI_Cron();
classes/email-options.php CHANGED
@@ -184,8 +184,10 @@ class ACUI_Email_Options{
184
  }
185
 
186
  public static function send_email( $user_object, $positions = array(), $headers = array(), $data = array(), $created = false, $password = '' ){
 
 
187
  $key = get_password_reset_key( $user_object );
188
- $wp_users_fields = acui_get_wp_users_fields();
189
 
190
  $user_id = $user_object->ID;
191
  $user_login= $user_object->user_login;
@@ -292,5 +294,4 @@ class ACUI_Email_Options{
292
  return $tags;
293
  }
294
  }
295
-
296
  new ACUI_Email_Options();
184
  }
185
 
186
  public static function send_email( $user_object, $positions = array(), $headers = array(), $data = array(), $created = false, $password = '' ){
187
+ $acui_helper = new ACUI_Helper();
188
+
189
  $key = get_password_reset_key( $user_object );
190
+ $wp_users_fields = $acui_helper->get_wp_users_fields();
191
 
192
  $user_id = $user_object->ID;
193
  $user_login= $user_object->user_login;
294
  return $tags;
295
  }
296
  }
 
297
  new ACUI_Email_Options();
classes/export.php CHANGED
@@ -21,7 +21,7 @@ class ACUI_Exporter{
21
  }
22
 
23
  public static function admin_gui(){
24
- $roles = acui_get_editable_roles();
25
  ?>
26
  <h3 id="acui_export_users_header"><?php _e( 'Export users', 'import-users-from-csv-with-meta' ); ?></h3>
27
  <form id="acui_export_users_wrapper" method="POST" target="_blank" enctype="multipart/form-data" action="<?php echo admin_url( 'admin-ajax.php' ); ?>">
@@ -137,10 +137,13 @@ class ACUI_Exporter{
137
  return $value;
138
  }
139
 
140
- public static function prepare( $key, $value, $datetime_format ){
141
  $timestamp_keys = apply_filters( 'acui_export_timestamp_keys', array( 'wc_last_active' ) );
142
  $non_date_keys = apply_filters( 'acui_export_non_date_keys', array() );
143
 
 
 
 
144
  if( is_array( $value ) ){
145
  return serialize( $value );
146
  }
@@ -158,7 +161,7 @@ class ACUI_Exporter{
158
  }
159
  }
160
 
161
- function get_role( $user_id ){
162
  $user = get_user_by( 'id', $user_id );
163
  return implode( ',', $user->roles );
164
  }
@@ -203,8 +206,6 @@ class ACUI_Exporter{
203
  $row[] = $key;
204
  }
205
 
206
- $row[] = "role";
207
-
208
  foreach ( $this->get_user_meta_keys() as $key ) {
209
  $row[] = $key;
210
  }
@@ -221,13 +222,11 @@ class ACUI_Exporter{
221
 
222
  foreach ( $this->user_data as $key ) {
223
  $key = apply_filters( 'acui_export_get_key_user_data', $key );
224
- $row[ $key ] = self::prepare( $key, $userdata->data->{$key}, $datetime_format );
225
  }
226
 
227
- $row['role'] = $this->get_role( $user );
228
-
229
  foreach ( $this->get_user_meta_keys() as $key ) {
230
- $row[ $key ] = self::prepare( $key, get_user_meta( $user, $key, true ), $datetime_format );
231
  }
232
 
233
  $row = apply_filters( 'acui_export_data', $row, $user, $datetime_format, $columns, $order_fields_alphabetically );
@@ -250,7 +249,7 @@ class ACUI_Exporter{
250
  header( "Content-type: text/csv;charset=utf-8" );
251
  header( "Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"" );
252
  header( "Content-length: $fsize" );
253
- header( "Cache-control: private" );
254
  header( "Content-Description: File Transfer" );
255
  header( "Content-Transfer-Encoding: binary" );
256
  header( "Expires: 0" );
21
  }
22
 
23
  public static function admin_gui(){
24
+ $roles = ACUI_Helper::get_editable_roles();
25
  ?>
26
  <h3 id="acui_export_users_header"><?php _e( 'Export users', 'import-users-from-csv-with-meta' ); ?></h3>
27
  <form id="acui_export_users_wrapper" method="POST" target="_blank" enctype="multipart/form-data" action="<?php echo admin_url( 'admin-ajax.php' ); ?>">
137
  return $value;
138
  }
139
 
140
+ public static function prepare( $key, $value, $datetime_format, $user = 0 ){
141
  $timestamp_keys = apply_filters( 'acui_export_timestamp_keys', array( 'wc_last_active' ) );
142
  $non_date_keys = apply_filters( 'acui_export_non_date_keys', array() );
143
 
144
+ if( $key == 'role' ){
145
+ return self::get_role( $user );
146
+ }
147
  if( is_array( $value ) ){
148
  return serialize( $value );
149
  }
161
  }
162
  }
163
 
164
+ static function get_role( $user_id ){
165
  $user = get_user_by( 'id', $user_id );
166
  return implode( ',', $user->roles );
167
  }
206
  $row[] = $key;
207
  }
208
 
 
 
209
  foreach ( $this->get_user_meta_keys() as $key ) {
210
  $row[] = $key;
211
  }
222
 
223
  foreach ( $this->user_data as $key ) {
224
  $key = apply_filters( 'acui_export_get_key_user_data', $key );
225
+ $row[ $key ] = self::prepare( $key, $userdata->data->{$key}, $datetime_format, $user );
226
  }
227
 
 
 
228
  foreach ( $this->get_user_meta_keys() as $key ) {
229
+ $row[ $key ] = self::prepare( $key, get_user_meta( $user, $key, true ), $datetime_format, $user );
230
  }
231
 
232
  $row = apply_filters( 'acui_export_data', $row, $user, $datetime_format, $columns, $order_fields_alphabetically );
249
  header( "Content-type: text/csv;charset=utf-8" );
250
  header( "Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"" );
251
  header( "Content-length: $fsize" );
252
+ header( "Cache-control: privfilefleate" );
253
  header( "Content-Description: File Transfer" );
254
  header( "Content-Transfer-Encoding: binary" );
255
  header( "Expires: 0" );
classes/frontend.php CHANGED
@@ -73,7 +73,7 @@ class ACUI_Frontend{
73
  else
74
  echo "<option value=''>" . __( 'Disable role assignment in frontend import', 'import-users-from-csv-with-meta' ) . "</option>";
75
 
76
- $list_roles = acui_get_editable_roles();
77
  foreach ($list_roles as $key => $value) {
78
  if($key == $role)
79
  echo "<option selected='selected' value='$key'>$value</option>";
@@ -178,7 +178,7 @@ class ACUI_Frontend{
178
  <div style="margin-left:25px;">
179
  <select name="change-role-not-present-role-frontend" id="change-role-not-present-role-frontend">
180
  <?php
181
- $list_roles = acui_get_editable_roles();
182
  foreach ($list_roles as $key => $value):
183
  ?>
184
  <option value='<?php echo $key; ?>' <?php selected( $change_role_not_present_role_frontend, $key ); ?>><?php echo $value; ?></option>
@@ -300,7 +300,9 @@ class ACUI_Frontend{
300
  $form_data[ "empty_cell_action" ] = "leave";
301
  $form_data[ "activate_users_wp_members" ] = empty( get_option( "acui_frontend_activate_users_wp_members" ) ) ? 'no_activate' : get_option( "acui_frontend_activate_users_wp_members" );
302
  $form_data[ "security" ] = wp_create_nonce( "codection-security" );
303
- acui_fileupload_process( $form_data, false, true );
 
 
304
 
305
  wp_delete_attachment( $csv_file_id, true );
306
 
73
  else
74
  echo "<option value=''>" . __( 'Disable role assignment in frontend import', 'import-users-from-csv-with-meta' ) . "</option>";
75
 
76
+ $list_roles = ACUI_Helper::get_editable_roles();
77
  foreach ($list_roles as $key => $value) {
78
  if($key == $role)
79
  echo "<option selected='selected' value='$key'>$value</option>";
178
  <div style="margin-left:25px;">
179
  <select name="change-role-not-present-role-frontend" id="change-role-not-present-role-frontend">
180
  <?php
181
+ $list_roles = ACUI_Helper::get_editable_roles();
182
  foreach ($list_roles as $key => $value):
183
  ?>
184
  <option value='<?php echo $key; ?>' <?php selected( $change_role_not_present_role_frontend, $key ); ?>><?php echo $value; ?></option>
300
  $form_data[ "empty_cell_action" ] = "leave";
301
  $form_data[ "activate_users_wp_members" ] = empty( get_option( "acui_frontend_activate_users_wp_members" ) ) ? 'no_activate' : get_option( "acui_frontend_activate_users_wp_members" );
302
  $form_data[ "security" ] = wp_create_nonce( "codection-security" );
303
+
304
+ $acui_import = new ACUI_Import();
305
+ $acui_import->fileupload_process( $form_data, false, true );
306
 
307
  wp_delete_attachment( $csv_file_id, true );
308
 
classes/helper.php ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class ACUI_Helper{
4
+ public function detect_delimiter( $file ) {
5
+ $delimiters = array(
6
+ ';' => 0,
7
+ ',' => 0,
8
+ "\t" => 0,
9
+ "|" => 0
10
+ );
11
+
12
+ $handle = @fopen($file, "r");
13
+ $firstLine = fgets($handle);
14
+ fclose($handle);
15
+ foreach ($delimiters as $delimiter => &$count) {
16
+ $count = count(str_getcsv($firstLine, $delimiter));
17
+ }
18
+
19
+ return array_search(max($delimiters), $delimiters);
20
+ }
21
+
22
+ public function user_id_exists( $user_id ){
23
+ if ( get_userdata( $user_id ) === false )
24
+ return false;
25
+ else
26
+ return true;
27
+ }
28
+
29
+ public function get_roles_by_user_id( $user_id ){
30
+ $roles = array();
31
+ $user = new WP_User( $user_id );
32
+
33
+ if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
34
+ foreach ( $user->roles as $role )
35
+ $roles[] = $role;
36
+ }
37
+
38
+ return $roles;
39
+ }
40
+
41
+ public static function get_editable_roles() {
42
+ global $wp_roles;
43
+
44
+ $all_roles = $wp_roles->roles;
45
+ $editable_roles = apply_filters('editable_roles', $all_roles);
46
+ $list_editable_roles = array();
47
+
48
+ foreach ($editable_roles as $key => $editable_role)
49
+ $list_editable_roles[$key] = $editable_role["name"];
50
+
51
+ return $list_editable_roles;
52
+ }
53
+
54
+ public function string_conversion( $string ){
55
+ if(!preg_match('%(?:
56
+ [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
57
+ |\xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
58
+ |[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
59
+ |\xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
60
+ |\xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
61
+ |[\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
62
+ |\xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
63
+ )+%xs', $string)){
64
+ return utf8_encode($string);
65
+ }
66
+ else
67
+ return $string;
68
+ }
69
+
70
+ public function get_wp_users_fields(){
71
+ 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" );
72
+ }
73
+
74
+ function get_restricted_fields(){
75
+ $wp_users_fields = $this->get_wp_users_fields();
76
+ $wp_min_fields = array( "Username", "Email", "role" );
77
+ $acui_restricted_fields = array_merge( $wp_users_fields, $wp_min_fields );
78
+
79
+ return apply_filters( 'acui_restricted_fields', $acui_restricted_fields );
80
+ }
81
+
82
+ function get_not_meta_fields(){
83
+ return apply_filters( 'acui_not_meta_fields', array() );
84
+ }
85
+
86
+ public function get_random_unique_username( $prefix = '' ){
87
+ do {
88
+ $rnd_str = sprintf("%06d", mt_rand(1, 999999));
89
+ } while( username_exists( $prefix . $rnd_str ) );
90
+
91
+ return $prefix . $rnd_str;
92
+ }
93
+
94
+ public function maybe_update_email( $user_id, $email, $password, $update_emails_existing_users ){
95
+ $user_object = get_user_by( 'id', $user_id );
96
+
97
+ if( $user_object->user_email == $email )
98
+ return $user_id;
99
+
100
+ switch( $update_emails_existing_users ){
101
+ case 'yes':
102
+ $user_id = wp_update_user( array( 'ID' => $user_id, 'user_email' => $email ) );
103
+ break;
104
+
105
+ case 'no':
106
+ $user_id = 0;
107
+ break;
108
+
109
+ case 'create':
110
+ $user_id = wp_insert_user( array(
111
+ 'user_login' => $this->get_random_unique_username( 'duplicated_username_' ),
112
+ 'user_email' => $email,
113
+ 'user_pass' => $password
114
+ ) );
115
+ break;
116
+
117
+ }
118
+
119
+ return $user_id;
120
+ }
121
+
122
+ public static function get_attachment_id_by_url( $url ) {
123
+ $wp_upload_dir = wp_upload_dir();
124
+ // Strip out protocols, so it doesn't fail because searching for http: in https: dir.
125
+ $dir = set_url_scheme( trailingslashit( $wp_upload_dir['baseurl'] ), 'relative' );
126
+
127
+ // Is URL in uploads directory?
128
+ if ( false !== strpos( $url, $dir ) ) {
129
+
130
+ $file = basename( $url );
131
+
132
+ $query_args = array(
133
+ 'post_type' => 'attachment',
134
+ 'post_status' => 'inherit',
135
+ 'fields' => 'ids',
136
+ 'meta_query' => array(
137
+ array(
138
+ 'key' => '_wp_attachment_metadata',
139
+ 'compare' => 'LIKE',
140
+ 'value' => $file,
141
+ ),
142
+ ),
143
+ );
144
+
145
+ $query = new WP_Query( $query_args );
146
+
147
+ if ( $query->have_posts() ) {
148
+ foreach ( $query->posts as $attachment_id ) {
149
+ $meta = wp_get_attachment_metadata( $attachment_id );
150
+ $original_file = basename( $meta['file'] );
151
+ $cropped_files = wp_list_pluck( $meta['sizes'], 'file' );
152
+
153
+ if ( $original_file === $file || in_array( $file, $cropped_files ) ) {
154
+ return (int) $attachment_id;
155
+ }
156
+ }
157
+ }
158
+ }
159
+
160
+ return false;
161
+ }
162
+ }
classes/homepage.php CHANGED
@@ -51,7 +51,7 @@ class ACUI_Homepage{
51
  <th scope="row"><label for="role"><?php _e( 'Default role', 'import-users-from-csv-with-meta' ); ?></label></th>
52
  <td>
53
  <?php
54
- $list_roles = acui_get_editable_roles();
55
 
56
  foreach ($list_roles as $key => $value) {
57
  if( in_array( $key, $last_roles_used ) )
@@ -181,7 +181,7 @@ class ACUI_Homepage{
181
  <div style="margin-left:25px;">
182
  <select id="change_role_not_present_role" name="change_role_not_present_role">
183
  <?php
184
- $list_roles = acui_get_editable_roles();
185
 
186
  foreach ($list_roles as $key => $value) {
187
  echo "<option value='$key'>$value</option>";
51
  <th scope="row"><label for="role"><?php _e( 'Default role', 'import-users-from-csv-with-meta' ); ?></label></th>
52
  <td>
53
  <?php
54
+ $list_roles = ACUI_Helper::get_editable_roles();
55
 
56
  foreach ($list_roles as $key => $value) {
57
  if( in_array( $key, $last_roles_used ) )
181
  <div style="margin-left:25px;">
182
  <select id="change_role_not_present_role" name="change_role_not_present_role">
183
  <?php
184
+ $list_roles = ACUI_Helper::get_editable_roles();
185
 
186
  foreach ($list_roles as $key => $value) {
187
  echo "<option value='$key'>$value</option>";
classes/import.php CHANGED
@@ -1,42 +1,771 @@
1
  <?php
2
 
3
  class ACUI_Import{
4
- public function get_random_unique_username( $prefix = '' ){
5
- do {
6
- $rnd_str = sprintf("%06d", mt_rand(1, 999999));
7
- } while( username_exists( $prefix . $rnd_str ) );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
- return $prefix . $rnd_str;
10
- }
11
-
12
- public function maybe_update_email( $user_id, $email, $password, $update_emails_existing_users ){
13
- $user_object = get_user_by( 'id', $user_id );
14
-
15
- if( $user_object->user_email == $email )
16
- return $user_id;
17
-
18
- switch( $update_emails_existing_users ){
19
- case 'yes':
20
- $user_id = wp_update_user( array( 'ID' => $user_id, 'user_email' => $email ) );
21
  break;
22
-
23
- case 'no':
24
- $user_id = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
- case 'create':
28
- $user_id = wp_insert_user( array(
29
- 'user_login' => $this->get_random_unique_username( 'duplicated_username_' ),
30
- 'user_email' => $email,
31
- 'user_pass' => $password
32
- ) );
33
- break;
34
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  }
 
 
36
 
37
- return $user_id;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  }
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  public function basic_css(){
41
  ?>
42
  <style type="text/css">
1
  <?php
2
 
3
  class ACUI_Import{
4
+ public function show(){
5
+ if ( !current_user_can( apply_filters( 'acui_capability', 'create_users' ) ) ) {
6
+ wp_die( __( 'You are not allowed to see this content.', 'import-users-from-csv-with-meta' ));
7
+ }
8
+
9
+ $tab = ( isset ( $_GET['tab'] ) ) ? $_GET['tab'] : 'homepage';
10
+
11
+ if( isset( $_POST ) && !empty( $_POST ) ):
12
+ if ( !wp_verify_nonce( $_POST['security'], 'codection-security' ) ) {
13
+ wp_die( __( 'Nonce check failed', 'import-users-from-csv-with-meta' ) );
14
+ }
15
+
16
+ switch ( $tab ){
17
+ case 'homepage':
18
+ update_option( 'acui_last_roles_used', ( empty( $_POST['role'] ) ? '' : array_map( 'sanitize_text_field', $_POST['role'] ) ) );
19
+ $this->fileupload_process( $_POST, false );
20
+ return;
21
+ break;
22
+
23
+ case 'frontend':
24
+ do_action( 'acui_frontend_save_settings', $_POST );
25
+ break;
26
+
27
+ case 'columns':
28
+ do_action( 'acui_columns_save_settings', $_POST );
29
+ break;
30
+
31
+ case 'mail-options':
32
+ do_action( 'acui_mail_options_save_settings', $_POST );
33
+ break;
34
+
35
+ case 'cron':
36
+ do_action( 'acui_cron_save_settings', $_POST );
37
+ break;
38
+ }
39
+ endif;
40
 
41
+ ACUI_Import::admin_tabs( $tab );
42
+
43
+ switch ( $tab ){
44
+ case 'homepage' :
45
+ ACUI_Homepage::admin_gui();
 
 
 
 
 
 
 
46
  break;
47
+
48
+ case 'export' :
49
+ ACUI_Exporter::admin_gui();
50
+ break;
51
+
52
+ case 'frontend':
53
+ ACUI_Frontend::admin_gui();
54
+ break;
55
+
56
+ case 'columns':
57
+ ACUI_Columns::admin_gui();
58
+ break;
59
+
60
+ case 'meta-keys':
61
+ ACUI_MetaKeys::admin_gui();
62
+ break;
63
+
64
+ case 'doc':
65
+ ACUI_Doc::message();
66
+ break;
67
+
68
+ case 'mail-options':
69
+ ACUI_Email_Options::admin_gui();
70
+ break;
71
+
72
+ case 'cron':
73
+ ACUI_Cron::admin_gui();
74
+ break;
75
+
76
+ case 'donate':
77
+ ACUI_Donate::message();
78
  break;
79
+
80
+ case 'help':
81
+ ACUI_Help::message();
82
+ break;
83
+
84
+ case 'new_features':
85
+ ACUI_NewFeatures::message();
86
+ break;
87
+
88
+ default:
89
+ do_action( 'acui_tab_action_' . $tab );
90
+ break;
91
+ }
92
+ }
93
 
94
+ public static function admin_tabs( $current = 'homepage' ) {
95
+ $tabs = array(
96
+ 'homepage' => __( 'Import', 'import-users-from-csv-with-meta' ),
97
+ 'export' => __( 'Export', 'import-users-from-csv-with-meta' ),
98
+ 'frontend' => __( 'Frontend', 'import-users-from-csv-with-meta' ),
99
+ 'cron' => __( 'Cron import', 'import-users-from-csv-with-meta' ),
100
+ 'columns' => __( 'Extra profile fields', 'import-users-from-csv-with-meta' ),
101
+ 'meta-keys' => __( 'Meta keys', 'import-users-from-csv-with-meta' ),
102
+ 'mail-options' => __( 'Mail options', 'import-users-from-csv-with-meta' ),
103
+ 'doc' => __( 'Documentation', 'import-users-from-csv-with-meta' ),
104
+ 'donate' => __( 'Donate/Patreon', 'import-users-from-csv-with-meta' ),
105
+ 'shop' => __( 'Shop', 'import-users-from-csv-with-meta' ),
106
+ 'help' => __( 'Hire an expert', 'import-users-from-csv-with-meta' ),
107
+ 'new_features' => __( 'New features', 'import-users-from-csv-with-meta' )
108
+ );
109
+
110
+ $tabs = apply_filters( 'acui_tabs', $tabs );
111
+
112
+ echo '<div id="icon-themes" class="icon32"><br></div>';
113
+ echo '<h2 class="nav-tab-wrapper">';
114
+ foreach( $tabs as $tab => $name ){
115
+ $class = ( $tab == $current ) ? ' nav-tab-active' : '';
116
+
117
+ if( $tab == "shop" ){
118
+ $href = "https://codection.com/tienda/";
119
+ $target = "_blank";
120
+ }
121
+ else{
122
+ $href = "?page=acui&tab=$tab";
123
+ $target = "_self";
124
+ }
125
+
126
+ echo "<a class='nav-tab$class' href='$href' target='$target'>$name</a>";
127
+
128
  }
129
+ echo '</h2>';
130
+ }
131
 
132
+ public function fileupload_process( $form_data, $is_cron = false, $is_frontend = false ) {
133
+ if ( !defined( 'DOING_CRON' ) && ( !isset( $form_data['security'] ) || !wp_verify_nonce( $form_data['security'], 'codection-security' ) ) ){
134
+ wp_die( __( 'Nonce check failed', 'import-users-from-csv-with-meta' ) );
135
+ }
136
+
137
+ if( empty( $_FILES['uploadfile']['name'] ) || $is_frontend ):
138
+ $path_to_file = wp_normalize_path( $form_data["path_to_file"] );
139
+
140
+ if( validate_file( $path_to_file ) !== 0 ){
141
+ wp_die( __( 'Error, path to file is not well written', 'import-users-from-csv-with-meta' ) . ": $path_to_file" );
142
+ }
143
+
144
+ if( !file_exists ( $path_to_file ) ){
145
+ wp_die( __( 'Error, we cannot find the file', 'import-users-from-csv-with-meta' ) . ": $path_to_file" );
146
+ }
147
+
148
+ $this->import_users( $path_to_file, $form_data, 0, $is_cron, $is_frontend );
149
+ else:
150
+ $uploadfile = wp_handle_upload( $_FILES['uploadfile'], array( 'test_form' => false, 'mimes' => array('csv' => 'text/csv') ) );
151
+
152
+ if ( !$uploadfile || isset( $uploadfile['error'] ) ) {
153
+ wp_die( __( 'Problem uploading file to import. Error details: ' . var_export( $uploadfile['error'], true ), 'import-users-from-csv-with-meta' ));
154
+ } else {
155
+ $this->import_users( $uploadfile['file'], $form_data, ACUI_Helper::get_attachment_id_by_url( $uploadfile['url'] ), $is_cron, $is_frontend );
156
+ }
157
+ endif;
158
  }
159
 
160
+ public function import_users( $file, $form_data, $attach_id = 0, $is_cron = false, $is_frontend = false ){
161
+ if ( ! function_exists( 'get_editable_roles' ) ) {
162
+ require_once ABSPATH . 'wp-admin/includes/user.php';
163
+ }
164
+ ?>
165
+ <div class="wrap">
166
+ <h2><?php echo apply_filters( 'acui_log_main_title', __('Importing users','import-users-from-csv-with-meta') ); ?></h2>
167
+ <?php
168
+ set_time_limit(0);
169
+
170
+ do_action( 'before_acui_import_users' );
171
+
172
+ $acui_helper = new ACUI_Helper();
173
+ $wp_users_fields = $acui_helper->get_wp_users_fields();
174
+ $acui_not_meta_fields = $acui_helper->get_not_meta_fields();
175
+ $acui_restricted_fields = $acui_helper->get_restricted_fields();
176
+ $all_roles = array_keys( wp_roles()->roles );
177
+ $editable_roles = array_keys( get_editable_roles() );
178
+
179
+ $users_registered = array();
180
+ $headers = array();
181
+ $headers_filtered = array();
182
+ $is_backend = !$is_frontend && !$is_cron;
183
+ $update_existing_users = isset( $form_data["update_existing_users"] ) ? sanitize_text_field( $form_data["update_existing_users"] ) : '';
184
+
185
+ $role_default = isset( $form_data["role"] ) ? $form_data["role"] : array( '' );
186
+ if( !is_array( $role_default ) )
187
+ $role_default = array( $role_default );
188
+ array_walk( $role_default, 'sanitize_text_field' );
189
+
190
+ $update_emails_existing_users = isset( $form_data["update_emails_existing_users"] ) ? sanitize_text_field( $form_data["update_emails_existing_users"] ) : 'yes';
191
+ $update_roles_existing_users = isset( $form_data["update_roles_existing_users"] ) ? sanitize_text_field( $form_data["update_roles_existing_users"] ) : '';
192
+ $update_allow_update_passwords = isset( $form_data["update_allow_update_passwords"] ) ? sanitize_text_field( $form_data["update_allow_update_passwords"] ) : 'yes';
193
+ $empty_cell_action = isset( $form_data["empty_cell_action"] ) ? sanitize_text_field( $form_data["empty_cell_action"] ) : '';
194
+ $delete_users = isset( $form_data["delete_users"] ) ? sanitize_text_field( $form_data["delete_users"] ) : '';
195
+ $delete_users_assign_posts = isset( $form_data["delete_users_assign_posts"] ) ? sanitize_text_field( $form_data["delete_users_assign_posts"] ) : '';
196
+ $delete_users_only_specified_role = isset( $form_data["delete_users_only_specified_role"] ) ? sanitize_text_field( $form_data["delete_users_only_specified_role"] ) : false;
197
+
198
+ $change_role_not_present = isset( $form_data["change_role_not_present"] ) ? sanitize_text_field( $form_data["change_role_not_present"] ) : '';
199
+ $change_role_not_present_role = isset( $form_data["change_role_not_present_role"] ) ? sanitize_text_field( $form_data["change_role_not_present_role"] ) : '';
200
+
201
+ if( $is_cron ){
202
+ $allow_multiple_accounts = ( get_option( "acui_cron_allow_multiple_accounts" ) == "allowed" ) ? "allowed" : "not_allowed";
203
+ }
204
+ else {
205
+ $allow_multiple_accounts = ( empty( $form_data["allow_multiple_accounts"] ) ) ? "not_allowed" : sanitize_text_field( $form_data["allow_multiple_accounts"] );
206
+ }
207
+
208
+ update_option( "acui_manually_send_mail", isset( $form_data["sends_email"] ) && $form_data["sends_email"] == 'yes' );
209
+ update_option( "acui_manually_send_mail_updated", isset( $form_data["send_email_updated"] ) && $form_data["send_email_updated"] == 'yes' );
210
+
211
+ // disable WordPress default emails if this must be disabled
212
+ if( !get_option('acui_automatic_wordpress_email') ){
213
+ add_filter( 'send_email_change_email', function() { return false; }, 999 );
214
+ add_filter( 'send_password_change_email', function() { return false; }, 999 );
215
+ }
216
+
217
+ // action
218
+ echo apply_filters( "acui_log_header", "<h3>" . __('Ready to registers','import-users-from-csv-with-meta') . "</h3>" );
219
+ echo apply_filters( "acui_log_header_first_row_explanation", "<p>" . __('First row represents the form of sheet','import-users-from-csv-with-meta') . "</p>" );
220
+
221
+ $row = 0;
222
+ $positions = array();
223
+ $error_importing = false;
224
+
225
+ ini_set('auto_detect_line_endings',TRUE);
226
+
227
+ $delimiter = $acui_helper->detect_delimiter( $file );
228
+
229
+ $manager = new SplFileObject( $file );
230
+ while ( $data = $manager->fgetcsv( $delimiter ) ):
231
+ if( count( $data ) == 1 )
232
+ $data = $data[0];
233
+
234
+ if( $data == NULL ){
235
+ break;
236
+ }
237
+ elseif( !is_array( $data ) ){
238
+ echo apply_filters( 'acui_message_csv_file_bad_formed', __( 'CSV file seems to be bad formed. Please use LibreOffice to create and manage CSV to be sure the format is correct', 'import-users-from-csv-with-meta') );
239
+ break;
240
+ }
241
+
242
+ foreach ( $data as $key => $value ){
243
+ $data[ $key ] = preg_replace( '/<script\b[^>]*>(.*?)<\/script>/is', '', trim( $value ) );
244
+ }
245
+
246
+ for( $i = 0; $i < count($data); $i++ ){
247
+ $data[$i] = $acui_helper->string_conversion( $data[$i] );
248
+
249
+ if( is_serialized( $data[$i] ) ) // serialized
250
+ $data[$i] = maybe_unserialize( $data[$i] );
251
+ elseif( strpos( $data[$i], "::" ) !== false ) // list of items
252
+ $data[$i] = explode( "::", $data[$i] );
253
+ }
254
+
255
+ if( $row == 0 ):
256
+ $data = apply_filters( 'pre_acui_import_header', $data );
257
+
258
+ // check min columns username - email
259
+ if(count( $data ) < 2){
260
+ echo "<div id='message' class='error'>" . __( 'File must contain at least 2 columns: username and email', 'import-users-from-csv-with-meta' ) . "</div>";
261
+ break;
262
+ }
263
+
264
+ $i = 0;
265
+ $password_position = false;
266
+ $id_position = false;
267
+
268
+ foreach ( $acui_restricted_fields as $acui_restricted_field ) {
269
+ $positions[ $acui_restricted_field ] = false;
270
+ }
271
+
272
+ foreach( $data as $element ){
273
+ $headers[] = $element;
274
+
275
+ if( in_array( strtolower( $element ) , $acui_restricted_fields ) )
276
+ $positions[ strtolower( $element ) ] = $i;
277
+
278
+ if( !in_array( strtolower( $element ), $acui_restricted_fields ) )
279
+ $headers_filtered[] = $element;
280
+
281
+ $i++;
282
+ }
283
+
284
+ $columns = count( $data );
285
+
286
+ update_option( "acui_columns", $headers_filtered );
287
+
288
+ $this->basic_css();
289
+ ?>
290
+
291
+ <h3><?php echo apply_filters( 'acui_log_inserting_updating_data_title', __( 'Inserting and updating data', 'import-users-from-csv-with-meta' ) ); ?></h3>
292
+ <table>
293
+ <tr>
294
+ <th><?php _e( 'Row', 'import-users-from-csv-with-meta' ); ?></th>
295
+ <?php foreach( $headers as $element ):
296
+ echo "<th>" . $element . "</th>";
297
+ endforeach; ?>
298
+ <?php do_action( 'acui_header_table_extra_rows' ); ?>
299
+ </tr>
300
+ <?php
301
+ $row++;
302
+ else:
303
+ $data = apply_filters( 'pre_acui_import_single_user_data', $data, $headers );
304
+
305
+ if( count( $data ) != $columns ): // if number of columns is not the same that columns in header
306
+ echo '<script>alert("' . __( 'Row number', 'import-users-from-csv-with-meta' ) . " $row " . __( 'does not have the same columns than the header, we are going to skip', 'import-users-from-csv-with-meta') . '");</script>';
307
+ $error_importing = true;
308
+ continue;
309
+ endif;
310
+
311
+ do_action('pre_acui_import_single_user', $headers, $data );
312
+ $data = apply_filters('pre_acui_import_single_user_data', $data, $headers);
313
+
314
+ $username = empty( $data[0] ) ? $acui_helper->get_random_unique_username( 'user_' ) : $data[0];
315
+ $email = $data[1];
316
+ $user_id = 0;
317
+ $problematic_row = false;
318
+ $password_position = $positions["password"];
319
+ $password = ( $password_position === false ) ? wp_generate_password( apply_filters( 'acui_auto_password_length', 12 ), apply_filters( 'acui_auto_password_special_chars', true ), apply_filters( 'acui_auto_password_extra_special_chars', false ) ) : $data[ $password_position ];
320
+ $role_position = $positions["role"];
321
+ $role = "";
322
+ $id_position = $positions["id"];
323
+ $id = ( empty( $id_position ) ) ? '' : $data[ $id_position ];
324
+ $created = true;
325
+
326
+ if( $role_position === false ){
327
+ $role = $role_default;
328
+ }
329
+ else{
330
+ $roles_cells = explode( ',', $data[ $role_position ] );
331
+
332
+ if( !is_array( $roles_cells ) )
333
+ $roles_cells = array( $roles_cells );
334
+
335
+ array_walk( $roles_cells, 'trim' );
336
+ $role = $roles_cells;
337
+ }
338
+
339
+ if ( !empty( array_diff( $role, $all_roles ) ) ){
340
+ $roles_printable = implode( ', ', $role );
341
+ $error_string = sprintf( __( 'Some of the next roles "%s" does not exists', 'import-users-from-csv-with-meta' ), $roles_printable );
342
+ echo '<script>alert("' . __( 'Problems with user: ', 'import-users-from-csv-with-meta' ) . $username . __( ', we are going to skip. \r\nError: ', 'import-users-from-csv-with-meta') . $error_string . '");</script>';
343
+ $created = false;
344
+ continue;
345
+ }
346
+
347
+ if ( !empty( array_diff( $role, $editable_roles ) ) ){ // users only are able to import users with a role they are allowed to edit
348
+ $roles_printable = implode( ', ', $role );
349
+ $error_string = sprintf( __( 'You do not have permission to assign some of the next roles "%s"', 'import-users-from-csv-with-meta' ), $roles_printable );
350
+ echo '<script>alert("' . __( 'Problems with user: ', 'import-users-from-csv-with-meta' ) . $username . __( ', we are going to skip. \r\nError: ', 'import-users-from-csv-with-meta') . $error_string . '");</script>';
351
+ $created = false;
352
+ continue;
353
+ }
354
+
355
+ if( !empty( $email ) && ( ( sanitize_email( $email ) == '' ) ) ){ // if email is invalid
356
+ $problematic_row = true;
357
+ $error_importing = true;
358
+ $data[0] = __('Invalid EMail','import-users-from-csv-with-meta')." ($email)";
359
+ }
360
+ elseif( empty( $email) ) {
361
+ $problematic_row = true;
362
+ $error_importing = true;
363
+ $data[0] = __( 'EMail not specified', 'import-users-from-csv-with-meta' );
364
+ }
365
+
366
+ if( !empty( $id ) ){ // if user have used id
367
+ if( $acui_helper->user_id_exists( $id ) ){
368
+ if( $update_existing_users == 'no' ){
369
+ continue;
370
+ }
371
+
372
+ // we check if username is the same than in row
373
+ $user = get_user_by( 'ID', $id );
374
+
375
+ if( $user->user_login == $username ){
376
+ $user_id = $id;
377
+
378
+ if( $password !== "" && $update_allow_update_passwords == 'yes' )
379
+ wp_set_password( $password, $user_id );
380
+
381
+ $new_user_id = $acui_helper->maybe_update_email( $user_id, $email, $password, $update_emails_existing_users );
382
+ if( empty( $new_user_id ) ){
383
+ continue;
384
+ }
385
+
386
+ if( is_wp_error( $new_user_id ) ){
387
+ $problematic_row = true;
388
+ $error_importing = true;
389
+ $data[0] = $new_user_id->get_error_message();
390
+ $created = false;
391
+ }
392
+ elseif( $new_user_id == $user_id)
393
+ $created = false;
394
+ else{
395
+ $user_id = $new_user_id;
396
+ $new_user = get_user_by( 'id', $new_user_id );
397
+ $problematic_row = true;
398
+ $error_importing = true;
399
+ $data[0] = sprintf( __( 'Email has changed, new user created with username %s', 'import-users-from-csv-with-meta' ), $new_user->user_login );
400
+ $created = true;
401
+ }
402
+ }
403
+ else{
404
+ echo '<script>alert("' . __( 'Problems with ID', 'import-users-from-csv-with-meta' ) . ": $id , " . __( 'username is not the same in the CSV and in database, we are going to skip.', 'import-users-from-csv-with-meta' ) . '");</script>';
405
+ continue;
406
+ }
407
+ }
408
+ else{
409
+ $user_id = wp_insert_user( array(
410
+ 'ID' => $id,
411
+ 'user_login' => $username,
412
+ 'user_email' => $email,
413
+ 'user_pass' => $password
414
+ ) );
415
+
416
+ $created = true;
417
+ }
418
+ }
419
+ elseif( username_exists( $username ) ){
420
+ if( $update_existing_users == 'no' ){
421
+ continue;
422
+ }
423
+
424
+ $user_object = get_user_by( "login", $username );
425
+ $user_id = $user_object->ID;
426
+
427
+ if( $password !== "" && $update_allow_update_passwords == 'yes' )
428
+ wp_set_password( $password, $user_id );
429
+
430
+ $new_user_id = $acui_helper->maybe_update_email( $user_id, $email, $password, $update_emails_existing_users );
431
+ if( empty( $new_user_id ) ){
432
+ continue;
433
+ }
434
+
435
+ if( is_wp_error( $new_user_id ) ){
436
+ $problematic_row = true;
437
+ $error_importing = true;
438
+ $data[0] = $new_user_id->get_error_message();
439
+ $created = false;
440
+ }
441
+ elseif( $new_user_id == $user_id)
442
+ $created = false;
443
+ else{
444
+ $user_id = $new_user_id;
445
+ $new_user = get_user_by( 'id', $new_user_id );
446
+ $problematic_row = true;
447
+ $error_importing = true;
448
+ $data[0] = sprintf( __( 'Email has changed, new user created with username %s', 'import-users-from-csv-with-meta' ), $new_user->user_login );
449
+ $created = true;
450
+ }
451
+ }
452
+ elseif( email_exists( $email ) && $allow_multiple_accounts == "not_allowed" ){ // if the email is registered, we take the user from this and we don't allow repeated emails
453
+ if( $update_existing_users == 'no' ){
454
+ continue;
455
+ }
456
+
457
+ $user_object = get_user_by( "email", $email );
458
+ $user_id = $user_object->ID;
459
+
460
+ $data[0] = __( 'User already exists as:', 'import-users-from-csv-with-meta' ) . $user_object->user_login . '<br/>' . __( '(in this CSV file is called:', 'import-users-from-csv-with-meta' ) . $username . ")";
461
+ $problematic_row = true;
462
+ $error_importing = true;
463
+
464
+ if( $password !== "" && $update_allow_update_passwords == 'yes' )
465
+ wp_set_password( $password, $user_id );
466
+
467
+ $created = false;
468
+ }
469
+ elseif( email_exists( $email ) && $allow_multiple_accounts == "allowed" ){ // if the email is registered and repeated emails are allowed
470
+ // if user is new, but the password in csv is empty, generate a password for this user
471
+ if( $password === "" ) {
472
+ $password = wp_generate_password( apply_filters( 'acui_auto_password_length', 12 ), apply_filters( 'acui_auto_password_special_chars', true ), apply_filters( 'acui_auto_password_extra_special_chars', false ) );
473
+ }
474
+
475
+ $hacked_email = ACUI_AllowMultipleAccounts::hack_email( $email );
476
+ $user_id = wp_create_user( $username, $password, $hacked_email );
477
+ ACUI_AllowMultipleAccounts::hack_restore_remapped_email_address( $user_id, $email );
478
+ }
479
+ else{
480
+ // if user is new, but the password in csv is empty, generate a password for this user
481
+ if( $password === "" ) {
482
+ $password = wp_generate_password( apply_filters( 'acui_auto_password_length', 12 ), apply_filters( 'acui_auto_password_special_chars', true ), apply_filters( 'acui_auto_password_extra_special_chars', false ) );
483
+ }
484
+
485
+ $user_id = wp_create_user( $username, $password, $email );
486
+ }
487
+
488
+ if( is_wp_error( $user_id ) ){ // in case the user is generating errors after this checks
489
+ $error_string = $user_id->get_error_message();
490
+ echo '<script>alert("' . __( 'Problems with user:', 'import-users-from-csv-with-meta' ) . $username . __( ', we are going to skip. \r\nError: ', 'import-users-from-csv-with-meta') . $error_string . '");</script>';
491
+ $error_importing = true;
492
+ continue;
493
+ }
494
+
495
+ $users_registered[] = $user_id;
496
+ $user_object = new WP_User( $user_id );
497
+
498
+ if( $created || $update_roles_existing_users != 'no' ){
499
+ if( !( in_array("administrator", $acui_helper->get_roles_by_user_id( $user_id ), FALSE) || is_multisite() && is_super_admin( $user_id ) )){
500
+
501
+ if( $update_roles_existing_users == 'yes' || $created ){
502
+ $default_roles = $user_object->roles;
503
+ foreach ( $default_roles as $default_role ) {
504
+ $user_object->remove_role( $default_role );
505
+ }
506
+ }
507
+
508
+ if( $update_roles_existing_users == 'yes' || $update_roles_existing_users == 'yes_no_override' || $created ){
509
+ if( !empty( $role ) ){
510
+ if( is_array( $role ) ){
511
+ foreach ($role as $single_role) {
512
+ $user_object->add_role( $single_role );
513
+ }
514
+ }
515
+ else{
516
+ $user_object->add_role( $role );
517
+ }
518
+ }
519
+
520
+ $invalid_roles = array();
521
+ if( !empty( $role ) ){
522
+ if( !is_array( $role ) ){
523
+ $role_tmp = $role;
524
+ $role = array();
525
+ $role[] = $role_tmp;
526
+ }
527
+
528
+ foreach ($role as $single_role) {
529
+ $single_role = strtolower($single_role);
530
+ if( get_role( $single_role ) ){
531
+ $user_object->add_role( $single_role );
532
+ }
533
+ else{
534
+ $invalid_roles[] = trim( $single_role );
535
+ }
536
+ }
537
+ }
538
+
539
+ if ( !empty( $invalid_roles ) ){
540
+ $problematic_row = true;
541
+ $error_importing = true;
542
+ if( count( $invalid_roles ) == 1 )
543
+ $data[0] = __('Invalid role','import-users-from-csv-with-meta').' (' . reset( $invalid_roles ) . ')';
544
+ else
545
+ $data[0] = __('Invalid roles','import-users-from-csv-with-meta').' (' . implode( ', ', $invalid_roles ) . ')';
546
+ }
547
+ }
548
+ }
549
+ }
550
+
551
+ // Multisite add user to current blog
552
+ if( is_multisite() && ( $created || $update_roles_existing_users != 'no' ) ){
553
+ if( !empty( $role ) ){
554
+ if( is_array( $role ) ){
555
+ foreach ($role as $single_role) {
556
+ add_user_to_blog( get_current_blog_id(), $user_id, $single_role );
557
+ }
558
+ }
559
+ else{
560
+ add_user_to_blog( get_current_blog_id(), $user_id, $role );
561
+ }
562
+ }
563
+ }
564
+
565
+ if( $columns > 2 ){
566
+ for( $i = 2 ; $i < $columns; $i++ ):
567
+ $data[$i] = apply_filters( 'pre_acui_import_single_user_single_data', $data[$i], $headers[$i], $i );
568
+
569
+ if( !empty( $data ) ){
570
+ if( strtolower( $headers[ $i ] ) == "password" ){ // passwords -> continue
571
+ continue;
572
+ }
573
+ elseif( strtolower( $headers[ $i ] ) == "user_pass" ){ // hashed pass
574
+ if( !$created && $update_allow_update_passwords == 'no' )
575
+ continue;
576
+
577
+ global $wpdb;
578
+ $wpdb->update( $wpdb->users, array( 'user_pass' => wp_slash( $data[ $i ] ) ), array( 'ID' => $user_id ) );
579
+ wp_cache_delete( $user_id, 'users' );
580
+ continue;
581
+ }
582
+ elseif( in_array( $headers[ $i ], $wp_users_fields ) ){ // wp_user data
583
+ if( $data[ $i ] === '' && $empty_cell_action == "leave" ){
584
+ continue;
585
+ }
586
+ else{
587
+ wp_update_user( array( 'ID' => $user_id, $headers[ $i ] => $data[ $i ] ) );
588
+ continue;
589
+ }
590
+ }
591
+ elseif( in_array( $headers[ $i ], $acui_not_meta_fields ) ){
592
+ continue;
593
+ }
594
+ else{
595
+ if( $data[ $i ] === '' ){
596
+ if( $empty_cell_action == "delete" )
597
+ delete_user_meta( $user_id, $headers[ $i ] );
598
+ else
599
+ continue;
600
+ }
601
+ else{
602
+ update_user_meta( $user_id, $headers[ $i ], $data[ $i ] );
603
+ continue;
604
+ }
605
+ }
606
+
607
+ }
608
+ endfor;
609
+ }
610
+
611
+ $styles = "";
612
+ if( $problematic_row )
613
+ $styles = "background-color:red; color:white;";
614
+
615
+ echo "<tr style='$styles' ><td>" . ($row - 1) . "</td>";
616
+ foreach ( $data as $element ){
617
+ if( is_array( $element ) )
618
+ $element = implode ( ',' , $element );
619
+
620
+ $element = sanitize_textarea_field( $element );
621
+ echo "<td>$element</td>";
622
+ }
623
+
624
+ do_action('post_acui_import_single_user', $headers, $data, $user_id, $role, $positions, $form_data );
625
+
626
+ echo "</tr>\n";
627
+
628
+ flush();
629
+
630
+ $mail_for_this_user = false;
631
+ if( $is_cron ){
632
+ if( get_option( "acui_cron_send_mail" ) ){
633
+ if( $created || ( !$created && get_option( "acui_cron_send_mail_updated" ) ) ){
634
+ $mail_for_this_user = true;
635
+ }
636
+ }
637
+ }
638
+ else{
639
+ if( isset( $form_data["sends_email"] ) && $form_data["sends_email"] ){
640
+ if( $created || ( !$created && ( isset( $form_data["send_email_updated"] ) && $form_data["send_email_updated"] ) ) )
641
+ $mail_for_this_user = true;
642
+ }
643
+ }
644
+
645
+ // wordpress default user created and edited emails
646
+ if( get_option('acui_automatic_created_edited_wordpress_email') == 'true' && $created ){
647
+ do_action( 'register_new_user', $user_id );
648
+ }
649
+
650
+ if( get_option('acui_automatic_created_edited_wordpress_email') == 'true' && !$created ){
651
+ do_action( 'edit_user_created_user', $user_id, 'both' );
652
+ }
653
+
654
+ // send mail
655
+ if( isset( $mail_for_this_user ) && $mail_for_this_user ){
656
+ ACUI_Email_Options::send_email( $user_object, $positions, $headers, $data, $created, $password );
657
+ }
658
+
659
+ endif;
660
+
661
+ $row++;
662
+ endwhile;
663
+
664
+ // let the filter of default WordPress emails as it were before deactivating them
665
+ if( !get_option('acui_automatic_wordpress_email') ){
666
+ remove_filter( 'send_email_change_email', function() { return false; }, 999 );
667
+ remove_filter( 'send_password_change_email', function() { return false; }, 999 );
668
+ }
669
+
670
+ if( $attach_id != 0 )
671
+ wp_delete_attachment( $attach_id );
672
+
673
+ // delete all users that have not been imported
674
+ $delete_users_flag = false;
675
+ $change_role_not_present_flag = false;
676
+
677
+ if( $delete_users == 'yes' ){
678
+ $delete_users_flag = true;
679
+ }
680
+
681
+ if( $is_cron && get_option( "acui_cron_delete_users" ) ){
682
+ $delete_users_flag = true;
683
+ $delete_users_assign_posts = get_option( "acui_cron_delete_users_assign_posts");
684
+ }
685
+
686
+ if( $is_backend && $change_role_not_present == 'yes' ){
687
+ $change_role_not_present_flag = true;
688
+ }
689
+
690
+ if( $is_cron && !empty( get_option( "acui_cron_change_role_not_present" ) ) ){
691
+ $change_role_not_present_flag = true;
692
+ $change_role_not_present_role = get_option( "acui_cron_change_role_not_present_role");
693
+ }
694
+
695
+ if( $is_frontend && !empty( get_option( "acui_frontend_change_role_not_present" ) ) ){
696
+ $change_role_not_present_flag = true;
697
+ $change_role_not_present_role = get_option( "acui_frontend_change_role_not_present_role");
698
+ }
699
+
700
+ if( $error_importing ){ // if there is some problem of some kind importing we won't proceed with delete or changing role to users not present to avoid problems
701
+ $delete_users_flag = false;
702
+ $change_role_not_present_flag = false;
703
+ }
704
+
705
+ if( $delete_users_flag ):
706
+ require_once( ABSPATH . 'wp-admin/includes/user.php');
707
+
708
+ global $wp_roles; // get all roles
709
+ $all_roles = $wp_roles->roles;
710
+ $exclude_roles = array_diff( array_keys( $all_roles ), $editable_roles ); // remove editable roles
711
+
712
+ if ( !in_array( 'administrator', $exclude_roles )){ // just to be sure
713
+ $exclude_roles[] = 'administrator';
714
+ }
715
+
716
+ $args = array(
717
+ 'fields' => array( 'ID' ),
718
+ 'role__not_in' => $exclude_roles,
719
+ 'exclude' => array( get_current_user_id() ), // current user never cannot be deleted
720
+ );
721
+
722
+ if( $delete_users_only_specified_role ){
723
+ $args[ 'role__in' ] = $role_default;
724
+ }
725
+
726
+ $all_users = get_users( $args );
727
+ $all_users_ids = array_map( function( $element ){ return intval( $element->ID ); }, $all_users );
728
+ $users_to_remove = array_diff( $all_users_ids, $users_registered );
729
+
730
+ $delete_users_assign_posts = ( get_userdata( $delete_users_assign_posts ) === false ) ? false : $delete_users_assign_posts;
731
+
732
+ foreach ( $users_to_remove as $user_id ) {
733
+ ( empty( $delete_users_assign_posts ) ) ? wp_delete_user( $user_id ) : wp_delete_user( $user_id, $delete_users_assign_posts );
734
+ }
735
+ endif;
736
+
737
+ if( $change_role_not_present_flag && !$delete_users_flag ):
738
+ require_once( ABSPATH . 'wp-admin/includes/user.php');
739
+
740
+ $all_users = get_users( array(
741
+ 'fields' => array( 'ID' ),
742
+ 'role__not_in' => array( 'administrator' )
743
+ ) );
744
+
745
+ foreach ( $all_users as $user ) {
746
+ if( !in_array( $user->ID, $users_registered ) ){
747
+ $user_object = new WP_User( $user->ID );
748
+ $user_object->set_role( $change_role_not_present_role );
749
+ }
750
+ }
751
+ endif;
752
+ ?>
753
+ </table>
754
+
755
+ <?php if( !$is_frontend ): ?>
756
+ <br/>
757
+ <p><?php _e( 'Process finished you can go', 'import-users-from-csv-with-meta' ); ?> <a href="<?php echo get_admin_url( null, 'users.php' ); ?>"><?php _e( 'here to see results', 'import-users-from-csv-with-meta' ); ?></a></p>
758
+ <?php endif; ?>
759
+
760
+ <?php
761
+ ini_set('auto_detect_line_endings',FALSE);
762
+
763
+ do_action( 'after_acui_import_users' );
764
+ ?>
765
+ </div>
766
+ <?php
767
+ }
768
+
769
  public function basic_css(){
770
  ?>
771
  <style type="text/css">
import-users-from-csv-with-meta.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: Import and export users and customers
4
  Plugin URI: https://www.codection.com
5
  Description: Using this plugin you will be able to import and export users or customers choosing many options and interacting with lots of other plugins
6
- Version: 1.16.4.1
7
  Author: codection
8
  Author URI: https://codection.com
9
  License: GPL2
@@ -107,8 +107,6 @@ class ImportExportUsersCustomers{
107
  foreach ( glob( plugin_dir_path( __FILE__ ) . "addons/*.php" ) as $file ) {
108
  include_once( $file );
109
  }
110
-
111
- require_once( "importer.php" );
112
  }
113
 
114
  public static function activate(){
@@ -124,7 +122,8 @@ class ImportExportUsersCustomers{
124
  }
125
 
126
  function menu() {
127
- add_submenu_page( 'tools.php', __( 'Import and export users and customers', 'import-users-from-csv-with-meta' ), __( 'Import and export users and customers', 'import-users-from-csv-with-meta' ), apply_filters( 'acui_capability', 'create_users' ), 'acui', 'acui_options' );
 
128
  }
129
 
130
  function admin_enqueue_scripts() {
3
  Plugin Name: Import and export users and customers
4
  Plugin URI: https://www.codection.com
5
  Description: Using this plugin you will be able to import and export users or customers choosing many options and interacting with lots of other plugins
6
+ Version: 1.17
7
  Author: codection
8
  Author URI: https://codection.com
9
  License: GPL2
107
  foreach ( glob( plugin_dir_path( __FILE__ ) . "addons/*.php" ) as $file ) {
108
  include_once( $file );
109
  }
 
 
110
  }
111
 
112
  public static function activate(){
122
  }
123
 
124
  function menu() {
125
+ $acui_import = new ACUI_Import();
126
+ add_submenu_page( 'tools.php', __( 'Import and export users and customers', 'import-users-from-csv-with-meta' ), __( 'Import and export users and customers', 'import-users-from-csv-with-meta' ), apply_filters( 'acui_capability', 'create_users' ), 'acui', array( $acui_import, 'show' ) );
127
  }
128
 
129
  function admin_enqueue_scripts() {
importer.php DELETED
@@ -1,1034 +0,0 @@
1
- <?php
2
- if ( ! defined( 'ABSPATH' ) ) exit;
3
-
4
- function acui_import_users( $file, $form_data, $attach_id = 0, $is_cron = false, $is_frontend = false ){
5
- if ( ! function_exists( 'get_editable_roles' ) ) {
6
- require_once ABSPATH . 'wp-admin/includes/user.php';
7
- }
8
- ?>
9
- <div class="wrap">
10
- <h2><?php echo apply_filters( 'acui_log_main_title', __('Importing users','import-users-from-csv-with-meta') ); ?></h2>
11
- <?php
12
- set_time_limit(0);
13
-
14
- do_action( 'before_acui_import_users' );
15
-
16
- global $wpdb;
17
- $acui_import = new ACUI_Import();
18
- $wp_users_fields = acui_get_wp_users_fields();
19
- $acui_not_meta_fields = acui_get_not_meta_fields();
20
- $acui_restricted_fields = acui_get_restricted_fields();
21
- $all_roles = array_keys( wp_roles()->roles );
22
- $editable_roles = array_keys( get_editable_roles() );
23
-
24
- if( is_plugin_active( 'wp-access-areas/wp-access-areas.php' ) ){
25
- $wpaa_labels = WPAA_AccessArea::get_available_userlabels();
26
- }
27
-
28
- $buddypress_fields = array();
29
-
30
- if( is_plugin_active( 'buddypress/bp-loader.php' ) || class_exists( 'BP_XProfile_Group' ) ){
31
- $profile_groups = BP_XProfile_Group::get( array( 'fetch_fields' => true ) );
32
-
33
- if ( !empty( $profile_groups ) ) {
34
- foreach ( $profile_groups as $profile_group ) {
35
- if ( !empty( $profile_group->fields ) ) {
36
- foreach ( $profile_group->fields as $field ) {
37
- $buddypress_fields[] = $field->name;
38
- $buddypress_types[] = $field->type;
39
- }
40
- }
41
- }
42
- }
43
- }
44
-
45
- $users_registered = array();
46
- $headers = array();
47
- $headers_filtered = array();
48
- $is_backend = !$is_frontend && !$is_cron;
49
- $update_existing_users = isset( $form_data["update_existing_users"] ) ? sanitize_text_field( $form_data["update_existing_users"] ) : '';
50
-
51
- $role_default = isset( $form_data["role"] ) ? $form_data["role"] : array( '' );
52
- if( !is_array( $role_default ) )
53
- $role_default = array( $role_default );
54
- array_walk( $role_default, 'sanitize_text_field' );
55
-
56
- $update_emails_existing_users = isset( $form_data["update_emails_existing_users"] ) ? sanitize_text_field( $form_data["update_emails_existing_users"] ) : 'yes';
57
- $update_roles_existing_users = isset( $form_data["update_roles_existing_users"] ) ? sanitize_text_field( $form_data["update_roles_existing_users"] ) : '';
58
- $update_allow_update_passwords = isset( $form_data["update_allow_update_passwords"] ) ? sanitize_text_field( $form_data["update_allow_update_passwords"] ) : 'yes';
59
- $empty_cell_action = isset( $form_data["empty_cell_action"] ) ? sanitize_text_field( $form_data["empty_cell_action"] ) : '';
60
- $delete_users = isset( $form_data["delete_users"] ) ? sanitize_text_field( $form_data["delete_users"] ) : '';
61
- $delete_users_assign_posts = isset( $form_data["delete_users_assign_posts"] ) ? sanitize_text_field( $form_data["delete_users_assign_posts"] ) : '';
62
- $delete_users_only_specified_role = isset( $form_data["delete_users_only_specified_role"] ) ? sanitize_text_field( $form_data["delete_users_only_specified_role"] ) : false;
63
-
64
- $change_role_not_present = isset( $form_data["change_role_not_present"] ) ? sanitize_text_field( $form_data["change_role_not_present"] ) : '';
65
- $change_role_not_present_role = isset( $form_data["change_role_not_present_role"] ) ? sanitize_text_field( $form_data["change_role_not_present_role"] ) : '';
66
- $activate_users_wp_members = ( !isset( $form_data["activate_users_wp_members"] ) || empty( $form_data["activate_users_wp_members"] ) ) ? "no_activate" : sanitize_text_field( $form_data["activate_users_wp_members"] );
67
-
68
- if( $is_cron ){
69
- $allow_multiple_accounts = ( get_option( "acui_cron_allow_multiple_accounts" ) == "allowed" ) ? "allowed" : "not_allowed";
70
- }
71
- else {
72
- $allow_multiple_accounts = ( empty( $form_data["allow_multiple_accounts"] ) ) ? "not_allowed" : sanitize_text_field( $form_data["allow_multiple_accounts"] );
73
- }
74
-
75
- $approve_users_new_user_approve = ( empty( $form_data["approve_users_new_user_appove"] ) ) ? "no_approve" : sanitize_text_field( $form_data["approve_users_new_user_appove"] );
76
-
77
- update_option( "acui_manually_send_mail", isset( $form_data["sends_email"] ) && $form_data["sends_email"] == 'yes' );
78
- update_option( "acui_manually_send_mail_updated", isset( $form_data["send_email_updated"] ) && $form_data["send_email_updated"] == 'yes' );
79
-
80
- // disable WordPress default emails if this must be disabled
81
- if( !get_option('acui_automatic_wordpress_email') ){
82
- add_filter( 'send_email_change_email', function() { return false; }, 999 );
83
- add_filter( 'send_password_change_email', function() { return false; }, 999 );
84
- }
85
-
86
- // action
87
- echo apply_filters( "acui_log_header", "<h3>" . __('Ready to registers','import-users-from-csv-with-meta') . "</h3>" );
88
- echo apply_filters( "acui_log_header_first_row_explanation", "<p>" . __('First row represents the form of sheet','import-users-from-csv-with-meta') . "</p>" );
89
-
90
- $row = 0;
91
- $positions = array();
92
- $error_importing = false;
93
-
94
- ini_set('auto_detect_line_endings',TRUE);
95
-
96
- $delimiter = acui_detect_delimiter( $file );
97
-
98
- $manager = new SplFileObject( $file );
99
- while ( $data = $manager->fgetcsv( $delimiter ) ):
100
- if( count( $data ) == 1 )
101
- $data = $data[0];
102
-
103
- if( $data == NULL ){
104
- break;
105
- }
106
- elseif( !is_array( $data ) ){
107
- echo apply_filters( 'acui_message_csv_file_bad_formed', __( 'CSV file seems to be bad formed. Please use LibreOffice to create and manage CSV to be sure the format is correct', 'import-users-from-csv-with-meta') );
108
- break;
109
- }
110
-
111
- foreach ( $data as $key => $value ){
112
- $data[ $key ] = preg_replace( '/<script\b[^>]*>(.*?)<\/script>/is', '', trim( $value ) );
113
- }
114
-
115
- for( $i = 0; $i < count($data); $i++ ){
116
- $data[$i] = acui_string_conversion( $data[$i] );
117
-
118
- if( is_serialized( $data[$i] ) ) // serialized
119
- $data[$i] = maybe_unserialize( $data[$i] );
120
- elseif( strpos( $data[$i], "::" ) !== false ) // list of items
121
- $data[$i] = explode( "::", $data[$i] );
122
- }
123
-
124
- if( $row == 0 ):
125
- $data = apply_filters( 'pre_acui_import_header', $data );
126
-
127
- // check min columns username - email
128
- if(count( $data ) < 2){
129
- echo "<div id='message' class='error'>" . __( 'File must contain at least 2 columns: username and email', 'import-users-from-csv-with-meta' ) . "</div>";
130
- break;
131
- }
132
-
133
- $i = 0;
134
- $password_position = false;
135
- $id_position = false;
136
-
137
- foreach ( $acui_restricted_fields as $acui_restricted_field ) {
138
- $positions[ $acui_restricted_field ] = false;
139
- }
140
-
141
- foreach( $data as $element ){
142
- $headers[] = $element;
143
-
144
- if( in_array( strtolower( $element ) , $acui_restricted_fields ) )
145
- $positions[ strtolower( $element ) ] = $i;
146
-
147
- if( !in_array( strtolower( $element ), $acui_restricted_fields ) && !in_array( $element, $buddypress_fields ) )
148
- $headers_filtered[] = $element;
149
-
150
- $i++;
151
- }
152
-
153
- $columns = count( $data );
154
-
155
- update_option( "acui_columns", $headers_filtered );
156
-
157
- $acui_import->basic_css();
158
- ?>
159
-
160
- <h3><?php echo apply_filters( 'acui_log_inserting_updating_data_title', __( 'Inserting and updating data', 'import-users-from-csv-with-meta' ) ); ?></h3>
161
- <table>
162
- <tr>
163
- <th><?php _e( 'Row', 'import-users-from-csv-with-meta' ); ?></th>
164
- <?php foreach( $headers as $element ):
165
- echo "<th>" . $element . "</th>";
166
- endforeach; ?>
167
- <?php do_action( 'acui_header_table_extra_rows' ); ?>
168
- </tr>
169
- <?php
170
- $row++;
171
- else:
172
- $data = apply_filters( 'pre_acui_import_single_user_data', $data, $headers );
173
-
174
- if( count( $data ) != $columns ): // if number of columns is not the same that columns in header
175
- echo '<script>alert("' . __( 'Row number', 'import-users-from-csv-with-meta' ) . " $row " . __( 'does not have the same columns than the header, we are going to skip', 'import-users-from-csv-with-meta') . '");</script>';
176
- $error_importing = true;
177
- continue;
178
- endif;
179
-
180
- do_action('pre_acui_import_single_user', $headers, $data );
181
- $data = apply_filters('pre_acui_import_single_user_data', $data, $headers);
182
-
183
- $username = empty( $data[0] ) ? $acui_import->get_random_unique_username( 'user_' ) : $data[0];
184
- $email = $data[1];
185
- $user_id = 0;
186
- $problematic_row = false;
187
- $password_position = $positions["password"];
188
- $password = ( $password_position === false ) ? wp_generate_password( apply_filters( 'acui_auto_password_length', 12 ), apply_filters( 'acui_auto_password_special_chars', true ), apply_filters( 'acui_auto_password_extra_special_chars', false ) ) : $data[ $password_position ];
189
- $role_position = $positions["role"];
190
- $role = "";
191
- $id_position = $positions["id"];
192
- $id = ( empty( $id_position ) ) ? '' : $data[ $id_position ];
193
- $created = true;
194
-
195
- if( $role_position === false ){
196
- $role = $role_default;
197
- }
198
- else{
199
- $roles_cells = explode( ',', $data[ $role_position ] );
200
-
201
- if( !is_array( $roles_cells ) )
202
- $roles_cells = array( $roles_cells );
203
-
204
- array_walk( $roles_cells, 'trim' );
205
- $role = $roles_cells;
206
- }
207
-
208
- if ( !empty( array_diff( $role, $all_roles ) ) ){
209
- $roles_printable = implode( ', ', $role );
210
- $error_string = sprintf( __( 'Some of the next roles "%s" does not exists', 'import-users-from-csv-with-meta' ), $roles_printable );
211
- echo '<script>alert("' . __( 'Problems with user: ', 'import-users-from-csv-with-meta' ) . $username . __( ', we are going to skip. \r\nError: ', 'import-users-from-csv-with-meta') . $error_string . '");</script>';
212
- $created = false;
213
- continue;
214
- }
215
-
216
- if ( !empty( array_diff( $role, $editable_roles ) ) ){ // users only are able to import users with a role they are allowed to edit
217
- $roles_printable = implode( ', ', $role );
218
- $error_string = sprintf( __( 'You do not have permission to assign some of the next roles "%s"', 'import-users-from-csv-with-meta' ), $roles_printable );
219
- echo '<script>alert("' . __( 'Problems with user: ', 'import-users-from-csv-with-meta' ) . $username . __( ', we are going to skip. \r\nError: ', 'import-users-from-csv-with-meta') . $error_string . '");</script>';
220
- $created = false;
221
- continue;
222
- }
223
-
224
- if( !empty( $email ) && ( ( sanitize_email( $email ) == '' ) ) ){ // if email is invalid
225
- $problematic_row = true;
226
- $error_importing = true;
227
- $data[0] = __('Invalid EMail','import-users-from-csv-with-meta')." ($email)";
228
- }
229
- elseif( empty( $email) ) {
230
- $problematic_row = true;
231
- $error_importing = true;
232
- $data[0] = __( 'EMail not specified', 'import-users-from-csv-with-meta' );
233
- }
234
-
235
- if( !empty( $id ) ){ // if user have used id
236
- if( acui_user_id_exists( $id ) ){
237
- if( $update_existing_users == 'no' ){
238
- continue;
239
- }
240
-
241
- // we check if username is the same than in row
242
- $user = get_user_by( 'ID', $id );
243
-
244
- if( $user->user_login == $username ){
245
- $user_id = $id;
246
-
247
- if( $password !== "" && $update_allow_update_passwords == 'yes' )
248
- wp_set_password( $password, $user_id );
249
-
250
- $new_user_id = $acui_import->maybe_update_email( $user_id, $email, $password, $update_emails_existing_users );
251
- if( empty( $new_user_id ) ){
252
- continue;
253
- }
254
-
255
- if( is_wp_error( $new_user_id ) ){
256
- $problematic_row = true;
257
- $error_importing = true;
258
- $data[0] = $new_user_id->get_error_message();
259
- $created = false;
260
- }
261
- elseif( $new_user_id == $user_id)
262
- $created = false;
263
- else{
264
- $user_id = $new_user_id;
265
- $new_user = get_user_by( 'id', $new_user_id );
266
- $problematic_row = true;
267
- $error_importing = true;
268
- $data[0] = sprintf( __( 'Email has changed, new user created with username %s', 'import-users-from-csv-with-meta' ), $new_user->user_login );
269
- $created = true;
270
- }
271
- }
272
- else{
273
- echo '<script>alert("' . __( 'Problems with ID', 'import-users-from-csv-with-meta' ) . ": $id , " . __( 'username is not the same in the CSV and in database, we are going to skip.', 'import-users-from-csv-with-meta' ) . '");</script>';
274
- continue;
275
- }
276
- }
277
- else{
278
- $user_id = wp_insert_user( array(
279
- 'ID' => $id,
280
- 'user_login' => $username,
281
- 'user_email' => $email,
282
- 'user_pass' => $password
283
- ) );
284
-
285
- $created = true;
286
- }
287
- }
288
- elseif( username_exists( $username ) ){
289
- if( $update_existing_users == 'no' ){
290
- continue;
291
- }
292
-
293
- $user_object = get_user_by( "login", $username );
294
- $user_id = $user_object->ID;
295
-
296
- if( $password !== "" && $update_allow_update_passwords == 'yes' )
297
- wp_set_password( $password, $user_id );
298
-
299
- $new_user_id = $acui_import->maybe_update_email( $user_id, $email, $password, $update_emails_existing_users );
300
- if( empty( $new_user_id ) ){
301
- continue;
302
- }
303
-
304
- if( is_wp_error( $new_user_id ) ){
305
- $problematic_row = true;
306
- $error_importing = true;
307
- $data[0] = $new_user_id->get_error_message();
308
- $created = false;
309
- }
310
- elseif( $new_user_id == $user_id)
311
- $created = false;
312
- else{
313
- $user_id = $new_user_id;
314
- $new_user = get_user_by( 'id', $new_user_id );
315
- $problematic_row = true;
316
- $error_importing = true;
317
- $data[0] = sprintf( __( 'Email has changed, new user created with username %s', 'import-users-from-csv-with-meta' ), $new_user->user_login );
318
- $created = true;
319
- }
320
- }
321
- elseif( email_exists( $email ) && $allow_multiple_accounts == "not_allowed" ){ // if the email is registered, we take the user from this and we don't allow repeated emails
322
- if( $update_existing_users == 'no' ){
323
- continue;
324
- }
325
-
326
- $user_object = get_user_by( "email", $email );
327
- $user_id = $user_object->ID;
328
-
329
- $data[0] = __( 'User already exists as:', 'import-users-from-csv-with-meta' ) . $user_object->user_login . '<br/>' . __( '(in this CSV file is called:', 'import-users-from-csv-with-meta' ) . $username . ")";
330
- $problematic_row = true;
331
- $error_importing = true;
332
-
333
- if( $password !== "" && $update_allow_update_passwords == 'yes' )
334
- wp_set_password( $password, $user_id );
335
-
336
- $created = false;
337
- }
338
- elseif( email_exists( $email ) && $allow_multiple_accounts == "allowed" ){ // if the email is registered and repeated emails are allowed
339
- // if user is new, but the password in csv is empty, generate a password for this user
340
- if( $password === "" ) {
341
- $password = wp_generate_password( apply_filters( 'acui_auto_password_length', 12 ), apply_filters( 'acui_auto_password_special_chars', true ), apply_filters( 'acui_auto_password_extra_special_chars', false ) );
342
- }
343
-
344
- $hacked_email = ACUI_AllowMultipleAccounts::hack_email( $email );
345
- $user_id = wp_create_user( $username, $password, $hacked_email );
346
- ACUI_AllowMultipleAccounts::hack_restore_remapped_email_address( $user_id, $email );
347
- }
348
- else{
349
- // if user is new, but the password in csv is empty, generate a password for this user
350
- if( $password === "" ) {
351
- $password = wp_generate_password( apply_filters( 'acui_auto_password_length', 12 ), apply_filters( 'acui_auto_password_special_chars', true ), apply_filters( 'acui_auto_password_extra_special_chars', false ) );
352
- }
353
-
354
- $user_id = wp_create_user( $username, $password, $email );
355
- }
356
-
357
- if( is_wp_error( $user_id ) ){ // in case the user is generating errors after this checks
358
- $error_string = $user_id->get_error_message();
359
- echo '<script>alert("' . __( 'Problems with user:', 'import-users-from-csv-with-meta' ) . $username . __( ', we are going to skip. \r\nError: ', 'import-users-from-csv-with-meta') . $error_string . '");</script>';
360
- $error_importing = true;
361
- continue;
362
- }
363
-
364
- $users_registered[] = $user_id;
365
- $user_object = new WP_User( $user_id );
366
-
367
- if( $created || $update_roles_existing_users != 'no' ){
368
- if( !( in_array("administrator", acui_get_roles($user_id), FALSE) || is_multisite() && is_super_admin( $user_id ) )){
369
-
370
- if( $update_roles_existing_users == 'yes' || $created ){
371
- $default_roles = $user_object->roles;
372
- foreach ( $default_roles as $default_role ) {
373
- $user_object->remove_role( $default_role );
374
- }
375
- }
376
-
377
- if( $update_roles_existing_users == 'yes' || $update_roles_existing_users == 'yes_no_override' || $created ){
378
- if( !empty( $role ) ){
379
- if( is_array( $role ) ){
380
- foreach ($role as $single_role) {
381
- $user_object->add_role( $single_role );
382
- }
383
- }
384
- else{
385
- $user_object->add_role( $role );
386
- }
387
- }
388
-
389
- $invalid_roles = array();
390
- if( !empty( $role ) ){
391
- if( !is_array( $role ) ){
392
- $role_tmp = $role;
393
- $role = array();
394
- $role[] = $role_tmp;
395
- }
396
-
397
- foreach ($role as $single_role) {
398
- $single_role = strtolower($single_role);
399
- if( get_role( $single_role ) ){
400
- $user_object->add_role( $single_role );
401
- }
402
- else{
403
- $invalid_roles[] = trim( $single_role );
404
- }
405
- }
406
- }
407
-
408
- if ( !empty( $invalid_roles ) ){
409
- $problematic_row = true;
410
- $error_importing = true;
411
- if( count( $invalid_roles ) == 1 )
412
- $data[0] = __('Invalid role','import-users-from-csv-with-meta').' (' . reset( $invalid_roles ) . ')';
413
- else
414
- $data[0] = __('Invalid roles','import-users-from-csv-with-meta').' (' . implode( ', ', $invalid_roles ) . ')';
415
- }
416
- }
417
- }
418
- }
419
-
420
- // Multisite add user to current blog
421
- if( is_multisite() && ( $created || $update_roles_existing_users != 'no' ) ){
422
- if( !empty( $role ) ){
423
- if( is_array( $role ) ){
424
- foreach ($role as $single_role) {
425
- add_user_to_blog( get_current_blog_id(), $user_id, $single_role );
426
- }
427
- }
428
- else{
429
- add_user_to_blog( get_current_blog_id(), $user_id, $role );
430
- }
431
- }
432
- }
433
-
434
- // WP Members activation
435
- if( $activate_users_wp_members == "activate" ){
436
- update_user_meta( $user_id, "active", true );
437
- }
438
-
439
- // New User Approve
440
- if( $approve_users_new_user_approve == "approve" ){
441
- update_user_meta( $user_id, "pw_user_status", "approved" );
442
- }
443
- else{
444
- update_user_meta( $user_id, "pending", true );
445
- }
446
-
447
- if( $columns > 2 ){
448
- for( $i = 2 ; $i < $columns; $i++ ):
449
- $data[$i] = apply_filters( 'pre_acui_import_single_user_single_data', $data[$i], $headers[$i], $i );
450
-
451
- if( !empty( $data ) ){
452
- if( strtolower( $headers[ $i ] ) == "password" ){ // passwords -> continue
453
- continue;
454
- }
455
- elseif( strtolower( $headers[ $i ] ) == "user_pass" ){ // hashed pass
456
- if( !$created && $update_allow_update_passwords == 'no' )
457
- continue;
458
-
459
- $wpdb->update( $wpdb->users, array( 'user_pass' => wp_slash( $data[ $i ] ) ), array( 'ID' => $user_id ) );
460
- wp_cache_delete( $user_id, 'users' );
461
- continue;
462
- }
463
- elseif( in_array( $headers[ $i ], $wp_users_fields ) ){ // wp_user data
464
- if( $data[ $i ] === '' && $empty_cell_action == "leave" ){
465
- continue;
466
- }
467
- else{
468
- wp_update_user( array( 'ID' => $user_id, $headers[ $i ] => $data[ $i ] ) );
469
- continue;
470
- }
471
- }
472
- elseif( strtolower( $headers[ $i ] ) == "wp-access-areas" && is_plugin_active( 'wp-access-areas/wp-access-areas.php' ) ){ // wp-access-areas
473
- $active_labels = array_map( 'trim', explode( "#", $data[ $i ] ) );
474
-
475
- foreach( $wpaa_labels as $wpa_label ){
476
- if( in_array( $wpa_label->cap_title , $active_labels )){
477
- acui_set_cap_for_user( $wpa_label->capability , $user_object , true );
478
- }
479
- else{
480
- acui_set_cap_for_user( $wpa_label->capability , $user_object , false );
481
- }
482
- }
483
-
484
- continue;
485
- }
486
- elseif( ( $bpf_pos = array_search( $headers[ $i ], $buddypress_fields ) ) !== false ){ // buddypress
487
- switch( $buddypress_types[ $bpf_pos ] ){
488
- case 'datebox':
489
- $date = $data[$i];
490
- switch( true ){
491
- case is_numeric( $date ):
492
- $UNIX_DATE = ($date - 25569) * 86400;
493
- $datebox = gmdate("Y-m-d H:i:s", $UNIX_DATE);break;
494
- case preg_match('/(\d{1,2})[\/-](\d{1,2})[\/-]([4567890]{1}\d{1})/',$date,$match):
495
- $match[3]='19'.$match[3];
496
- case preg_match('/(\d{1,2})[\/-](\d{1,2})[\/-](20[4567890]{1}\d{1})/',$date,$match):
497
- case preg_match('/(\d{1,2})[\/-](\d{1,2})[\/-](19[4567890]{1}\d{1})/',$date,$match):
498
- $datebox= ($match[3].'-'.$match[2].'-'.$match[1]);
499
- break;
500
-
501
- default:
502
- $datebox = $date;
503
- }
504
-
505
- $datebox = strtotime( $datebox );
506
- xprofile_set_field_data( $headers[$i], $user_id, date( 'Y-m-d H:i:s', $datebox ) );
507
- unset( $datebox );
508
- break;
509
- default:
510
- xprofile_set_field_data( $headers[$i], $user_id, $data[$i] );
511
- }
512
-
513
- continue;
514
- }
515
- elseif( $headers[ $i ] == 'bp_group' ){ // buddypress group
516
- $groups = explode( ',', $data[ $i ] );
517
- $groups_role = explode( ',', $data[ $positions[ 'bp_group_role' ] ] );
518
-
519
- for( $j = 0; $j < count( $groups ); $j++ ){
520
- $group_id = BP_Groups_Group::group_exists( $groups[ $j ] );
521
-
522
- if( !empty( $group_id ) ){
523
- groups_join_group( $group_id, $user_id );
524
-
525
- if( $groups_role[ $j ] == 'Moderator' ){
526
- groups_promote_member( $user_id, $group_id, 'mod' );
527
- }
528
- elseif( $groups_role[ $j ] == 'Administrator' ){
529
- groups_promote_member( $user_id, $group_id, 'admin' );
530
- }
531
- }
532
- }
533
-
534
- continue;
535
- }
536
- elseif( $headers[ $i ] == 'member_type' ){ // buddypress member_type
537
- bp_set_member_type( $user_id, $data[$i] );
538
- continue;
539
- }
540
- elseif( $headers[ $i ] == 'bp_group_role' ){
541
- continue;
542
- }
543
- elseif( in_array( $headers[ $i ], $acui_not_meta_fields ) ){
544
- continue;
545
- }
546
- else{ // wp_usermeta data
547
- if( $data[ $i ] === '' ){
548
- if( $empty_cell_action == "delete" )
549
- delete_user_meta( $user_id, $headers[ $i ] );
550
- else
551
- continue;
552
- }
553
- else{
554
- update_user_meta( $user_id, $headers[ $i ], $data[ $i ] );
555
- continue;
556
- }
557
- }
558
-
559
- }
560
- endfor;
561
- }
562
-
563
- $styles = "";
564
- if( $problematic_row )
565
- $styles = "background-color:red; color:white;";
566
-
567
- echo "<tr style='$styles' ><td>" . ($row - 1) . "</td>";
568
- foreach ( $data as $element ){
569
- if( is_array( $element ) )
570
- $element = implode ( ',' , $element );
571
-
572
- $element = sanitize_textarea_field( $element );
573
- echo "<td>$element</td>";
574
- }
575
-
576
- do_action('post_acui_import_single_user', $headers, $data, $user_id, $role );
577
-
578
- echo "</tr>\n";
579
-
580
- flush();
581
-
582
- $mail_for_this_user = false;
583
- if( $is_cron ){
584
- if( get_option( "acui_cron_send_mail" ) ){
585
- if( $created || ( !$created && get_option( "acui_cron_send_mail_updated" ) ) ){
586
- $mail_for_this_user = true;
587
- }
588
- }
589
- }
590
- else{
591
- if( isset( $form_data["sends_email"] ) && $form_data["sends_email"] ){
592
- if( $created || ( !$created && ( isset( $form_data["send_email_updated"] ) && $form_data["send_email_updated"] ) ) )
593
- $mail_for_this_user = true;
594
- }
595
- }
596
-
597
- // wordpress default user created and edited emails
598
- if( get_option('acui_automatic_created_edited_wordpress_email') == 'true' && $created ){
599
- do_action( 'register_new_user', $user_id );
600
- }
601
-
602
- if( get_option('acui_automatic_created_edited_wordpress_email') == 'true' && !$created ){
603
- do_action( 'edit_user_created_user', $user_id, 'both' );
604
- }
605
-
606
- // send mail
607
- if( isset( $mail_for_this_user ) && $mail_for_this_user ){
608
- ACUI_Email_Options::send_email( $user_object, $positions, $headers, $data, $created, $password );
609
- }
610
-
611
- endif;
612
-
613
- $row++;
614
- endwhile;
615
-
616
- // let the filter of default WordPress emails as it were before deactivating them
617
- if( !get_option('acui_automatic_wordpress_email') ){
618
- remove_filter( 'send_email_change_email', function() { return false; }, 999 );
619
- remove_filter( 'send_password_change_email', function() { return false; }, 999 );
620
- }
621
-
622
- if( $attach_id != 0 )
623
- wp_delete_attachment( $attach_id );
624
-
625
- // delete all users that have not been imported
626
- $delete_users_flag = false;
627
- $change_role_not_present_flag = false;
628
-
629
- if( $delete_users == 'yes' ){
630
- $delete_users_flag = true;
631
- }
632
-
633
- if( $is_cron && get_option( "acui_cron_delete_users" ) ){
634
- $delete_users_flag = true;
635
- $delete_users_assign_posts = get_option( "acui_cron_delete_users_assign_posts");
636
- }
637
-
638
- if( $is_backend && $change_role_not_present == 'yes' ){
639
- $change_role_not_present_flag = true;
640
- }
641
-
642
- if( $is_cron && !empty( get_option( "acui_cron_change_role_not_present" ) ) ){
643
- $change_role_not_present_flag = true;
644
- $change_role_not_present_role = get_option( "acui_cron_change_role_not_present_role");
645
- }
646
-
647
- if( $is_frontend && !empty( get_option( "acui_frontend_change_role_not_present" ) ) ){
648
- $change_role_not_present_flag = true;
649
- $change_role_not_present_role = get_option( "acui_frontend_change_role_not_present_role");
650
- }
651
-
652
- if( $error_importing ){ // if there is some problem of some kind importing we won't proceed with delete or changing role to users not present to avoid problems
653
- $delete_users_flag = false;
654
- $change_role_not_present_flag = false;
655
- }
656
-
657
- if( $delete_users_flag ):
658
- require_once( ABSPATH . 'wp-admin/includes/user.php');
659
-
660
- global $wp_roles; // get all roles
661
- $all_roles = $wp_roles->roles;
662
- $exclude_roles = array_diff( array_keys( $all_roles ), $editable_roles ); // remove editable roles
663
-
664
- if ( !in_array( 'administrator', $exclude_roles )){ // just to be sure
665
- $exclude_roles[] = 'administrator';
666
- }
667
-
668
- $args = array(
669
- 'fields' => array( 'ID' ),
670
- 'role__not_in' => $exclude_roles,
671
- 'exclude' => array( get_current_user_id() ), // current user never cannot be deleted
672
- );
673
-
674
- if( $delete_users_only_specified_role ){
675
- $args[ 'role__in' ] = $role_default;
676
- }
677
-
678
- $all_users = get_users( $args );
679
- $all_users_ids = array_map( function( $element ){ return intval( $element->ID ); }, $all_users );
680
- $users_to_remove = array_diff( $all_users_ids, $users_registered );
681
-
682
- $delete_users_assign_posts = ( get_userdata( $delete_users_assign_posts ) === false ) ? false : $delete_users_assign_posts;
683
-
684
- foreach ( $users_to_remove as $user_id ) {
685
- ( empty( $delete_users_assign_posts ) ) ? wp_delete_user( $user_id ) : wp_delete_user( $user_id, $delete_users_assign_posts );
686
- }
687
- endif;
688
-
689
- if( $change_role_not_present && !$delete_users_flag ):
690
- require_once( ABSPATH . 'wp-admin/includes/user.php');
691
-
692
- $all_users = get_users( array(
693
- 'fields' => array( 'ID' ),
694
- 'role__not_in' => array( 'administrator' )
695
- ) );
696
-
697
- foreach ( $all_users as $user ) {
698
- if( !in_array( $user->ID, $users_registered ) ){
699
- $user_object = new WP_User( $user->ID );
700
- $user_object->set_role( $change_role_not_present_role );
701
- }
702
- }
703
- endif;
704
- ?>
705
- </table>
706
-
707
- <?php if( !$is_frontend ): ?>
708
- <br/>
709
- <p><?php _e( 'Process finished you can go', 'import-users-from-csv-with-meta' ); ?> <a href="<?php echo get_admin_url( null, 'users.php' ); ?>"><?php _e( 'here to see results', 'import-users-from-csv-with-meta' ); ?></a></p>
710
- <?php endif; ?>
711
-
712
- <?php
713
- ini_set('auto_detect_line_endings',FALSE);
714
-
715
- do_action( 'after_acui_import_users' );
716
- ?>
717
- </div>
718
- <?php
719
- }
720
-
721
- function acui_options(){
722
- if ( !current_user_can( apply_filters( 'acui_capability', 'create_users' ) ) ) {
723
- wp_die( __( 'You are not allowed to see this content.', 'import-users-from-csv-with-meta' ));
724
- }
725
-
726
- if ( isset ( $_GET['tab'] ) )
727
- $tab = $_GET['tab'];
728
- else
729
- $tab = 'homepage';
730
-
731
- if( isset( $_POST ) && !empty( $_POST ) ):
732
- if ( !wp_verify_nonce( $_POST['security'], 'codection-security' ) ) {
733
- wp_die( __( 'Nonce check failed', 'import-users-from-csv-with-meta' ) );
734
- }
735
-
736
- switch ( $tab ){
737
- case 'homepage':
738
- update_option( 'acui_last_roles_used', ( empty( $_POST['role'] ) ? '' : array_map( 'sanitize_text_field', $_POST['role'] ) ) );
739
- acui_fileupload_process( $_POST, false );
740
- return;
741
- break;
742
-
743
- case 'frontend':
744
- do_action( 'acui_frontend_save_settings', $_POST );
745
- break;
746
-
747
- case 'columns':
748
- acui_manage_extra_profile_fields( $_POST );
749
- break;
750
-
751
- case 'mail-options':
752
- do_action( 'acui_mail_options_save_settings', $_POST );
753
- break;
754
-
755
- case 'cron':
756
- do_action( 'acui_cron_save_settings', $_POST );
757
- break;
758
- }
759
- endif;
760
-
761
- if ( isset ( $_GET['tab'] ) )
762
- acui_admin_tabs( $_GET['tab'] );
763
- else
764
- acui_admin_tabs('homepage');
765
-
766
- switch ( $tab ){
767
- case 'homepage' :
768
- ACUI_Homepage::admin_gui();
769
- break;
770
-
771
- case 'export' :
772
- ACUI_Exporter::admin_gui();
773
- break;
774
-
775
- case 'frontend':
776
- ACUI_Frontend::admin_gui();
777
- break;
778
-
779
- case 'columns':
780
- ACUI_Columns::admin_gui();
781
- break;
782
-
783
- case 'meta-keys':
784
- ACUI_MetaKeys::admin_gui();
785
- break;
786
-
787
- case 'doc':
788
- ACUI_Doc::message();
789
- break;
790
-
791
- case 'mail-options':
792
- ACUI_Email_Options::admin_gui();
793
- break;
794
-
795
- case 'cron':
796
- ACUI_Cron::admin_gui();
797
- break;
798
-
799
- case 'donate':
800
- ACUI_Donate::message();
801
- break;
802
-
803
- case 'help':
804
- ACUI_Help::message();
805
- break;
806
-
807
- case 'new_features':
808
- ACUI_NewFeatures::message();
809
- break;
810
-
811
- default:
812
- do_action( 'acui_tab_action_' . $tab );
813
- break;
814
- }
815
- }
816
-
817
- function acui_get_wp_users_fields(){
818
- 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" );
819
- }
820
-
821
- function acui_get_restricted_fields(){
822
- $wp_users_fields = acui_get_wp_users_fields();
823
- $wp_min_fields = array( "Username", "Email", "bp_group", "bp_group_role", "role" );
824
- $acui_restricted_fields = array_merge( $wp_users_fields, $wp_min_fields );
825
-
826
- return apply_filters( 'acui_restricted_fields', $acui_restricted_fields );
827
- }
828
-
829
- function acui_get_not_meta_fields(){
830
- return apply_filters( 'acui_not_meta_fields', array() );
831
- }
832
-
833
- function acui_detect_delimiter( $file ) {
834
- $delimiters = array(
835
- ';' => 0,
836
- ',' => 0,
837
- "\t" => 0,
838
- "|" => 0
839
- );
840
-
841
- $handle = @fopen($file, "r");
842
- $firstLine = fgets($handle);
843
- fclose($handle);
844
- foreach ($delimiters as $delimiter => &$count) {
845
- $count = count(str_getcsv($firstLine, $delimiter));
846
- }
847
-
848
- return array_search(max($delimiters), $delimiters);
849
- }
850
-
851
- function acui_string_conversion( $string ){
852
- if(!preg_match('%(?:
853
- [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
854
- |\xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
855
- |[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
856
- |\xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
857
- |\xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
858
- |[\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
859
- |\xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
860
- )+%xs', $string)){
861
- return utf8_encode($string);
862
- }
863
- else
864
- return $string;
865
- }
866
-
867
- function acui_user_id_exists( $user_id ){
868
- if ( get_userdata( $user_id ) === false )
869
- return false;
870
- else
871
- return true;
872
- }
873
-
874
- function acui_get_roles( $user_id ){
875
- $roles = array();
876
- $user = new WP_User( $user_id );
877
-
878
- if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
879
- foreach ( $user->roles as $role )
880
- $roles[] = $role;
881
- }
882
-
883
- return $roles;
884
- }
885
-
886
- function acui_get_editable_roles() {
887
- global $wp_roles;
888
-
889
- $all_roles = $wp_roles->roles;
890
- $editable_roles = apply_filters('editable_roles', $all_roles);
891
- $list_editable_roles = array();
892
-
893
- foreach ($editable_roles as $key => $editable_role)
894
- $list_editable_roles[$key] = $editable_role["name"];
895
-
896
- return $list_editable_roles;
897
- }
898
-
899
- function acui_admin_tabs( $current = 'homepage' ) {
900
- $tabs = array(
901
- 'homepage' => __( 'Import', 'import-users-from-csv-with-meta' ),
902
- 'export' => __( 'Export', 'import-users-from-csv-with-meta' ),
903
- 'frontend' => __( 'Frontend', 'import-users-from-csv-with-meta' ),
904
- 'cron' => __( 'Cron import', 'import-users-from-csv-with-meta' ),
905
- 'columns' => __( 'Extra profile fields', 'import-users-from-csv-with-meta' ),
906
- 'meta-keys' => __( 'Meta keys', 'import-users-from-csv-with-meta' ),
907
- 'mail-options' => __( 'Mail options', 'import-users-from-csv-with-meta' ),
908
- 'doc' => __( 'Documentation', 'import-users-from-csv-with-meta' ),
909
- 'donate' => __( 'Donate/Patreon', 'import-users-from-csv-with-meta' ),
910
- 'shop' => __( 'Shop', 'import-users-from-csv-with-meta' ),
911
- 'help' => __( 'Hire an expert', 'import-users-from-csv-with-meta' ),
912
- 'new_features' => __( 'New features', 'import-users-from-csv-with-meta' )
913
- );
914
-
915
- $tabs = apply_filters( 'acui_tabs', $tabs );
916
-
917
- echo '<div id="icon-themes" class="icon32"><br></div>';
918
- echo '<h2 class="nav-tab-wrapper">';
919
- foreach( $tabs as $tab => $name ){
920
- $class = ( $tab == $current ) ? ' nav-tab-active' : '';
921
-
922
- if( $tab == "shop" ){
923
- $href = "https://codection.com/tienda/";
924
- $target = "_blank";
925
- }
926
- else{
927
- $href = "?page=acui&tab=$tab";
928
- $target = "_self";
929
- }
930
-
931
- echo "<a class='nav-tab$class' href='$href' target='$target'>$name</a>";
932
-
933
- }
934
- echo '</h2>';
935
- }
936
-
937
- function acui_fileupload_process( $form_data, $is_cron = false, $is_frontend = false ) {
938
- if ( !defined( 'DOING_CRON' ) && ( !isset( $form_data['security'] ) || !wp_verify_nonce( $form_data['security'], 'codection-security' ) ) ){
939
- wp_die( __( 'Nonce check failed', 'import-users-from-csv-with-meta' ) );
940
- }
941
-
942
- if( empty( $_FILES['uploadfile']['name'] ) || $is_frontend ):
943
- $path_to_file = wp_normalize_path( $form_data["path_to_file"] );
944
-
945
- if( validate_file( $path_to_file ) !== 0 ){
946
- wp_die( __( 'Error, path to file is not well written', 'import-users-from-csv-with-meta' ) . ": $path_to_file" );
947
- }
948
-
949
- if( !file_exists ( $path_to_file ) ){
950
- wp_die( __( 'Error, we cannot find the file', 'import-users-from-csv-with-meta' ) . ": $path_to_file" );
951
- }
952
-
953
- acui_import_users( $path_to_file, $form_data, 0, $is_cron, $is_frontend );
954
- else:
955
- $uploadfile = wp_handle_upload( $_FILES['uploadfile'], array( 'test_form' => false, 'mimes' => array('csv' => 'text/csv') ) );
956
-
957
- if ( !$uploadfile || isset( $uploadfile['error'] ) ) {
958
- wp_die( __( 'Problem uploading file to import. Error details: ' . var_export( $uploadfile['error'], true ), 'import-users-from-csv-with-meta' ));
959
- } else {
960
- acui_import_users( $uploadfile['file'], $form_data, acui_get_attachment_id_by_url( $uploadfile['url'] ), $is_cron, $is_frontend );
961
- }
962
- endif;
963
- }
964
-
965
- function acui_manage_extra_profile_fields( $form_data ){
966
- if ( !isset( $form_data['security'] ) || !wp_verify_nonce( $form_data['security'], 'codection-security' ) ) {
967
- wp_die( __( 'Nonce check failed', 'import-users-from-csv-with-meta' ) );
968
- }
969
-
970
- if( isset( $form_data['show-profile-fields-action'] ) && $form_data['show-profile-fields-action'] == 'update' )
971
- update_option( "acui_show_profile_fields", isset( $form_data["show-profile-fields"] ) && $form_data["show-profile-fields"] == "yes" );
972
-
973
- if( isset( $form_data['reset-profile-fields-action'] ) && $form_data['reset-profile-fields-action'] == 'reset' )
974
- update_option( "acui_columns", array() );
975
- }
976
-
977
- // wp-access-areas functions
978
- function acui_set_cap_for_user( $capability , &$user , $add ) {
979
- $has_cap = $user->has_cap( $capability );
980
- $is_change = ($add && ! $has_cap) || (!$add && $has_cap);
981
- if ( $is_change ) {
982
- if ( $add ) {
983
- $user->add_cap( $capability , true );
984
- do_action( 'wpaa_grant_access' , $user , $capability );
985
- do_action( "wpaa_grant_{$capability}" , $user );
986
- } else if ( ! $add ) {
987
- $user->remove_cap( $capability );
988
- do_action( 'wpaa_revoke_access' , $user , $capability );
989
- do_action( "wpaa_revoke_{$capability}" , $user );
990
- }
991
- }
992
- }
993
-
994
- // misc
995
- function acui_get_attachment_id_by_url( $url ) {
996
- $wp_upload_dir = wp_upload_dir();
997
- // Strip out protocols, so it doesn't fail because searching for http: in https: dir.
998
- $dir = set_url_scheme( trailingslashit( $wp_upload_dir['baseurl'] ), 'relative' );
999
-
1000
- // Is URL in uploads directory?
1001
- if ( false !== strpos( $url, $dir ) ) {
1002
-
1003
- $file = basename( $url );
1004
-
1005
- $query_args = array(
1006
- 'post_type' => 'attachment',
1007
- 'post_status' => 'inherit',
1008
- 'fields' => 'ids',
1009
- 'meta_query' => array(
1010
- array(
1011
- 'key' => '_wp_attachment_metadata',
1012
- 'compare' => 'LIKE',
1013
- 'value' => $file,
1014
- ),
1015
- ),
1016
- );
1017
-
1018
- $query = new WP_Query( $query_args );
1019
-
1020
- if ( $query->have_posts() ) {
1021
- foreach ( $query->posts as $attachment_id ) {
1022
- $meta = wp_get_attachment_metadata( $attachment_id );
1023
- $original_file = basename( $meta['file'] );
1024
- $cropped_files = wp_list_pluck( $meta['sizes'], 'file' );
1025
-
1026
- if ( $original_file === $file || in_array( $file, $cropped_files ) ) {
1027
- return (int) $attachment_id;
1028
- }
1029
- }
1030
- }
1031
- }
1032
-
1033
- return false;
1034
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: https://codection.com/go/donate-import-users-from-csv-with-meta/
4
  Tags: csv, import, importer, meta data, meta, user, users, user meta, editor, profile, custom, fields, delimiter, update, insert
5
  Requires at least: 3.4
6
  Tested up to: 5.6
7
- Stable tag: 1.16.4.1
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -103,6 +103,11 @@ Plugin will automatically detect:
103
 
104
  == Changelog ==
105
 
 
 
 
 
 
106
  = 1.16.4.1 =
107
  * Fixed problem in "Mail options" that does not allow to remove attachments
108
 
4
  Tags: csv, import, importer, meta data, meta, user, users, user meta, editor, profile, custom, fields, delimiter, update, insert
5
  Requires at least: 3.4
6
  Tested up to: 5.6
7
+ Stable tag: 1.17
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
103
 
104
  == Changelog ==
105
 
106
+ = 1.17 =
107
+ * Many code changes, making it simpler to include more features and make it easier to debug in a future
108
+ * Export bug fixed: the plugin exports an empty role column that breaks the CSV
109
+ * You can now test cron task from the "Cron" settings tab
110
+
111
  = 1.16.4.1 =
112
  * Fixed problem in "Mail options" that does not allow to remove attachments
113