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
WooCommerceBlocksIntegration.php
194 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\PostEditorBlocks; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema; |
| 9 | use Automattic\WooCommerce\StoreApi\Schemas\V1\CheckoutSchema; |
| 10 | use Automattic\WooCommerce\StoreApi\StoreApi; |
| 11 | use MailPoet\Config\Env; |
| 12 | use MailPoet\Entities\SubscriberEntity; |
| 13 | use MailPoet\Segments\WooCommerce as WooSegment; |
| 14 | use MailPoet\Settings\SettingsController; |
| 15 | use MailPoet\Subscribers\SubscribersRepository; |
| 16 | use MailPoet\WooCommerce\Helper as WooHelper; |
| 17 | use MailPoet\WooCommerce\Subscription as WooCommerceSubscription; |
| 18 | use MailPoet\WP\Functions as WPFunctions; |
| 19 | |
| 20 | class WooCommerceBlocksIntegration { |
| 21 | /** @var SettingsController */ |
| 22 | private $settings; |
| 23 | |
| 24 | /** @var WPFunctions */ |
| 25 | private $wp; |
| 26 | |
| 27 | /** @var WooCommerceSubscription */ |
| 28 | private $woocommerceSubscription; |
| 29 | |
| 30 | /** @var WooSegment */ |
| 31 | private $wooSegment; |
| 32 | |
| 33 | /** @var SubscribersRepository */ |
| 34 | private $subscribersRepository; |
| 35 | |
| 36 | /** @var WooHelper */ |
| 37 | private $wooHelper; |
| 38 | |
| 39 | public function __construct( |
| 40 | WPFunctions $wp, |
| 41 | SettingsController $settings, |
| 42 | WooCommerceSubscription $woocommerceSubscription, |
| 43 | WooSegment $wooSegment, |
| 44 | SubscribersRepository $subscribersRepository, |
| 45 | WooHelper $wooHelper |
| 46 | ) { |
| 47 | $this->wp = $wp; |
| 48 | $this->settings = $settings; |
| 49 | $this->woocommerceSubscription = $woocommerceSubscription; |
| 50 | $this->wooSegment = $wooSegment; |
| 51 | $this->subscribersRepository = $subscribersRepository; |
| 52 | $this->wooHelper = $wooHelper; |
| 53 | } |
| 54 | |
| 55 | public function init() { |
| 56 | $this->wp->addAction( |
| 57 | 'woocommerce_blocks_checkout_block_registration', |
| 58 | [$this, 'registerCheckoutFrontendBlocks'], |
| 59 | 15 // Run after AutomateWoo hooks |
| 60 | ); |
| 61 | $addDataAttributesToBlockHook = '__experimental_woocommerce_blocks_checkout_update_order_from_request'; |
| 62 | $hooksVersionMatrix = [ |
| 63 | '7.2.0' => 'woocommerce_store_api_checkout_update_order_from_request', |
| 64 | '6.3.0' => 'woocommerce_blocks_checkout_update_order_from_request', |
| 65 | ]; |
| 66 | foreach ($hooksVersionMatrix as $version => $hook) { |
| 67 | if (!$this->wooHelper->isWooCommerceBlocksActive($version)) { |
| 68 | continue; |
| 69 | } |
| 70 | |
| 71 | $addDataAttributesToBlockHook = $hook; |
| 72 | break; |
| 73 | } |
| 74 | $this->wp->addAction( |
| 75 | $addDataAttributesToBlockHook, |
| 76 | [$this, 'processCheckoutBlockOptin'], |
| 77 | 5, // Run before AutomateWoo hooks |
| 78 | 2 |
| 79 | ); |
| 80 | $this->wp->addFilter( |
| 81 | '__experimental_woocommerce_blocks_add_data_attributes_to_block', |
| 82 | [$this, 'addDataAttributesToBlock'] |
| 83 | ); |
| 84 | $block = $this->wp->registerBlockTypeFromMetadata(Env::$assetsPath . '/dist/js/marketing-optin-block'); |
| 85 | // We need to force the script to load in the footer. register_block_type always adds the script to the header. |
| 86 | if ($block instanceof \WP_Block_Type && isset($block->editor_script) && $block->editor_script) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 87 | $wpScripts = $this->wp->getWpScripts(); |
| 88 | $wpScripts->add_data($block->editor_script, 'group', 1); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 89 | } |
| 90 | |
| 91 | $this->extendRestApi(); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Load blocks in frontend with Checkout. |
| 96 | */ |
| 97 | public function registerCheckoutFrontendBlocks($integration_registry) { |
| 98 | $integration_registry->register(new MarketingOptinBlock( |
| 99 | [ |
| 100 | 'defaultText' => $this->settings->get('woocommerce.optin_on_checkout.message', ''), |
| 101 | 'optinEnabled' => $this->settings->get('woocommerce.optin_on_checkout.enabled', false), |
| 102 | ], |
| 103 | $this->wp |
| 104 | )); |
| 105 | |
| 106 | $this->unregisterAutomateWooCheckoutBlock($integration_registry); |
| 107 | } |
| 108 | |
| 109 | public function unregisterAutomateWooCheckoutBlock($integration_registry) { |
| 110 | if (!$this->settings->get('woocommerce.optin_on_checkout.enabled', false)) { |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | $blockName = 'automatewoo'; |
| 115 | |
| 116 | $isAutomateWooRegistered = $integration_registry->is_registered($blockName); |
| 117 | if ($isAutomateWooRegistered) { |
| 118 | $integration_registry->unregister($blockName); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | public function addDataAttributesToBlock(array $blocks) { |
| 123 | $blocks[] = 'mailpoet/marketing-optin-block'; |
| 124 | return $blocks; |
| 125 | } |
| 126 | |
| 127 | public function extendRestApi() { |
| 128 | if (!$this->settings->get('woocommerce.optin_on_checkout.enabled', false)) { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | $extend = StoreApi::container()->get(ExtendSchema::class); |
| 133 | $extend->register_endpoint_data( |
| 134 | [ |
| 135 | 'endpoint' => CheckoutSchema::IDENTIFIER, |
| 136 | 'namespace' => 'mailpoet', |
| 137 | 'schema_callback' => function () { |
| 138 | return [ |
| 139 | 'optin' => [ |
| 140 | 'description' => __('Subscribe to marketing opt-in.', 'mailpoet'), |
| 141 | 'type' => ['boolean', 'null'], |
| 142 | ], |
| 143 | ]; |
| 144 | }, |
| 145 | ] |
| 146 | ); |
| 147 | |
| 148 | $this->unregisterAutomateWooCheckoutApiEndpoint(); |
| 149 | } |
| 150 | |
| 151 | public function unregisterAutomateWooCheckoutApiEndpoint() { |
| 152 | $extend = StoreApi::container()->get(ExtendSchema::class); |
| 153 | $extend->register_endpoint_data( |
| 154 | [ |
| 155 | 'endpoint' => CheckoutSchema::IDENTIFIER, |
| 156 | 'namespace' => 'automatewoo', |
| 157 | 'schema_callback' => null, |
| 158 | ] |
| 159 | ); |
| 160 | } |
| 161 | |
| 162 | public function processCheckoutBlockOptin(\WC_Order $order, $request) { |
| 163 | $checkoutOptin = isset($request['extensions']['mailpoet']['optin']) ? (bool)$request['extensions']['mailpoet']['optin'] : false; |
| 164 | |
| 165 | // Emulate checkout opt-in triggering for AutomateWoo |
| 166 | if ($checkoutOptin) { |
| 167 | // Multi-dimensional array inside an ArrayAccess object |
| 168 | // cannot be modified directly, so an intermediate variable is used |
| 169 | $requestExtensions = $request['extensions']; |
| 170 | $requestExtensions['automatewoo']['optin'] = 'On'; |
| 171 | $request['extensions'] = $requestExtensions; |
| 172 | } |
| 173 | |
| 174 | if (!$order->get_billing_email()) { |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | // Fetch existing woo subscriber and in case there is not any sync as guest |
| 179 | $email = $order->get_billing_email(); |
| 180 | $subscriber = $this->subscribersRepository->findOneBy(['email' => $email, 'isWoocommerceUser' => true]); |
| 181 | if (!$subscriber instanceof SubscriberEntity) { |
| 182 | $this->wooSegment->synchronizeGuestCustomer($order->get_id()); |
| 183 | $subscriber = $this->subscribersRepository->findOneBy(['email' => $email, 'isWoocommerceUser' => true]); |
| 184 | } |
| 185 | |
| 186 | // Subscriber not found and guest sync failed |
| 187 | if (!$subscriber instanceof SubscriberEntity) { |
| 188 | return null; |
| 189 | } |
| 190 | |
| 191 | $this->woocommerceSubscription->handleSubscriberOptin($subscriber, $checkoutOptin); |
| 192 | } |
| 193 | } |
| 194 |