add name to background processes #62
This commit is contained in:
parent
c19ae53cc6
commit
7d1f934795
|
@ -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,12 +72,36 @@ 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;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Save queue
|
||||
|
@ -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')
|
||||
]
|
||||
);
|
||||
|
|
|
@ -9,6 +9,11 @@ class Background_Importer extends Background_Process {
|
|||
*/
|
||||
protected $action = 'import';
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
$this->set_name( __('Importer', 'tainacan') );
|
||||
}
|
||||
|
||||
function task($batch) {
|
||||
|
||||
$data = $batch->data;
|
||||
|
|
|
@ -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']) ) {
|
||||
|
|
|
@ -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,
|
||||
|
@ -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,
|
||||
");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -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']);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue