Really Simple CSV Importer - Version 0.5.1

Version Description

  • Enhancement: Check whether both posts has same post type when updating.
Download this release

Release Info

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

Code changes from version 0.5 to 0.5.1

Files changed (2) hide show
  1. readme.txt +26 -22
  2. rs-csv-importer.php +54 -15
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
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
@@ -16,29 +16,32 @@ Alternative CSV Importer plugin. Simple and powerful.
16
  * Category support
17
  * Tag support
18
  * Custom field support
19
- * Adcanved Custom Fields support (beta)
20
  * Custom Taxonomy support
21
  * Custom Post Type support
22
 
23
- Contains CSV file sample in `/wp-content/plugins/really-simple-csv-importer/sample` directory.
24
 
25
  = Available column names and values: =
26
  * `ID` or `post_id`: (int) post id.
27
  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.
28
- * `post_author`: (login or ID) author
29
- * `post_date`: (string) publish date
30
- * `post_content`: (string) post content
31
- * `post_title`: (string) post title
32
- * `post_excerpt`: (string) post excerpt
33
- * `post_status`: (string) post status
34
- * `post_name`: (string) post slug
35
- * `post_parent`: (int) post parent id. Used for page or hierarchical post type.
36
  * `menu_order`: (int)
37
- * `post_type`: (string) post type
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. Taxonomy must already exist. Entries are names, not slugs
 
 
 
42
 
43
  = Advanced Custom Fields plugin integrate =
44
  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.
@@ -66,8 +69,8 @@ This filter is applied to post data.
66
 
67
  Parameters:
68
 
69
- * $post - (array)(required) post data
70
- * $is_update (bool) update existing post data, or insert new post data
71
 
72
  Example:
73
 
@@ -96,9 +99,9 @@ This filter is applied to post meta data.
96
 
97
  Parameters:
98
 
99
- * $meta - (array)(required) post meta data
100
- * $post - (array) post data
101
- * $is_update (bool)
102
 
103
  Example:
104
 
