order_no, externalorder, externalorderdate' ); ?>

order_no' ); ?>

run_import('inprint_csv', 'import_inprint_protocol', 'import_inprint_protocol_nonce', 'import_inprint_protocol'); } public function handle_import_delivered_protocol() { $this->run_import('delivered_csv', 'import_delivered_protocol', 'import_delivered_protocol_nonce', 'import_delivered_protocol'); } /** * Common pipeline: capability check → nonce → file validation → parse → dispatch → redirect. * * @param string $file_field * @param string $action Protocol id (inprint|delivered) used to pick the DB handler. * @param string $nonce_field * @param string $nonce_action */ private function run_import($file_field, $action, $nonce_field, $nonce_action) { if (!current_user_can('manage_woocommerce')) { wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'studiou-wc-ord-print-statuses')); } check_admin_referer($nonce_action, $nonce_field); $file = $this->validated_upload($file_field); if ($file === null) { $this->redirect_back(); } $csv_data = Studiou_DB_Manager::parse_csv($file['tmp_name']); if (empty($csv_data)) { UtilsLog::message( __('CSV file could not be parsed or contained no data rows.', 'studiou-wc-ord-print-statuses'), 'error' ); $this->redirect_back(); } if ($action === 'import_inprint_protocol') { $result = Studiou_DB_Manager::import_inprint_protocol($csv_data); } else { $result = Studiou_DB_Manager::import_delivered_protocol($csv_data); } $this->emit_import_summary($result); $this->redirect_back(); } /** * Validate the file upload superglobal entry. Emits a notice and returns null on failure. * * @return array|null */ private function validated_upload($field) { if (!isset($_FILES[$field]) || !is_array($_FILES[$field])) { UtilsLog::message(__('No file uploaded.', 'studiou-wc-ord-print-statuses'), 'error'); return null; } $file = $_FILES[$field]; if (isset($file['error']) && $file['error'] !== UPLOAD_ERR_OK) { UtilsLog::message( sprintf( /* translators: %d is the PHP upload error code. */ __('File upload failed (error code %d).', 'studiou-wc-ord-print-statuses'), (int) $file['error'] ), 'error' ); return null; } if (empty($file['tmp_name']) || !is_uploaded_file($file['tmp_name'])) { UtilsLog::message(__('Uploaded file is not accessible.', 'studiou-wc-ord-print-statuses'), 'error'); return null; } return $file; } private function emit_import_summary($result) { $updated = isset($result['updated_count']) ? (int) $result['updated_count'] : 0; $errors = isset($result['errors']) && is_array($result['errors']) ? $result['errors'] : array(); UtilsLog::message( sprintf( _n( '%d order updated successfully.', '%d orders updated successfully.', $updated, 'studiou-wc-ord-print-statuses' ), $updated ), $updated > 0 ? 'success' : 'info' ); if (!empty($errors)) { UtilsLog::message( sprintf( /* translators: 1: error count, 2: comma-separated error messages. */ __('%1$d errors occurred: %2$s', 'studiou-wc-ord-print-statuses'), count($errors), esc_html(implode(', ', $errors)) ), 'error' ); } } private function redirect_back() { wp_safe_redirect(admin_url('admin.php?page=studiou-import-protocols')); exit; } }