CSV Importer - Version 0.3.7

Version Description

  • Make hierarchical custom taxonomy line splitting more robust
  • Fix deprecation warnings
Download this release

Release Info

Developer soflyy
Plugin Icon wp plugin CSV Importer
Version 0.3.7
Comparing to
See all releases

Code changes from version 0.3.6 to 0.3.7

Files changed (2) hide show
  1. csv_importer.php +118 -45
  2. readme.txt +13 -5
csv_importer.php CHANGED
@@ -2,7 +2,7 @@
2
  /*
3
  Plugin Name: CSV Importer
4
  Description: Import data as posts from a CSV file. <em>You can reach the author at <a href="mailto:d.v.kobozev@gmail.com">d.v.kobozev@gmail.com</a></em>.
5
- Version: 0.3.6
6
  Author: Denis Kobozev
7
  */
8
 
@@ -37,22 +37,29 @@ Author: Denis Kobozev
37
 
38
  class CSVImporterPlugin {
39
  var $defaults = array(
40
- 'csv_post_title' => null,
41
- 'csv_post_post' => null,
42
- 'csv_post_type' => null,
43
- 'csv_post_excerpt' => null,
44
- 'csv_post_date' => null,
45
- 'csv_post_tags' => null,
46
  'csv_post_categories' => null,
47
- 'csv_post_author' => null,
48
- 'csv_post_slug' => null,
49
- 'csv_post_parent' => 0,
50
  );
51
 
52
  var $log = array();
53
 
54
- // determine value of option $name from database, $default value or $params,
55
- // save it to the db if needed and return it
 
 
 
 
 
 
 
56
  function process_option($name, $default, $params) {
57
  if (array_key_exists($name, $params)) {
58
  $value = stripslashes($params[$name]);
@@ -85,7 +92,11 @@ class CSVImporterPlugin {
85
  return $value;
86
  }
87
 
88
- // Plugin's interface
 
 
 
 
89
  function form() {
90
  $opt_draft = $this->process_option('csv_importer_import_as_draft',
91
  'publish', $_POST);
@@ -162,7 +173,12 @@ class CSVImporterPlugin {
162
  }
163
  }
164
 
165
- // Handle POST submission
 
 
 
 
 
166
  function post($options) {
167
  if (empty($_FILES['csv_import']['tmp_name'])) {
168
  $this->log['error'][] = 'No file uploaded, aborting.';
@@ -234,16 +250,16 @@ class CSVImporterPlugin {
234
  }
235
 
236
  $new_post = array(
237
- 'post_title' => convert_chars($data['csv_post_title']),
238
  'post_content' => wpautop(convert_chars($data['csv_post_post'])),
239
- 'post_status' => $opt_draft,
240
- 'post_type' => $type,
241
- 'post_date' => $this->parse_date($data['csv_post_date']),
242
  'post_excerpt' => convert_chars($data['csv_post_excerpt']),
243
- 'post_name' => $data['csv_post_slug'],
244
- 'post_author' => $this->get_auth_id($data['csv_post_author']),
245
- 'tax_input' => $this->get_taxonomies($data),
246
- 'post_parent' => $data['csv_post_parent'],
247
  );
248
 
249
  // pages don't have tags or categories
@@ -273,10 +289,9 @@ class CSVImporterPlugin {
273
  /**
274
  * Return an array of category ids for a post.
275
  *
276
- * @param string $data csv_post_categories cell contents
277
  * @param integer $common_parent_id common parent id for all categories
278
- *
279
- * @return array() category ids
280
  */
281
  function create_or_get_categories($data, $common_parent_id) {
282
  $ids = array(
@@ -308,7 +323,7 @@ class CSVImporterPlugin {
308
  }
309
  foreach ($categories as $category) {
310
  if ($category) {
311
- $term = is_term($category, 'category', $parent_id);
312
  if ($term) {
313
  $term_id = $term['term_id'];
314
  } else {
@@ -327,20 +342,25 @@ class CSVImporterPlugin {
327
  return $ids;
328
  }
329
 
330
- // Parse taxonomy data from the file
331
- //
332
- // array(
333
- // // hierarchical taxonomy name => ID array
334
- // 'my taxonomy 1' => array(1, 2, 3, ...),
335
- // // non-hierarchical taxonomy name => term names string
336
- // 'my taxonomy 2' => array('term1', 'term2', ...),
337
- // )
 
 
 
 
 
338
  function get_taxonomies($data) {
339
  $taxonomies = array();
340
  foreach ($data as $k => $v) {
341
  if (preg_match('/^csv_ctax_(.*)$/', $k, $matches)) {
342
  $t_name = $matches[1];
343
- if (is_taxonomy($t_name)) {
344
  $taxonomies[$t_name] = $this->create_terms($t_name,
345
  $data[$k]);
346
  } else {
@@ -351,9 +371,15 @@ class CSVImporterPlugin {
351
  return $taxonomies;
352
  }
353
 
354
- // Return an array of term IDs for hierarchical taxonomies or the original
355
- // string from CSV for non-hierarchical taxonomies. The original string
356
- // should have the same format as csv_post_tags.
 
 
 
 
 
 
357
  function create_terms($taxonomy, $field) {
358
  if (is_taxonomy_hierarchical($taxonomy)) {
359
  $term_ids = array();
@@ -361,7 +387,7 @@ class CSVImporterPlugin {
361
  list($parent, $child) = $row;
362
  $parent_ok = true;
363
  if ($parent) {
364
- $parent_info = is_term($parent, $taxonomy);
365
  if (!$parent_info) {
366
  // create parent
367
  $parent_info = wp_insert_term($parent, $taxonomy);
@@ -377,7 +403,7 @@ class CSVImporterPlugin {
377
  }
378
 
379
  if ($parent_ok) {
380
- $child_info = is_term($child, $taxonomy, $parent_id);
381
  if (!$child_info) {
382
  // create child
383
  $child_info = wp_insert_term($child, $taxonomy,
@@ -394,11 +420,38 @@ class CSVImporterPlugin {
394
  }
395
  }
396
 
397
- // hierarchical taxonomy fields are tiny CSV files in their own right
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
398
  function _parse_tax($field) {
399
  $data = array();
400
  if (function_exists('str_getcsv')) { // PHP 5 >= 5.3.0
401
- $lines = explode("\n", $field);
402
 
403
  foreach ($lines as $line) {
404
  $data[] = str_getcsv($line, ',', '"');
@@ -419,6 +472,15 @@ class CSVImporterPlugin {
419
  return $data;
420
  }
421
 
 
 
 
 
 
 
 
 
 
422
  function add_comments($post_id, $data) {
423
  // First get a list of the comments for this post
424
  $comments = array();
@@ -491,7 +553,12 @@ class CSVImporterPlugin {
491
  return ($author_data) ? $author_data->ID : 0;
492
  }
493
 
494
- // Convert date in CSV file to 1999-12-31 23:52:00 format
 
 
 
 
 
495
  function parse_date($data) {
496
  $timestamp = strtotime($data);
497
  if (false === $timestamp) {
@@ -501,7 +568,12 @@ class CSVImporterPlugin {
501
  }
502
  }
503
 
504
- // delete BOM from UTF-8 file
 
 
 
 
 
505
  function stripBOM($fname) {
506
  $res = fopen($fname, 'rb');
507
  if (false !== $res) {
@@ -532,7 +604,8 @@ class CSVImporterPlugin {
532
  function csv_admin_menu() {
533
  require_once ABSPATH . '/wp-admin/admin.php';
534
  $plugin = new CSVImporterPlugin;
535
- add_management_page('edit.php', 'CSV Importer', 9, __FILE__, array($plugin, 'form'));
 
536
  }
537
 
538
  add_action('admin_menu', 'csv_admin_menu');
2
  /*
3
  Plugin Name: CSV Importer
4
  Description: Import data as posts from a CSV file. <em>You can reach the author at <a href="mailto:d.v.kobozev@gmail.com">d.v.kobozev@gmail.com</a></em>.
5
+ Version: 0.3.7
6
  Author: Denis Kobozev
7
  */
8
 
37
 
38
  class CSVImporterPlugin {
39
  var $defaults = array(
40
+ 'csv_post_title' => null,
41
+ 'csv_post_post' => null,
42
+ 'csv_post_type' => null,
43
+ 'csv_post_excerpt' => null,
44
+ 'csv_post_date' => null,
45
+ 'csv_post_tags' => null,
46
  'csv_post_categories' => null,
47
+ 'csv_post_author' => null,
48
+ 'csv_post_slug' => null,
49
+ 'csv_post_parent' => 0,
50
  );
51
 
52
  var $log = array();
53
 
54
+ /**
55
+ * Determine value of option $name from database, $default value or $params,
56
+ * save it to the db if needed and return it.
57
+ *
58
+ * @param string $name
59
+ * @param mixed $default
60
+ * @param array $params
61
+ * @return string
62
+ */
63
  function process_option($name, $default, $params) {
64
  if (array_key_exists($name, $params)) {
65
  $value = stripslashes($params[$name]);
92
  return $value;
93
  }
94
 
95
+ /**
96
+ * Plugin's interface
97
+ *
98
+ * @return void
99
+ */
100
  function form() {
101
  $opt_draft = $this->process_option('csv_importer_import_as_draft',
102
  'publish', $_POST);
173
  }
174
  }
175
 
176
+ /**
177
+ * Handle POST submission
178
+ *
179
+ * @param array $options
180
+ * @return void
181
+ */
182
  function post($options) {
183
  if (empty($_FILES['csv_import']['tmp_name'])) {
184
  $this->log['error'][] = 'No file uploaded, aborting.';
250
  }
251
 
252
  $new_post = array(
253
+ 'post_title' => convert_chars($data['csv_post_title']),
254
  'post_content' => wpautop(convert_chars($data['csv_post_post'])),
255
+ 'post_status' => $opt_draft,
256
+ 'post_type' => $type,
257
+ 'post_date' => $this->parse_date($data['csv_post_date']),
258
  'post_excerpt' => convert_chars($data['csv_post_excerpt']),
259
+ 'post_name' => $data['csv_post_slug'],
260
+ 'post_author' => $this->get_auth_id($data['csv_post_author']),
261
+ 'tax_input' => $this->get_taxonomies($data),
262
+ 'post_parent' => $data['csv_post_parent'],
263
  );
264
 
265
  // pages don't have tags or categories
289
  /**
290
  * Return an array of category ids for a post.
291
  *
292
+ * @param string $data csv_post_categories cell contents
293
  * @param integer $common_parent_id common parent id for all categories
294
+ * @return array category ids
 
295
  */
296
  function create_or_get_categories($data, $common_parent_id) {
297
  $ids = array(
323
  }
324
  foreach ($categories as $category) {
325
  if ($category) {
326
+ $term = $this->term_exists($category, 'category', $parent_id);
327
  if ($term) {
328
  $term_id = $term['term_id'];
329
  } else {
342
  return $ids;
343
  }
344
 
345
+ /**
346
+ * Parse taxonomy data from the file
347
+ *
348
+ * array(
349
+ * // hierarchical taxonomy name => ID array
350
+ * 'my taxonomy 1' => array(1, 2, 3, ...),
351
+ * // non-hierarchical taxonomy name => term names string
352
+ * 'my taxonomy 2' => array('term1', 'term2', ...),
353
+ * )
354
+ *
355
+ * @param array $data
356
+ * @return array
357
+ */
358
  function get_taxonomies($data) {
359
  $taxonomies = array();
360
  foreach ($data as $k => $v) {
361
  if (preg_match('/^csv_ctax_(.*)$/', $k, $matches)) {
362
  $t_name = $matches[1];
363
+ if ($this->taxonomy_exists($t_name)) {
364
  $taxonomies[$t_name] = $this->create_terms($t_name,
365
  $data[$k]);
366
  } else {
371
  return $taxonomies;
372
  }
373
 
374
+ /**
375
+ * Return an array of term IDs for hierarchical taxonomies or the original
376
+ * string from CSV for non-hierarchical taxonomies. The original string
377
+ * should have the same format as csv_post_tags.
378
+ *
379
+ * @param string $taxonomy
380
+ * @param string $field
381
+ * @return mixed
382
+ */
383
  function create_terms($taxonomy, $field) {
384
  if (is_taxonomy_hierarchical($taxonomy)) {
385
  $term_ids = array();
387
  list($parent, $child) = $row;
388
  $parent_ok = true;
389
  if ($parent) {
390
+ $parent_info = $this->term_exists($parent, $taxonomy);
391
  if (!$parent_info) {
392
  // create parent
393
  $parent_info = wp_insert_term($parent, $taxonomy);
403
  }
404
 
405
  if ($parent_ok) {
406
+ $child_info = $this->term_exists($child, $taxonomy, $parent_id);
407
  if (!$child_info) {
408
  // create child
409
  $child_info = wp_insert_term($child, $taxonomy,
420
  }
421
  }
422
 
423
+ /**
424
+ * Compatibility wrapper for WordPress term lookup.
425
+ */
426
+ function term_exists($term, $taxonomy = '', $parent = 0) {
427
+ if (function_exists('term_exists')) { // 3.0 or later
428
+ return term_exists($term, $taxonomy, $parent);
429
+ } else {
430
+ return is_term($term, $taxonomy, $parent);
431
+ }
432
+ }
433
+
434
+ /**
435
+ * Compatibility wrapper for WordPress taxonomy lookup.
436
+ */
437
+ function taxonomy_exists($taxonomy) {
438
+ if (function_exists('taxonomy_exists')) { // 3.0 or later
439
+ return taxonomy_exists($taxonomy);
440
+ } else {
441
+ return is_taxonomy($taxonomy);
442
+ }
443
+ }
444
+
445
+ /**
446
+ * Hierarchical taxonomy fields are tiny CSV files in their own right.
447
+ *
448
+ * @param string $field
449
+ * @return array
450
+ */
451
  function _parse_tax($field) {
452
  $data = array();
453
  if (function_exists('str_getcsv')) { // PHP 5 >= 5.3.0
454
+ $lines = $this->split_lines($field);
455
 
456
  foreach ($lines as $line) {
457
  $data[] = str_getcsv($line, ',', '"');
472
  return $data;
473
  }
474
 
475
+ /**
476
+ * Try to split lines of text correctly regardless of the platform the text
477
+ * is coming from.
478
+ */
479
+ function split_lines($text) {
480
+ $lines = preg_split("/(\r\n|\n|\r)/", $text);
481
+ return $lines;
482
+ }
483
+
484
  function add_comments($post_id, $data) {
485
  // First get a list of the comments for this post
486
  $comments = array();
553
  return ($author_data) ? $author_data->ID : 0;
554
  }
555
 
556
+ /**
557
+ * Convert date in CSV file to 1999-12-31 23:52:00 format
558
+ *
559
+ * @param string $data
560
+ * @return string
561
+ */
562
  function parse_date($data) {
563
  $timestamp = strtotime($data);
564
  if (false === $timestamp) {
568
  }
569
  }
570
 
571
+ /**
572
+ * Delete BOM from UTF-8 file.
573
+ *
574
+ * @param string $fname
575
+ * @return void
576
+ */
577
  function stripBOM($fname) {
578
  $res = fopen($fname, 'rb');
579
  if (false !== $res) {
604
  function csv_admin_menu() {
605
  require_once ABSPATH . '/wp-admin/admin.php';
606
  $plugin = new CSVImporterPlugin;
607
+ add_management_page('edit.php', 'CSV Importer', 'manage_options', __FILE__,
608
+ array($plugin, 'form'));
609
  }
610
 
611
  add_action('admin_menu', 'csv_admin_menu');
readme.txt CHANGED
@@ -1,10 +1,9 @@
1
  === CSV Importer ===
2
  Contributors: dvkob
3
- Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=4YJEU5U2Y4LTS&lc=US&item_name=Support%20CSV%20Importer%20development&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted
4
  Tags: csv, import, batch, spreadsheet, excel
5
  Requires at least: 2.0.2
6
- Tested up to: 3.0.4
7
- Stable tag: trunk
8
 
9
  Import posts from CSV files into WordPress.
10
 
@@ -25,12 +24,12 @@ a CSV file and the plugin will take care of the rest.
25
  write your posts)
26
  * Columns in the CSV file can be in any order, provided that they have correct
27
  headings
28
- * Multilanguage support
29
 
30
 
31
  == Screenshots ==
32
 
33
- 1. Plugin's interface
34
 
35
 
36
  == Installation ==
@@ -240,6 +239,7 @@ Contributors:
240
  * Edir Pedro (root category option and tableless HTML markup)
241
  * Frank Loeffler (comments support)
242
  * Micah Gates (subcategory syntax)
 
243
 
244
  [3]: http://code.google.com/p/php-csv-parser/
245
  [4]: http://www.jayblogger.com/the-birth-of-my-first-plugin-import-csv/
@@ -247,6 +247,10 @@ Contributors:
247
 
248
  == Changelog ==
249
 
 
 
 
 
250
  = 0.3.6 =
251
  * Fix category cleanup bug
252
 
@@ -313,6 +317,10 @@ Contributors:
313
 
314
  == Upgrade Notice ==
315
 
 
 
 
 
316
  = 0.3.6 =
317
  Fix for 'Invalid argument supplied for foreach() on line 268' error message
318
 
1
  === CSV Importer ===
2
  Contributors: dvkob
 
3
  Tags: csv, import, batch, spreadsheet, excel
4
  Requires at least: 2.0.2
5
+ Tested up to: 3.2.1
6
+ Stable tag: 0.3.6
7
 
8
  Import posts from CSV files into WordPress.
9
 
24
  write your posts)
25
  * Columns in the CSV file can be in any order, provided that they have correct
26
  headings
27
+ * Multi-language support
28
 
29
 
30
  == Screenshots ==
31
 
32
+ 1. Plugin interface
33
 
34
 
35
  == Installation ==
239
  * Edir Pedro (root category option and tableless HTML markup)
240
  * Frank Loeffler (comments support)
241
  * Micah Gates (subcategory syntax)
242
+ * David Hollander (deprecation warnings, linebreak handling)
243
 
244
  [3]: http://code.google.com/p/php-csv-parser/
245
  [4]: http://www.jayblogger.com/the-birth-of-my-first-plugin-import-csv/
247
 
248
  == Changelog ==
249
 
250
+ = 0.3.7 =
251
+ * Make hierarchical custom taxonomy line splitting more robust
252
+ * Fix deprecation warnings
253
+
254
  = 0.3.6 =
255
  * Fix category cleanup bug
256
 
317
 
318
  == Upgrade Notice ==
319
 
320
+ = 0.3.7 =
321
+ More robust handling of hierarchical custom taxonomies; removed deprecation
322
+ warnings.
323
+
324
  = 0.3.6 =
325
  Fix for 'Invalid argument supplied for foreach() on line 268' error message
326