Rename block attribute

[MAILPOET-1798]
This commit is contained in:
Pavel Dohnal
2020-02-06 13:41:13 +01:00
committed by Jack Kitterhing
parent 831a865c9e
commit 62c5a8cc84
3 changed files with 19 additions and 42 deletions

View File

@ -17,11 +17,11 @@ function Edit({ attributes, setAttributes }) {
<select
onChange={(event) => {
setAttributes({
selectedForm: parseInt(event.target.value, 10),
formId: parseInt(event.target.value, 10),
});
}}
className="mailpoet-block-create-forms-list"
value={attributes.selectedForm}
value={attributes.formId}
>
<option value="" disabled selected>{window.locale.selectForm}</option>
{allForms.map((form) => (
@ -36,8 +36,8 @@ function Edit({ attributes, setAttributes }) {
function renderForm() {
return (
<ServerSideRender
block="mailpoet/form-block-render"
attributes={{ form: attributes.selectedForm }}
block="mailpoet/subscription-form-block-render"
attributes={{ form: attributes.formId }}
/>
);
}
@ -66,7 +66,7 @@ function Edit({ attributes, setAttributes }) {
</InspectorControls>
<div className="mailpoet-block-div">
{
attributes.selectedForm === null && (
attributes.formId === null && (
<Placeholder
className="mailpoet-block-create-new"
icon={<BlockIcon icon={Icon} showColors />}
@ -77,7 +77,7 @@ function Edit({ attributes, setAttributes }) {
)
}
{
attributes.selectedForm !== null && (
attributes.formId !== null && (
renderForm()
)
}
@ -88,7 +88,7 @@ function Edit({ attributes, setAttributes }) {
Edit.propTypes = {
attributes: PropTypes.shape({
selectedForm: PropTypes.number,
formId: PropTypes.number,
}).isRequired,
setAttributes: PropTypes.func.isRequired,
};

View File

@ -10,7 +10,7 @@ registerBlockType('mailpoet/form-block', {
category: 'widgets',
example: {},
attributes: {
selectedForm: {
formId: {
type: 'number',
default: null,
},

View File

@ -1,13 +1,15 @@
<?php
namespace MailPoet\Config;
namespace MailPoet\PostEditorBlocks;
use MailPoet\Config\Env;
use MailPoet\Config\Renderer;
use MailPoet\Entities\FormEntity;
use MailPoet\Form\FormsRepository;
use MailPoet\Form\Widget;
use MailPoet\WP\Functions as WPFunctions;
class PostEditorBlock {
class SubscriptionFormBlock {
/** @var Renderer */
private $renderer;
@ -28,16 +30,7 @@ class PostEditorBlock {
}
public function init() {
// this has to be here until we drop support for WordPress < 5.0
if (!function_exists('register_block_type')) return;
if (is_admin()) {
$this->initAdmin();
} else {
$this->initFrontend();
}
$this->wp->registerBlockType('mailpoet/form-block-render', [
$this->wp->registerBlockType('mailpoet/subscription-form-block-render', [
'attributes' => [
'form' => [
'type' => 'number',
@ -48,25 +41,10 @@ class PostEditorBlock {
]);
}
private function initAdmin() {
$this->wp->wpEnqueueScript(
'mailpoet-block-form-block-js',
Env::$assetsUrl . '/dist/js/' . $this->renderer->getJsAsset('post_editor_block.js'),
['wp-blocks', 'wp-components', 'wp-server-side-render', 'wp-block-editor'],
Env::$version,
true
);
$this->wp->wpEnqueueStyle(
'mailpoetblock-form-block-css',
Env::$assetsUrl . '/dist/css/' . $this->renderer->getCssAsset('post-editor-block.css'),
['wp-edit-blocks'],
Env::$version
);
$this->wp->registerBlockType('mailpoet/form-block', [
public function initAdmin() {
$this->wp->registerBlockType('mailpoet/subscription-form-block', [
'style' => 'mailpoetblock-form-block-css',
'editor_script' => 'mailpoet/form-block',
'editor_script' => 'mailpoet/subscription-form-block',
]);
$this->wp->addAction('admin_head', function() {
@ -86,13 +64,13 @@ class PostEditorBlock {
});
}
private function initFrontend() {
$this->wp->registerBlockType('mailpoet/form-block', [
public function initFrontend() {
$this->wp->registerBlockType('mailpoet/subscription-form-block', [
'render_callback' => [$this, 'renderForm'],
]);
}
public function renderForm($attributes) {
public function renderForm(array $attributes = []): string {
if (!$attributes || !isset($attributes['form'])) {
return '';
}
@ -102,5 +80,4 @@ class PostEditorBlock {
'form_type' => 'html',
]);
}
}