Checkbox implementation and refactor naming structure
[MAILPOET-3920]
This commit is contained in:
83
lib/PostEditorBlocks/MarketingOptinBlock.php
Normal file
83
lib/PostEditorBlocks/MarketingOptinBlock.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\PostEditorBlocks;
|
||||
|
||||
use Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface;
|
||||
use MailPoet\Config\Env;
|
||||
|
||||
/**
|
||||
* Class MarketingOptinBlock
|
||||
*
|
||||
* Class for integrating marketing optin block with WooCommerce Checkout.
|
||||
*/
|
||||
class MarketingOptinBlock implements IntegrationInterface {
|
||||
/** @var array */
|
||||
private $options;
|
||||
|
||||
public function __construct(
|
||||
array $options
|
||||
) {
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* The name of the integration.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_name() { // phpcs:ignore
|
||||
return 'mailpoet';
|
||||
}
|
||||
|
||||
/**
|
||||
* Register block scripts and assets.
|
||||
*/
|
||||
public function initialize() {
|
||||
$script_asset_path = Env::$assetsUrl . '/dist/js/marketing_optin_block/marketing-optin-block-frontend.asset.php';
|
||||
$script_asset = file_exists($script_asset_path)
|
||||
? require $script_asset_path
|
||||
: [
|
||||
'dependencies' => [],
|
||||
'version' => Env::$version,
|
||||
];
|
||||
wp_register_script(
|
||||
'mailpoet-marketing-optin-block-frontend',
|
||||
Env::$assetsUrl . '/dist/js/marketing_optin_block/marketing-optin-block-frontend.js',
|
||||
$script_asset['dependencies'],
|
||||
$script_asset['version'],
|
||||
true
|
||||
);
|
||||
wp_set_script_translations(
|
||||
'mailpoet-marketing-optin-block-frontend',
|
||||
'mailpoet',
|
||||
Env::$languagesPath
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of script handles to enqueue in the frontend context.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function get_script_handles() { // phpcs:ignore
|
||||
return ['mailpoet-marketing-optin-block-frontend'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of script handles to enqueue in the editor context.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function get_editor_script_handles() { // phpcs:ignore
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* An array of key, value pairs of data made available to the block on the client side.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_script_data() { // phpcs:ignore
|
||||
return $this->options;
|
||||
}
|
||||
}
|
@@ -1,93 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\PostEditorBlocks;
|
||||
|
||||
use MailPoet\Config\Env;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
use MailPoet\Settings\SettingsController;
|
||||
use Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry;
|
||||
use Automattic\WooCommerce\Blocks\Package;
|
||||
use Automattic\WooCommerce\Blocks\Domain\Services\ExtendRestApi;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Schemas\CheckoutSchema;
|
||||
|
||||
// @todo Disable if blocks is inactive
|
||||
class NewsletterBlock {
|
||||
/** @var SettingsController */
|
||||
private $settings;
|
||||
|
||||
/** @var WPFunctions */
|
||||
private $wp;
|
||||
|
||||
public function __construct(
|
||||
WPFunctions $wp,
|
||||
SettingsController $settings
|
||||
) {
|
||||
$this->wp = $wp;
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
public function init() {
|
||||
$this->wp->registerBlockType( Env::$assetsPath . '/js/src/newsletter_block' );
|
||||
$this->wp->addFilter(
|
||||
'__experimental_woocommerce_blocks_add_data_attributes_to_block',
|
||||
[$this, 'addDataAttributesToBlock']
|
||||
);
|
||||
$this->wp->addAction(
|
||||
'__experimental_woocommerce_blocks_checkout_update_order_from_request',
|
||||
[$this, 'process_checkout_block_optin']
|
||||
);
|
||||
$this->addScriptData();
|
||||
$this->extendRestApi();
|
||||
}
|
||||
|
||||
public function addDataAttributesToBlock( array $blocks ) {
|
||||
$blocks[] = 'mailpoet/newsletter-block';
|
||||
return $blocks;
|
||||
}
|
||||
|
||||
public function addScriptData() {
|
||||
$data_registry = Package::container()->get(
|
||||
AssetDataRegistry::class
|
||||
);
|
||||
$data_registry->add( 'mailpoet_data', array(
|
||||
'newsletterDefaultText' => $this->settings->get('woocommerce.optin_on_checkout.message', ''),
|
||||
'newsletterEnabled' => $this->settings->get('woocommerce.optin_on_checkout.enabled', false),
|
||||
) );
|
||||
}
|
||||
|
||||
public function extendRestApi() {
|
||||
$extend = Package::container()->get(
|
||||
ExtendRestApi::class
|
||||
);
|
||||
$extend->register_endpoint_data(
|
||||
array(
|
||||
'endpoint' => CheckoutSchema::IDENTIFIER,
|
||||
'namespace' => 'mailpoet',
|
||||
'schema_callback' => function() {
|
||||
return array(
|
||||
'newsletter-opt-in' => array(
|
||||
'description' => __( 'Subscribe to newsletter opt-in.', 'woo-gutenberg-products-block' ),
|
||||
'type' => 'boolean',
|
||||
),
|
||||
);
|
||||
},
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function process_checkout_block_optin( \WC_Order $order, array $request ) {
|
||||
if ( ! $this->settings->get('woocommerce.optin_on_checkout.enabled', false) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! $order ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! isset( $request['extensions']['mailpoet'][ 'optin' ] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// @todo do optin
|
||||
}
|
||||
}
|
109
lib/PostEditorBlocks/WooCommerceBlocksIntegration.php
Normal file
109
lib/PostEditorBlocks/WooCommerceBlocksIntegration.php
Normal file
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\PostEditorBlocks;
|
||||
|
||||
use Automattic\WooCommerce\Blocks\Domain\Services\ExtendRestApi;
|
||||
use Automattic\WooCommerce\Blocks\Package;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Schemas\CheckoutSchema;
|
||||
use MailPoet\Config\Env;
|
||||
use MailPoet\Models\Subscriber;
|
||||
use MailPoet\Settings\SettingsController;
|
||||
use MailPoet\WooCommerce\Subscription as WooCommerceSubscription;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
// @todo Disable if blocks is inactive
|
||||
class WooCommerceBlocksIntegration {
|
||||
/** @var SettingsController */
|
||||
private $settings;
|
||||
|
||||
/** @var WPFunctions */
|
||||
private $wp;
|
||||
|
||||
public function __construct(
|
||||
WPFunctions $wp,
|
||||
SettingsController $settings,
|
||||
WooCommerceSubscription $woocommerceSubscription
|
||||
) {
|
||||
$this->wp = $wp;
|
||||
$this->settings = $settings;
|
||||
$this->woocommerceSubscription = $woocommerceSubscription;
|
||||
}
|
||||
|
||||
public function init() {
|
||||
$this->wp->addAction(
|
||||
'woocommerce_blocks_checkout_block_registration',
|
||||
[$this, 'registerCheckoutFrontendBlocks']
|
||||
);
|
||||
$this->wp->addAction(
|
||||
'__experimental_woocommerce_blocks_checkout_update_order_from_request',
|
||||
[$this, 'processCheckoutBlockOptin'],
|
||||
10,
|
||||
2
|
||||
);
|
||||
$this->wp->addFilter(
|
||||
'__experimental_woocommerce_blocks_add_data_attributes_to_block',
|
||||
[$this, 'addDataAttributesToBlock']
|
||||
);
|
||||
$this->wp->registerBlockType(Env::$assetsPath . '/js/src/marketing_optin_block');
|
||||
$this->extendRestApi();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load blocks in frontend with Checkout.
|
||||
*/
|
||||
public function registerCheckoutFrontendBlocks($integration_registry) {
|
||||
$integration_registry->register(new MarketingOptinBlock([
|
||||
'defaultText' => $this->settings->get('woocommerce.optin_on_checkout.message', ''),
|
||||
'optinEnabled' => $this->settings->get('woocommerce.optin_on_checkout.enabled', false),
|
||||
'defaultStatus' => $this->woocommerceSubscription->isCurrentUserSubscribed(),
|
||||
]));
|
||||
}
|
||||
|
||||
public function addDataAttributesToBlock(array $blocks) {
|
||||
$blocks[] = 'mailpoet/marketing-optin-block';
|
||||
return $blocks;
|
||||
}
|
||||
|
||||
public function extendRestApi() {
|
||||
$extend = Package::container()->get(ExtendRestApi::class);
|
||||
$extend->register_endpoint_data(
|
||||
[
|
||||
'endpoint' => CheckoutSchema::IDENTIFIER,
|
||||
'namespace' => 'mailpoet',
|
||||
'schema_callback' => function () {
|
||||
return [
|
||||
'optin' => [
|
||||
'description' => __('Subscribe to marketing opt-in.', 'mailpoet'),
|
||||
'type' => 'boolean',
|
||||
],
|
||||
];
|
||||
},
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function processCheckoutBlockOptin(\WC_Order $order, $request) {
|
||||
if (!$this->settings->get('woocommerce.optin_on_checkout.enabled', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$order || !$order->get_billing_email()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isset($request['extensions']['mailpoet']['optin'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
// See Subscription::subscribeOnCheckout
|
||||
$subscriber = Subscriber::where('email', $order->get_billing_email())
|
||||
->where('is_woocommerce_user', 1)
|
||||
->findOne();
|
||||
|
||||
if (!$subscriber) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->woocommerceSubscription->handleSubscriberOptin($subscriber, (bool)$request['extensions']['mailpoet']['optin']);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user