add a list of special_fields if are exists #299

This commit is contained in:
vnmedeiros 2019-09-19 12:05:29 -03:00
parent 9b21fc121c
commit f779349100
2 changed files with 30 additions and 1 deletions

View File

@ -248,7 +248,8 @@ class REST_Importers_Controller extends REST_Controller {
$response = [
'source_metadata' => false,
'source_total_items' => false
'source_total_items' => false,
'source_special_fields' => false
];
if ( method_exists($importer, 'get_source_metadata') ) {
@ -259,6 +260,10 @@ class REST_Importers_Controller extends REST_Controller {
$response['source_total_items'] = $importer->get_source_number_of_items();
}
if ( method_exists($importer, 'get_source_special_fields') ) {
$response['source_special_fields'] = $importer->get_source_special_fields();
}
return new \WP_REST_Response( $response, 200 );
}

View File

@ -60,6 +60,30 @@ class CSV extends Importer {
return [];
}
public function get_source_special_fields() {
if (($handle = fopen($this->tmp_file, "r")) !== false) {
if( $this->get_option('enclosure') && strlen($this->get_option('enclosure')) > 0 ) {
$rawColumns = $this->handle_enclosure( $handle );
} else {
$rawColumns = fgetcsv($handle, 0, $this->get_option('delimiter'));
}
$columns = [];
if( $rawColumns ) {
foreach( $rawColumns as $index => $rawColumn ) {
if( $rawColumn !== 'special_item_id' )
if( strpos($rawColumn,'special_') === 0 ) {
$columns[] = $rawColumn;
}
}
if( !empty($columns) )
return $columns;
}
}
return false;
}
/**
*
* returns all header including special