From 565da8b52eaa01fe75883f02ef8df63bbd7ec42e Mon Sep 17 00:00:00 2001 From: Pavel Dohnal Date: Tue, 23 Jun 2020 11:15:18 +0200 Subject: [PATCH] Move Template Selection to a separate page [MAILPOET-2987] --- lib/AdminPages/Pages/FormEditor.php | 19 +++++++------------ lib/Config/Menu.php | 24 ++++++++++++++++++++++++ lib/Form/FormFactory.php | 4 ++++ views/form/template_selection.html | 2 +- views/forms.html | 2 +- 5 files changed, 37 insertions(+), 14 deletions(-) diff --git a/lib/AdminPages/Pages/FormEditor.php b/lib/AdminPages/Pages/FormEditor.php index edcda255a2..60d16fe995 100644 --- a/lib/AdminPages/Pages/FormEditor.php +++ b/lib/AdminPages/Pages/FormEditor.php @@ -38,7 +38,7 @@ class FormEditor { 'params' => ['label' => 'Email', 'class_name' => '', 'required' => '1'], 'id' => 'email', 'name' => 'Email', - 'styles' => ['full_width' => '1'] + 'styles' => ['full_width' => '1'], ], [ 'type' => 'text', @@ -47,7 +47,7 @@ class FormEditor { 'name' => 'First name', 'styles' => ['full_width' => '1'], ], - ] + ], ], [ 'type' => 'column', @@ -191,12 +191,12 @@ class FormEditor { } public function render() { - if (!isset($_GET['id']) && !isset($_GET['action'])) { + if (!isset($_GET['id']) && !isset($_GET['action'])) { $this->renderTemplateSelection(); return; } if (isset($_GET['action']) && $_GET['action'] === 'create') { - $this->createForm($_GET['template-id']); + $this->createForm(); } $form = Form::findOne((int)$_GET['id']); if ($form instanceof Form) { @@ -233,7 +233,7 @@ class FormEditor { $this->pageRenderer->displayPage('form/editor.html', $data); } - private function renderTemplateSelection() { + public function renderTemplateSelection() { $templates = array_values(self::TEMPLATES); if (empty($templates) || !$this->flagsController->isSupported(FeaturesController::TEMPLATES_SELECTION)) { $this->createForm(); @@ -244,13 +244,8 @@ class FormEditor { $this->pageRenderer->displayPage('form/template_selection.html', $data); } - private function createForm($templateId = null) { - if (!is_null($templateId) && isset(self::TEMPLATES[$templateId])) { - $form = $this->formsFactory->createFormFromTemplate(self::TEMPLATES[$templateId]); - } - if (!isset($form)) { - $form = $this->formsFactory->createEmptyForm(); - } + private function createForm() { + $form = $this->formsFactory->createEmptyForm(); $this->wp->wpSafeRedirect( $this->wp->getSiteUrl(null, diff --git a/lib/Config/Menu.php b/lib/Config/Menu.php index 96ab11e714..4094c0ae2e 100644 --- a/lib/Config/Menu.php +++ b/lib/Config/Menu.php @@ -188,6 +188,26 @@ class Menu { }); }); + // form editor templates + $formTemplateSelectionEditorPage = $this->wp->addSubmenuPage( + true, + $this->setPageTitle(__('Form Editor', 'mailpoet')), + $this->wp->__('Form Editor', 'mailpoet'), + AccessControl::PERMISSION_MANAGE_FORMS, + 'mailpoet-form-editor-template-selection', + [ + $this, + 'formEditorTemplateSelection', + ] + ); + + // add body class for form editor page + $this->wp->addAction('load-' . $formTemplateSelectionEditorPage, function() { + $this->wp->addAction('admin_body_class', function ($classes) { + return ltrim($classes . ' block-editor-page'); + }); + }); + // Subscribers page $subscribersPage = $this->wp->addSubmenuPage( @@ -454,6 +474,10 @@ class Menu { $this->container->get(FormEditor::class)->render(); } + public function formEditorTemplateSelection() { + $this->container->get(FormEditor::class)->renderTemplateSelection(); + } + public function setPageTitle($title) { return sprintf( '%s - %s', diff --git a/lib/Form/FormFactory.php b/lib/Form/FormFactory.php index e6889e1465..c30e01cd6a 100644 --- a/lib/Form/FormFactory.php +++ b/lib/Form/FormFactory.php @@ -5,6 +5,10 @@ namespace MailPoet\Form; use MailPoet\Models\Form; class FormFactory { + /** + * @param array $template + * @return Form + */ public function createFormFromTemplate(array $template) { if (isset($template['id'])) { unset($template['id']); diff --git a/views/form/template_selection.html b/views/form/template_selection.html index 7b206b89ea..273d5625ae 100644 --- a/views/form/template_selection.html +++ b/views/form/template_selection.html @@ -21,7 +21,7 @@ <% autoescape 'js' %> var mailpoet_templates = <%= json_encode(templates) %>; var mailpoet_form_edit_url = - "<%= admin_url('admin.php?page=mailpoet-form-editor&action=create&template-id=') %>"; + "<%= admin_url('admin.php?page=mailpoet-form-editor&id=') %>"; <% endautoescape %> diff --git a/views/forms.html b/views/forms.html index b83db1e297..a772b45a56 100644 --- a/views/forms.html +++ b/views/forms.html @@ -16,7 +16,7 @@ var mailpoet_listing_per_page = <%= items_per_page %>; var mailpoet_segments = <%= json_encode(segments) %>; var mailpoet_form_edit_url = - "<%= admin_url('admin.php?page=mailpoet-form-editor') %>"; + "<%= admin_url('admin.php?page=mailpoet-form-editor-template-selection') %>"; var mailpoet_beacon_articles = [ '5e43d3ec2c7d3a7e9ae79da9', '5e3a166204286364bc94dda4',