Import users from CSV with meta - Version 1.15.6

Version Description

  • ACF compatibility included
  • BuddyPress member_type import included thanks to @morenolq (https://wordpress.org/support/topic/import-user-member-type-as-csv-field/)
Download this release

Release Info

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

Code changes from version 1.15.5.13 to 1.15.6

addons/advanced-custom-fields.php ADDED
@@ -0,0 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ if ( ! defined( 'ABSPATH' ) ) exit;
4
+
5
+ if( !is_plugin_active( 'advanced-custom-fields-pro/acf.php' ) && !is_plugin_active( 'advanced-custom-fields/acf.php' ) ){
6
+ return;
7
+ }
8
+
9
+ class ACUI_ACF{
10
+ function __construct(){
11
+ add_filter( 'acui_restricted_fields', array( $this, 'restricted_fields' ), 10, 1 );
12
+ add_action( 'acui_documentation_after_plugins_activated', array( $this, 'documentation' ) );
13
+ add_action( 'post_acui_import_single_user', array( $this, 'import' ), 10, 3 );
14
+ }
15
+
16
+ function restricted_fields( $acui_restricted_fields ){
17
+ return array_merge( $acui_restricted_fields, $this->get_user_fields_keys() );
18
+ }
19
+
20
+ function documentation(){
21
+ $fields = $this->get_user_fields();
22
+ ?>
23
+ <tr valign="top">
24
+ <th scope="row"><?php _e( "Advaced Custom Fields is activated", 'import-users-from-csv-with-meta' ); ?></th>
25
+ <td>
26
+ <?php _e( "You can import those fields, look at every group which fields you can import using the column names shown below.", 'import-users-from-csv-with-meta' ); ?>.
27
+ <ul style="list-style:disc outside none; margin-left:2em;">
28
+ <?php foreach ( $this->get_user_fields() as $group => $fields ): ?>
29
+ <li><?php _e( "Group name", 'import-users-from-csv-with-meta' ); ?>: <em><?php echo $group; ?></em></li>
30
+ <ul style="list-style:square inside none; margin-left:2em;">
31
+ <?php foreach ( $fields as $field ): ?>
32
+ <li><?php echo $field['label']; ?> <em>(type: <?php echo $field['type']; ?>)</em> - Column name in the CSV: <strong><?php echo $field['name']; ?></strong></li>
33
+ <?php endforeach; ?>
34
+ </ul>
35
+ <?php endforeach; ?>
36
+ </ul>
37
+ </td>
38
+ </tr>
39
+ <?php
40
+ }
41
+
42
+ function import( $headers, $row, $user_id ){
43
+ $fields_positions = array();
44
+ $types = $this->get_user_fields_types();
45
+
46
+ foreach ( $types as $key => $type ) {
47
+ $pos = array_search( $key, $headers );
48
+
49
+ if( $pos === FALSE )
50
+ continue;
51
+
52
+ $fields_positions[ $pos ] = $key;
53
+ }
54
+
55
+ foreach ( $fields_positions as $pos => $key ) {
56
+ $data = explode( ',', $row[ $pos ] );
57
+ $data = array_filter( $data, function( $value ){ return $value !== ''; } );
58
+
59
+ // slugs in relationship
60
+ if( $types[ $key ] == 'relationship' && (string)(int)$data[0] != $data[0] ){
61
+ foreach ( $data as $it => $slug ) {
62
+ $data[ $it ] = $this->get_post_id_by_slug( $slug );
63
+ }
64
+ }
65
+
66
+ update_field( $key, $data, "user_" . $user_id );
67
+ }
68
+ }
69
+
70
+ function get_user_fields(){
71
+ $post_id = "user_new";
72
+ $fields = array();
73
+
74
+ $args = array(
75
+ 'user_id' => 'new',
76
+ 'user_form' => '#your-profile'
77
+ );
78
+
79
+ $field_groups = acf_get_field_groups( array( 'user_id' => 'new', 'user_form' => '#your-profile' ) );
80
+
81
+ if( empty($field_groups) )
82
+ return;
83
+
84
+ acf_form_data( array( 'post_id' => "user_new", 'nonce' => 'user' ) );
85
+
86
+ foreach( $field_groups as $field_group ) {
87
+ $fields[ $field_group['title'] ] = acf_get_fields( $field_group );
88
+ }
89
+
90
+ return $fields;
91
+ }
92
+
93
+ function get_user_fields_keys(){
94
+ $fields = $this->get_user_fields();
95
+ $fields_keys = array();
96
+
97
+ foreach ( $this->get_user_fields() as $group => $fields ){
98
+ foreach ( $fields as $field ){
99
+ $fields_keys[] = $field['name'];
100
+ }
101
+ }
102
+
103
+ return $fields_keys;
104
+ }
105
+
106
+ function get_user_fields_types(){
107
+ $fields = $this->get_user_fields();
108
+ $fields_keys = array();
109
+
110
+ foreach ( $this->get_user_fields() as $group => $fields ){
111
+ foreach ( $fields as $field ){
112
+ $fields_keys[ $field['name'] ] = $field['type'];
113
+ }
114
+ }
115
+
116
+ return $fields_keys;
117
+ }
118
+
119
+ function get_post_id_by_slug( $slug ){
120
+ global $wpdb;
121
+
122
+ $page_path = rawurlencode( urldecode( $slug ) );
123
+ $page_path = str_replace( '%2F', '/', $page_path );
124
+ $page_path = str_replace( '%20', ' ', $page_path );
125
+ $parts = explode( '/', trim( $page_path, '/' ) );
126
+ $parts = array_map( 'sanitize_title_for_query', $parts );
127
+ $escaped_parts = esc_sql( $parts );
128
+
129
+ $in_string = "'" . implode( "','", $escaped_parts ) . "'";
130
+
131
+ $sql = "SELECT ID, post_name, post_parent, post_type FROM $wpdb->posts WHERE post_name IN ($in_string)";
132
+
133
+ $pages = $wpdb->get_results( $sql, OBJECT_K );
134
+
135
+ $revparts = array_reverse( $parts );
136
+
137
+ $foundid = 0;
138
+
139
+ foreach ( (array) $pages as $page ) {
140
+ if ( $page->post_name == $revparts[0] ) {
141
+ $count = 0;
142
+ $p = $page;
143
+
144
+ while ( 0 != $p->post_parent && isset( $pages[ $p->post_parent ] ) ) {
145
+ $count++;
146
+ $parent = $pages[ $p->post_parent ];
147
+ if ( ! isset( $revparts[ $count ] ) || $parent->post_name != $revparts[ $count ] ) {
148
+ break;
149
+ }
150
+ $p = $parent;
151
+ }
152
+
153
+ if ( 0 == $p->post_parent && count( $revparts ) == $count + 1 && $p->post_name == $revparts[ $count ] ) {
154
+ $foundid = $page->ID;
155
+ if ( $page->post_type == $post_type ) {
156
+ break;
157
+ }
158
+ }
159
+ }
160
+ }
161
+
162
+ return $foundid;
163
+ }
164
+ }
165
+
166
+ new ACUI_ACF();
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.15.5.13
7
  Author: codection
8
  Author URI: https://codection.com
9
  License: GPL2
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.15.6
7
  Author: codection
8
  Author URI: https://codection.com
9
  License: GPL2
importer.php CHANGED
@@ -510,6 +510,10 @@ function acui_import_users( $file, $form_data, $attach_id = 0, $is_cron = false,
510
 
511
  continue;
512
  }
 
 
 
 
513
  elseif( $headers[ $i ] == 'bp_group_role' ){
514
  continue;
515
  }
510
 
511
  continue;
512
  }
513
+ elseif( $headers[ $i ] == 'member_type' ){ // buddypress member_type
514
+ bp_set_member_type( $user_id, $data[$i] );
515
+ continue;
516
+ }
517
  elseif( $headers[ $i ] == 'bp_group_role' ){
518
  continue;
519
  }
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.4.1
7
- Stable tag: 1.15.5.13
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -99,6 +99,10 @@ Plugin will automatically detect:
99
 
100
  == Changelog ==
101
 
 
 
 
 
102
  = 1.15.5.13 =
103
  * var_dump forgotten
104
  * Up to version updated
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.4.1
7
+ Stable tag: 1.15.6
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
99
 
100
  == Changelog ==
101
 
102
+ = 1.15.6 =
103
+ * ACF compatibility included
104
+ * BuddyPress member_type import included thanks to @morenolq (https://wordpress.org/support/topic/import-user-member-type-as-csv-field/)
105
+
106
  = 1.15.5.13 =
107
  * var_dump forgotten
108
  * Up to version updated