Really Simple CSV Importer - Version 1.2

Version Description

  • Enhancement: Smart Custom Fields support
  • Check if the provided post status is already registered
Download this release

Release Info

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

Code changes from version 1.1 to 1.2

class-rscsv_import_post_helper.php CHANGED
@@ -8,6 +8,7 @@
8
  class RSCSV_Import_Post_Helper
9
  {
10
  const CFS_PREFIX = 'cfs_';
 
11
 
12
  /**
13
  * @var $post WP_Post object
@@ -141,12 +142,18 @@ class RSCSV_Import_Post_Helper
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) {
@@ -158,10 +165,11 @@ class RSCSV_Import_Post_Helper
158
  }
159
  }
160
  }
161
- if (!$is_acf && !$is_cfs) {
162
  $this->updateMeta($key, $value);
163
  }
164
  }
 
165
  }
166
 
167
  /**
@@ -203,7 +211,8 @@ class RSCSV_Import_Post_Helper
203
  /**
204
  * A wrapper of CFS()->save()
205
  *
206
- * @param (array) $data
 
207
  */
208
  protected function cfsSave($key, $value)
209
  {
@@ -221,6 +230,32 @@ class RSCSV_Import_Post_Helper
221
  }
222
  }
223
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
224
  /**
225
  * A wrapper of wp_set_post_tags
226
  *
8
  class RSCSV_Import_Post_Helper
9
  {
10
  const CFS_PREFIX = 'cfs_';
11
+ const SCF_PREFIX = 'scf_';
12
 
13
  /**
14
  * @var $post WP_Post object
142
  */
143
  public function setMeta($data)
144
  {
145
+ $scf_array = array();
146
  foreach ($data as $key => $value) {
147
  $is_cfs = 0;
148
+ $is_scf = 0;
149
  $is_acf = 0;
150
  if (strpos($key, self::CFS_PREFIX) === 0) {
151
  $this->cfsSave(substr($key, strlen(self::CFS_PREFIX)), $value);
152
  $is_cfs = 1;
153
+ } elseif(strpos($key, self::SCF_PREFIX) === 0) {
154
+ $scf_key = substr($key, strlen(self::SCF_PREFIX));
155
+ $scf_array[$scf_key][] = $value;
156
+ $is_scf = 1;
157
  } else {
158
  if (function_exists('get_field_object')) {
159
  if (strpos($key, 'field_') === 0) {
165
  }
166
  }
167
  }
168
+ if (!$is_acf && !$is_cfs && !$is_scf) {
169
  $this->updateMeta($key, $value);
170
  }
171
  }
172
+ $this->scfSave($scf_array);
173
  }
174
 
175
  /**
211
  /**
212
  * A wrapper of CFS()->save()
213
  *
214
+ * @param (string) $key
215
+ * @param (string/array) $value
216
  */
217
  protected function cfsSave($key, $value)
218
  {
230
  }
231
  }
232
 
