From 5161747d46b4eb569eeba1545ec69942ec75472b Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Thu, 2 Aug 2018 11:01:13 -0300 Subject: [PATCH] CSV importer correctly handles attachments and documents --- src/importer/class-tainacan-csv.php | 88 +++++++++++++++-------------- tests/test-importer.php | 10 ++-- 2 files changed, 51 insertions(+), 47 deletions(-) diff --git a/src/importer/class-tainacan-csv.php b/src/importer/class-tainacan-csv.php index 0355a0a65..6f055adcd 100644 --- a/src/importer/class-tainacan-csv.php +++ b/src/importer/class-tainacan-csv.php @@ -252,29 +252,31 @@ class CSV extends Importer { $TainacanMedia = \Tainacan\Media::get_instance(); if( strpos($column_value,'url:') === 0 ){ - $correct_value = substr($column_value, 4); + $correct_value = trim(substr($column_value, 4)); $item_inserted->set_document( $correct_value ); $item_inserted->set_document_type( 'url' ); - if( $item_inserted->validate() ) - $this->items_repo->update($item_inserted); + if( $item_inserted->validate() ) { + $item_inserted = $this->items_repo->update($item_inserted); + } } else if( strpos($column_value,'text:') === 0 ){ - $correct_value = substr($column_value, 5); + $correct_value = trim(substr($column_value, 5)); $item_inserted->set_document( $correct_value ); $item_inserted->set_document_type( 'text' ); - if( $item_inserted->validate() ) - $this->items_repo->update($item_inserted); + if( $item_inserted->validate() ) { + $item_inserted = $this->items_repo->update($item_inserted); + } } else if( strpos($column_value,'file:') === 0 ){ - $correct_value = substr($column_value, 5); + $correct_value = trim(substr($column_value, 5)); if( filter_var($correct_value, FILTER_VALIDATE_URL) ){ - $id = $TainacanMedia->insert_attachment_from_url($correct_value, $item_inserted->get_id()); + $id = $TainacanMedia->insert_attachment_from_url($correct_value); if(!$id){ - $this->add_log('Error in Document file imported from URL ' . $correct_value); + $this->add_error_log('Error in Document file imported from URL ' . $correct_value); return false; } @@ -282,29 +284,39 @@ class CSV extends Importer { $item_inserted->set_document_type( 'attachment' ); $this->add_log('Document file URL imported from ' . $correct_value); - if( $item_inserted->validate() ) - $this->items_repo->update($item_inserted); + if( $item_inserted->validate() ) { + $item_inserted = $this->items_repo->update($item_inserted); + } + } else { + $server_path_files = trailingslashit($this->get_option('server_path')); + $id = $TainacanMedia->insert_attachment_from_file($server_path_files . $correct_value); - return true; - } + if(!$id){ + $this->add_error_log('Error in Document file imported from server ' . $correct_value); + return false; + } - $server_path_files = $this->get_option('server_path'); - $id = $TainacanMedia->insert_attachment_from_file($correct_value, $item_inserted->get_id()); + $item_inserted->set_document( $id ); + $item_inserted->set_document_type( 'attachment' ); + $this->add_log('Document file in Server imported from ' . $correct_value); + + if( $item_inserted->validate() ) { + $item_inserted = $this->items_repo->update($item_inserted); + } - if(!$id){ - $this->add_log('Error in Document file imported from server ' . $correct_value); - return false; } - $item_inserted->set_document( $id ); - $item_inserted->set_document_type( 'attachment' ); - $this->add_log('Document file in Server imported from ' . $correct_value); - - if( $item_inserted->validate() ) - $this->items_repo->update($item_inserted); - - return true; } + + $thumb_id = $this->items_repo->get_thumbnail_id_from_document($item_inserted); + + if (!is_null($thumb_id)) { + $this->add_log('Setting item thumbnail: ' . $thumb_id); + set_post_thumbnail( $item_inserted->get_id(), (int) $thumb_id ); + } + + return true; + } /** @@ -318,36 +330,28 @@ class CSV extends Importer { if( $attachments ){ foreach( $attachments as $attachment ){ - if( strpos($attachment,'file:') !== 0 ){ - $this->add_log('Attachment must have "file:" previously the path or url '); - continue; - } - - - $correct_value = substr($attachment, 5); - - if( filter_var($correct_value, FILTER_VALIDATE_URL) ){ - $id = $TainacanMedia->insert_attachment_from_url($correct_value, $item_inserted->get_id()); + if( filter_var($attachment, FILTER_VALIDATE_URL) ){ + $id = $TainacanMedia->insert_attachment_from_url($attachment, $item_inserted->get_id()); if(!$id){ - $this->add_log('Error in Attachment file imported from URL ' . $correct_value); + $this->add_error_log('Error in Attachment file imported from URL ' . $attachment); return false; } - $this->add_log('Attachment file URL imported from ' . $correct_value); + $this->add_log('Attachment file URL imported from ' . $attachment); continue; } - $server_path_files = $this->get_option('server_path'); - $id = $TainacanMedia->insert_attachment_from_file($correct_value, $item_inserted->get_id()); + $server_path_files = trailingslashit($this->get_option('server_path')); + $id = $TainacanMedia->insert_attachment_from_file($server_path_files . $attachment, $item_inserted->get_id()); if(!$id){ - $this->add_log('Error in Attachment file imported from server ' . $correct_value); + $this->add_log('Error in Attachment file imported from server ' . $attachment); continue; } - $this->add_log('Attachment file in Server imported from ' . $correct_value); + $this->add_log('Attachment file in Server imported from ' . $attachment); } } } diff --git a/tests/test-importer.php b/tests/test-importer.php index 374ecfa17..7542cb7ca 100644 --- a/tests/test-importer.php +++ b/tests/test-importer.php @@ -389,16 +389,16 @@ class ImporterTests extends TAINACAN_UnitTestCase { // Sample data $data = array( - array('Data 11', '456', 'Data 13||TESTE', 'file:https://www.w3schools.com/w3css/img_lights.jpg', 'text:Vou dormir mais tarde'), - array('Data 21', '457', 'Data 23', 'file:photos/unnamed.jpg', 'url:https://www.youtube.com/watch?v=V8dpmD4HG5s&start_radio=1&list=RDEMZS6OrHEAut8dOA38mVtVpg'), + array('Data 11', '456', 'Data 13||TESTE', 'https://www.w3schools.com/w3css/img_lights.jpg', 'text:Vou dormir mais tarde'), + array('Data 21', '457', 'Data 23', 'photos/unnamed.jpg', 'url:https://www.youtube.com/watch?v=V8dpmD4HG5s&start_radio=1&list=RDEMZS6OrHEAut8dOA38mVtVpg'), array( 'Data 31', '458', utf8_decode( 'Data 33||Rééço' ), - 'https://www.codeproject.com/KB/GDI-plus/ImageProcessing2/img.jpg||file:https://cloud.netlifyusercontent.com/assets/344dbf88-fdf9-42bb-adb4-46f01eedd629/58f72418-b5ee-4765-8e80-e463623a921d/01-httparchive-opt-small.png', + 'https://www.codeproject.com/KB/GDI-plus/ImageProcessing2/img.jpg||https://cloud.netlifyusercontent.com/assets/344dbf88-fdf9-42bb-adb4-46f01eedd629/58f72418-b5ee-4765-8e80-e463623a921d/01-httparchive-opt-small.png', 'file:https://images.pexels.com/photos/248797/pexels-photo-248797.jpeg'), - array('Data 41', '459', 'Data 43||limbbo', 'file:photos/SamplePNGImage_100kbmb.png||file:audios/SampleAudio_0.4mb.mp3', 'url:http://www.pdf995.com/samples/pdf.pdf'), - array('Data 51', '500', 'Data 53', 'file:http://techslides.com/demos/samples/sample.mp4', '') + array('Data 41', '459', 'Data 43||limbbo', 'photos/SamplePNGImage_100kbmb.png||audios/SampleAudio_0.4mb.mp3', 'url:http://www.pdf995.com/samples/pdf.pdf'), + array('Data 51', '500', 'Data 53', 'http://techslides.com/demos/samples/sample.mp4', '') ); // save each row of the data