Create a new form for the user
[MAILPOET-1798]
This commit is contained in:
committed by
Jack Kitterhing
parent
3443f082ef
commit
0fd954184d
@ -7,7 +7,6 @@
|
||||
@import '../../../node_modules/@wordpress/block-library/build-style/editor';
|
||||
@import '../../../node_modules/codemirror/lib/codemirror';
|
||||
@import '../../../node_modules/codemirror/theme/neo';
|
||||
|
||||
@import './components/formEditor/components/form_title';
|
||||
@import './components/formEditor/components/sidebar';
|
||||
@import './components/formEditor/components/block_editor';
|
||||
|
@ -46,7 +46,7 @@ function Edit({ attributes, setAttributes }) {
|
||||
return (
|
||||
<div className="mailpoet-block-create-new-content">
|
||||
<a
|
||||
href="admin.php?page=mailpoet-forms"
|
||||
href="admin.php?page=mailpoet-form-editor&action=create"
|
||||
target="_blank"
|
||||
className="mailpoet-block-create-new-link"
|
||||
>
|
||||
|
@ -6,6 +6,7 @@ use MailPoet\API\JSON\Endpoint as APIEndpoint;
|
||||
use MailPoet\API\JSON\Error as APIError;
|
||||
use MailPoet\Config\AccessControl;
|
||||
use MailPoet\Form\DisplayFormInWPContent;
|
||||
use MailPoet\Form\FormFactory;
|
||||
use MailPoet\Form\Renderer as FormRenderer;
|
||||
use MailPoet\Form\Util;
|
||||
use MailPoet\Listing;
|
||||
@ -31,6 +32,9 @@ class Forms extends APIEndpoint {
|
||||
/** @var FormRenderer */
|
||||
private $formRenderer;
|
||||
|
||||
/** @var FormFactory */
|
||||
private $formFactory;
|
||||
|
||||
public $permissions = [
|
||||
'global' => AccessControl::PERMISSION_MANAGE_FORMS,
|
||||
];
|
||||
@ -40,13 +44,15 @@ class Forms extends APIEndpoint {
|
||||
Listing\Handler $listingHandler,
|
||||
Util\Styles $formStylesUtils,
|
||||
UserFlagsController $userFlags,
|
||||
FormRenderer $formRenderer
|
||||
FormRenderer $formRenderer,
|
||||
FormFactory $formFactory
|
||||
) {
|
||||
$this->bulkAction = $bulkAction;
|
||||
$this->listingHandler = $listingHandler;
|
||||
$this->formStylesUtils = $formStylesUtils;
|
||||
$this->userFlags = $userFlags;
|
||||
$this->formRenderer = $formRenderer;
|
||||
$this->formFactory = $formFactory;
|
||||
}
|
||||
|
||||
public function get($data = []) {
|
||||
@ -86,43 +92,10 @@ class Forms extends APIEndpoint {
|
||||
}
|
||||
|
||||
public function create() {
|
||||
// create new form
|
||||
$formData = [
|
||||
'name' => '',
|
||||
'body' => [
|
||||
[
|
||||
'id' => 'email',
|
||||
'name' => WPFunctions::get()->__('Email', 'mailpoet'),
|
||||
'type' => 'text',
|
||||
'static' => true,
|
||||
'params' => [
|
||||
'label' => WPFunctions::get()->__('Email', 'mailpoet'),
|
||||
'required' => true,
|
||||
'label_within' => true,
|
||||
],
|
||||
],
|
||||
[
|
||||
'id' => 'submit',
|
||||
'name' => WPFunctions::get()->__('Submit', 'mailpoet'),
|
||||
'type' => 'submit',
|
||||
'static' => true,
|
||||
'params' => [
|
||||
'label' => WPFunctions::get()->__('Subscribe!', 'mailpoet'),
|
||||
],
|
||||
],
|
||||
],
|
||||
'settings' => [
|
||||
'on_success' => 'message',
|
||||
'success_message' => Form::getDefaultSuccessMessage(),
|
||||
'segments' => null,
|
||||
'segments_selected_by' => 'admin',
|
||||
],
|
||||
];
|
||||
return $this->save($formData);
|
||||
return $this->save($this->formFactory->createEmptyForm());
|
||||
}
|
||||
|
||||
public function save($data = []) {
|
||||
$form = Form::createOrUpdate($data);
|
||||
public function save(Form $form) {
|
||||
$errors = $form->getErrors();
|
||||
|
||||
if (empty($errors)) {
|
||||
|
@ -6,11 +6,13 @@ use MailPoet\AdminPages\PageRenderer;
|
||||
use MailPoet\API\JSON\ResponseBuilders\CustomFieldsResponseBuilder;
|
||||
use MailPoet\CustomFields\CustomFieldsRepository;
|
||||
use MailPoet\Form\Block;
|
||||
use MailPoet\Form\FormFactory;
|
||||
use MailPoet\Form\Renderer as FormRenderer;
|
||||
use MailPoet\Form\Util\Export;
|
||||
use MailPoet\Models\Form;
|
||||
use MailPoet\Models\Segment;
|
||||
use MailPoet\Settings\Pages;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
class FormEditor {
|
||||
/** @var PageRenderer */
|
||||
@ -28,22 +30,44 @@ class FormEditor {
|
||||
/** @var Block\Date */
|
||||
private $dateBlock;
|
||||
|
||||
/** @var WPFunctions */
|
||||
private $wp;
|
||||
|
||||
/**
|
||||
* @var FormFactory
|
||||
*/
|
||||
private $formsFactory;
|
||||
|
||||
public function __construct(
|
||||
PageRenderer $pageRenderer,
|
||||
CustomFieldsRepository $customFieldsRepository,
|
||||
CustomFieldsResponseBuilder $customFieldsResponseBuilder,
|
||||
FormRenderer $formRenderer,
|
||||
Block\Date $dateBlock
|
||||
Block\Date $dateBlock,
|
||||
WPFunctions $wp,
|
||||
FormFactory $formsFactory
|
||||
) {
|
||||
$this->pageRenderer = $pageRenderer;
|
||||
$this->customFieldsRepository = $customFieldsRepository;
|
||||
$this->customFieldsResponseBuilder = $customFieldsResponseBuilder;
|
||||
$this->formRenderer = $formRenderer;
|
||||
$this->dateBlock = $dateBlock;
|
||||
$this->wp = $wp;
|
||||
$this->formsFactory = $formsFactory;
|
||||
}
|
||||
|
||||
public function render() {
|
||||
$id = (isset($_GET['id']) ? (int)$_GET['id'] : 0);
|
||||
if (isset($_GET['action']) && $_GET['action'] === 'create') {
|
||||
$form = $this->formsFactory->createEmptyForm();
|
||||
|
||||
$this->wp->wpSafeRedirect(
|
||||
$this->wp->getSiteUrl(null,
|
||||
'/wp-admin/admin.php?page=mailpoet-form-editor&id=' . $form->id()
|
||||
)
|
||||
);
|
||||
exit;
|
||||
}
|
||||
$form = Form::findOne($id);
|
||||
if ($form instanceof Form) {
|
||||
$form = $form->asArray();
|
||||
|
@ -185,6 +185,7 @@ class ContainerConfigurator implements IContainerConfigurator {
|
||||
$container->autowire(\MailPoet\Form\Block\Submit::class);
|
||||
$container->autowire(\MailPoet\Form\Block\Text::class);
|
||||
$container->autowire(\MailPoet\Form\Block\Textarea::class);
|
||||
$container->autowire(\MailPoet\Form\FormFactory::class);
|
||||
$container->autowire(\MailPoet\Form\Util\Styles::class);
|
||||
// Helpscout
|
||||
$container->autowire(\MailPoet\Helpscout\Beacon::class);
|
||||
|
45
lib/Form/FormFactory.php
Normal file
45
lib/Form/FormFactory.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\Form;
|
||||
|
||||
use MailPoet\Models\Form;
|
||||
|
||||
class FormFactory {
|
||||
|
||||
/** @return Form */
|
||||
public function createEmptyForm() {
|
||||
$data = [
|
||||
'name' => '',
|
||||
'body' => [
|
||||
[
|
||||
'id' => 'email',
|
||||
'name' => __('Email', 'mailpoet'),
|
||||
'type' => 'text',
|
||||
'static' => true,
|
||||
'params' => [
|
||||
'label' => __('Email', 'mailpoet'),
|
||||
'required' => true,
|
||||
'label_within' => true,
|
||||
],
|
||||
],
|
||||
[
|
||||
'id' => 'submit',
|
||||
'name' => __('Submit', 'mailpoet'),
|
||||
'type' => 'submit',
|
||||
'static' => true,
|
||||
'params' => [
|
||||
'label' => __('Subscribe!', 'mailpoet'),
|
||||
],
|
||||
],
|
||||
],
|
||||
'settings' => [
|
||||
'on_success' => 'message',
|
||||
'success_message' => Form::getDefaultSuccessMessage(),
|
||||
'segments' => null,
|
||||
'segments_selected_by' => 'admin',
|
||||
],
|
||||
];
|
||||
return Form::createOrUpdate($data);
|
||||
}
|
||||
|
||||
}
|
@ -71,7 +71,7 @@ class FormsTest extends \MailPoetTest {
|
||||
'name' => 'My First Form',
|
||||
];
|
||||
|
||||
$response = $this->endpoint->save($formData);
|
||||
$response = $this->endpoint->save(Form::createOrUpdate($formData));
|
||||
expect($response->status)->equals(APIResponse::STATUS_OK);
|
||||
expect($response->data)->equals(
|
||||
Form::where('name', 'My First Form')->findOne()->asArray()
|
||||
|
Reference in New Issue
Block a user