@@ -122,9 +125,9 @@ This filter is applied to post taxonomy data (categories and tags are not includ
122
 
123
  Parameters:
124
 
125
- * $tax - (array)(required) post taxonomy data
126
- * $post - (array) post data
127
- * $is_update (bool)
128
 
129
  Example:
130
 
@@ -150,9 +153,10 @@ add_filter( 'really_simple_csv_importer_save_tax', 'really_simple_csv_importer_s
150
 
151
  == Changelog ==
152
 
 
 
153
  = 0.5 =
154
  * New feature: Added filter hooks to customize import data
155
- * Bug fix
156
  = 0.4.2 =
157
  * Post title bug fix
158
  = 0.4.1 =
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
 
16
  * Category support
17
  * Tag support
18
  * Custom field support
19
+ * Advanced Custom Fields support (beta)
20
  * Custom Taxonomy support
21
  * Custom Post Type support
22
 
23
+ Contains CSV file samples in `/wp-content/plugins/really-simple-csv-importer/sample` directory.
24
 
25
  = Available column names and values: =
26
  * `ID` or `post_id`: (int) post id.
27
  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.
28
+ * `post_author`: (login or ID) The user name or user ID number of the author.
29
+ * `post_date`: (string) The time of publish date.
30
+ * `post_content`: (string) The full text of the post.
31
+ * `post_title`: (string) The title of the post.
32
+ * `post_excerpt`: (string) For all your post excerpt needs.
33
+ * `post_status`: ('draft' or 'publish' or 'pending' or 'future' or 'private' or custom registered status) The status of the post.
34
+ * `post_name`: (string) The slug of the post.
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.
69
 
70
  Parameters:
71
 
72
+ * `$post` - (array)(required) post data
73
+ * `$is_update` - (bool) update existing post data, or insert new post data
74
 
75
  Example:
76
 
99
 
100
  Parameters:
101
 
102
+ * `$meta` - (array)(required) post meta data
103
+ * `$post` - (array) post data
104
+ * `$is_update` - (bool)
105
 
106
  Example:
107
 
125
 
126
  Parameters:
127
 
128
+ * `$tax` - (array)(required) post taxonomy data
129
+ * `$post` - (array) post data
130
+ * `$is_update` - (bool)
131
 
132
  Example:
133
 
153
 
154
  == Changelog ==
155
 
156
+ = 0.5.1 =
157
+ * Enhancement: Check whether both posts has same post type when updating.
158
  = 0.5 =
159
  * New feature: Added filter hooks to customize import data
 
160
  = 0.4.2 =
161
  * Post title bug fix
162
  = 0.4.1 =
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.5
11
  */
12
 
13
  if ( !defined('WP_LOAD_IMPORTERS') )
@@ -91,12 +91,16 @@ class RS_CSV_Importer extends WP_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 array $terms
98
  * @param bool $is_update
99
- * More information: https://gist.github.com/4084471
100
  */
101
  function save_post($post,$meta,$terms,$is_update) {
102
  $ph = new wp_post_helper($post);
@@ -150,19 +154,32 @@ class RS_CSV_Importer extends WP_Importer {
150
  $h->parse_columns( $this, $data );
151
  $is_first = false;
152
  } else {
 
 
153
  $post = array();
154
  $is_update = false;
 
 
 
 
 
 
 
155
 
156
  // (int) post id
157
  $post_id = $h->get_data($this,$data,'ID');
158
  $post_id = ($post_id) ? $post_id : $h->get_data($this,$data,'post_id');
159
  if ($post_id) {
160
  $post_exist = get_post($post_id);
161
- if ( is_null( $post_exist ) ) {
162
  $post['import_id'] = $post_id;
163
  } else {
164
- $post['ID'] = $post_id;
165
- $is_update = true;
 
 
 
 
166
  }
167
  }
168
 
@@ -192,12 +209,6 @@ class RS_CSV_Importer extends WP_Importer {
192
  $post['post_date'] = date("Y-m-d H:i:s", strtotime($post_date));
193
  }
194
 
195
- // (string) post type
196
- $post_type = $h->get_data($this,$data,'post_type');
197
- if ($post_type) {
198
- $post['post_type'] = $post_type;
199
- }
200
-
201
  // (string) post status
202
  $post_status = $h->get_data($this,$data,'post_status');
203
  if ($post_status) {
@@ -255,6 +266,7 @@ class RS_CSV_Importer extends WP_Importer {
255
  $meta = array();
256
  $tax = array();
257
 
 
258
  foreach ($data as $key => $value) {
259
  if (!empty($value) && isset($this->column_keys[$key])) {
260
  // check if meta is custom taxonomy
@@ -273,16 +285,43 @@ class RS_CSV_Importer extends WP_Importer {
273
  }
274
  }
275
 
 
 
 
 
 
 
276
  $post = apply_filters( 'really_simple_csv_importer_save_post', $post, $is_update );
 
 
 
 
 
 
 
277
  $meta = apply_filters( 'really_simple_csv_importer_save_meta', $meta, $post, $is_update );
 
 
 
 
 
 
 
278
  $tax = apply_filters( 'really_simple_csv_importer_save_tax', $tax, $post, $is_update );
279
 
 
280
  $result = $this->save_post($post,$meta,$tax,$is_update);
281
  if (!$result) {
282
- echo '<li>'.sprintf(__('An error occurred during processing %s', 'rs-csv-importer'), esc_html($post_title)).'</li>';
283
- } else {
284
- echo '<li>'.esc_html($post_title).'</li>';
 
 
 
285
  }
 
 
 
286
  }
287
  }
288
 
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') )
91
  return $result;
92
  }
93
 
94
+ /**
95
+ * Insert post and postmeta using wp_post_helper.
96
+ *
97
+ * More information: https://gist.github.com/4084471
98
+ *
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);
154
  $h->parse_columns( $this, $data );
155
  $is_first = false;
156
  } else {
157
+ echo '<li>';
158
+
159
  $post = array();
160
  $is_update = false;
161
+ $error = new WP_Error();
162
+
163
+ // (string) post type
164
+ $post_type = $h->get_data($this,$data,'post_type');
165
+ if ($post_type) {
166
+ $post['post_type'] = $post_type;
167
+ }
168
 
169
  // (int) post id
170
  $post_id = $h->get_data($this,$data,'ID');
171
  $post_id = ($post_id) ? $post_id : $h->get_data($this,$data,'post_id');
172
  if ($post_id) {
173
  $post_exist = get_post($post_id);
174
+ if ( is_null( $post_exist ) ) { // if the post id is not exists
175
  $post['import_id'] = $post_id;
176
  } else {
177
+ if ( !$post_type || $post_exist->post_type == $post_type ) {
178
+ $post['ID'] = $post_id;
179
+ $is_update = true;
180
+ } else {
181
+ $error->add( 'post_type_check', sprintf(__('The post id %d is exists, but post types does not match.', 'rs-csv-importer'), $post_id) );
182
+ }
183
  }
184
  }
185
 
209
  $post['post_date'] = date("Y-m-d H:i:s", strtotime($post_date));
210
  }
211
 
 
 
 
 
 
 
212
  // (string) post status
213
  $post_status = $h->get_data($this,$data,'post_status');
214
  if ($post_status) {
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
285
  }
286
  }
287
 
288
+ /**
289
+ * Filter post data.
290
+ *
291
+ * @param array $post (required)
292
+ * @param bool $is_update
293
+ */
294
  $post = apply_filters( 'really_simple_csv_importer_save_post', $post, $is_update );
295
+ /**
296
+ * Filter meta data.
297
+ *
298
+ * @param array $meta (required)
299
+ * @param array $post
300
+ * @param bool $is_update
301
+ */
302
  $meta = apply_filters( 'really_simple_csv_importer_save_meta', $meta, $post, $is_update );
303
+ /**
304
+ * Filter taxonomy data.
305
+ *
306
+ * @param array $tax (required)
307
+ * @param array $post
308
+ * @param bool $is_update
309
+ */
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
319
+ foreach ($error->get_error_messages() as $message) {
320
+ echo esc_html($message).'<br>';
321
  }
322
+ echo esc_html(sprintf(__('Processing "%s" done.', 'rs-csv-importer'), $post_title));
323
+
324
+ echo '</li>';
325
  }
326
  }
327