233
+ /**
234
+ * A wrapper of Smart_Custom_Fields_Meta()->save()
235
+ *
236
+ * @param (array) $data
237
+ */
238
+ protected function scfSave($data)
239
+ {
240
+ $post = $this->getPost();
241
+ if ($post instanceof WP_Post) {
242
+ if (class_exists('Smart_Custom_Fields_Meta') && is_array($data)) {
243
+ $_data = array();
244
+ $_data['smart-custom-fields'] = $data;
245
+ $meta = new Smart_Custom_Fields_Meta($post);
246
+ $meta->save($_data);
247
+ } elseif(is_array($data)) {
248
+ foreach ($data as $key => $array) {
249
+ foreach ((array) $array as $value) {
250
+ $this->updateMeta($key, $value);
251
+ }
252
+ }
253
+ }
254
+ } else {
255
+ $this->addError('post_is_not_set', __('WP_Post object is not set.', 'really-simple-csv-importer'));
256
+ }
257
+ }
258
+
259
  /**
260
  * A wrapper of wp_set_post_tags
261
  *
readme.txt CHANGED
@@ -1,9 +1,9 @@
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.1
6
- Stable tag: 1.1
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
@@ -16,6 +16,7 @@ Alternative CSV Importer plugin. Simple and powerful, best for geeks.
16
  * Category support
17
  * Tag support
18
  * Custom field support
 
19
  * [Custom Field Suite](http://customfieldsuite.com/) support
20
  * [Advanced Custom Fields](http://www.advancedcustomfields.com/) support
21
  * Custom Taxonomy support
@@ -45,7 +46,8 @@ You can get example CSV files in `/wp-content/plugins/really-simple-csv-importer
45
  * `post_tags`: (string, comma separated) name of post tags
46
  * `tax_{taxonomy}`: (string, comma separated) Any field prefixed with `tax_` will be used as a custom taxonomy. Taxonomy must already exist. Entries are names or slugs of terms.
47
  * `{custom_field_key}`: (string) Any other column labels used as custom field
48
- * `cfs_{field_name}`: (string) If you would like to import data to custom fields set by Custom Field Suite, please add prefix `cfs_`
 
49
 
50
  Note: Empty cells in the csv file means "keep it", not "delete it".
51
  Note: To set the page template of a page, use custom field key of `_wp_page_template`.
@@ -86,7 +88,7 @@ Yes. Please use ID field to specify the new post ID.
86
 
87
  Yes. You can use column names same as wp_post table, but if the column name does not match, it creates a custom field (post meta) data. Importing custom taxonomy is a bit more complicated, "tax_{taxonomy}" means, "tax_" is prefix, and {taxonomy} is name of custom taxonomy (not labels).
88
 
89
- Here is a example.
90
 
91
  **csv file**
92
  "post_title","released","tax_actors"
@@ -105,7 +107,9 @@ Because PHP cannot read multibyte text cells in some cases.
105
 
106
  = Can I insert multiple values to CFS or ACF fields like Select or Checkbox? =
107
 
108
- Yes. Please use `really_simple_csv_importer_save_meta` filter to make array data.
 
 
109
 
110
  == How to debug import data ==
111
 
@@ -210,7 +214,7 @@ add_filter( 'really_simple_csv_importer_save_tax', 'really_simple_csv_importer_s
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
 
@@ -218,10 +222,13 @@ Example: [gist](https://gist.github.com/hissy/fe0aa2582b78394a3a82)
218
 
219
  This filter provides availability to completely replace the `RS_CSV_Importer#save_post` method.
220
 
221
- Example: [gist](https://gist.github.com/hissy/199ad9be855ec9be1e54)
222
 
223
  == Changelog ==
224
 
 
 
 
225
  = 1.1 =
226
  * Enhancement: Support localization
227
  * Bug fixes
1
  === Really Simple CSV Importer ===
2
  Contributors: hissy
3
+ Tags: importer, csv, acf, cfs, scf
4
  Requires at least: 3.6
5
+ Tested up to: 4.2.1
6
+ Stable tag: 1.2
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
+ * [Smart Custom Fields](https://wordpress.org/plugins/smart-custom-fields/) support
20
  * [Custom Field Suite](http://customfieldsuite.com/) support
21
  * [Advanced Custom Fields](http://www.advancedcustomfields.com/) support
22
  * Custom Taxonomy support
46
  * `post_tags`: (string, comma separated) name of post tags
47
  * `tax_{taxonomy}`: (string, comma separated) Any field prefixed with `tax_` will be used as a custom taxonomy. Taxonomy must already exist. Entries are names or slugs of terms.
48
  * `{custom_field_key}`: (string) Any other column labels used as custom field
49
+ * `cfs_{field_name}`: (string) If you would like to import data to custom fields set by Custom Field Suite, please add prefix `cfs_` to column header name.
50
+ * `scf_{field_name}`: (string) If you would like to import data to custom fields set by Smart Custom Fields, please add prefix `scf_` to column header name.
51
 
52
  Note: Empty cells in the csv file means "keep it", not "delete it".
53
  Note: To set the page template of a page, use custom field key of `_wp_page_template`.
88
 
89
  Yes. You can use column names same as wp_post table, but if the column name does not match, it creates a custom field (post meta) data. Importing custom taxonomy is a bit more complicated, "tax_{taxonomy}" means, "tax_" is prefix, and {taxonomy} is name of custom taxonomy (not labels).
90
 
91
+ Here is an example.
92
 
93
  **csv file**
94
  "post_title","released","tax_actors"
107
 
108
  = Can I insert multiple values to CFS or ACF fields like Select or Checkbox? =
109
 
110
+ Yes. Please create additional plugin and use `really_simple_csv_importer_save_meta` filter to make array data.
111
+
112
+ [Add-on development example](https://gist.github.com/hissy/d2041481a72510b7f394)
113
 
114
  == How to debug import data ==
115
 
214
 
215
  This action provides availability to run some tasks after importing.
216
 
217
+ Example: Download image from remote url to custom field (Download from [gist](https://gist.github.com/hissy/0973a6a9977129a6ebd0))
218
 
219
  == How to customize the importing process entirely ==
220
 
222
 
223
  This filter provides availability to completely replace the `RS_CSV_Importer#save_post` method.
224
 
225
+ Example: Update row based on a custom field ID/key match (Download from [gist](https://gist.github.com/hissy/199ad9be855ec9be1e54))
226
 
227
  == Changelog ==
228
 
229
+ = 1.2 =
230
+ * Enhancement: Smart Custom Fields support
231
+ * Check if the provided post status is already registered
232
  = 1.1 =
233
  * Enhancement: Support localization
234
  * Bug fixes
rs-csv-importer.php CHANGED
@@ -7,7 +7,7 @@ Author: Takuro Hishikawa
7
  Author URI: https://en.digitalcube.jp/
8
  Text Domain: really-simple-csv-importer
9
  License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10
- Version: 1.1
11
  */
