Render segment names in form using actual segment names
[MAILPOET-2801]
This commit is contained in:
committed by
Veljko V
parent
936a75c277
commit
9701f46a77
@@ -234,6 +234,7 @@ class ContainerConfigurator implements IContainerConfigurator {
|
|||||||
$container->autowire(\MailPoet\Segments\SubscribersListings::class)->setPublic(true);
|
$container->autowire(\MailPoet\Segments\SubscribersListings::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Segments\WooCommerce::class)->setPublic(true);
|
$container->autowire(\MailPoet\Segments\WooCommerce::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Segments\SubscribersFinder::class);
|
$container->autowire(\MailPoet\Segments\SubscribersFinder::class);
|
||||||
|
$container->autowire(\MailPoet\Segments\SegmentsRepository::class);
|
||||||
// Services
|
// Services
|
||||||
$container->autowire(\MailPoet\Services\Bridge::class)->setPublic(true);
|
$container->autowire(\MailPoet\Services\Bridge::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Services\AuthorizedEmailsController::class);
|
$container->autowire(\MailPoet\Services\AuthorizedEmailsController::class);
|
||||||
|
@@ -3,8 +3,11 @@
|
|||||||
namespace MailPoet\Form\Block;
|
namespace MailPoet\Form\Block;
|
||||||
|
|
||||||
use MailPoet\Form\BlockWrapperRenderer;
|
use MailPoet\Form\BlockWrapperRenderer;
|
||||||
|
use MailPoet\Segments\SegmentsRepository;
|
||||||
use MailPoet\WP\Functions as WPFunctions;
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
|
|
||||||
|
use function MailPoetVendor\array_column;
|
||||||
|
|
||||||
class Segment {
|
class Segment {
|
||||||
|
|
||||||
/** @var BlockRendererHelper */
|
/** @var BlockRendererHelper */
|
||||||
@@ -16,10 +19,19 @@ class Segment {
|
|||||||
/** @var BlockWrapperRenderer */
|
/** @var BlockWrapperRenderer */
|
||||||
private $wrapper;
|
private $wrapper;
|
||||||
|
|
||||||
public function __construct(BlockRendererHelper $rendererHelper, BlockWrapperRenderer $wrapper, WPFunctions $wp) {
|
/** @var SegmentsRepository */
|
||||||
|
private $segmentsRepository;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
BlockRendererHelper $rendererHelper,
|
||||||
|
BlockWrapperRenderer $wrapper,
|
||||||
|
WPFunctions $wp,
|
||||||
|
SegmentsRepository $segmentsRepository
|
||||||
|
) {
|
||||||
$this->rendererHelper = $rendererHelper;
|
$this->rendererHelper = $rendererHelper;
|
||||||
$this->wrapper = $wrapper;
|
$this->wrapper = $wrapper;
|
||||||
$this->wp = $wp;
|
$this->wp = $wp;
|
||||||
|
$this->segmentsRepository = $segmentsRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function render(array $block, array $formSettings): string {
|
public function render(array $block, array $formSettings): string {
|
||||||
@@ -35,8 +47,14 @@ class Segment {
|
|||||||
: []
|
: []
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$options = array_map(function ($option) {
|
||||||
|
$option['id'] = intval($option['id']);
|
||||||
|
return $option;
|
||||||
|
}, $options);
|
||||||
|
$segmentsNamesMap = $this->getSegmentsNames($options);
|
||||||
|
|
||||||
foreach ($options as $option) {
|
foreach ($options as $option) {
|
||||||
if (!isset($option['id']) || !isset($option['name'])) continue;
|
if (!isset($option['id']) || !isset($segmentsNamesMap[$option['id']])) continue;
|
||||||
|
|
||||||
$isChecked = (isset($option['is_checked']) && $option['is_checked']) ? 'checked="checked"' : '';
|
$isChecked = (isset($option['is_checked']) && $option['is_checked']) ? 'checked="checked"' : '';
|
||||||
|
|
||||||
@@ -47,7 +65,7 @@ class Segment {
|
|||||||
$html .= 'name="' . $fieldName . '[]" ';
|
$html .= 'name="' . $fieldName . '[]" ';
|
||||||
$html .= 'value="' . $option['id'] . '" ' . $isChecked . ' ';
|
$html .= 'value="' . $option['id'] . '" ' . $isChecked . ' ';
|
||||||
$html .= $fieldValidation;
|
$html .= $fieldValidation;
|
||||||
$html .= ' /> ' . $this->wp->escAttr($option['name']);
|
$html .= ' /> ' . $this->wp->escAttr($segmentsNamesMap[$option['id']]);
|
||||||
$html .= '</label>';
|
$html .= '</label>';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,4 +73,14 @@ class Segment {
|
|||||||
|
|
||||||
return $this->wrapper->render($block, $html);
|
return $this->wrapper->render($block, $html);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getSegmentsNames($values): array {
|
||||||
|
$ids = array_column($values, 'id');
|
||||||
|
$segments = $this->segmentsRepository->findBy(['id' => $ids]);
|
||||||
|
$namesMap = [];
|
||||||
|
foreach ($segments as $segment) {
|
||||||
|
$namesMap[$segment->getId()] = $segment->getName();
|
||||||
|
}
|
||||||
|
return $namesMap;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
19
lib/Segments/SegmentsRepository.php
Normal file
19
lib/Segments/SegmentsRepository.php
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MailPoet\Segments;
|
||||||
|
|
||||||
|
use MailPoet\Doctrine\Repository;
|
||||||
|
use MailPoet\Entities\SegmentEntity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @method SegmentEntity[] findBy(array $criteria, array $orderBy = null, int $limit = null, int $offset = null)
|
||||||
|
* @method SegmentEntity|null findOneBy(array $criteria, array $orderBy = null)
|
||||||
|
* @method SegmentEntity|null findOneById(mixed $id)
|
||||||
|
* @method void persist(SegmentEntity $entity)
|
||||||
|
* @method void remove(SegmentEntity $entity)
|
||||||
|
*/
|
||||||
|
class SegmentsRepository extends Repository {
|
||||||
|
protected function getEntityClassName() {
|
||||||
|
return SegmentEntity::class;
|
||||||
|
}
|
||||||
|
}
|
@@ -2,9 +2,11 @@
|
|||||||
|
|
||||||
namespace MailPoet\Test\Form\Block;
|
namespace MailPoet\Test\Form\Block;
|
||||||
|
|
||||||
|
use MailPoet\Entities\SegmentEntity;
|
||||||
use MailPoet\Form\Block\BlockRendererHelper;
|
use MailPoet\Form\Block\BlockRendererHelper;
|
||||||
use MailPoet\Form\Block\Segment;
|
use MailPoet\Form\Block\Segment;
|
||||||
use MailPoet\Form\BlockWrapperRenderer;
|
use MailPoet\Form\BlockWrapperRenderer;
|
||||||
|
use MailPoet\Segments\SegmentsRepository;
|
||||||
use MailPoet\Test\Form\HtmlParser;
|
use MailPoet\Test\Form\HtmlParser;
|
||||||
use MailPoet\WP\Functions as WPFunctions;
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
use PHPUnit\Framework\MockObject\MockObject;
|
use PHPUnit\Framework\MockObject\MockObject;
|
||||||
@@ -24,6 +26,9 @@ class SegmentTest extends \MailPoetUnitTest {
|
|||||||
/** @var MockObject & BlockWrapperRenderer */
|
/** @var MockObject & BlockWrapperRenderer */
|
||||||
private $wrapperMock;
|
private $wrapperMock;
|
||||||
|
|
||||||
|
/** @var MockObject & SegmentsRepository */
|
||||||
|
private $segmentsRepositoryMock;
|
||||||
|
|
||||||
/** @var HtmlParser */
|
/** @var HtmlParser */
|
||||||
private $htmlParser;
|
private $htmlParser;
|
||||||
|
|
||||||
@@ -36,11 +41,10 @@ class SegmentTest extends \MailPoetUnitTest {
|
|||||||
'params' => [
|
'params' => [
|
||||||
'label' => 'Select lists',
|
'label' => 'Select lists',
|
||||||
'values' => [[
|
'values' => [[
|
||||||
'name' => 'List 1',
|
'name' => 'Old ignored value',
|
||||||
'id' => '1',
|
'id' => '1',
|
||||||
'is_checked' => '1',
|
'is_checked' => '1',
|
||||||
], [
|
], [
|
||||||
'name' => 'List 2',
|
|
||||||
'id' => '2',
|
'id' => '2',
|
||||||
]],
|
]],
|
||||||
],
|
],
|
||||||
@@ -54,7 +58,8 @@ class SegmentTest extends \MailPoetUnitTest {
|
|||||||
$this->wrapperMock = $this->createMock(BlockWrapperRenderer::class);
|
$this->wrapperMock = $this->createMock(BlockWrapperRenderer::class);
|
||||||
$this->wrapperMock->method('render')->will($this->returnArgument(1));
|
$this->wrapperMock->method('render')->will($this->returnArgument(1));
|
||||||
$this->rendererHelperMock = $this->createMock(BlockRendererHelper::class);
|
$this->rendererHelperMock = $this->createMock(BlockRendererHelper::class);
|
||||||
$this->segment = new Segment($this->rendererHelperMock, $this->wrapperMock, $this->wpMock);
|
$this->segmentsRepositoryMock = $this->createMock(SegmentsRepository::class);
|
||||||
|
$this->segment = new Segment($this->rendererHelperMock, $this->wrapperMock, $this->wpMock, $this->segmentsRepositoryMock);
|
||||||
$this->htmlParser = new HtmlParser();
|
$this->htmlParser = new HtmlParser();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,6 +67,10 @@ class SegmentTest extends \MailPoetUnitTest {
|
|||||||
$this->rendererHelperMock->expects($this->once())->method('renderLabel')->willReturn('<label></label>');
|
$this->rendererHelperMock->expects($this->once())->method('renderLabel')->willReturn('<label></label>');
|
||||||
$this->rendererHelperMock->expects($this->once())->method('getInputValidation')->willReturn('validation="1"');
|
$this->rendererHelperMock->expects($this->once())->method('getInputValidation')->willReturn('validation="1"');
|
||||||
$this->rendererHelperMock->expects($this->once())->method('getFieldName')->willReturn('Segments');
|
$this->rendererHelperMock->expects($this->once())->method('getFieldName')->willReturn('Segments');
|
||||||
|
$this->segmentsRepositoryMock->expects($this->once())->method('findBy')->willReturn([
|
||||||
|
$this->createSegmentMock(1, 'List 1'),
|
||||||
|
$this->createSegmentMock(2, 'List 2'),
|
||||||
|
]);
|
||||||
|
|
||||||
$html = $this->segment->render($this->block, []);
|
$html = $this->segment->render($this->block, []);
|
||||||
|
|
||||||
@@ -78,4 +87,14 @@ class SegmentTest extends \MailPoetUnitTest {
|
|||||||
expect($this->htmlParser->getAttribute($checkbox2Input, 'name')->value)->equals('data[Segments][]');
|
expect($this->htmlParser->getAttribute($checkbox2Input, 'name')->value)->equals('data[Segments][]');
|
||||||
expect($this->htmlParser->getAttribute($checkbox1Input, 'checked')->value)->equals('checked');
|
expect($this->htmlParser->getAttribute($checkbox1Input, 'checked')->value)->equals('checked');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return MockObject & SegmentEntity
|
||||||
|
*/
|
||||||
|
private function createSegmentMock(int $id, string $name) {
|
||||||
|
$mock = $this->createMock(SegmentEntity::class);
|
||||||
|
$mock->method('getId')->willReturn($id);
|
||||||
|
$mock->method('getName')->willReturn($name);
|
||||||
|
return $mock;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user