Really Simple CSV Importer - Version 0.1.1

Version Description

Download this release

Release Info

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

Code changes from version 0.1 to 0.1.1

Files changed (2) hide show
  1. readme.txt +9 -5
  2. rs-csv-importer.php +45 -20
readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: hissy, wokamoto
3
  Tags: importer, csv
4
  Requires at least: 3.0
5
  Tested up to: 3.6
6
- Stable tag: 0.1
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
@@ -20,7 +20,7 @@ Alternative CSV Importer plugin. Simple and powerful.
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
@@ -32,12 +32,16 @@ Available values:
32
  * post_tags: (string, comma divided) name of post tags
33
  * {custom_field}: any other column labels used as custom field
34
 
 
 
35
  == Installation ==
36
 
37
- 1. Upload All files to the `/wp-content/plugins/` directory
38
- 2. Activate the plugin through the 'Plugins' menu in WordPress
 
 
39
 
40
  == Changelog ==
41
 
42
  = 0.1 =
43
- * First Release (beta)
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
 
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
32
  * post_tags: (string, comma divided) name of post tags
33
  * {custom_field}: any other column labels used as custom field
34
 
35
+ Add star and read future issues about rs-csv-importer on [GitHub](https://github.com/hissy/rs-csv-importer)!
36
+
37
  == Installation ==
38
 
39
+ 1. Upload All files to the `/wp-content/plugins/` directory.
40
+ 2. Activate the plugin through the 'Plugins' menu in WordPress.
41
+ 3. Go to the Import page under Tools menu.
42
+ 4. Click CSV link, read the notification, then just upload and import.
43
 
44
  == Changelog ==
45
 
46
  = 0.1 =
47
+ * First Release (beta)
rs-csv-importer.php CHANGED
@@ -6,7 +6,7 @@ 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
10
  */
11
 
12
  if ( !defined('WP_LOAD_IMPORTERS') )
@@ -71,7 +71,6 @@ class RS_CSV_Importer extends WP_Importer {
71
 
72
  // Step 1
73
  function greet() {
74
- $this->header();
75
  echo '<p>'.__( 'Choose a CSV (.csv) file to upload, then click Upload file and import.', 'rs-csv-importer' ).'</p>';
76
  echo '<ol>';
77
  echo '<li>'.__( 'Select UTF-8 as charset.', 'rs-csv-importer' ).'</li>';
@@ -79,27 +78,28 @@ class RS_CSV_Importer extends WP_Importer {
79
  echo '<li>'.__( 'You must quote all text cells.', 'rs-csv-importer' ).'</li>';
80
  echo '</ol>';
81
  wp_import_upload_form( add_query_arg('step', 1) );
82
- $this->footer();
83
  }
84
 
85
  // Step 2
86
  function import() {
87
- $this->header();
88
-
89
  $file = wp_import_handle_upload();
90
 
91
  if ( isset( $file['error'] ) ) {
92
- echo '<p>' . esc_html( $file['error'] ) . '</p>';
 
 
 
 
 
 
93
  return false;
94
  }
95
-
96
  $this->id = (int) $file['id'];
97
  $this->file = get_attached_file($this->id);
98
  $result = $this->process_posts();
99
  if ( is_wp_error( $result ) )
100
  return $result;
101
-
102
- $this->footer();
103
  }
104
 
105
  /** Parsing header row, setup the columns definition.
@@ -136,8 +136,11 @@ class RS_CSV_Importer extends WP_Importer {
136
  global $wpdb;
137
 
138
  $handle = $this->fopen($this->file, 'r');
139
- if ( $handle == null )
 
 
140
  return false;
 
141
 
142
  $is_first = true;
143
 
@@ -151,13 +154,17 @@ class RS_CSV_Importer extends WP_Importer {
151
  $post = array();
152
 
153
  // (string) post slug
154
- if (isset($data[$this->columns['post_name']]) && !empty($data[$this->columns['post_name']])) {
 
 
155
  $post['post_name'] = $data[$this->columns['post_name']];
156
  unset($data[$this->columns['post_name']]);
157
  }
158
 
159
  // (login or ID) post_author
160
- if (isset($data[$this->columns['post_author']]) && !empty($data[$this->columns['post_author']])) {
 
 
161
  $post_author = $data[$this->columns['post_author']];
162
  if (is_int($post_author)) {
163
  $post['post_author'] = $post_author;
@@ -172,7 +179,9 @@ class RS_CSV_Importer extends WP_Importer {
172
  }
173
 
174
  // (string) publish date
175
- if (isset($data[$this->columns['post_date']]) && !empty($data[$this->columns['post_date']])) {
 
 
176
  $post_date = $data[$this->columns['post_date']];
177
  $post_date = date("Y-m-d H:i:s", strtotime($post_date));
178
  $post['post_date'] = $post_date;
@@ -181,32 +190,42 @@ class RS_CSV_Importer extends WP_Importer {
181
  }
182
 
183
  // (string) post type
184
- if (isset($data[$this->columns['post_type']]) && !empty($data[$this->columns['post_type']])) {
 
 
185
  $post['post_type'] = $data[$this->columns['post_type']];
186
  unset($data[$this->columns['post_type']]);
187
  }
188
 
189
  // (string) post status
190
- if (isset($data[$this->columns['post_status']]) && !empty($data[$this->columns['post_status']])) {
 
 
191
  $post['post_status'] = $data[$this->columns['post_status']];
192
  unset($data[$this->columns['post_status']]);
193
  }
194
 
195
  // (string) post title
196
  $post['post_title'] = '';
197
- if (isset($data[$this->columns['post_title']]) && !empty($data[$this->columns['post_title']])) {
 
 
198
  $post['post_title'] = $data[$this->columns['post_title']];
199
  unset($data[$this->columns['post_title']]);
200
  }
201
 
202
  // (string) post content
203
- if (isset($data[$this->columns['post_content']]) && !empty($data[$this->columns['post_content']])) {
 
 
204
  $post['post_content'] = $data[$this->columns['post_content']];
205
  unset($data[$this->columns['post_content']]);
206
  }
207
 
208
  // (string, comma divided) slug of post categories
209
- if (isset($data[$this->columns['post_category']]) && !empty($data[$this->columns['post_category']])) {
 
 
210
  $categories = preg_split("/[\s,]+/", $data[$this->columns['post_category']]);
211
  if ($categories) {
212
  $post['post_category'] = wp_create_categories($categories);
@@ -216,7 +235,9 @@ class RS_CSV_Importer extends WP_Importer {
216
  }
217
 
218
  // (string, comma divided) name of post tags
219
- if (isset($data[$this->columns['post_tags']]) && !empty($data[$this->columns['post_tags']])) {
 
 
220
  $tags = preg_split("/[\s,]+/", $data[$this->columns['post_tags']]);
221
  if ($tags) {
222
  $post['post_tags'] = $tags;
@@ -226,7 +247,7 @@ class RS_CSV_Importer extends WP_Importer {
226
 
227
  $meta = array();
228
  foreach ($data as $key => $value) {
229
- if (!empty($value)) {
230
  $meta[$this->column_raw[$key]] = $value;
231
  }
232
  }
@@ -248,6 +269,8 @@ class RS_CSV_Importer extends WP_Importer {
248
 
249
  // dispatcher
250
  function dispatch() {
 
 
251
  if (empty ($_GET['step']))
252
  $step = 0;
253
  else
@@ -265,6 +288,8 @@ class RS_CSV_Importer extends WP_Importer {
265
  echo $result->get_error_message();
266
  break;
267
  }
 
 
268
  }
269
 
270
  }
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') )
71
 
72
  // Step 1
73
  function greet() {
 
74
  echo '<p>'.__( 'Choose a CSV (.csv) file to upload, then click Upload file and import.', 'rs-csv-importer' ).'</p>';
75
  echo '<ol>';
76
  echo '<li>'.__( 'Select UTF-8 as charset.', 'rs-csv-importer' ).'</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
 
83
  // Step 2
84
  function import() {
 
 
85
  $file = wp_import_handle_upload();
86
 
87
  if ( isset( $file['error'] ) ) {
88
+ echo '<p><strong>' . __( 'Sorry, there has been an error.', 'rs-csv-importer' ) . '</strong><br />';
89
+ echo esc_html( $file['error'] ) . '</p>';
90
+ return false;
91
+ } else if ( ! file_exists( $file['file'] ) ) {
92
+ echo '<p><strong>' . __( 'Sorry, there has been an error.', 'rs-csv-importer' ) . '</strong><br />';
93
+ printf( __( 'The export file could not be found at <code>%s</code>. It is likely that this was caused by a permissions problem.', 'rs-csv-importer' ), esc_html( $file['file'] ) );
94
+ echo '</p>';
95
  return false;
96
  }
97
+
98
  $this->id = (int) $file['id'];
99
  $this->file = get_attached_file($this->id);
100
  $result = $this->process_posts();
101
  if ( is_wp_error( $result ) )
102
  return $result;
 
 
103
  }
104
 
105
  /** Parsing header row, setup the columns definition.
136
  global $wpdb;
137
 
138
  $handle = $this->fopen($this->file, 'r');
139
+ if ( $handle == false ) {
140
+ echo '<p><strong>'.__( 'Failed to open file.', 'rs-csv-importer' ).'</strong></p>';
141
+ wp_import_cleanup($this->id);
142
  return false;
143
+ }
144
 
145
  $is_first = true;
146
 
154
  $post = array();
155
 
156
  // (string) post slug
157
+ if (isset($this->columns['post_name']) &&
158
+ isset($data[$this->columns['post_name']]) &&
159
+ ! empty($data[$this->columns['post_name']])) {
160
  $post['post_name'] = $data[$this->columns['post_name']];
161
  unset($data[$this->columns['post_name']]);
162
  }
163
 
164
  // (login or ID) post_author
165
+ if (isset($this->columns['post_author']) &&
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;
179
  }
180
 
181
  // (string) publish date
182
+ if (isset($this->columns['post_date']) &&
183
+ isset($data[$this->columns['post_date']]) &&
184
+ ! empty($data[$this->columns['post_date']])) {
185
  $post_date = $data[$this->columns['post_date']];
186
  $post_date = date("Y-m-d H:i:s", strtotime($post_date));
187
  $post['post_date'] = $post_date;
190
  }
191
 
192
  // (string) post type
193
+ if (isset($this->columns['post_type']) &&
194
+ isset($data[$this->columns['post_type']]) &&
195
+ ! empty($data[$this->columns['post_type']])) {
196
  $post['post_type'] = $data[$this->columns['post_type']];
197
  unset($data[$this->columns['post_type']]);
198
  }
199
 
200
  // (string) post status
201
+ if (isset($this->columns['post_status']) &&
202
+ isset($data[$this->columns['post_status']]) &&
203
+ ! empty($data[$this->columns['post_status']])) {
204
  $post['post_status'] = $data[$this->columns['post_status']];
205
  unset($data[$this->columns['post_status']]);
206
  }
207
 
208
  // (string) post title
209
  $post['post_title'] = '';
210
+ if (isset($this->columns['post_title']) &&
211
+ isset($data[$this->columns['post_title']]) &&
212
+ ! empty($data[$this->columns['post_title']])) {
213
  $post['post_title'] = $data[$this->columns['post_title']];
214
  unset($data[$this->columns['post_title']]);
215
  }
216
 
217
  // (string) post content
218
+ if (isset($this->columns['post_content']) &&
219
+ isset($data[$this->columns['post_content']]) &&
220
+ ! empty($data[$this->columns['post_content']])) {
221
  $post['post_content'] = $data[$this->columns['post_content']];
222
  unset($data[$this->columns['post_content']]);
223
  }
224
 
225
  // (string, comma divided) slug of post categories
226
+ if (isset($this->columns['post_category']) &&
227
+ isset($data[$this->columns['post_category']]) &&
228
+ ! empty($data[$this->columns['post_category']])) {
229
  $categories = preg_split("/[\s,]+/", $data[$this->columns['post_category']]);
230
  if ($categories) {
231
  $post['post_category'] = wp_create_categories($categories);
235
  }
236
 
237
  // (string, comma divided) name of post tags
238
+ if (isset($this->columns['post_tags']) &&
239
+ isset($data[$this->columns['post_tags']]) &&
240
+ ! empty($data[$this->columns['post_tags']])) {
241
  $tags = preg_split("/[\s,]+/", $data[$this->columns['post_tags']]);
242
  if ($tags) {
243
  $post['post_tags'] = $tags;
247
 
248
  $meta = array();
249
  foreach ($data as $key => $value) {
250
+ if (!empty($value) && isset($this->column_raw[$key])) {
251
  $meta[$this->column_raw[$key]] = $value;
252
  }
253
  }
269
 
270
  // dispatcher
271
  function dispatch() {
272
+ $this->header();
273
+
274
  if (empty ($_GET['step']))
275
  $step = 0;
276
  else
288
  echo $result->get_error_message();
289
  break;
290
  }
291
+
292
+ $this->footer();
293
  }
294
 
295
  }