diff --git a/src/classes/class-tainacan-background-process.php b/src/classes/class-tainacan-background-process.php index 0d2472f12..c362bf0f5 100644 --- a/src/classes/class-tainacan-background-process.php +++ b/src/classes/class-tainacan-background-process.php @@ -50,13 +50,21 @@ abstract class Background_Process extends \WP_Background_Process { /** * Action * - * (default value: 'background_process') + * (default value: 'process') * * @var string * @access protected */ protected $action = 'process'; - + + /** + * Name + * + * @var string + * @access protected + */ + protected $name = 'Background Process'; + /** * Initiate new background process */ @@ -64,11 +72,35 @@ abstract class Background_Process extends \WP_Background_Process { parent::__construct(); global $wpdb; $this->table = $wpdb->prefix . 'tnc_bg_process'; + $this->set_name( __('Background Process', 'tainacan') ); } public function get_id() { return $this->ID; } + + /** + * Gets the name of the process. + * + * Override this method to set a name to the process + * + * Default "Background Process" + * + * @return string + */ + public function get_name() { + return $this->name; + } + + /** + * Set name + * + * @return $this + */ + public function set_name($name) { + $this->name = $name; + return $this; + } /** @@ -87,6 +119,7 @@ abstract class Background_Process extends \WP_Background_Process { 'user_id' => get_current_user_id(), 'priority' => $priority, 'action' => $this->action, + 'name' => $this->get_name(), 'queued_on' => date('Y-m-d H:i:s') ] ); diff --git a/src/importer/class-tainacan-bg-importer.php b/src/importer/class-tainacan-bg-importer.php index 835b1b868..0dad87004 100644 --- a/src/importer/class-tainacan-bg-importer.php +++ b/src/importer/class-tainacan-bg-importer.php @@ -8,6 +8,11 @@ class Background_Importer extends Background_Process { * @var string */ protected $action = 'import'; + + public function __construct() { + parent::__construct(); + $this->set_name( __('Importer', 'tainacan') ); + } function task($batch) { diff --git a/src/importer/class-tainacan-importer-handler.php b/src/importer/class-tainacan-importer-handler.php index 409dc0405..8dc5fc7cc 100644 --- a/src/importer/class-tainacan-importer-handler.php +++ b/src/importer/class-tainacan-importer-handler.php @@ -23,15 +23,15 @@ class Importer_Handler { 'class_name' => '\Tainacan\Importer\CSV' ]); $this->register_importer([ - 'name' => 'Test Importer', + 'name' => 'Test', 'description' => __('Create 2 test colletions with random items', 'tainacan'), 'slug' => 'test', 'class_name' => '\Tainacan\Importer\Test_Importer' ]); $this->register_importer([ - 'name' => 'Tainacan Old', - 'description' => __('Import structure from previously version of tainacan', 'tainacan'), + 'name' => 'Tainacan Legacy', + 'description' => __('Import structure from legacy version of Tainacan', 'tainacan'), 'slug' => 'tainacan_old', 'class_name' => '\Tainacan\Importer\Old_Tainacan' ]); @@ -42,7 +42,12 @@ class Importer_Handler { function add_to_queue(\Tainacan\Importer\Importer $importer_object) { $data = $importer_object->_to_Array(true); - $bg_process = $this->bg_importer->data($data)->save(); + $importer = $this->get_importer_by_object($importer_object); + + // Translators: The name of the importer process. E.g. CSV Importer, Legacy Tainacan Importer + $importer_name = sprintf( __('%s Importer', 'tainacan'), $importer['name'] ); + + $bg_process = $this->bg_importer->data($data)->set_name($importer_name)->save(); if ( is_wp_error($bg_process->dispatch()) ) { return false; } @@ -79,6 +84,18 @@ class Importer_Handler { return null; } + function get_importer_by_object(\Tainacan\Importer\Importer $importer_object) { + $class_name = get_class($importer_object); + // add first bracket + $class_name = '\\' . $class_name; + $importers = $this->get_registered_importers(); + foreach ($importers as $importer) { + if ($importer['class_name'] == $class_name) + return $importer; + } + return null; + } + public function initialize_importer($importer_slug) { $importer = $this->get_importer($importer_slug); if ( is_array($importer) && isset($importer['class_name']) && class_exists($importer['class_name']) ) { diff --git a/src/setup-db.php b/src/setup-db.php index ccec5c14c..3df1291cf 100644 --- a/src/setup-db.php +++ b/src/setup-db.php @@ -14,6 +14,7 @@ function tainacan_create_bd_process_db() { processed_last datetime NOT NULL default '0000-00-00 00:00:00', data longtext NOT NULL, action text NOT NULL, + name text NOT NULL, done boolean not null default 0, progress_label text, progress_value int, @@ -33,6 +34,15 @@ function tainacan_create_bd_process_db() { ADD progress_value int "); } + + $column_exists = $wpdb->get_results( "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '{$wpdb->prefix}tnc_bg_process' AND column_name = 'name'" ); + + if(empty($column_exists)) { + $wpdb->query(" + ALTER TABLE {$wpdb->prefix}tnc_bg_process + ADD name text NOT NULL, + "); + } } diff --git a/tests/test-importer.php b/tests/test-importer.php index 40e8be6d6..1d1394ace 100644 --- a/tests/test-importer.php +++ b/tests/test-importer.php @@ -359,4 +359,15 @@ class ImporterTests extends TAINACAN_UnitTestCase { $this->assertEquals( $_SESSION['tainacan_importer'][$id]->get_source_number_of_items(), count( $items ) ); } + + /** + * @group xis + */ + public function test_get_registered() { + global $Tainacan_Importer_Handler; + $csv_importer = new Importer\CSV(); + $registered = $Tainacan_Importer_Handler->get_importer_by_object($csv_importer); + $this->assertEquals('csv', $registered['slug']); + $this->assertEquals('CSV', $registered['name']); + } }