add name to background processes #62

This commit is contained in:
Leo Germani 2018-07-20 11:05:27 -03:00
parent c19ae53cc6
commit 7d1f934795
5 changed files with 82 additions and 6 deletions

View File

@ -50,13 +50,21 @@ abstract class Background_Process extends \WP_Background_Process {
/** /**
* Action * Action
* *
* (default value: 'background_process') * (default value: 'process')
* *
* @var string * @var string
* @access protected * @access protected
*/ */
protected $action = 'process'; protected $action = 'process';
/**
* Name
*
* @var string
* @access protected
*/
protected $name = 'Background Process';
/** /**
* Initiate new background process * Initiate new background process
*/ */
@ -64,12 +72,36 @@ abstract class Background_Process extends \WP_Background_Process {
parent::__construct(); parent::__construct();
global $wpdb; global $wpdb;
$this->table = $wpdb->prefix . 'tnc_bg_process'; $this->table = $wpdb->prefix . 'tnc_bg_process';
$this->set_name( __('Background Process', 'tainacan') );
} }
public function get_id() { public function get_id() {
return $this->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;
}
/** /**
* Save queue * Save queue
@ -87,6 +119,7 @@ abstract class Background_Process extends \WP_Background_Process {
'user_id' => get_current_user_id(), 'user_id' => get_current_user_id(),
'priority' => $priority, 'priority' => $priority,
'action' => $this->action, 'action' => $this->action,
'name' => $this->get_name(),
'queued_on' => date('Y-m-d H:i:s') 'queued_on' => date('Y-m-d H:i:s')
] ]
); );

View File

@ -9,6 +9,11 @@ class Background_Importer extends Background_Process {
*/ */
protected $action = 'import'; protected $action = 'import';
public function __construct() {
parent::__construct();
$this->set_name( __('Importer', 'tainacan') );
}
function task($batch) { function task($batch) {
$data = $batch->data; $data = $batch->data;

View File

@ -23,15 +23,15 @@ class Importer_Handler {
'class_name' => '\Tainacan\Importer\CSV' 'class_name' => '\Tainacan\Importer\CSV'
]); ]);
$this->register_importer([ $this->register_importer([
'name' => 'Test Importer', 'name' => 'Test',
'description' => __('Create 2 test colletions with random items', 'tainacan'), 'description' => __('Create 2 test colletions with random items', 'tainacan'),
'slug' => 'test', 'slug' => 'test',
'class_name' => '\Tainacan\Importer\Test_Importer' 'class_name' => '\Tainacan\Importer\Test_Importer'
]); ]);
$this->register_importer([ $this->register_importer([
'name' => 'Tainacan Old', 'name' => 'Tainacan Legacy',
'description' => __('Import structure from previously version of tainacan', 'tainacan'), 'description' => __('Import structure from legacy version of Tainacan', 'tainacan'),
'slug' => 'tainacan_old', 'slug' => 'tainacan_old',
'class_name' => '\Tainacan\Importer\Old_Tainacan' 'class_name' => '\Tainacan\Importer\Old_Tainacan'
]); ]);
@ -42,7 +42,12 @@ class Importer_Handler {
function add_to_queue(\Tainacan\Importer\Importer $importer_object) { function add_to_queue(\Tainacan\Importer\Importer $importer_object) {
$data = $importer_object->_to_Array(true); $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()) ) { if ( is_wp_error($bg_process->dispatch()) ) {
return false; return false;
} }
@ -79,6 +84,18 @@ class Importer_Handler {
return null; 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) { public function initialize_importer($importer_slug) {
$importer = $this->get_importer($importer_slug); $importer = $this->get_importer($importer_slug);
if ( is_array($importer) && isset($importer['class_name']) && class_exists($importer['class_name']) ) { if ( is_array($importer) && isset($importer['class_name']) && class_exists($importer['class_name']) ) {

View File

@ -14,6 +14,7 @@ function tainacan_create_bd_process_db() {
processed_last datetime NOT NULL default '0000-00-00 00:00:00', processed_last datetime NOT NULL default '0000-00-00 00:00:00',
data longtext NOT NULL, data longtext NOT NULL,
action text NOT NULL, action text NOT NULL,
name text NOT NULL,
done boolean not null default 0, done boolean not null default 0,
progress_label text, progress_label text,
progress_value int, progress_value int,
@ -34,6 +35,15 @@ function tainacan_create_bd_process_db() {
"); ");
} }
$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,
");
}
} }

View File

@ -359,4 +359,15 @@ class ImporterTests extends TAINACAN_UnitTestCase {
$this->assertEquals( $_SESSION['tainacan_importer'][$id]->get_source_number_of_items(), count( $items ) ); $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']);
}
} }