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
ManageSubscriptionBlock.php
99 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\Form\AssetsController; |
| 9 | use MailPoet\Subscription\Pages as SubscriptionPages; |
| 10 | use MailPoet\WP\Functions as WPFunctions; |
| 11 | |
| 12 | // phpcs:disable Generic.Files.InlineHTML |
| 13 | class ManageSubscriptionBlock { |
| 14 | /** @var WPFunctions */ |
| 15 | private $wp; |
| 16 | |
| 17 | /** @var SubscriptionPages */ |
| 18 | private $subscriptionPages; |
| 19 | |
| 20 | /** @var AssetsController */ |
| 21 | private $assetsController; |
| 22 | |
| 23 | public function __construct( |
| 24 | WPFunctions $wp, |
| 25 | SubscriptionPages $subscriptionPages, |
| 26 | AssetsController $assetsController |
| 27 | ) { |
| 28 | $this->wp = $wp; |
| 29 | $this->subscriptionPages = $subscriptionPages; |
| 30 | $this->assetsController = $assetsController; |
| 31 | } |
| 32 | |
| 33 | public function init() { |
| 34 | // Registered in every context (including REST) so the editor's |
| 35 | // ServerSideRender preview can render this block. |
| 36 | $this->wp->registerBlockType('mailpoet/manage-subscription-block-render', [ |
| 37 | 'attributes' => [ |
| 38 | 'preview' => [ |
| 39 | 'type' => 'boolean', |
| 40 | 'default' => false, |
| 41 | ], |
| 42 | ], |
| 43 | 'render_callback' => [$this, 'renderManageSubscription'], |
| 44 | ]); |
| 45 | } |
| 46 | |
| 47 | public function initAdmin() { |
| 48 | $this->wp->registerBlockType('mailpoet/manage-subscription-block', [ |
| 49 | 'editor_script' => 'mailpoet/manage-subscription-block', |
| 50 | ]); |
| 51 | |
| 52 | $this->wp->addAction('admin_head', function() { |
| 53 | ?> |
| 54 | <script type="text/javascript"> |
| 55 | window.mailpoetManageSubscriptionBlock = { |
| 56 | title: '<?php echo esc_js(__('MailPoet Manage Subscription', 'mailpoet')); ?>', |
| 57 | description: '<?php echo esc_js(__('Lets logged-in subscribers manage their lists and subscription status.', 'mailpoet')); ?>', |
| 58 | }; |
| 59 | </script> |
| 60 | <?php |
| 61 | }); |
| 62 | } |
| 63 | |
| 64 | public function initFrontend() { |
| 65 | $this->wp->registerBlockType('mailpoet/manage-subscription-block', [ |
| 66 | 'render_callback' => [$this, 'renderManageSubscription'], |
| 67 | ]); |
| 68 | } |
| 69 | |
| 70 | public function renderManageSubscription(array $attributes = []): string { |
| 71 | // getManageContent() does not enqueue the front-end form assets itself |
| 72 | // (the subscription page flow does that separately), so load them here. |
| 73 | $this->assetsController->setupFrontEndDependencies(); |
| 74 | |
| 75 | if (!empty($attributes['preview']) && $this->wp->currentUserCan('edit_posts')) { |
| 76 | // The editor's ServerSideRender preview passes preview=true so it shows a |
| 77 | // representative demo form instead of the current admin's own subscription |
| 78 | // (or the "subscribers only" message when the admin isn't a subscriber). |
| 79 | // Force the demo regardless of any prior Pages state. WordPress passes |
| 80 | // attributes that a block type doesn't register through to the render |
| 81 | // callback verbatim, so hand-written block markup in content can carry |
| 82 | // preview=true; the capability check (the same one the block-renderer |
| 83 | // REST endpoint enforces) keeps visitors from forcing the demo form. |
| 84 | $this->subscriptionPages->init(false, ['preview' => true]); |
| 85 | } elseif (!$this->subscriptionPages->isInitialized()) { |
| 86 | // Make sure the Pages instance has its internal state initialised (notably |
| 87 | // $data, which isPreview() reads and which is null until init()) before |
| 88 | // rendering, so it resolves the current logged-in user as a subscriber on |
| 89 | // any page or the WooCommerce My Account page. Pages is registered |
| 90 | // non-shared, so this instance belongs to this block alone and persists |
| 91 | // only across renders within a single request; the guard just avoids |
| 92 | // re-initialising it on repeated renders. |
| 93 | $this->subscriptionPages->init(); |
| 94 | } |
| 95 | |
| 96 | return (string)$this->subscriptionPages->getManageContent(); |
| 97 | } |
| 98 | } |
| 99 |