Really Simple CSV Importer - Version 1.0

Version Description

  • Enhancement: Replace a helper class to more better one
  • Enhancement: Add action hook
  • Bug fixes
Download this release

Release Info

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

Code changes from version 0.6.3 to 1.0

class-rs_csv_helper.php ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * A helper class for get data from CSV files.
5
+ *
6
+ * @package Really Simple CSV Importer
7
+ */
8
+ class RS_CSV_Helper {
9
+
10
+ const DELIMITER = ",";
11
+
12
+ // File utility functions
13
+ public function fopen($filename, $mode='r') {
14
+ return fopen($filename, $mode);
15
+ }
16
+
17
+ public function fgetcsv($handle, $length = 0) {
18
+ return fgetcsv($handle, $length, self::DELIMITER);
19
+ }
20
+
21
+ public function fclose($fp) {
22
+ return fclose($fp);
23
+ }
24
+
25
+ public function parse_columns(&$obj, $array) {
26
+ if (!is_array($array) || count($array) == 0)
27
+ return false;
28
+
29
+ $bom = pack("CCC", 0xef, 0xbb, 0xbf);
30
+ if (0 == strncmp($array[0], $bom, 3)) {
31
+ $array[0] = substr($array[0], 3);
32
+ }
33
+
34
+ $keys = array_keys($array);
35
+ $values = array_values($array);
36
+
37
+ $obj->column_indexes = array_combine($values, $keys);
38
+ $obj->column_keys = array_combine($keys, $values);
39
+ }
40
+
41
+ public function get_data($obj, &$array, $key) {
42
+ if (!isset($obj->column_indexes) || !is_array($array) || count($array) == 0)
43
+ return false;
44
+
45
+ if (isset($obj->column_indexes[$key])) {
46
+ $index = $obj->column_indexes[$key];
47
+ if (isset($array[$index]) && !empty($array[$index])) {
48
+ $value = $array[$index];
49
+ unset($array[$index]);
50
+ return $value;
51
+ } elseif (isset($array[$index])) {
52
+ unset($array[$index]);
53
+ }
54
+ }
55
+
56
+ return false;
57
+ }
58
+
59
+ }
class-rscsv_import_post_helper.php ADDED
@@ -0,0 +1,381 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * A helper class for insert or update post data.
5
+ *
6
+ * @package Really Simple CSV Importer
7
+ */
8
+ class RSCSV_Import_Post_Helper
9
+ {
10
+ const CFS_PREFIX = 'cfs_';
11
+
12
+ /**
13
+ * @var $post WP_Post object
14
+ */
15
+ private $post;
16
+
17
+ /**
18
+ * @var $error WP_Error object
19
+ */
20
+ private $error;
21
+
22
+ /**
23
+ * Add an error or append additional message to this object.
24
+ *
25
+ * @param string|int $code Error code.
26
+ * @param string $message Error message.
27
+ * @param mixed $data Optional. Error data.
28
+ */
29
+ public function addError($code, $message, $data = '')
30
+ {
31
+ if (!$this->isError()) {
32
+ $e = new WP_Error();
33
+ $this->error = $e;
34
+ }
35
+ $this->error->add($code, $message, $data);
36
+ }
37
+
38
+ /**
39
+ * Get the error of this object
40
+ *
41
+ * @return (WP_Error)
42
+ */
43
+ public function getError()
44
+ {
45
+ if (!$this->isError()) {
46
+ $e = new WP_Error();
47
+ return $e;
48
+ }
49
+ return $this->error;
50
+ }
51
+
52
+ /**
53
+ * Check the object has some Errors.
54
+ *
55
+ * @return (bool)
56
+ */
57
+ public function isError()
58
+ {
59
+ return is_wp_error($this->error);
60
+ }
61
+
62
+ /**
63
+ * Set WP_Post object
64
+ *
65
+ * @param (int) $post_id Post ID
66
+ */
67
+ protected function setPost($post_id)
68
+ {
69
+ $post = get_post($post_id);
70
+ if (is_object($post)) {
71
+ $this->post = $post;
72
+ } else {
73
+ $this->addError('post_id_not_found', __('Provided Post ID not found.', 'rs-csv-importer'));
74
+ }
75
+ }
76
+
77
+ /**
78
+ * Get WP_Post object
79
+ *
80
+ * @return (WP_Post|null)
81
+ */
82
+ public function getPost()
83
+ {
84
+ return $this->post;
85
+ }
86
+
87
+ /**
88
+ * Get object by post id.
89
+ *
90
+ * @param (int) $post_id Post ID
91
+ * @return (RSCSV_Import_Post_Helper)
92
+ */
93
+ public static function getByID($post_id)
94
+ {
95
+ $object = new RSCSV_Import_Post_Helper();
96
+ $object->setPost($post_id);
97
+ return $object;
98
+ }
99
+
100
+ /**
101
+ * Add a post
102
+ *
103
+ * @param (array) $data An associative array of the post data
104
+ * @return (RSCSV_Import_Post_Helper)
105
+ */
106
+ public static function add($data)
107
+ {
108
+ $object = new RSCSV_Import_Post_Helper();
109
+ $post_id = wp_insert_post($data, true);
110
+ if (is_wp_error($post_id)) {
111
+ $object->addError($post_id->get_error_code(), $post_id->get_error_message());
112
+ } else {
113
+ $object->setPost($post_id);
114
+ }
115
+ return $object;
116
+ }
117
+
118
+ /**
119
+ * Update post
120
+ *
121
+ * @param (array) $data An associative array of the post data
122
+ */
123
+ public function update($data)
124
+ {
125
+ $post = $this->getPost();
126
+ if ($post instanceof WP_Post) {
127
+ $data['ID'] = $post->ID;
128
+ }
129
+ $post_id = wp_update_post($data, true);
130
+ if (is_wp_error($post_id)) {
131
+ $this->addError($post_id->get_error_code(), $post_id->get_error_message());
132
+ } else {
133
+ $this->setPost($post_id);
134
+ }
135
+ }
136
+
137
+ /**
138
+ * Set meta fields by array
139
+ *
140
+ * @param (array) $data An associative array of metadata
141
+ */
142
+ public function setMeta($data)
143
+ {
144
+ foreach ($data as $key => $value) {
145
+ $is_cfs = 0;
146
+ $is_acf = 0;
147
+ if (strpos($key, self::CFS_PREFIX) === 0) {
148
+ $this->cfsSave(substr($key, strlen(self::CFS_PREFIX)), $value);
149
+ $is_cfs = 1;
150
+ } else {
151
+ if (function_exists('get_field_object')) {
152
+ if (strpos($key, 'field_') === 0) {
153
+ $fobj = get_field_object($key);
154
+ if (is_array($fobj) && isset($fobj['key']) && $fobj['key'] == $key) {
155
+ $this->acfUpdateField($key, $value);
156
+ $is_acf = 1;
157
+ }
158
+ }
159
+ }
160
+ }
161
+ if (!$is_acf && !$is_cfs) {
162
+ $this->updateMeta($key, $value);
163
+ }
164
+ }
165
+ }
166
+
167
+ /**
168
+ * A wrapper of update_post_meta
169
+ *
170
+ * @param (string) $key
171
+ * @param (string/array) $value
172
+ */
173
+ protected function updateMeta($key, $value)
174
+ {
175
+ $post = $this->getPost();
176
+ if ($post instanceof WP_Post) {
177
+ update_post_meta($post->ID, $key, $value);
178
+ } else {
179
+ $this->addError('post_is_not_set', __('WP_Post object is not set.', 'rs-csv-importer'));
180
+ }
181
+ }
182
+
183
+ /**
184
+ * A wrapper of update_field of Advanced Custom Fields
185
+ *
186
+ * @param (string) $key
187
+ * @param (string/array) $value
188
+ */
189
+ protected function acfUpdateField($key, $value)
190
+ {
191
+ $post = $this->getPost();
192
+ if ($post instanceof WP_Post) {
193
+ if (function_exists('update_field')) {
194
+ update_field($key, $value, $post->ID);
195
+ } else {
196
+ $this->updateMeta($key, $value);
197
+ }
198
+ } else {
199
+ $this->addError('post_is_not_set', __('WP_Post object is not set.', 'rs-csv-importer'));
200
+ }
201
+ }
202
+
203
+ /**
204
+ * A wrapper of CFS()->save()
205
+ *
206
+ * @param (array) $data
207
+ */
208
+ protected function cfsSave($key, $value)
209
+ {
210
+ $post = $this->getPost();
211
+ if ($post instanceof WP_Post) {
212
+ if (function_exists('CFS')) {
213
+ $field_data = array($key => $value);
214
+ $post_data = array('ID' => $post->ID);
215
+ CFS()->save($field_data, $post_data);
216
+ } else {
217
+ $this->updateMeta($key, $value);
218
+ }
219
+ } else {
220
+ $this->addError('post_is_not_set', __('WP_Post object is not set.', 'rs-csv-importer'));
221
+ }
222
+ }
223
+
224
+ /**
225
+ * A wrapper of wp_set_post_tags
226
+ *
227
+ * @param (array) $tags
228
+ */
229
+ public function setPostTags($tags)
230
+ {
231
+ $post = $this->getPost();
232
+ if ($post instanceof WP_Post) {
233
+ wp_set_post_tags($post->ID, $tags);
234
+ } else {
235
+ $this->addError('post_is_not_set', __('WP_Post object is not set.', 'rs-csv-importer'));
236
+ }
237
+ }
238
+
239
+ /**
240
+ * A wrapper of wp_set_object_terms
241
+ *
242
+ * @param (array/string) $taxonomy The context in which to relate the term to the object
243
+ * @param (array/int/string) $terms The slug or id of the term
244
+ */
245
+ public function setObjectTerms($taxonomy, $terms)
246
+ {
247
+ $post = $this->getPost();
248
+ if ($post instanceof WP_Post) {
249
+ wp_set_object_terms($post->ID, $terms, $taxonomy);
250
+ } else {
251
+ $this->addError('post_is_not_set', __('WP_Post object is not set.', 'rs-csv-importer'));
252
+ }
253
+ }
254
+
255
+ /**
256
+ * Add attachment file. Automatically get remote file
257
+ *
258
+ * @param (string) $file
259
+ * @return (boolean) True on success, false on failure.
260
+ */
261
+ public function addMediaFile($file)
262
+ {
263
+ if (parse_url($file, PHP_URL_SCHEME)) {
264
+ $file = $this->remoteGet($file);
265
+ }
266
+ $id = $this->setAttachment($file);
267
+ if ($id) {
268
+ return true;
269
+ }
270
+
271
+ return false;
272
+ }
273
+
274
+ /**
275
+ * Add attachment file and set as thumbnail. Automatically get remote file
276
+ *
277
+ * @param (string) $file
278
+ * @return (boolean) True on success, false on failure.
279
+ */
280
+ public function addThumbnail($file)
281
+ {
282
+ $post = $this->getPost();
283
+ if ($post instanceof WP_Post) {
284
+ if (parse_url($file, PHP_URL_SCHEME)) {
285
+ $file = $this->remoteGet($file);
286
+ }
287
+ $thumbnail_id = $this->setAttachment($file);
288
+ if ($thumbnail_id) {
289
+ $meta_id = set_post_thumbnail($post, $thumbnail_id);
290
+ if ($meta_id) {
291
+ return true;
292
+ }
293
+ }
294
+ }
295
+
296
+ return false;
297
+ }
298
+
299
+ /**
300
+ * A wrapper of wp_insert_attachment and wp_update_attachment_metadata
301
+ *
302
+ * @param (string) $file
303
+ * @return (int) Return the attachment id on success, 0 on failure.
304
+ */
305
+ public function setAttachment($file)
306
+ {
307
+ $post = $this->getPost();
308
+ if ($post instanceof WP_Post) {
309
+ if ( $file && file_exists($file) ) {
310
+ $filename = basename($file);
311
+ $wp_filetype = wp_check_filetype_and_ext($file, $filename);
312
+ $ext = empty( $wp_filetype['ext'] ) ? '' : $wp_filetype['ext'];
313
+ $type = empty( $wp_filetype['type'] ) ? '' : $wp_filetype['type'];
314
+ $proper_filename = empty( $wp_filetype['proper_filename'] ) ? '' : $wp_filetype['proper_filename'];
315
+ $filename = ($proper_filename) ? $proper_filename : $filename;
316
+ $filename = sanitize_file_name($filename);
317
+
318
+ $upload_dir = wp_upload_dir();
319
+ $guid = $upload_dir['baseurl'] . '/' . _wp_relative_upload_path($file);
320
+
321
+ $attachment = array(
322
+ 'post_mime_type' => $type,
323
+ 'guid' => $guid,
324
+ 'post_title' => $filename,
325
+ 'post_content' => '',
326
+ 'post_status' => 'inherit'
327
+ );
328
+ $attachment_id = wp_insert_attachment($attachment, $file, $post->ID);
329
+ $attachment_metadata = wp_generate_attachment_metadata( $attachment_id, $file );
330
+ wp_update_attachment_metadata($attachment_id, $attachment_metadata);
331
+ return $attachment_id;
332
+ }
333
+ }
334
+ // On failure
335
+ return 0;
336
+ }
337
+
338
+ /**
339
+ * A wrapper of wp_safe_remote_get
340
+ *
341
+ * @param (string) $url
342
+ * @param (array) $args
343
+ * @return (string) file path
344
+ */
345
+ public function remoteGet($url, $args = array())
346
+ {
347
+ global $wp_filesystem;
348
+ if (!is_object($wp_filesystem)) {
349
+ WP_Filesystem();
350
+ }
351
+
352
+ if ($url && is_object($wp_filesystem)) {
353
+ $response = wp_safe_remote_get($url, $args);
354
+ if (!is_wp_error($response) && $response['response']['code'] === 200) {
355
+ $destination = wp_upload_dir();
356
+ $filename = basename($url);
357
+ $filepath = $destination['path'] . '/' . wp_unique_filename($destination['path'], $filename);
358
+
359
+ $body = wp_remote_retrieve_body($response);
360
+
361
+ if ( $body && $wp_filesystem->put_contents($filepath , $body, FS_CHMOD_FILE) ) {
362
+ return $filepath;
363
+ } else {
364
+ $this->addError('remote_get_failed', __('Could not get remote file.', 'rs-csv-importer'));
365
+ }
366
+ } elseif (is_wp_error($response)) {
367
+ $this->addError($response->get_error_code(), $response->get_error_message());
368
+ }
369
+ }
370
+
371
+ return '';
372
+ }
373
+
374
+ /**
375
+ * Unset WP_Post object
376
+ */
377
+ public function __destruct()
378
+ {
379
+ unset($this->post);
380
+ }
381
+ }
readme.txt CHANGED
@@ -1,9 +1,9 @@
1
  === Really Simple CSV Importer ===