12
 
13
  if ( !defined('WP_LOAD_IMPORTERS') )
@@ -151,6 +151,7 @@ class RS_CSV_Importer extends WP_Importer {
151
  }
152
 
153
  $is_first = true;
 
154
 
155
  echo '<ol>';
156
 
@@ -223,7 +224,9 @@ class RS_CSV_Importer extends WP_Importer {
223
  // (string) post status
224
  $post_status = $h->get_data($this,$data,'post_status');
225
  if ($post_status) {
226
- $post['post_status'] = $post_status;
 
 
227
  }
228
 
229
  // (string) post title
7
  Author URI: https://en.digitalcube.jp/
8
  Text Domain: really-simple-csv-importer
9
  License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10
+ Version: 1.2
11
  */
12
 
13
  if ( !defined('WP_LOAD_IMPORTERS') )
151
  }
152
 
153
  $is_first = true;
154
+ $post_statuses = get_post_stati();
155
 
156
  echo '<ol>';
157
 
224
  // (string) post status
225
  $post_status = $h->get_data($this,$data,'post_status');
226
  if ($post_status) {
227
+ if (in_array($post_status, $post_statuses)) {
228
+ $post['post_status'] = $post_status;
229
+ }
230
  }
231
 
232
  // (string) post title
sample/post_status_test.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ "post_title","post_status","post_type"
2
+ "Valid status","publish","post"
3
+ "Invalid status","foo","post"
sample/smart_custom_fields.csv ADDED
@@ -0,0 +1,2 @@
 
 
1
+ "post_title","post_type","post_status","scf_text","scf_textarea","scf_checkbox","scf_radio","scf_select","scf_file","scf_color","scf_date","scf_relation"
2
+ "SCF Test","post","publish","test","test","A","B","C",644,"#dd3333",2014-04-06,1001
sample/smart_custom_fields.ods ADDED
Binary file