diff --git a/mailpoet/lib/Subscription/ManageSubscriptionFormRenderer.php b/mailpoet/lib/Subscription/ManageSubscriptionFormRenderer.php index 705ac158d0..e76deaf0fd 100644 --- a/mailpoet/lib/Subscription/ManageSubscriptionFormRenderer.php +++ b/mailpoet/lib/Subscription/ManageSubscriptionFormRenderer.php @@ -65,7 +65,23 @@ class ManageSubscriptionFormRenderer { $this->subscribersRepository = $subscribersRepository; } - public function renderForm(Subscriber $subscriber, string $formState = self::FORM_STATE_NOT_SUBMITTED): string { + public function renderForm(SubscriberEntity $subscriberEntity, string $formState = self::FORM_STATE_NOT_SUBMITTED): string { + // Once the old Subscriber model is removed from this class, the if below can be removed as well and $subscriberEntity + // can be used directly (probably worth renaming it to $subscriber). For now this if is needed, as when previewing the + // manage subscription page, a demo object is created in Pages::getManageContent() that is not persisted to the database + // so $subscriberEntity->getId() is null and thus it is not possible to use Subscriber::findOne() to get the old model. + if ($subscriberEntity->getId()) { + $subscriber = Subscriber::findOne($subscriberEntity->getId())->withSubscriptions()->withCustomFields(); + } else { + $subscriber = Subscriber::create(); + $subscriber->hydrate([ + 'email' => $subscriberEntity->getEmail(), + 'first_name' => $subscriberEntity->getFirstName(), + 'last_name' => $subscriberEntity->getLastName(), + 'link_token' => $subscriberEntity->getLinkToken(), + ]); + } + $basicFields = $this->getBasicFields($subscriber); $customFields = $this->getCustomFields($subscriber); $segmentField = $this->getSegmentField($subscriber); diff --git a/mailpoet/lib/Subscription/Pages.php b/mailpoet/lib/Subscription/Pages.php index a5b1ba8b3e..596ea9e8b0 100644 --- a/mailpoet/lib/Subscription/Pages.php +++ b/mailpoet/lib/Subscription/Pages.php @@ -8,7 +8,6 @@ use MailPoet\Entities\StatisticsUnsubscribeEntity; use MailPoet\Entities\SubscriberEntity; use MailPoet\Features\FeaturesController; use MailPoet\Form\AssetsController; -use MailPoet\Models\Subscriber; use MailPoet\Newsletter\Scheduler\WelcomeScheduler; use MailPoet\Settings\SettingsController; use MailPoet\Settings\TrackingConfig; @@ -416,18 +415,13 @@ class Pages { public function getManageContent() { if ($this->isPreview()) { - $subscriber = Subscriber::create(); - $subscriber->hydrate([ - 'email' => self::DEMO_EMAIL, - 'first_name' => 'John', - 'last_name' => 'Doe', - 'link_token' => 'bfd0889dbc7f081e171fa0cee7401df2', - ]); + $subscriber = new SubscriberEntity(); + $subscriber->setEmail(self::DEMO_EMAIL); + $subscriber->setFirstName('John'); + $subscriber->setLastName('Doe'); + $subscriber->setLinkToken('bfd0889dbc7f081e171fa0cee7401df2'); } else if ($this->subscriber !== null) { - $subscriberModel = Subscriber::findOne($this->subscriber->getId()); - $subscriber = $subscriberModel - ->withCustomFields() - ->withSubscriptions(); + $subscriber = $this->subscriber; } else { return $this->wp->__('Subscription management form is only available to mailing lists subscribers.', 'mailpoet'); } diff --git a/mailpoet/tests/integration/Subscription/ManageSubscriptionFormRendererTest.php b/mailpoet/tests/integration/Subscription/ManageSubscriptionFormRendererTest.php index edff25dc88..c38cb9597f 100644 --- a/mailpoet/tests/integration/Subscription/ManageSubscriptionFormRendererTest.php +++ b/mailpoet/tests/integration/Subscription/ManageSubscriptionFormRendererTest.php @@ -7,7 +7,6 @@ use MailPoet\Entities\SegmentEntity; use MailPoet\Entities\SubscriberCustomFieldEntity; use MailPoet\Entities\SubscriberEntity; use MailPoet\Entities\SubscriberSegmentEntity; -use MailPoet\Models\Subscriber; use MailPoet\Test\DataFactories\CustomField as CustomFieldFactory; use MailPoet\WP\Functions as WPFunctions; @@ -23,7 +22,7 @@ class ManageSubscriptionFormRendererTest extends \MailPoetTest { public function testItGeneratesForm() { $subscriber = $this->getSubscriber($this->getSegment()); - $form = $this->formRenderer->renderForm(Subscriber::findOne($subscriber->getId())->withSubscriptions()->withCustomFields()); + $form = $this->formRenderer->renderForm($subscriber); expect($form)->regExp('/
/'); expect($form)->stringContainsString(''); expect($form)->regExp('//'); @@ -50,7 +49,7 @@ class ManageSubscriptionFormRendererTest extends \MailPoetTest { ]; return $fields; }); - $form = $this->formRenderer->renderForm(Subscriber::findOne($subscriber->getId())->withSubscriptions()->withCustomFields()); + $form = $this->formRenderer->renderForm($subscriber); expect($form)->regExp('//'); }