Really Simple CSV Importer - Version 0.5.2

Version Description

  • New feature: Add Post Thumbnail support
  • Bug fixes
Download this release

Release Info

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

Code changes from version 0.5.1 to 0.5.2

readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: hissy, wokamoto
3
  Tags: importer, csv, acf
4
  Requires at least: 3.0
5
  Tested up to: 3.6.1
6
- Stable tag: 0.5.1
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
@@ -35,20 +35,20 @@ Contains CSV file samples in `/wp-content/plugins/really-simple-csv-importer/sam
35
  * `post_parent`: (int) The post parent id. Used for page or hierarchical post type.
36
  * `menu_order`: (int)
37
  * `post_type`: ('post' or 'page' or any other post type name)
 
 
38
  * `post_category`: (string, comma divided) slug of post categories
39
  * `post_tags`: (string, comma divided) name of post tags
40
  * `{custom_field}`: (string) any other column labels used as custom field
41
  * `{tax_$taxonomy}`: (string, comma divided) any field prefixed with tax_ in the "custom_field" area will be used as a custom taxonomy. Entries are names, not slugs
42
 
43
- Note: To set the page template of a page, use custom field key of `_wp_page_template`.
44
  Note: If providing a post_status of 'future' you must specify the post_date in order for WordPress to know when to publish your post.
45
 
46
  = Advanced Custom Fields plugin integrate =
47
  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.
48
  How to find advanced custom field key: [Finding the field key](http://www.advancedcustomfields.com/resources/functions/update_field/#finding-the%20field%20key)
49
 
50
- Note: multiple value is not supported yet.
51
-
52
  = Official public repository =
53
  Add star and read future issues about rs-csv-importer on [GitHub](https://github.com/hissy/rs-csv-importer)!
54
 
@@ -59,7 +59,31 @@ Add star and read future issues about rs-csv-importer on [GitHub](https://github
59
  3. Go to the Import page under Tools menu.
60
  4. Click CSV link, read the notification, then just upload and import.
61
 
62
- == How to customize import rules ==
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
  There are three filters available in the importer.
65
 
@@ -153,6 +177,9 @@ add_filter( 'really_simple_csv_importer_save_tax', 'really_simple_csv_importer_s
153
 
154
  == Changelog ==
155
 
 
 
 
156
  = 0.5.1 =
157
  * Enhancement: Check whether both posts has same post type when updating.
158
  = 0.5 =
3
  Tags: importer, csv, acf
4
  Requires at least: 3.0
5
  Tested up to: 3.6.1
6
+ Stable tag: 0.5.2
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
35
  * `post_parent`: (int) The post parent id. Used for page or hierarchical post type.
36
  * `menu_order`: (int)
37
  * `post_type`: ('post' or 'page' or any other post type name)
38
+ * `post_thumbnail`: (string) The uri or path of the post thumbnail.
39
+ E.g. http://example.com/example.jpg or /path/to/example.jpg
40
  * `post_category`: (string, comma divided) slug of post categories
41
  * `post_tags`: (string, comma divided) name of post tags
42
  * `{custom_field}`: (string) any other column labels used as custom field
43
  * `{tax_$taxonomy}`: (string, comma divided) any field prefixed with tax_ in the "custom_field" area will be used as a custom taxonomy. Entries are names, not slugs
44
 
45
+ Note: To set the page template of a page, use custom field key of `_wp_page_template`.
46
  Note: If providing a post_status of 'future' you must specify the post_date in order for WordPress to know when to publish your post.
47
 
48
  = Advanced Custom Fields plugin integrate =
49
  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.
50
  How to find advanced custom field key: [Finding the field key](http://www.advancedcustomfields.com/resources/functions/update_field/#finding-the%20field%20key)
51
 
 
 
52
  = Official public repository =
53
  Add star and read future issues about rs-csv-importer on [GitHub](https://github.com/hissy/rs-csv-importer)!
54
 
59
  3. Go to the Import page under Tools menu.
60
  4. Click CSV link, read the notification, then just upload and import.
61
 
62
+ == Frequently Asked Questions ==
63
+
64
+ = Should I fill all columns of post data? =
65
+
66
+ No. Only columns you need.
67
+
68
+ = Can I update existing post data? =
69
+
70
+ Yes. Please use ID field.
71
+
72
+ = Can I insert post with specific post id? =
73
+
74
+ Yes. Please use ID field.
75
+
76
+ = Why should I quote text cells when I save csv file? =
77
+
78
+ Because PHP cannot read multibyte text cells in some cases.
79
+
80
+ > Locale setting is taken into account by this function. If LANG is e.g. en_US.UTF-8, files in one-byte encoding are read wrong by this function.
81
+
82
+ = Can I insert multiple value to ACF field like Select or Checkbox? =
83
+
84
+ Yes. Please use `really_simple_csv_importer_save_meta` filter to make array data.
85
+
86
+ == How to customize import post data ==
87
 
88
  There are three filters available in the importer.
89
 
177
 
178
  == Changelog ==
179
 
180
+ = 0.5.2 =
181
+ * New feature: Add Post Thumbnail support
182
+ * Bug fixes
183
  = 0.5.1 =
184
  * Enhancement: Check whether both posts has same post type when updating.
185
  = 0.5 =
rs-csv-helper.php CHANGED
@@ -5,15 +5,15 @@ class RS_CSV_Helper {
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
 
@@ -28,7 +28,7 @@ class RS_CSV_Helper {
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
 
@@ -38,6 +38,8 @@ class RS_CSV_Helper {
38
  $value = $array[$index];
39
  unset($array[$index]);
40
  return $value;
 
 
41
  }
42
  }
43
 
5
  const DELIMITER = ",";
6
 
7
  // File utility functions
8
+ public function fopen($filename, $mode='r') {
9
  return fopen($filename, $mode);
10
  }
11
 
12
+ public function fgetcsv($handle, $length = 0) {
13
  return fgetcsv($handle, $length, self::DELIMITER);
14
  }
15
 
16
+ public function fclose($fp) {
17
  return fclose($fp);
18
  }
19
 
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
 
38
  $value = $array[$index];
39
  unset($array[$index]);
40
  return $value;
41
+ } elseif (isset($array[$index])) {
42
+ unset($array[$index]);
43
  }
44
  }
45
 
rs-csv-importer.php CHANGED
@@ -4,10 +4,10 @@ Plugin Name: Really Simple CSV Importer
4
  Plugin URI: http://wordpress.org/plugins/really-simple-csv-importer/
5
  Description: Import posts, categories, tags, custom fields from simple csv file.
6
  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.5.1
11
  */
12
 
13
  if ( !defined('WP_LOAD_IMPORTERS') )
@@ -38,8 +38,8 @@ class RS_CSV_Importer extends WP_Importer {
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() {
@@ -99,10 +99,11 @@ class RS_CSV_Importer extends WP_Importer {
99
  * @param array $post
100
  * @param array $meta
101
  * @param array $terms
 
102
  * @param bool $is_update
103
  * @return int|false Saved post id. If failed, return false.
104
  */
105
- function save_post($post,$meta,$terms,$is_update) {
106
  $ph = new wp_post_helper($post);
107
 
108
  foreach ($meta as $key => $value) {
@@ -124,6 +125,8 @@ class RS_CSV_Importer extends WP_Importer {
124
  $ph->add_terms($key, $value);
125
  }
126
 
 
 
127
  if ($is_update)
128
  $result = $ph->update();
129
  else
@@ -263,12 +266,17 @@ class RS_CSV_Importer extends WP_Importer {
263
  }
264
  }
265
 
 
 
 
 
 
266
  $meta = array();
267
  $tax = array();
268
 
269
  // add any other data to post meta
270
  foreach ($data as $key => $value) {
271
- if (!empty($value) && isset($this->column_keys[$key])) {
272
  // check if meta is custom taxonomy
273
  if (substr($this->column_keys[$key], 0, 4) == 'tax_') {
274
  // (string, comma divided) name of custom taxonomies
@@ -310,9 +318,9 @@ class RS_CSV_Importer extends WP_Importer {
310
  $tax = apply_filters( 'really_simple_csv_importer_save_tax', $tax, $post, $is_update );
311
 
312
  // save post data
313
- $result = $this->save_post($post,$meta,$tax,$is_update);
314
  if (!$result) {
315
- $error->add( 'save_post', sprintf(__('An error occurred while saving the post to database.', 'rs-csv-importer')) );
316
  }
317
 
318
  // show results
4
  Plugin URI: http://wordpress.org/plugins/really-simple-csv-importer/
5
  Description: Import posts, categories, tags, custom fields from simple csv file.
6
  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.5.2
11
  */
12
 
13
  if ( !defined('WP_LOAD_IMPORTERS') )
38
  /** Sheet columns
39
  * @value array
40
  */
41
+ public $column_indexes = array();
42
+ public $column_keys = array();
43
 
44
  // User interface wrapper start
45
  function header() {
99
  * @param array $post
100
  * @param array $meta
101
  * @param array $terms
102
+ * @param string $thumbnail The uri or path of thumbnail image.
103
  * @param bool $is_update
104
  * @return int|false Saved post id. If failed, return false.
105
  */
106
+ function save_post($post,$meta,$terms,$thumbnail,$is_update) {
107
  $ph = new wp_post_helper($post);
108
 
109
  foreach ($meta as $key => $value) {
125
  $ph->add_terms($key, $value);
126
  }
127
 
128
+ if ($thumbnail) $ph->add_media($thumbnail,'','','',true);
129
+
130
  if ($is_update)
131
  $result = $ph->update();
132
  else
266
  }
267
  }
268
 
269
+ // (string) post thumbnail image uri
270
+ $post_thumbnail = $h->get_data($this,$data,'post_thumbnail');
271
+ if (parse_url($post_thumbnail, PHP_URL_SCHEME))
272
+ $post_thumbnail = remote_get_file($post_thumbnail);
273
+
274
  $meta = array();
275
  $tax = array();
276
 
277
  // add any other data to post meta
278
  foreach ($data as $key => $value) {
279
+ if ($value !== false && isset($this->column_keys[$key])) {
280
  // check if meta is custom taxonomy
281
  if (substr($this->column_keys[$key], 0, 4) == 'tax_') {
282
  // (string, comma divided) name of custom taxonomies
318
  $tax = apply_filters( 'really_simple_csv_importer_save_tax', $tax, $post, $is_update );
319
 
320
  // save post data
321
+ $result = $this->save_post($post,$meta,$tax,$post_thumbnail,$is_update);
322
  if (!$result) {
323
+ $error->add( 'save_post', __('An error occurred while saving the post to database.', 'rs-csv-importer') );
324
  }
325
 
326
  // show results
wp_post_helper/class-wp_post_helper.php CHANGED
@@ -297,7 +297,7 @@ class wp_post_helper {
297
  if (!$this->postid)
298
  $this->metas[$metakey] = array($val, $unique);
299
  else
300
- return $val ? add_post_meta($this->postid, $metakey, $val, $unique) : false;
301
  }
302
 
303
  // Add Advanced Custom Field
@@ -309,6 +309,7 @@ class wp_post_helper {
309
  }
310
  }
311
 
 
312
  function remote_get_file($url = null, $file_dir = '') {
313
  if (!$url)
314
  return false;
@@ -336,7 +337,11 @@ function remote_get_file($url = null, $file_dir = '') {
336
  // remote get!
337
  $photo = $file_dir . basename($url);
338
  if ( !file_exists($photo) ) {
339
- $response = wp_remote_get($url);
 
 
 
 
340
  if ( !is_wp_error($response) && $response["response"]["code"] === 200 ) {
341
  $photo_data = $response["body"];
342
  file_put_contents($photo, $photo_data);
@@ -348,5 +353,6 @@ function remote_get_file($url = null, $file_dir = '') {
348
  }
349
  return file_exists($photo) ? $photo : false;
350
  }
 
351
 
352
  endif;
297
  if (!$this->postid)
298
  $this->metas[$metakey] = array($val, $unique);
299
  else
300
+ return (isset($val) && $val !== false) ? add_post_meta($this->postid, $metakey, $val, $unique) : false;
301
  }
302
 
303
  // Add Advanced Custom Field
309
  }
310
  }
311
 
312
+ if (!function_exists('remote_get_file')) :
313
  function remote_get_file($url = null, $file_dir = '') {
314
  if (!$url)
315
  return false;
337
  // remote get!
338
  $photo = $file_dir . basename($url);
339
  if ( !file_exists($photo) ) {
340
+ if (function_exists('wp_safe_remote_get')) {
341
+ $response = wp_safe_remote_get($url);
342
+ } else {
343
+ $response = wp_remote_get($url);
344
+ }
345
  if ( !is_wp_error($response) && $response["response"]["code"] === 200 ) {
346
  $photo_data = $response["body"];
347
  file_put_contents($photo, $photo_data);
353
  }
354
  return file_exists($photo) ? $photo : false;
355
  }
356
+ endif;
357
 
358
  endif;