Append form bellow content
[MAILPOET-2639]
This commit is contained in:
committed by
Jack Kitterhing
parent
d667ad916e
commit
d06fc2d00d
@ -3,6 +3,7 @@
|
||||
namespace MailPoet\Config;
|
||||
|
||||
use MailPoet\DynamicSegments\DynamicSegmentHooks;
|
||||
use MailPoet\Form\DisplayFormInWPContent;
|
||||
use MailPoet\Mailer\WordPress\Replacer;
|
||||
use MailPoet\Mailer\WordPress\WordpressMailerReplacer;
|
||||
use MailPoet\Newsletter\Scheduler\PostNotificationScheduler;
|
||||
@ -58,6 +59,9 @@ class Hooks {
|
||||
/** @var DynamicSegmentHooks */
|
||||
private $dynamicSegmentHooks;
|
||||
|
||||
/** @var DisplayFormInWPContent */
|
||||
private $displayFormInWPContent;
|
||||
|
||||
public function __construct(
|
||||
Form $subscriptionForm,
|
||||
Comment $subscriptionComment,
|
||||
@ -71,6 +75,7 @@ class Hooks {
|
||||
WooCommercePurchases $woocommercePurchases,
|
||||
PostNotificationScheduler $postNotificationScheduler,
|
||||
WordpressMailerReplacer $wordpressMailerReplacer,
|
||||
DisplayFormInWPContent $displayFormInWPContent,
|
||||
DynamicSegmentHooks $dynamicSegmentHooks
|
||||
) {
|
||||
$this->subscriptionForm = $subscriptionForm;
|
||||
@ -86,6 +91,7 @@ class Hooks {
|
||||
$this->postNotificationScheduler = $postNotificationScheduler;
|
||||
$this->wordpressMailerReplacer = $wordpressMailerReplacer;
|
||||
$this->dynamicSegmentHooks = $dynamicSegmentHooks;
|
||||
$this->displayFormInWPContent = $displayFormInWPContent;
|
||||
}
|
||||
|
||||
public function init() {
|
||||
@ -195,6 +201,10 @@ class Hooks {
|
||||
'admin_post_nopriv_mailpoet_subscription_form',
|
||||
[$this->subscriptionForm, 'onSubmit']
|
||||
);
|
||||
$this->wp->addFilter(
|
||||
'the_content',
|
||||
[$this->displayFormInWPContent, 'display']
|
||||
);
|
||||
}
|
||||
|
||||
public function setupMailer() {
|
||||
|
@ -168,6 +168,7 @@ class ContainerConfigurator implements IContainerConfigurator {
|
||||
// Form
|
||||
$container->autowire(\MailPoet\Form\Util\FieldNameObfuscator::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\Form\AssetsController::class);
|
||||
$container->autowire(\MailPoet\Form\DisplayFormInWPContent::class);
|
||||
$container->autowire(\MailPoet\Form\FormsRepository::class);
|
||||
$container->autowire(\MailPoet\Form\Util\Styles::class);
|
||||
// Helpscout
|
||||
|
56
lib/Form/DisplayFormInWPContent.php
Normal file
56
lib/Form/DisplayFormInWPContent.php
Normal file
@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\Form;
|
||||
|
||||
use MailPoet\Entities\FormEntity;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
class DisplayFormInWPContent {
|
||||
|
||||
/** @var WPFunctions */
|
||||
private $wp;
|
||||
|
||||
/** @var FormsRepository */
|
||||
private $formsRepository;
|
||||
|
||||
public function __construct(WPFunctions $wp, FormsRepository $formsRepository) {
|
||||
$this->wp = $wp;
|
||||
$this->formsRepository = $formsRepository;
|
||||
}
|
||||
|
||||
// TODO remove transient in the api on form save
|
||||
|
||||
public function display(string $content): string {
|
||||
$result = $content;
|
||||
if (!$this->wp->isSingle()) return $result;
|
||||
$forms = $this->formsRepository->findAll();
|
||||
foreach ($forms as $form) {
|
||||
$result .= $this->getContentBellow($form);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function getContentBellow(FormEntity $form): string {
|
||||
if (!$this->shouldDisplayFormBellowContent($form)) return '';
|
||||
return Renderer::render([
|
||||
'body' => $form->getBody(),
|
||||
'styles' => $form->getStyles(),
|
||||
]);
|
||||
}
|
||||
|
||||
private function shouldDisplayFormBellowContent(FormEntity $form): bool {
|
||||
$settings = $form->getSettings();
|
||||
if (!is_array($settings)) return false;
|
||||
if (!isset($settings['placeFormBellowAllPosts'])) return false;
|
||||
if (
|
||||
($settings['placeFormBellowAllPosts'] === '1')
|
||||
&& !$this->wp->isPage()
|
||||
) return true;
|
||||
if (
|
||||
($settings['placeFormBellowAllPages'] === '1')
|
||||
&& $this->wp->isPage()
|
||||
) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
167
tests/unit/Form/DisplayFormInWPContentTest.php
Normal file
167
tests/unit/Form/DisplayFormInWPContentTest.php
Normal file
@ -0,0 +1,167 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\Form;
|
||||
|
||||
use MailPoet\Entities\FormEntity;
|
||||
use MailPoet\Settings\SettingsController;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
class DisplayFormInWPContentTest extends \MailPoetUnitTest {
|
||||
|
||||
/** @var FormsRepository|\PHPUnit_Framework_MockObject_MockObject */
|
||||
private $repository;
|
||||
|
||||
/** @var WPFunctions|\PHPUnit_Framework_MockObject_MockObject */
|
||||
private $wp;
|
||||
|
||||
/** @var DisplayFormInWPContent */
|
||||
private $hook;
|
||||
|
||||
public function _before() {
|
||||
parent::_before();
|
||||
// settings is needed by renderer
|
||||
$settings = $this->createMock(SettingsController::class);
|
||||
SettingsController::setInstance($settings);
|
||||
|
||||
$this->repository = $this->createMock(FormsRepository::class);
|
||||
$this->wp = $this->createMock(WPFunctions::class);
|
||||
$this->hook = new DisplayFormInWPContent($this->wp, $this->repository);
|
||||
}
|
||||
|
||||
public function testAppendsRenderedFormAfterPostContent() {
|
||||
$this->wp->expects($this->once())->method('isSingle')->willReturn(true);
|
||||
$this->wp->expects($this->any())->method('isPage')->willReturn(false);
|
||||
$form = new FormEntity('My Form');
|
||||
$form->setSettings([
|
||||
'segments' => ['3'],
|
||||
'placeFormBellowAllPages' => '',
|
||||
'placeFormBellowAllPosts' => '1',
|
||||
]);
|
||||
$form->setBody([[
|
||||
'type' => 'submit',
|
||||
'params' => ['label' => 'Subscribe!'],
|
||||
'id' => 'submit',
|
||||
'name' => 'Submit',
|
||||
]]);
|
||||
$this->repository->expects($this->once())->method('findAll')->willReturn([$form]);
|
||||
|
||||
$result = $this->hook->display('content');
|
||||
expect($result)->notEquals('content');
|
||||
expect($result)->regExp('/content.*input type="submit"/is');
|
||||
}
|
||||
|
||||
public function testDoesNotAppendFormIfDisabled() {
|
||||
$this->wp->expects($this->once())->method('isSingle')->willReturn(true);
|
||||
$this->wp->expects($this->any())->method('isPage')->willReturn(false);
|
||||
$form = new FormEntity('My Form');
|
||||
$form->setSettings([
|
||||
'segments' => ['3'],
|
||||
'placeFormBellowAllPages' => '',
|
||||
'placeFormBellowAllPosts' => '',
|
||||
]);
|
||||
$form->setBody([[
|
||||
'type' => 'submit',
|
||||
'params' => ['label' => 'Subscribe!'],
|
||||
'id' => 'submit',
|
||||
'name' => 'Submit',
|
||||
]]);
|
||||
$this->repository->expects($this->once())->method('findAll')->willReturn([$form]);
|
||||
|
||||
$result = $this->hook->display('content');
|
||||
expect($result)->equals('content');
|
||||
}
|
||||
|
||||
public function testAppendsMultipleRenderedFormAfterPostContent() {
|
||||
$this->wp->expects($this->once())->method('isSingle')->willReturn(true);
|
||||
$this->wp->expects($this->any())->method('isPage')->willReturn(false);
|
||||
$form1 = new FormEntity('My Form');
|
||||
$form1->setSettings([
|
||||
'segments' => ['3'],
|
||||
'placeFormBellowAllPages' => '',
|
||||
'placeFormBellowAllPosts' => '1',
|
||||
]);
|
||||
$form1->setBody([[
|
||||
'type' => 'submit',
|
||||
'params' => ['label' => 'Subscribe1'],
|
||||
'id' => 'submit',
|
||||
'name' => 'Submit',
|
||||
]]);
|
||||
$form2 = new FormEntity('My Form');
|
||||
$form2->setSettings([
|
||||
'segments' => ['3'],
|
||||
'placeFormBellowAllPages' => '',
|
||||
'placeFormBellowAllPosts' => '1',
|
||||
]);
|
||||
$form2->setBody([[
|
||||
'type' => 'submit',
|
||||
'params' => ['label' => 'Subscribe2'],
|
||||
'id' => 'submit',
|
||||
'name' => 'Submit',
|
||||
]]);
|
||||
$this->repository->expects($this->once())->method('findAll')->willReturn([$form1, $form2]);
|
||||
|
||||
$result = $this->hook->display('content');
|
||||
expect($result)->notEquals('content');
|
||||
expect($result)->regExp('/content.*input.*value="Subscribe1".*input.*value="Subscribe2"/is');
|
||||
}
|
||||
|
||||
public function testDoesNotAppendFormIfNotOnSinglePage() {
|
||||
$this->wp->expects($this->once())->method('isSingle')->willReturn(false);
|
||||
$this->repository->expects($this->never())->method('findAll');
|
||||
|
||||
$result = $this->hook->display('content');
|
||||
expect($result)->equals('content');
|
||||
}
|
||||
|
||||
public function testDoesNotAppendFormIfNotOnPost() {
|
||||
$this->wp->expects($this->once())->method('isSingle')->willReturn(true);
|
||||
$this->wp->expects($this->once())->method('isPage')->willReturn(true);
|
||||
$form = new FormEntity('My Form');
|
||||
$form->setSettings([
|
||||
'segments' => ['3'],
|
||||
'placeFormBellowAllPages' => '',
|
||||
'placeFormBellowAllPosts' => '1',
|
||||
]);
|
||||
$form->setBody([[
|
||||
'type' => 'submit',
|
||||
'params' => ['label' => 'Subscribe!'],
|
||||
'id' => 'submit',
|
||||
'name' => 'Submit',
|
||||
]]);
|
||||
$this->repository->expects($this->once())->method('findAll')->willReturn([$form]);
|
||||
|
||||
$result = $this->hook->display('content');
|
||||
expect($result)->equals('content');
|
||||
}
|
||||
|
||||
public function testAppendsRenderedFormAfterPageContent() {
|
||||
$this->wp->expects($this->once())->method('isSingle')->willReturn(true);
|
||||
$this->wp->expects($this->any())->method('isPage')->willReturn(true);
|
||||
$form = new FormEntity('My Form');
|
||||
$form->setSettings([
|
||||
'segments' => ['3'],
|
||||
'placeFormBellowAllPages' => '1',
|
||||
'placeFormBellowAllPosts' => '',
|
||||
]);
|
||||
$form->setBody([[
|
||||
'type' => 'submit',
|
||||
'params' => ['label' => 'Subscribe!'],
|
||||
'id' => 'submit',
|
||||
'name' => 'Submit',
|
||||
]]);
|
||||
$this->repository->expects($this->once())->method('findAll')->willReturn([$form]);
|
||||
|
||||
$result = $this->hook->display('content');
|
||||
expect($result)->notEquals('content');
|
||||
expect($result)->regExp('/content.*input type="submit"/is');
|
||||
}
|
||||
|
||||
public function testSetsTransientToImprovePerformance() {
|
||||
|
||||
}
|
||||
|
||||
public function testDoesNotQueryDatabaseIfTransientIsSet() {
|
||||
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user