Really Simple CSV Importer - Version 0.3

Version Description

  • New feature: Advanced Custom Fields integrate.
  • Enhancement: Use post_id if not already present when inserting post.
Download this release

Release Info

Developer hissy
Plugin Icon wp plugin Really Simple CSV Importer
Version 0.3
Comparing to
See all releases

Code changes from version 0.2 to 0.3

readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: hissy, wokamoto
3
  Tags: importer, csv
4
  Requires at least: 3.0
5
  Tested up to: 3.6.1
6
- Stable tag: 0.2
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
@@ -16,12 +16,14 @@ Alternative CSV Importer plugin. Simple and powerful.
16
  * Category support
17
  * Tag support
18
  * Custom field support
 
19
  * Custom post type support
20
 
21
  Contains CSV file sample in `/wp-content/plugins/really-simple-csv-importer/sample` directory.
22
 
23
  = Available column names and values: =
24
- * ID or post_id: (int) post id. update post data if this value is defined. default is insert
 
25
  * post_author: (login or ID) author
26
  * post_date: (string) publish date
27
  * post_content: (string) post content
@@ -33,6 +35,13 @@ Contains CSV file sample in `/wp-content/plugins/really-simple-csv-importer/samp
33
  * post_tags: (string, comma divided) name of post tags
34
  * {custom_field}: any other column labels used as custom field
35
 
 
 
 
 
 
 
 
36
  Add star and read future issues about rs-csv-importer on [GitHub](https://github.com/hissy/rs-csv-importer)!
37
 
38
  == Installation ==
@@ -44,6 +53,9 @@ Add star and read future issues about rs-csv-importer on [GitHub](https://github
44
 
45
  == Changelog ==
46
 
 
 
 
47
  = 0.2 =
48
  * New feature: Add post_id column. It enables to update post data.
49
  * Some bug fixes
3
  Tags: importer, csv
4
  Requires at least: 3.0
5
  Tested up to: 3.6.1
6
+ Stable tag: 0.3
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
16
  * Category support
17
  * Tag support
18
  * Custom field support
19
+ * Adcanved Custom Fields support (beta)
20
  * Custom post type support
21
 
22
  Contains CSV file sample in `/wp-content/plugins/really-simple-csv-importer/sample` directory.
23
 
24
  = Available column names and values: =
25
+ * ID or post_id: (int) post id.
26
+ This value is not required. The post ID is already exists in your blog, importer will update that post data. If the ID is not exists, importer will trying to create a new post with suggested ID.
27
  * post_author: (login or ID) author
28
  * post_date: (string) publish date
29
  * post_content: (string) post content
35
  * post_tags: (string, comma divided) name of post tags
36
  * {custom_field}: any other column labels used as custom field
37
 
38
+ = Advanced Custom Fields plugin integrate =
39
+ If advanced custom field key is exists, importer will trying to use [update_field](http://www.advancedcustomfields.com/resources/functions/update_field/) function instead of built-in add_post_meta function.
40
+ How to find advanced custom field key: [Finding the field key](http://www.advancedcustomfields.com/resources/functions/update_field/#finding-the+field+key)
41
+
42
+ Note: multiple value is not supported yet.
43
+
44
+ = Official public repository =
45
  Add star and read future issues about rs-csv-importer on [GitHub](https://github.com/hissy/rs-csv-importer)!
46
 
47
  == Installation ==
53
 
54
  == Changelog ==
55
 
56
+ = 0.3 =
57
+ * New feature: Advanced Custom Fields integrate.
58
+ * Enhancement: Use post_id if not already present when inserting post.
59
  = 0.2 =
60
  * New feature: Add post_id column. It enables to update post data.
61
  * Some bug fixes
rs-csv-helper.php ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class RS_CSV_Helper {
4
+
5
+ const DELIMITER = ",";
6
+
7
+ // File utility functions
8
+ function fopen($filename, $mode='r') {
9
+ return fopen($filename, $mode);
10
+ }
11
+
12
+ function fgetcsv($handle, $length = 0) {
13
+ return fgetcsv($handle, $length, self::DELIMITER);
14
+ }
15
+
16
+ function fclose($fp) {
17
+ return fclose($fp);
18
+ }
19
+
20
+ public function parse_columns(&$obj, $array) {
21
+ if (!is_array($array) || count($array) == 0)
22
+ return false;
23
+
24
+ $keys = array_keys($array);
25
+ $values = array_values($array);
26
+
27
+ $obj->column_indexes = array_combine($values, $keys);
28
+ $obj->column_keys = array_combine($keys, $values);
29
+ }
30
+
31
+ public function get_data(&$obj, &$array, $key) {
32
+ if (!isset($obj->column_indexes) || !is_array($array) || count($array) == 0)
33
+ return false;
34
+
35
+ if (isset($obj->column_indexes[$key])) {
36
+ $index = $obj->column_indexes[$key];
37
+ if (isset($array[$index]) && !empty($array[$index])) {
38
+ $value = $array[$index];
39
+ unset($array[$index]);
40
+ return $value;
41
+ }
42
+ }
43
+
44
+ return false;
45
+ }
46
+
47
+ }
rs-csv-importer.php CHANGED
@@ -7,7 +7,7 @@ Author: Takuro Hishikawa, wokamoto
7
  Author URI: http://notnil-creative.com/
8
  Text Domain: rs-csv-importer
9
  License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10
- Version: 0.2
11
  */
12
 
13
  if ( !defined('WP_LOAD_IMPORTERS') )
@@ -22,7 +22,8 @@ if ( !class_exists( 'WP_Importer' ) ) {
22
  require_once $class_wp_importer;
23
  }
24
 
25
- // Load WP Post Helper
 
26
  require dirname( __FILE__ ) . '/wp_post_helper/class-wp_post_helper.php';
27
 
28
  /**
@@ -37,26 +38,8 @@ class RS_CSV_Importer extends WP_Importer {
37
  /** Sheet columns
38
  * @value array
39
  */
40
- var $columns = array();
41
- var $column_raw = array();
42
-
43
- /** Delimiter
44
- * @var string
45
- */
46
- const DELIMITER = ",";
47
-
48
- // Utility functions
49
- function fopen($filename, $mode='r') {
50
- return fopen($filename, $mode);
51
- }
52
-
53
- function fgetcsv($handle, $length = 0) {
54
- return fgetcsv($handle, $length, self::DELIMITER);
55
- }
56
-
57
- function fclose($fp) {
58
- return fclose($fp);
59
- }
60
 
61
  // User interface wrapper start
62
  function header() {
@@ -73,9 +56,10 @@ class RS_CSV_Importer extends WP_Importer {
73
  // Step 1
74
  function greet() {
75
  echo '<p>'.__( 'Choose a CSV (.csv) file to upload, then click Upload file and import.', 'rs-csv-importer' ).'</p>';
 
76
  echo '<ol>';
77
  echo '<li>'.__( 'Select UTF-8 as charset.', 'rs-csv-importer' ).'</li>';
78
- echo '<li>'.sprintf( __( 'You must use field delimiter as "%s"', 'rs-csv-importer'), self::DELIMITER ).'</li>';
79
  echo '<li>'.__( 'You must quote all text cells.', 'rs-csv-importer' ).'</li>';
80
  echo '</ol>';
81
  echo '<p>'.__( 'Sample CSV file download:', 'rs-csv-importer' );
@@ -107,43 +91,45 @@ class RS_CSV_Importer extends WP_Importer {
107
  return $result;
108
  }
109
 
110
- /** Parsing header row, setup the columns definition.
111
- * @param array $data name of columns
112
- */
113
- function parse_columns($data=array()) {
114
- $columns = array();
115
- foreach ($data as $key => $value) {
116
- $columns[$value] = $key;
117
- }
118
- $this->columns = $columns;
119
- $this->column_raw = $data;
120
- }
121
-
122
  /** Insert post and postmeta using wp_post_helper
123
  * @param array $post
124
  * @param array $meta
 
125
  * More information: https://gist.github.com/4084471
126
  */
127
  function save_post($post,$meta,$is_update) {
128
  $ph = new wp_post_helper($post);
129
 
130
  foreach ($meta as $key => $value) {
131
- $ph->add_meta($key,$value,true);
 
 
 
 
 
 
 
 
 
 
 
132
  }
133
 
134
  if ($is_update)
135
- $ph->update();
136
  else
137
- $ph->insert();
138
 
139
  unset($ph);
 
 
140
  }
141
 
142
  // process parse csv ind insert posts
143
  function process_posts() {
144
- global $wpdb;
145
 
146
- $handle = $this->fopen($this->file, 'r');
147
  if ( $handle == false ) {
148
  echo '<p><strong>'.__( 'Failed to open file.', 'rs-csv-importer' ).'</strong></p>';
149
  wp_import_cleanup($this->id);
@@ -154,43 +140,36 @@ class RS_CSV_Importer extends WP_Importer {
154
 
155
  echo '<ol>';
156
 
157
- while (($data = $this->fgetcsv($handle)) !== FALSE) {
158
  if ($is_first) {
159
- $this->parse_columns( $data );
160
  $is_first = false;
161
  } else {
162
  $post = array();
163
  $is_update = false;
164
 
165
  // (int) post id
166
- if (isset($this->columns['ID']) &&
167
- isset($data[$this->columns['ID']]) &&
168
- ! empty($data[$this->columns['ID']])) {
169
- $post['ID'] = $data[$this->columns['ID']];
170
- unset($data[$this->columns['ID']]);
171
- $is_update = true;
172
- }
173
- if (isset($this->columns['post_id']) &&
174
- isset($data[$this->columns['post_id']]) &&
175
- ! empty($data[$this->columns['post_id']])) {
176
- $post['post_id'] = $data[$this->columns['post_id']];
177
- unset($data[$this->columns['post_id']]);
178
- $is_update = true;
179
  }
180
 
181
  // (string) post slug
182
- if (isset($this->columns['post_name']) &&
183
- isset($data[$this->columns['post_name']]) &&
184
- ! empty($data[$this->columns['post_name']])) {
185
- $post['post_name'] = $data[$this->columns['post_name']];
186
- unset($data[$this->columns['post_name']]);
187
  }
188
 
189
  // (login or ID) post_author
190
- if (isset($this->columns['post_author']) &&
191
- isset($data[$this->columns['post_author']]) &&
192
- ! empty($data[$this->columns['post_author']])) {
193
- $post_author = $data[$this->columns['post_author']];
194
  if (is_numeric($post_author)) {
195
  $user = get_user_by('id',$post_author);
196
  } else {
@@ -200,93 +179,90 @@ class RS_CSV_Importer extends WP_Importer {
200
  $post['post_author'] = $user->ID;
201
  unset($user);
202
  }
203
- unset($data[$this->columns['post_author']]);
204
- unset($post_author);
205
  }
206
 
207
  // (string) publish date
208
- if (isset($this->columns['post_date']) &&
209
- isset($data[$this->columns['post_date']]) &&
210
- ! empty($data[$this->columns['post_date']])) {
211
- $post_date = $data[$this->columns['post_date']];
212
- $post_date = date("Y-m-d H:i:s", strtotime($post_date));
213
- $post['post_date'] = $post_date;
214
- unset($data[$this->columns['post_date']]);
215
- unset($post_date);
216
  }
217
 
218
  // (string) post type
219
- if (isset($this->columns['post_type']) &&
220
- isset($data[$this->columns['post_type']]) &&
221
- ! empty($data[$this->columns['post_type']])) {
222
- $post['post_type'] = $data[$this->columns['post_type']];
223
- unset($data[$this->columns['post_type']]);
224
  }
225
 
226
  // (string) post status
227
- if (isset($this->columns['post_status']) &&
228
- isset($data[$this->columns['post_status']]) &&
229
- ! empty($data[$this->columns['post_status']])) {
230
- $post['post_status'] = $data[$this->columns['post_status']];
231
- unset($data[$this->columns['post_status']]);
232
  }
233
 
234
  // (string) post title
235
- $post['post_title'] = '';
236
- if (isset($this->columns['post_title']) &&
237
- isset($data[$this->columns['post_title']]) &&
238
- ! empty($data[$this->columns['post_title']])) {
239
- $post['post_title'] = $data[$this->columns['post_title']];
240
- unset($data[$this->columns['post_title']]);
241
- }
242
 
243
  // (string) post content
244
- if (isset($this->columns['post_content']) &&
245
- isset($data[$this->columns['post_content']]) &&
246
- ! empty($data[$this->columns['post_content']])) {
247
- $post['post_content'] = $data[$this->columns['post_content']];
248
- unset($data[$this->columns['post_content']]);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249
  }
250
 
251
  // (string, comma divided) slug of post categories
252
- if (isset($this->columns['post_category']) &&
253
- isset($data[$this->columns['post_category']]) &&
254
- ! empty($data[$this->columns['post_category']])) {
255
- $categories = preg_split("/[\s,]+/", $data[$this->columns['post_category']]);
256
  if ($categories) {
257
  $post['post_category'] = wp_create_categories($categories);
258
  }
259
- unset($data[$this->columns['post_category']]);
260
- unset($categories);
261
  }
262
 
263
  // (string, comma divided) name of post tags
264
- if (isset($this->columns['post_tags']) &&
265
- isset($data[$this->columns['post_tags']]) &&
266
- ! empty($data[$this->columns['post_tags']])) {
267
- $tags = preg_split("/[\s,]+/", $data[$this->columns['post_tags']]);
268
  if ($tags) {
269
  $post['post_tags'] = $tags;
270
  }
271
- unset($data[$this->columns['post_tags']]);
272
  }
273
 
274
  $meta = array();
275
  foreach ($data as $key => $value) {
276
- if (!empty($value) && isset($this->column_raw[$key])) {
277
- $meta[$this->column_raw[$key]] = $value;
278
  }
279
  }
280
 
281
- $this->save_post($post,$meta,$is_update);
282
-
283
- echo '<li>'.esc_html($post['post_title']).'</li>';
 
 
 
284
  }
285
  }
286
 
287
  echo '</ol>';
288
 
289
- $this->fclose($handle);
290
 
291
  wp_import_cleanup($this->id);
292
 
@@ -325,4 +301,4 @@ $rs_csv_importer = new RS_CSV_Importer();
325
 
326
  register_importer('csv', __('CSV', 'rs-csv-importer'), __('Import posts, categories, tags, custom fields from simple csv file.', 'rs-csv-importer'), array ($rs_csv_importer, 'dispatch'));
327
 
328
- } // class_exists( 'WP_Importer' )
7
  Author URI: http://notnil-creative.com/
8
  Text Domain: rs-csv-importer
9
  License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10
+ Version: 0.3
11
  */
12
 
13
  if ( !defined('WP_LOAD_IMPORTERS') )
22
  require_once $class_wp_importer;
23
  }
24
 
25
+ // Load Helpers
26
+ require dirname( __FILE__ ) . '/rs-csv-helper.php';
27
  require dirname( __FILE__ ) . '/wp_post_helper/class-wp_post_helper.php';
28
 
29
  /**
38
  /** Sheet columns
39
  * @value array
40
  */
41
+ var $column_indexes = array();
42
+ var $column_keys = array();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
  // User interface wrapper start
45
  function header() {
56
  // Step 1
57
  function greet() {
58
  echo '<p>'.__( 'Choose a CSV (.csv) file to upload, then click Upload file and import.', 'rs-csv-importer' ).'</p>';
59
+ echo '<p>'.__( 'Maybe Excel-style CSV file is not best for import data. Follow export options below. LibreOffice might be good for you.', 'rs-csv-importer' ).'</p>';
60
  echo '<ol>';
61
  echo '<li>'.__( 'Select UTF-8 as charset.', 'rs-csv-importer' ).'</li>';
62
+ echo '<li>'.sprintf( __( 'You must use field delimiter as "%s"', 'rs-csv-importer'), RS_CSV_Helper::DELIMITER ).'</li>';
63
  echo '<li>'.__( 'You must quote all text cells.', 'rs-csv-importer' ).'</li>';
64
  echo '</ol>';
65
  echo '<p>'.__( 'Sample CSV file download:', 'rs-csv-importer' );
91
  return $result;
92
  }
93
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  /** Insert post and postmeta using wp_post_helper
95
  * @param array $post
96
  * @param array $meta
97
+ * @param bool $is_update
98
  * More information: https://gist.github.com/4084471
99
  */
100
  function save_post($post,$meta,$is_update) {
101
  $ph = new wp_post_helper($post);
102
 
103
  foreach ($meta as $key => $value) {
104
+ $is_acf = 0;
105
+ if (function_exists('get_field_object')) {
106
+ if (strpos($key, 'field_') === 0) {
107
+ $fobj = get_field_object($key);
108
+ if (is_array($fobj) && isset($fobj['key']) && $fobj['key'] == $key) {
109
+ $ph->add_field($key,$value);
110
+ $is_acf = 1;
111
+ }
112
+ }
113
+ }
114
+ if (!$is_acf)
115
+ $ph->add_meta($key,$value,true);
116
  }
117
 
118
  if ($is_update)
119
+ $result = $ph->update();
120
  else
121
+ $result = $ph->insert();
122
 
123
  unset($ph);
124
+
125
+ return $result;
126
  }
127
 
128
  // process parse csv ind insert posts
129
  function process_posts() {
130
+ $h = new RS_CSV_Helper;
131
 
132
+ $handle = $h->fopen($this->file, 'r');
133
  if ( $handle == false ) {
134
  echo '<p><strong>'.__( 'Failed to open file.', 'rs-csv-importer' ).'</strong></p>';
135
  wp_import_cleanup($this->id);
140
 
141
  echo '<ol>';
142
 
143
+ while (($data = $h->fgetcsv($handle)) !== FALSE) {
144
  if ($is_first) {
145
+ $h->parse_columns( $this, $data );
146
  $is_first = false;
147
  } else {
148
  $post = array();
149
  $is_update = false;
150
 
151
  // (int) post id
152
+ $post_id = $h->get_data($this,$data,'ID');
153
+ $post_id = ($post_id) ? $post_id : $h->get_data($this,$data,'post_id');
154
+ if ($post_id) {
155
+ $post_exist = get_post($post_id);
156
+ if ( is_null( $post_exist ) ) {
157
+ $post['import_id'] = $post_id;
158
+ } else {
159
+ $post['ID'] = $post_id;
160
+ $is_update = true;
161
+ }
 
 
 
162
  }
163
 
164
  // (string) post slug
165
+ $post_name = $h->get_data($this,$data,'post_name');
166
+ if ($post_name) {
167
+ $post['post_name'] = $post_name;
 
 
168
  }
169
 
170
  // (login or ID) post_author
171
+ $post_author = $h->get_data($this,$data,'post_author');
172
+ if ($post_author) {
 
 
173
  if (is_numeric($post_author)) {
174
  $user = get_user_by('id',$post_author);
175
  } else {
179
  $post['post_author'] = $user->ID;
180
  unset($user);
181
  }
 
 
182
  }
183
 
184
  // (string) publish date
185
+ $post_date = $h->get_data($this,$data,'post_date');
186
+ if ($post_date) {
187
+ $post['post_date'] = date("Y-m-d H:i:s", strtotime($post_date));
 
 
 
 
 
188
  }
189
 
190
  // (string) post type
191
+ $post_type = $h->get_data($this,$data,'post_type');
192
+ if ($post_type) {
193
+ $post['post_type'] = $post_type;
 
 
194
  }
195
 
196
  // (string) post status
197
+ $post_status = $h->get_data($this,$data,'post_status');
198
+ if ($post_status) {
199
+ $post['post_status'] = $post_status;
 
 
200
  }
201
 
202
  // (string) post title
203
+ $post['post_title'] = $h->get_data($this,$data,'post_title');
 
 
 
 
 
 
204
 
205
  // (string) post content
206
+ $post_content = $h->get_data($this,$data,'post_content');
207
+ if ($post_content) {
208
+ $post['post_content'] = $post_content;
209
+ }
210
+
211
+ // (string) post excerpt
212
+ $post_excerpt = $h->get_data($this,$data,'post_excerpt');
213
+ if ($post_excerpt) {
214
+ $post['post_excerpt'] = $post_excerpt;
215
+ }
216
+
217
+ // (int) post parent
218
+ $post_parent = $h->get_data($this,$data,'post_parent');
219
+ if ($post_parent) {
220
+ $post['post_parent'] = $post_parent;
221
+ }
222
+
223
+ // (int) menu order
224
+ $menu_order = $h->get_data($this,$data,'menu_order');
225
+ if ($menu_order) {
226
+ $post['menu_order'] = $menu_order;
227
  }
228
 
229
  // (string, comma divided) slug of post categories
230
+ $post_category = $h->get_data($this,$data,'post_category');
231
+ if ($post_category) {
232
+ $categories = preg_split("/[\s,]+/", $post_category);
 
233
  if ($categories) {
234
  $post['post_category'] = wp_create_categories($categories);
235
  }
 
 
236
  }
237
 
238
  // (string, comma divided) name of post tags
239
+ $post_tags = $h->get_data($this,$data,'post_tags');
240
+ if ($post_tags) {
241
+ $tags = preg_split("/[\s,]+/", $post_tags);
 
242
  if ($tags) {
243
  $post['post_tags'] = $tags;
244
  }
 
245
  }
246
 
247
  $meta = array();
248
  foreach ($data as $key => $value) {
249
+ if (!empty($value) && isset($this->column_keys[$key])) {
250
+ $meta[$this->column_keys[$key]] = $value;
251
  }
252
  }
253
 
254
+ $result = $this->save_post($post,$meta,$is_update);
255
+ if (!$result) {
256
+ echo '<li>'.sprintf(__('An error occurred during processing %s', 'rs-csv-importer'), esc_html($post['post_title'])).'</li>';
257
+ } else {
258
+ echo '<li>'.esc_html($post['post_title']).'</li>';
259
+ }
260
  }
261
  }
262
 
263
  echo '</ol>';
264
 
265
+ $h->fclose($handle);
266
 
267
  wp_import_cleanup($this->id);
268
 
301
 
302
  register_importer('csv', __('CSV', 'rs-csv-importer'), __('Import posts, categories, tags, custom fields from simple csv file.', 'rs-csv-importer'), array ($rs_csv_importer, 'dispatch'));
303
 
304
+ } // class_exists( 'WP_Importer' )
wp_post_helper/class-wp_post_helper.php CHANGED
@@ -104,6 +104,10 @@ class wp_post_helper {
104
  }
105
  unset($post);
106
  }
 
 
 
 
107
 
108
  $post = $this->post;
109
  foreach ($post as $key => &$val) {
104
  }
105
  unset($post);
106
  }
107
+
108
+ if (isset($args['import_id'])) {
109
+ $this->post->import_id = $args['import_id'];
110
+ }
111
 
112
  $post = $this->post;
113
  foreach ($post as $key => &$val) {