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
NewsletterBlock.php
107 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\API\REST\API as RestApi; |
| 9 | use MailPoet\Newsletter\Embed\NewsletterEmbedService; |
| 10 | use MailPoet\Newsletter\Embed\RestApi\Endpoints\NewsletterEmbedSelectorEndpoint; |
| 11 | use MailPoet\WP\Functions as WPFunctions; |
| 12 | |
| 13 | class NewsletterBlock { |
| 14 | /** @var WPFunctions */ |
| 15 | private $wp; |
| 16 | |
| 17 | /** @var NewsletterEmbedService */ |
| 18 | private $newsletterEmbedService; |
| 19 | |
| 20 | /** @var RestApi */ |
| 21 | private $api; |
| 22 | |
| 23 | public function __construct( |
| 24 | WPFunctions $wp, |
| 25 | NewsletterEmbedService $newsletterEmbedService, |
| 26 | RestApi $api |
| 27 | ) { |
| 28 | $this->wp = $wp; |
| 29 | $this->newsletterEmbedService = $newsletterEmbedService; |
| 30 | $this->api = $api; |
| 31 | } |
| 32 | |
| 33 | public function init() { |
| 34 | if ($this->wp->isAdmin() || (defined('REST_REQUEST') && REST_REQUEST)) { |
| 35 | $this->wp->registerBlockType('mailpoet/newsletter-render', [ |
| 36 | 'attributes' => $this->getAttributes(), |
| 37 | 'render_callback' => [$this, 'renderNewsletter'], |
| 38 | ]); |
| 39 | } |
| 40 | |
| 41 | $this->wp->addAction(RestApi::REST_API_INIT_ACTION, function(): void { |
| 42 | $this->api->registerGetRoute('newsletter-embeds', NewsletterEmbedSelectorEndpoint::class); |
| 43 | }); |
| 44 | } |
| 45 | |
| 46 | public function initAdmin() { |
| 47 | $this->wp->registerBlockType('mailpoet/newsletter', [ |
| 48 | 'style' => 'mailpoetblock-form-block-css', |
| 49 | 'editor_script' => 'mailpoet/subscription-form-block', |
| 50 | 'attributes' => $this->getAttributes(), |
| 51 | 'supports' => [ |
| 52 | 'align' => ['wide', 'full'], |
| 53 | ], |
| 54 | ]); |
| 55 | } |
| 56 | |
| 57 | public function initFrontend() { |
| 58 | $this->wp->registerBlockType('mailpoet/newsletter', [ |
| 59 | 'attributes' => $this->getAttributes(), |
| 60 | 'supports' => [ |
| 61 | 'align' => ['wide', 'full'], |
| 62 | ], |
| 63 | 'render_callback' => [$this, 'renderNewsletter'], |
| 64 | ]); |
| 65 | } |
| 66 | |
| 67 | public function renderNewsletter(array $blockSettings = []): string { |
| 68 | return $this->newsletterEmbedService->render($blockSettings); |
| 69 | } |
| 70 | |
| 71 | private function getAttributes(): array { |
| 72 | return [ |
| 73 | 'newsletterId' => [ |
| 74 | 'type' => 'number', |
| 75 | 'default' => null, |
| 76 | ], |
| 77 | 'height' => [ |
| 78 | 'type' => 'number', |
| 79 | 'default' => NewsletterEmbedService::DEFAULT_HEIGHT, |
| 80 | ], |
| 81 | 'width' => [ |
| 82 | 'type' => 'number', |
| 83 | 'default' => NewsletterEmbedService::DEFAULT_WIDTH, |
| 84 | ], |
| 85 | 'showFallbackLink' => [ |
| 86 | 'type' => 'boolean', |
| 87 | 'default' => true, |
| 88 | ], |
| 89 | 'fallbackLinkAlignment' => [ |
| 90 | 'type' => 'string', |
| 91 | 'default' => 'center', |
| 92 | ], |
| 93 | 'iframeAlignment' => [ |
| 94 | 'type' => 'string', |
| 95 | 'default' => 'center', |
| 96 | ], |
| 97 | 'showEmailBackground' => [ |
| 98 | 'type' => 'boolean', |
| 99 | 'default' => true, |
| 100 | ], |
| 101 | 'align' => [ |
| 102 | 'type' => 'string', |
| 103 | ], |
| 104 | ]; |
| 105 | } |
| 106 | } |
| 107 |