Store importer and exporter sessions in the database #313

This commit is contained in:
Leo Germani 2019-10-08 20:27:50 -03:00
parent 1782f3b0ed
commit 47e391b92e
6 changed files with 90 additions and 27 deletions

View File

@ -121,6 +121,7 @@ class REST_Exporters_Controller extends REST_Controller {
if ($object = $Tainacan_Exporter_Handler->initialize_exporter($slug)) {
$response = $object->_to_Array();
$Tainacan_Exporter_Handler->save_exporter_instance($object);
return new \WP_REST_Response($response, 201);
} else {
return new \WP_REST_Response([
@ -146,13 +147,14 @@ class REST_Exporters_Controller extends REST_Controller {
foreach ($body as $att => $value) {
$attributes[$att] = $value;
}
$importer = $_SESSION['tainacan_exporter'][$session_id];
if($importer) {
global $Tainacan_Exporter_Handler;
$exporter = $Tainacan_Exporter_Handler->get_exporter_instance_by_session_id($session_id);
if($exporter) {
foreach ($body as $att => $value) {
if ($att == 'collection') {
if (is_array($value) && isset($value['id'])) {
$importer->add_collection($value);
$exporter->add_collection($value);
continue;
} else {
return new \WP_REST_Response([
@ -162,12 +164,13 @@ class REST_Exporters_Controller extends REST_Controller {
}
}
$method = 'set_' . $att;
if (method_exists($importer, $method)) {
$importer->$method($value);
if (method_exists($exporter, $method)) {
$exporter->$method($value);
}
}
$response = $importer->_to_Array();
$response = $exporter->_to_Array();
$Tainacan_Exporter_Handler->save_exporter_instance($exporter);
return new \WP_REST_Response( $response, 200 );
}
@ -192,7 +195,8 @@ class REST_Exporters_Controller extends REST_Controller {
*/
public function run($request) {
$session_id = $request['session_id'];
$exporter = $_SESSION['tainacan_exporter'][$session_id];
global $Tainacan_Exporter_Handler;
$exporter = $Tainacan_Exporter_Handler->get_exporter_instance_by_session_id($session_id);
if(!$exporter) {
return new \WP_REST_Response([
@ -212,6 +216,7 @@ class REST_Exporters_Controller extends REST_Controller {
$response = [
'bg_process_id' => $process->ID
];
$Tainacan_Exporter_Handler->delete_exporter_instance($exporter);
return new \WP_REST_Response( $response, 200 );
}

View File

@ -165,6 +165,7 @@ class REST_Importers_Controller extends REST_Controller {
if ($object = $Tainacan_Importer_Handler->initialize_importer($slug)) {
$response = $object->_to_Array();
$Tainacan_Importer_Handler->save_importer_instance($object);
return new \WP_REST_Response($response, 201);
} else {
return new \WP_REST_Response([
@ -192,9 +193,10 @@ class REST_Importers_Controller extends REST_Controller {
foreach ($body as $att => $value){
$attributes[$att] = $value;
}
$importer = $_SESSION['tainacan_importer'][$session_id];
global $Tainacan_Importer_Handler;
$importer = $Tainacan_Importer_Handler->get_importer_instance_by_session_id($session_id);
if($importer) {
foreach ($body as $att => $value){
@ -218,6 +220,7 @@ class REST_Importers_Controller extends REST_Controller {
}
$response = $importer->_to_Array();
$Tainacan_Importer_Handler->save_importer_instance($importer);
return new \WP_REST_Response( $response, 200 );
}
@ -237,7 +240,8 @@ class REST_Importers_Controller extends REST_Controller {
public function source_info( $request ) {
$session_id = $request['session_id'];
$importer = $_SESSION['tainacan_importer'][$session_id];
global $Tainacan_Importer_Handler;
$importer = $Tainacan_Importer_Handler->get_importer_instance_by_session_id($session_id);
if(!$importer) {
return new \WP_REST_Response([
@ -271,7 +275,8 @@ class REST_Importers_Controller extends REST_Controller {
public function get_saved_mapping( $request ){
$session_id = $request['session_id'];
$collection_id = $request['collection_id'];
$importer = $_SESSION['tainacan_importer'][$session_id];
global $Tainacan_Importer_Handler;
$importer = $Tainacan_Importer_Handler->get_importer_instance_by_session_id($session_id);
$response = false;
if(!$importer) {
@ -290,7 +295,8 @@ class REST_Importers_Controller extends REST_Controller {
public function get_item( $request ) {
$session_id = $request['session_id'];
$importer = $_SESSION['tainacan_importer'][$session_id];
global $Tainacan_Importer_Handler;
$importer = $Tainacan_Importer_Handler->get_importer_instance_by_session_id($session_id);
if(!$importer) {
return new \WP_REST_Response([
@ -306,7 +312,8 @@ class REST_Importers_Controller extends REST_Controller {
public function add_file( $request ) {
$session_id = $request['session_id'];
$importer = $_SESSION['tainacan_importer'][$session_id];
global $Tainacan_Importer_Handler;
$importer = $Tainacan_Importer_Handler->get_importer_instance_by_session_id($session_id);
if(!$importer) {
return new \WP_REST_Response([
@ -320,6 +327,7 @@ class REST_Importers_Controller extends REST_Controller {
if ( isset($files['file']) && $importer->add_file($files['file']) ) {
$response = $importer->_to_Array();
$Tainacan_Importer_Handler->save_importer_instance($importer);
return new \WP_REST_Response( $response, 200 );
} else {
return new \WP_REST_Response([
@ -335,7 +343,8 @@ class REST_Importers_Controller extends REST_Controller {
public function run($request) {
$session_id = $request['session_id'];
$importer = $_SESSION['tainacan_importer'][$session_id];
global $Tainacan_Importer_Handler;
$importer = $Tainacan_Importer_Handler->get_importer_instance_by_session_id($session_id);
if(!$importer) {
return new \WP_REST_Response([
@ -358,6 +367,7 @@ class REST_Importers_Controller extends REST_Controller {
$response = [
'bg_process_id' => $process->ID
];
$Tainacan_Importer_Handler->delete_importer_instance($importer);
return new \WP_REST_Response( $response, 200 );
}

View File

@ -103,6 +103,34 @@ class Export_Handler {
}
return false;
}
/**
* Save exporter instance to the database
* @param Tainacan\Exporter\Exporter $exporter The Importer object
* @return void
*/
public function save_exporter_instance(\Tainacan\Exporter\Exporter $exporter) {
update_option('tnc_transient_' . $exporter->get_id(), $exporter);
}
/**
* Retrieves an Importer instance from the database based on its session_id
* @param string $session_id The Importer ID
* @return \Tainacan\Exporter\Exporter|false The Importer object, if found. False otherwise
*/
public function get_exporter_instance_by_session_id($session_id) {
$exporter = get_option('tnc_transient_' . $session_id);
return $exporter;
}
/**
* Deletes this exporter instance from the database
* @param Tainacan\Exporter\Exporter $exporter The Importer object
* @return bool True, if exporter is successfully deleted. False on failure.
*/
public function delete_exporter_instance(\Tainacan\Exporter\Exporter $exporter) {
return delete_option('tnc_transient_' . $exporter->get_id());
}
}
global $Tainacan_Exporter_Handler;

View File

@ -6,9 +6,6 @@ use Tainacan\Entities;
abstract class CommunImportExport {
public function __construct( ) {
if (!session_id()) {
@session_start();
}
$this->id = uniqid();
$author = get_current_user_id();
if($author) {
@ -20,7 +17,7 @@ abstract class CommunImportExport {
* The ID for this importer/exporter session
*
* When creating a new importer/exporter session via API, an id is returned and used to access this
* importer/exporter instance in the SESSION array
* importer/exporter instance. This is temporarly saved in the database and discarded after the bg process is triggered
*
* @var identifier
*/
@ -516,7 +513,7 @@ class Exporter extends CommunImportExport {
'send_email'
]);
parent::__construct();
$_SESSION['tainacan_exporter'][$this->get_id()] = $this;
if (!empty($attributess)) {
foreach ($attributess as $attr => $value) {
$method = 'set_' . $attr;

View File

@ -187,6 +187,34 @@ class Importer_Handler {
return false;
}
/**
* Save importer instance to the database
* @param Tainacan\Importer\Importer $importer The Importer object
* @return void
*/
public function save_importer_instance(\Tainacan\Importer\Importer $importer) {
update_option('tnc_transient_' . $importer->get_id(), $importer);
}
/**
* Retrieves an Importer instance from the database based on its session_id
* @param string $session_id The Importer ID
* @return \Tainacan\Importer\Importer|false The Importer object, if found. False otherwise
*/
public function get_importer_instance_by_session_id($session_id) {
$importer = get_option('tnc_transient_' . $session_id);
return $importer;
}
/**
* Deletes this importer instance from the database
* @param Tainacan\Importer\Importer $importer The Importer object
* @return bool True, if importer is successfully deleted. False on failure.
*/
public function delete_importer_instance(\Tainacan\Importer\Importer $importer) {
return delete_option('tnc_transient_' . $importer->get_id());
}
}

View File

@ -9,7 +9,7 @@ abstract class Importer {
* The ID for this importer session
*
* When creating a new importer session via API, an id is returned and used to access this
* importer instance in the SESSION array
* importer instance. This is temporarily stored in the database and discarded after the bg process is triggered
*
* @var identifier
*/
@ -137,9 +137,6 @@ abstract class Importer {
];
public function __construct($attributess = array()) {
if (!session_id()) {
@session_start();
}
$this->id = uniqid();
@ -148,8 +145,6 @@ abstract class Importer {
$this->add_transient('author', $author);
}
$_SESSION['tainacan_importer'][$this->get_id()] = $this;
if (!empty($attributess)) {
foreach ($attributess as $attr => $value) {
$method = 'set_' . $attr;