Really Simple CSV Importer - Version 0.2

Version Description

  • New feature: Add post_id column. It enables to update post data.
  • Some bug fixes
Download this release

Release Info

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

Code changes from version 0.1.1 to 0.2

Files changed (4) hide show
  1. readme.txt +15 -9
  2. rs-csv-importer.php +38 -12
  3. sample/sample.csv +7 -4
  4. sample/sample.ods +0 -0
readme.txt CHANGED
@@ -2,12 +2,12 @@
2
  Contributors: hissy, wokamoto
3
  Tags: importer, csv
4
  Requires at least: 3.0
5
- Tested up to: 3.6
6
- Stable tag: 0.1.1
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
10
- Import posts, custom fields, taxonomies from csv file.
11
 
12
  == Description ==
13
 
@@ -18,16 +18,17 @@ Alternative CSV Importer plugin. Simple and powerful.
18
  * Custom field support
19
  * Custom post type support
20
 
21
- Contains CSV file sample in plugin_dir/rs-csv-importer/sample .
22
 
23
- = Available values: =
24
- * post_name: (string) post slug
25
  * post_author: (login or ID) author
26
  * post_date: (string) publish date
27
- * post_type: (string) post type
28
- * post_status: (string) post status
29
- * post_title: (string) post title
30
  * post_content: (string) post content
 
 
 
 
31
  * post_category: (string, comma divided) slug of post categories
32
  * post_tags: (string, comma divided) name of post tags
33
  * {custom_field}: any other column labels used as custom field