2
- Contributors: hissy, wokamoto
3
  Tags: importer, csv, acf, cfs
4
- Requires at least: 3.0
5
  Tested up to: 4.0.1
6
- Stable tag: 0.6.3
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
@@ -20,9 +20,11 @@ Alternative CSV Importer plugin. Simple and powerful, best for geeks.
20
  * [Advanced Custom Fields](http://www.advancedcustomfields.com/) support
21
  * Custom Taxonomy support
22
  * Custom Post Type support
23
- * Many useful filter hooks
 
 
24
 
25
- You can download CSV file examples in `/wp-content/plugins/really-simple-csv-importer/sample` directory.
26
 
27
  = Available column names and values: =
28
  * `ID` or `post_id`: (int) post id.
@@ -70,15 +72,15 @@ Cover banner designed by @[luchino__](http://uwasora.com/)
70
 
71
  = Should I fill all columns of post data? =
72
 
73
- No. Only columns you need.
74
 
75
  = Can I update existing post data? =
76
 
77
- Yes. Please use ID field.
78
 
79
  = Can I insert post with specific post id? =
80
 
81
- Yes. Please use ID field.
82
 
83
  = Can I import custom field/custom taxonomy of the post? =
84
 
@@ -202,6 +204,16 @@ function really_simple_csv_importer_save_tax_filter( $tax, $post, $is_update ) {
202
  add_filter( 'really_simple_csv_importer_save_tax', 'really_simple_csv_importer_save_tax_filter', 10, 3 );
203
  `
204
 
 
 
 
 
 
 
 
 
 
 
205
  = really_simple_csv_importer_class =
206
 
207
  This filter provides availability to completely replace the `RS_CSV_Importer::save_post` method.
@@ -210,6 +222,10 @@ Example: [gist](https://gist.github.com/hissy/1ea54a46fd07be9f4334)
210
 
211
  == Changelog ==
212
 
 
 
 
 
213
  = 0.6.3 =
214
  * Enhancement: Remove Byte Order Mark automatically (Thanks lucianosantana!)
215
  = 0.6.2 =
@@ -252,3 +268,8 @@ Example: [gist](https://gist.github.com/hissy/1ea54a46fd07be9f4334)
252
  * Bug fix
253
  = 0.1 =
254
  * First Release (beta)
 
 
 
 
 
1
  === Really Simple CSV Importer ===
2
+ Contributors: hissy
3
  Tags: importer, csv, acf, cfs
4
+ Requires at least: 3.6
5
  Tested up to: 4.0.1
6
+ Stable tag: 1.0
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
20
  * [Advanced Custom Fields](http://www.advancedcustomfields.com/) support
21
  * Custom Taxonomy support
22
  * Custom Post Type support
23
+ * Filter hook for dry-run-testing
24
+ * Filter hooks for customize csv data before importing to database
25
+ * Action hook for update post data after importing to database
26
 
27
+ You can get example CSV files in `/wp-content/plugins/really-simple-csv-importer/sample` directory.
28
 
29
  = Available column names and values: =
30
  * `ID` or `post_id`: (int) post id.
72
 
73
  = Should I fill all columns of post data? =
74
 
75
+ No. Only columns which you want to update.
76
 
77
  = Can I update existing post data? =
78
 
79
+ Yes. Please use ID field to specify the existing post.
80
 
81
  = Can I insert post with specific post id? =
82
 
83
+ Yes. Please use ID field to specify the new post ID.
84
 
85
  = Can I import custom field/custom taxonomy of the post? =
86
 
204
  add_filter( 'really_simple_csv_importer_save_tax', 'really_simple_csv_importer_save_tax_filter', 10, 3 );
205
  `
206
 
207
+ == How to customize the post data after importing to database ==
208
+
209
+ = really_simple_csv_importer_post_saved =
210
+
211
+ This action provides availability to run some tasks after importing.
212
+
213
+ Example: [gist](https://gist.github.com/hissy/fe0aa2582b78394a3a82)
214
+
215
+ == How to customize the importing process entirely ==
216
+
217
  = really_simple_csv_importer_class =
218
 
219
  This filter provides availability to completely replace the `RS_CSV_Importer::save_post` method.
222
 
223
  == Changelog ==
224
 
225
+ = 1.0 =
226
+ * Enhancement: Replace a helper class to more better one
227
+ * Enhancement: Add action hook
228
+ * Bug fixes
229
  = 0.6.3 =
230
  * Enhancement: Remove Byte Order Mark automatically (Thanks lucianosantana!)
231
  = 0.6.2 =
268
  * Bug fix
269
  = 0.1 =
270
  * First Release (beta)
271
+
272
+ == Upgrade Notice ==
273
+
274
+ = 1.0 =
275
+ * wp_post_helper class is deprecated.
rs-csv-importer.php CHANGED
@@ -7,7 +7,7 @@ Author: Takuro Hishikawa, wokamoto
7
  Author URI: https://en.digitalcube.jp/
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.6.3
11
  */
12
 
13
  if ( !defined('WP_LOAD_IMPORTERS') )
@@ -23,8 +23,8 @@ if ( !class_exists( '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
  /**
30
  * CSV Importer
@@ -44,7 +44,6 @@ class RS_CSV_Importer extends WP_Importer {
44
  // User interface wrapper start
45
  function header() {
46
  echo '<div class="wrap">';
47
- screen_icon();
48
  echo '<h2>'.__('Import CSV', 'rs-csv-importer').'</h2>';
49
  }
50
 
@@ -103,47 +102,41 @@ class RS_CSV_Importer extends WP_Importer {
103
  * @param array $terms
104
  * @param string $thumbnail The uri or path of thumbnail image.
105
  * @param bool $is_update
106
- * @return int|false Saved post id. If failed, return false.
107
  */
108
  public function save_post($post,$meta,$terms,$thumbnail,$is_update) {
109
- $ph = new wp_post_helper($post);
110
 
111
- foreach ($meta as $key => $value) {
112
- $is_cfs = 0;
113
- $is_acf = 0;
114
- $cfs_prefix = 'cfs_';
115
- if (strpos($key, $cfs_prefix) === 0) {
116
- $ph->add_cfs_field( substr($key, strlen($cfs_prefix)), $value );
117
- $is_cfs = 1;
118
- } else {
119
- if (function_exists('get_field_object')) {
120
- if (strpos($key, 'field_') === 0) {
121
- $fobj = get_field_object($key);
122
- if (is_array($fobj) && isset($fobj['key']) && $fobj['key'] == $key) {
123
- $ph->add_field($key,$value);
124
- $is_acf = 1;
125
- }
126
- }
127
- }
128
- }
129
- if (!$is_acf && !$is_cfs)
130
- $ph->add_meta($key,$value,true);
131
  }
132
-
133
- foreach ($terms as $key => $value) {
134
- $ph->add_terms($key, $value);
 
 
 
 
135
  }
136
 
137
- if ($thumbnail) $ph->add_media($thumbnail,'','','',true);
 
138
 
139
- if ($is_update)
140
- $result = $ph->update();
141
- else
142
- $result = $ph->insert();
143
 
144
- unset($ph);
 
 
 
145
 
146
- return $result;
 
 
 
 
 
147
  }
148
 
149
  // process parse csv ind insert posts
@@ -280,8 +273,6 @@ class RS_CSV_Importer extends WP_Importer {
280
 
281
  // (string) post thumbnail image uri
282
  $post_thumbnail = $h->get_data($this,$data,'post_thumbnail');
283
- if (parse_url($post_thumbnail, PHP_URL_SCHEME))
284
- $post_thumbnail = remote_get_file($post_thumbnail);
285
 
286
  $meta = array();
287
  $tax = array();
@@ -330,7 +321,9 @@ class RS_CSV_Importer extends WP_Importer {
330
  $tax = apply_filters( 'really_simple_csv_importer_save_tax', $tax, $post, $is_update );
331
 
332
  /**
333
- * Option for dry run
 
 
334
  *
335
  * @param bool false
336
  */
@@ -341,6 +334,8 @@ class RS_CSV_Importer extends WP_Importer {
341
  /**
342
  * Get Alternative Importer Class name.
343
  *
 
 
344
  * @param string Class name to override Importer class. Default to null (do not override).
345
  */
346
  $class = apply_filters( 'really_simple_csv_importer_class', null );
@@ -353,10 +348,23 @@ class RS_CSV_Importer extends WP_Importer {
353
  $result = $this->save_post($post,$meta,$tax,$post_thumbnail,$is_update);
354
  }
355
 
356
- if ($result) {
357
- echo esc_html(sprintf(__('Processing "%s" done.', 'rs-csv-importer'), $post_title));
358
  } else {
359
- $error->add( 'save_post', __('An error occurred while saving the post to database.', 'rs-csv-importer') );
 
 
 
 
 
 
 
 
 
 
 
 
 
360
  }
361
  }
362
 
7
  Author URI: https://en.digitalcube.jp/
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: 1.0
11
  */
12
 
13
  if ( !defined('WP_LOAD_IMPORTERS') )
23
  }
24
 
25
  // Load Helpers
26
+ require dirname( __FILE__ ) . '/class-rs_csv_helper.php';
27
+ require dirname( __FILE__ ) . '/class-rscsv_import_post_helper.php';
28
 
29
  /**
30
  * CSV Importer
44
  // User interface wrapper start
45
  function header() {
46
  echo '<div class="wrap">';
 
47
  echo '<h2>'.__('Import CSV', 'rs-csv-importer').'</h2>';
48
  }
49
 
102
  * @param array $terms
103
  * @param string $thumbnail The uri or path of thumbnail image.
104
  * @param bool $is_update
105
+ * @return RSCSV_Import_Post_Helper
106
  */
107
  public function save_post($post,$meta,$terms,$thumbnail,$is_update) {
 
108
 
109
+ // Separate the post tags from $post array
110
+ if (isset($post['post_tags']) && !empty($post['post_tags'])) {
111
+ $post_tags = $post['post_tags'];
112
+ unset($post['post_tags']);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  }
114
+
115
+ // Add or update the post
116
+ if ($is_update) {
117
+ $h = RSCSV_Import_Post_Helper::getByID($post['ID']);
118
+ $h->update($post);
119
+ } else {
120
+ $h = RSCSV_Import_Post_Helper::add($post);
121
  }
122
 
123
+ // Set post tags
124
+ $h->setPostTags($post_tags);
125
 
126
+ // Set meta data
127
+ $h->setMeta($meta);
 
 
128
 
129
+ // Set terms
130
+ foreach ($terms as $key => $value) {
131
+ $h->setObjectTerms($key, $value);
132
+ }
133
 
134
+ // Add thumbnail
135
+ if ($thumbnail) {
136
+ $h->addThumbnail($thumbnail);
137
+ }
138
+
139
+ return $h;
140
  }
141
 
142
  // process parse csv ind insert posts
273
 
274
  // (string) post thumbnail image uri
275
  $post_thumbnail = $h->get_data($this,$data,'post_thumbnail');
 
 
276
 
277
  $meta = array();
278
  $tax = array();
321
  $tax = apply_filters( 'really_simple_csv_importer_save_tax', $tax, $post, $is_update );
322
 
323
  /**
324
+ * Option for dry run testing
325
+ *
326
+ * @since 0.5.7
327
  *
328
  * @param bool false
329
  */
334
  /**
335
  * Get Alternative Importer Class name.
336
  *
337
+ * @since 0.6
338
+ *
339
  * @param string Class name to override Importer class. Default to null (do not override).
340
  */
341
  $class = apply_filters( 'really_simple_csv_importer_class', null );
348
  $result = $this->save_post($post,$meta,$tax,$post_thumbnail,$is_update);
349
  }
350
 
351
+ if ($result->isError()) {
352
+ $error = $result->getError();
353
  } else {
354
+ $post_object = $result->getPost();
355
+
356
+ if (is_object($post_object)) {
357
+ /**
358
+ * Fires adter the post imported.
359
+ *
360
+ * @since 1.0
361
+ *
362
+ * @param WP_Post $post_object
363
+ */
364
+ do_action( 'really_simple_csv_importer_post_saved', $post_object );
365
+ }
366
+
367
+ echo esc_html(sprintf(__('Processing "%s" done.', 'rs-csv-importer'), $post_title));
368
  }
369
  }
370
 
sample/import_thumbnail.csv ADDED
@@ -0,0 +1,2 @@
 
 
1
+ "post_title","post_type","post_thumbnail"
2
+ "Remote get test","post","http://upload.wikimedia.org/wikipedia/commons/a/a2/WordPress_MP6_dashboard.png"
sample/import_thumbnail.ods ADDED
Binary file