Refactor block registering
[MAILPOET-1798]
This commit is contained in:
committed by
Jack Kitterhing
parent
f4b9836332
commit
fd2c13a2e3
66
lib/PostEditorBlocks/PostEditorBlock.php
Normal file
66
lib/PostEditorBlocks/PostEditorBlock.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
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 {
|
||||
/** @var Renderer */
|
||||
private $renderer;
|
||||
|
||||
/** @var WPFunctions */
|
||||
private $wp;
|
||||
|
||||
/** @var SubscriptionFormBlock */
|
||||
private $subscriptionFormBlock;
|
||||
|
||||
public function __construct(
|
||||
Renderer $renderer,
|
||||
WPFunctions $wp,
|
||||
SubscriptionFormBlock $subscriptionFormBlock
|
||||
) {
|
||||
$this->renderer = $renderer;
|
||||
$this->wp = $wp;
|
||||
$this->subscriptionFormBlock = $subscriptionFormBlock;
|
||||
}
|
||||
|
||||
public function init() {
|
||||
// this has to be here until we drop support for WordPress < 5.0
|
||||
if (!function_exists('register_block_type')) return;
|
||||
$this->subscriptionFormBlock->init();
|
||||
|
||||
if (is_admin()) {
|
||||
$this->initAdmin();
|
||||
} else {
|
||||
$this->initFrontend();
|
||||
}
|
||||
}
|
||||
|
||||
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->subscriptionFormBlock->initAdmin();
|
||||
}
|
||||
|
||||
private function initFrontend() {
|
||||
$this->subscriptionFormBlock->initFrontend();
|
||||
}
|
||||
}
|
83
lib/PostEditorBlocks/SubscriptionFormBlock.php
Normal file
83
lib/PostEditorBlocks/SubscriptionFormBlock.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
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 SubscriptionFormBlock {
|
||||
/** @var Renderer */
|
||||
private $renderer;
|
||||
|
||||
/** @var WPFunctions */
|
||||
private $wp;
|
||||
|
||||
/** @var FormsRepository */
|
||||
private $formsRepository;
|
||||
|
||||
public function __construct(
|
||||
Renderer $renderer,
|
||||
WPFunctions $wp,
|
||||
FormsRepository $formsRepository
|
||||
) {
|
||||
$this->renderer = $renderer;
|
||||
$this->wp = $wp;
|
||||
$this->formsRepository = $formsRepository;
|
||||
}
|
||||
|
||||
public function init() {
|
||||
$this->wp->registerBlockType('mailpoet/subscription-form-block-render', [
|
||||
'attributes' => [
|
||||
'form' => [
|
||||
'type' => 'number',
|
||||
'default' => null,
|
||||
],
|
||||
],
|
||||
'render_callback' => [$this, 'renderForm'],
|
||||
]);
|
||||
}
|
||||
|
||||
public function initAdmin() {
|
||||
$this->wp->registerBlockType('mailpoet/subscription-form-block', [
|
||||
'style' => 'mailpoetblock-form-block-css',
|
||||
'editor_script' => 'mailpoet/subscription-form-block',
|
||||
]);
|
||||
|
||||
$this->wp->addAction('admin_head', function() {
|
||||
$forms = $this->formsRepository->findAllNotDeleted();
|
||||
?>
|
||||
<script type="text/javascript">
|
||||
window.mailpoet_forms = <?php echo json_encode(array_map(function(FormEntity $form) {
|
||||
return $form->toArray();
|
||||
}, $forms)) ?>;
|
||||
window.locale = {
|
||||
selectForm: '<?php echo __('Select a MailPoet form', 'mailpoet') ?>',
|
||||
createForm: '<?php echo __('Create a new form', 'mailpoet') ?>',
|
||||
subscriptionForm: '<?php echo __('MailPoet Subscription Form', 'mailpoet') ?>',
|
||||
};
|
||||
</script>
|
||||
<?php
|
||||
});
|
||||
}
|
||||
|
||||
public function initFrontend() {
|
||||
$this->wp->registerBlockType('mailpoet/subscription-form-block', [
|
||||
'render_callback' => [$this, 'renderForm'],
|
||||
]);
|
||||
}
|
||||
|
||||
public function renderForm(array $attributes = []): string {
|
||||
if (!$attributes || !isset($attributes['formId'])) {
|
||||
return '';
|
||||
}
|
||||
$basicForm = new Widget();
|
||||
return $basicForm->widget([
|
||||
'form' => (int)$attributes['formId'],
|
||||
'form_type' => 'html',
|
||||
]);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user