@@ -43,5 +44,10 @@ Add star and read future issues about rs-csv-importer on [GitHub](https://github
43
 
44
  == Changelog ==
45
 
 
 
 
 
 
46
  = 0.1 =
47
  * First Release (beta)
2
  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
 
10
+ Import posts, categories, tags, custom fields from simple csv file.
11
 
12
  == Description ==
13
 
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
28
+ * post_title: (string) post title
29
+ * post_status: (string) post status
30
+ * post_name: (string) post slug
31
+ * post_type: (string) post type
32
  * post_category: (string, comma divided) slug of post categories
33
  * post_tags: (string, comma divided) name of post tags
34
  * {custom_field}: any other column labels used as custom field
44
 
45
  == Changelog ==
46
 
47
+ = 0.2 =
48
+ * New feature: Add post_id column. It enables to update post data.
49
+ * Some bug fixes
50
+ = 0.1.1 =
51
+ * Bug fix
52
  = 0.1 =
53
  * First Release (beta)
rs-csv-importer.php CHANGED
@@ -1,12 +1,13 @@
1
  <?php
2
  /*
3
  Plugin Name: Really Simple CSV Importer
4
- Description: Import posts, custom fields, taxonomies from csv file.
 
5
  Author: Takuro Hishikawa, wokamoto
6
  Author URI: http://notnil-creative.com/
7
  Text Domain: rs-csv-importer
8
  License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
9
- Version: 0.1.1
10
  */
11
 
12
  if ( !defined('WP_LOAD_IMPORTERS') )
@@ -77,6 +78,10 @@ class RS_CSV_Importer extends WP_Importer {
77
  echo '<li>'.sprintf( __( 'You must use field delimiter as "%s"', 'rs-csv-importer'), self::DELIMITER ).'</li>';
78
  echo '<li>'.__( 'You must quote all text cells.', 'rs-csv-importer' ).'</li>';
79
  echo '</ol>';
 
 
 
 
80
  wp_import_upload_form( add_query_arg('step', 1) );
81
  }
82
 
@@ -119,14 +124,17 @@ class RS_CSV_Importer extends WP_Importer {
119
  * @param array $meta
120
  * More information: https://gist.github.com/4084471
121
  */
122
- function save_post($post,$meta) {
123
  $ph = new wp_post_helper($post);
124
 
125
  foreach ($meta as $key => $value) {
126
  $ph->add_meta($key,$value,true);
127
  }
128
 
129
- $ph->insert();
 
 
 
130
 
131
  unset($ph);
132
  }
@@ -152,6 +160,23 @@ class RS_CSV_Importer extends WP_Importer {
152
  $is_first = false;
153
  } else {
154
  $post = array();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
 
156
  // (string) post slug
157
  if (isset($this->columns['post_name']) &&
@@ -166,13 +191,14 @@ class RS_CSV_Importer extends WP_Importer {
166
  isset($data[$this->columns['post_author']]) &&
167
  ! empty($data[$this->columns['post_author']])) {
168
  $post_author = $data[$this->columns['post_author']];
169
- if (is_int($post_author)) {
170
- $post['post_author'] = $post_author;
171
  } else {
172
- $post_author = get_user_by('login',$post_author);
173
- if (is_object($post_author)) {
174
- $post['post_author'] = $data[$post_author->ID];
175
- }
 
176
  }
177
  unset($data[$this->columns['post_author']]);
178
  unset($post_author);
@@ -252,7 +278,7 @@ class RS_CSV_Importer extends WP_Importer {
252
  }
253
  }
254
 
255
- $this->save_post($post,$meta);
256
 
257
  echo '<li>'.esc_html($post['post_title']).'</li>';
258
  }
@@ -297,6 +323,6 @@ class RS_CSV_Importer extends WP_Importer {
297
  // setup importer
298
  $rs_csv_importer = new RS_CSV_Importer();
299
 
300
- register_importer('csv', __('CSV', 'rs-csv-importer'), __('Import posts, custom fields, taxonomies from csv file.', 'rs-csv-importer'), array ($rs_csv_importer, 'dispatch'));
301
 
302
  } // class_exists( 'WP_Importer' )
1
  <?php
2
  /*
3
  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.2
11
  */
12
 
13
  if ( !defined('WP_LOAD_IMPORTERS') )
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' );
82
+ echo ' <a href="'.plugin_dir_url( __FILE__ ).'sample/sample.csv">'.__( 'csv', 'rs-csv-importer' ).'</a>,';
83
+ echo ' <a href="'.plugin_dir_url( __FILE__ ).'sample/sample.ods">'.__( 'ods (OpenDocument Spreadsheet file format)', 'rs-csv-importer' ).'</a>';
84
+ echo '</p>';
85
  wp_import_upload_form( add_query_arg('step', 1) );
86
  }
87
 
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
  }
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']) &&
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 {
197
+ $user = get_user_by('login',$post_author);
198
+ }
199
+ if (isset($user) && is_object($user)) {
200
+ $post['post_author'] = $user->ID;
201
+ unset($user);
202
  }
203
  unset($data[$this->columns['post_author']]);
204
  unset($post_author);
278
  }
279
  }
280
 
281
+ $this->save_post($post,$meta,$is_update);
282
 
283
  echo '<li>'.esc_html($post['post_title']).'</li>';
284
  }
323
  // setup importer
324
  $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' )
sample/sample.csv CHANGED
@@ -1,4 +1,7 @@
1
- "post_name","post_author","post_date","post_type","post_status","post_title","post_content","post_category","post_tags","custom_field","_is_imported"
2
- "Import-test","admin",2013/9/13 0:00,,"publish","CSV Import Test","This is a post for csv import.
3
- Hello dolly.","fruits","apple,banana","this is custom field value.",1
4
- ,"editor",,"post",,"editor post",,"undefined",,,
 
 
 
1
+ "post_id","post_name","post_author","post_date","post_type","post_status","post_title","post_content","post_category","post_tags","custom_field"
2
+ ,"Import-test","admin",2013/9/13 0:00,"post","publish","CSV Import Test","This is a post for csv import.
3
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
4
+ Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.","fruits","apple,banana","this is custom field value."
5
+ ,,"editor",,,,"define author test",,,,
6
+ ,,2,,,,"define author id test",,,,
7
+ 1,"Hello-world-updated",,,,,"Hello world! Updated!","Welcome to WordPress. This is your first post. Updated by RS CSV Importer!",,,
sample/sample.ods CHANGED
Binary file