/** * Helper method to trigger a background import. */ public function run_background_import() { // Make sure a background import has been requested. if (!isset($_GET['sme_background_import']) || !$_GET['sme_background_import']) { return; } // Make sure a batch ID has been provided. if (!isset($_GET['sme_batch_id']) || !$_GET['sme_batch_id']) { return; } // Make sure a background import key has been provided. if (!isset($_GET['sme_import_key']) || !$_GET['sme_import_key']) { return; } $batch_id = intval($_GET['sme_batch_id']); $import_key = $_GET['sme_import_key']; $batch_dao = $this->dao_factory->create('Batch'); // Get batch from database. $batch = $batch_dao->find($batch_id); // No batch to import found, error. if (!$batch) { error_log(sprintf('Batch with ID %d could not be imported.', $batch_id)); wp_die(__('Something went wrong', 'sme-content-staging')); } // Validate key. if ($import_key !== $this->api->get_import_key($batch->get_id())) { error_log('Unauthorized batch import attempt terminated.'); $this->api->add_deploy_message($batch->get_id(), __('Something went wrong', 'sme-content-staging'), 'error'); $this->api->set_deploy_status($batch->get_id(), 2); wp_die(__('Something went wrong', 'sme-content-staging')); } // Background import is running. Make the old import key useless. $this->api->generate_import_key($batch); // Create the importer. $importer = new Batch_Background_Importer($batch); // Trigger import. $importer->import(); }
public function tear_down() { // Import finished, update import status. $this->api->set_deploy_status($this->batch->get_id(), 3); do_action('sme_imported', $this->batch); }