Include list column for excel subscribers export

MAILPOET-3852
This commit is contained in:
oluwaseun Olorunsola
2021-10-28 12:00:01 +01:00
committed by Veljko V
parent 69da013483
commit 1cdbf31b50
2 changed files with 18 additions and 5 deletions

View File

@@ -19,6 +19,7 @@ class Export {
public $subscriberFields; public $subscriberFields;
public $subscriberCustomFields; public $subscriberCustomFields;
public $formattedSubscriberFields; public $formattedSubscriberFields;
public $formattedSubscriberFieldsWithList;
public $exportPath; public $exportPath;
public $exportFile; public $exportFile;
public $exportFileURL; public $exportFileURL;
@@ -64,6 +65,8 @@ class Export {
$this->subscriberFields, $this->subscriberFields,
$this->subscriberCustomFields $this->subscriberCustomFields
); );
$this->formattedSubscriberFieldsWithList = $this->formattedSubscriberFields;
$this->formattedSubscriberFieldsWithList[] = WPFunctions::get()->__('List', 'mailpoet');
$this->exportPath = self::getExportPath(); $this->exportPath = self::getExportPath();
$this->exportFile = $this->getExportFile($this->exportFormatOption); $this->exportFile = $this->getExportFile($this->exportFormatOption);
$this->exportFileURL = $this->getExportFileURL($this->exportFile); $this->exportFileURL = $this->getExportFileURL($this->exportFile);
@@ -105,7 +108,7 @@ class Export {
public function generateCSV(): int { public function generateCSV(): int {
$processedSubscribers = 0; $processedSubscribers = 0;
$formattedSubscriberFields = $this->formattedSubscriberFields; $formattedSubscriberFields = $this->formattedSubscriberFieldsWithList;
$cSVFile = fopen($this->exportFile, 'w'); $cSVFile = fopen($this->exportFile, 'w');
if ($cSVFile === false) { if ($cSVFile === false) {
throw new \Exception(__('Failed opening file for export.', 'mailpoet')); throw new \Exception(__('Failed opening file for export.', 'mailpoet'));
@@ -113,7 +116,6 @@ class Export {
$formatCSV = function($row) { $formatCSV = function($row) {
return '"' . str_replace('"', '\"', $row) . '"'; return '"' . str_replace('"', '\"', $row) . '"';
}; };
$formattedSubscriberFields[] = WPFunctions::get()->__('List', 'mailpoet');
// add UTF-8 BOM (3 bytes, hex EF BB BF) at the start of the file for // add UTF-8 BOM (3 bytes, hex EF BB BF) at the start of the file for
// Excel to automatically recognize the encoding // Excel to automatically recognize the encoding
fwrite($cSVFile, chr(0xEF) . chr(0xBB) . chr(0xBF)); fwrite($cSVFile, chr(0xEF) . chr(0xBB) . chr(0xBF));
@@ -165,7 +167,7 @@ class Export {
$this->writeXLSX( $this->writeXLSX(
$xLSXWriter, $xLSXWriter,
$subscriber['segment_name'], $subscriber['segment_name'],
$this->formattedSubscriberFields $this->formattedSubscriberFieldsWithList
); );
$processedSegments[] = $currentSegment; $processedSegments[] = $currentSegment;
} }
@@ -174,14 +176,18 @@ class Export {
$rTLRegex = '/\p{Arabic}|\p{Hebrew}/u'; $rTLRegex = '/\p{Arabic}|\p{Hebrew}/u';
if (!$xLSXWriter->rtl && ( if (!$xLSXWriter->rtl && (
preg_grep($rTLRegex, $subscriber) || preg_grep($rTLRegex, $subscriber) ||
preg_grep($rTLRegex, $this->formattedSubscriberFields)) preg_grep($rTLRegex, $this->formattedSubscriberFieldsWithList))
) { ) {
$xLSXWriter->rtl = true; $xLSXWriter->rtl = true;
} }
$xlsxData = $this->formatSubscriberData($subscriber);
$xlsxData[] = ucwords($subscriber['segment_name']);
$this->writeXLSX( $this->writeXLSX(
$xLSXWriter, $xLSXWriter,
$lastSegment, $lastSegment,
$this->formatSubscriberData($subscriber) $xlsxData
); );
} }
} }

View File

@@ -153,6 +153,13 @@ class ExportTest extends \MailPoetTest {
$this->export->subscriberCustomFields $this->export->subscriberCustomFields
) )
); );
expect($this->export->formattedSubscriberFieldsWithList)
->equals(
\array_merge(
$this->export->formattedSubscriberFields,
[\MailPoet\WP\Functions::get()->__('List', 'mailpoet')]
)
);
expect( expect(
preg_match( preg_match(
'|' . '|' .