ManageSubscriptionBlock.php
1 week ago
MarketingOptinBlock.php
2 years ago
NewsletterBlock.php
1 month ago
PostEditorBlock.php
1 week ago
SubscriptionFormBlock.php
6 months ago
WooCommerceBlocksIntegration.php
2 years ago
index.php
3 years ago
SubscriptionFormBlock.php
90 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\PostEditorBlocks; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Entities\FormEntity; |
| 9 | use MailPoet\Form\FormsRepository; |
| 10 | use MailPoet\Form\Widget; |
| 11 | use MailPoet\WP\Functions as WPFunctions; |
| 12 | |
| 13 | // phpcs:disable Generic.Files.InlineHTML |
| 14 | class SubscriptionFormBlock { |
| 15 | /** @var WPFunctions */ |
| 16 | private $wp; |
| 17 | |
| 18 | /** @var FormsRepository */ |
| 19 | private $formsRepository; |
| 20 | |
| 21 | public function __construct( |
| 22 | WPFunctions $wp, |
| 23 | FormsRepository $formsRepository |
| 24 | ) { |
| 25 | $this->wp = $wp; |
| 26 | $this->formsRepository = $formsRepository; |
| 27 | } |
| 28 | |
| 29 | public function init() { |
| 30 | $this->wp->registerBlockType('mailpoet/subscription-form-block-render', [ |
| 31 | 'attributes' => [ |
| 32 | 'formId' => [ |
| 33 | 'type' => 'number', |
| 34 | 'default' => null, |
| 35 | ], |
| 36 | ], |
| 37 | 'render_callback' => [$this, 'renderForm'], |
| 38 | ]); |
| 39 | } |
| 40 | |
| 41 | public function initAdmin() { |
| 42 | $this->wp->registerBlockType('mailpoet/subscription-form-block', [ |
| 43 | 'style' => 'mailpoetblock-form-block-css', |
| 44 | 'editor_script' => 'mailpoet/subscription-form-block', |
| 45 | ]); |
| 46 | |
| 47 | $this->wp->addAction('admin_head', function() { |
| 48 | $forms = $this->formsRepository->findAllNotDeleted(); |
| 49 | $form_json = wp_json_encode( |
| 50 | array_map( |
| 51 | function(FormEntity $form) { |
| 52 | return $form->toArray(); |
| 53 | }, |
| 54 | $forms |
| 55 | ), |
| 56 | JSON_HEX_TAG | JSON_UNESCAPED_SLASHES |
| 57 | ); |
| 58 | ?> |
| 59 | <script type="text/javascript"> |
| 60 | window.mailpoet_forms = <?php echo $form_json; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>; |
| 61 | window.mailpoet_form_edit_url = '<?php echo esc_js($this->wp->adminUrl('admin.php?page=mailpoet-form-editor-template-selection')); ?>'; |
| 62 | window.locale = { |
| 63 | selectForm: '<?php echo esc_js(__('Select a MailPoet form', 'mailpoet')) ?>', |
| 64 | createForm: '<?php echo esc_js(__('Create a new form', 'mailpoet')) ?>', |
| 65 | subscriptionForm: '<?php echo esc_js(__('MailPoet Subscription Form', 'mailpoet')) ?>', |
| 66 | inactive: '<?php echo esc_js(__('inactive', 'mailpoet')) ?>', |
| 67 | }; |
| 68 | </script> |
| 69 | <?php |
| 70 | }); |
| 71 | } |
| 72 | |
| 73 | public function initFrontend() { |
| 74 | $this->wp->registerBlockType('mailpoet/subscription-form-block', [ |
| 75 | 'render_callback' => [$this, 'renderForm'], |
| 76 | ]); |
| 77 | } |
| 78 | |
| 79 | public function renderForm(array $attributes = []): string { |
| 80 | if (!$attributes || !isset($attributes['formId'])) { |
| 81 | return ''; |
| 82 | } |
| 83 | $basicForm = new Widget(); |
| 84 | return $basicForm->widget([ |
| 85 | 'form' => (int)$attributes['formId'], |
| 86 | 'form_type' => 'html', |
| 87 | ]); |
| 88 | } |
| 89 | } |
| 90 |