You've already forked wc-licensed-product
128 lines
3.5 KiB
PHP
128 lines
3.5 KiB
PHP
|
|
<?php
|
||
|
|
/**
|
||
|
|
* WooCommerce Checkout Blocks Integration
|
||
|
|
*
|
||
|
|
* @package Jeremias\WcLicensedProduct\Checkout
|
||
|
|
*/
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace Jeremias\WcLicensedProduct\Checkout;
|
||
|
|
|
||
|
|
use Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Integration with WooCommerce Checkout Blocks
|
||
|
|
*/
|
||
|
|
final class CheckoutBlocksIntegration implements IntegrationInterface
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* The name of the integration
|
||
|
|
*/
|
||
|
|
public function get_name(): string
|
||
|
|
{
|
||
|
|
return 'wc-licensed-product';
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Initialize the integration
|
||
|
|
*/
|
||
|
|
public function initialize(): void
|
||
|
|
{
|
||
|
|
$this->registerScripts();
|
||
|
|
$this->registerBlockExtensionData();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Register scripts for the checkout block
|
||
|
|
*/
|
||
|
|
private function registerScripts(): void
|
||
|
|
{
|
||
|
|
$scriptPath = WC_LICENSED_PRODUCT_PLUGIN_DIR . 'assets/js/checkout-blocks.js';
|
||
|
|
$scriptUrl = WC_LICENSED_PRODUCT_PLUGIN_URL . 'assets/js/checkout-blocks.js';
|
||
|
|
|
||
|
|
if (file_exists($scriptPath)) {
|
||
|
|
wp_register_script(
|
||
|
|
'wc-licensed-product-checkout-blocks',
|
||
|
|
$scriptUrl,
|
||
|
|
['wc-blocks-checkout', 'wp-element', 'wp-components', 'wp-i18n'],
|
||
|
|
WC_LICENSED_PRODUCT_VERSION,
|
||
|
|
true
|
||
|
|
);
|
||
|
|
|
||
|
|
wp_set_script_translations(
|
||
|
|
'wc-licensed-product-checkout-blocks',
|
||
|
|
'wc-licensed-product',
|
||
|
|
WC_LICENSED_PRODUCT_PLUGIN_DIR . 'languages'
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Register block extension data
|
||
|
|
*/
|
||
|
|
private function registerBlockExtensionData(): void
|
||
|
|
{
|
||
|
|
// Pass data to the checkout block script
|
||
|
|
add_filter(
|
||
|
|
'woocommerce_blocks_checkout_block_registration_data',
|
||
|
|
function (array $data): array {
|
||
|
|
$data['wc-licensed-product'] = [
|
||
|
|
'hasLicensedProducts' => $this->cartHasLicensedProducts(),
|
||
|
|
];
|
||
|
|
return $data;
|
||
|
|
}
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Returns an array of script handles to enqueue in the frontend context
|
||
|
|
*/
|
||
|
|
public function get_script_handles(): array
|
||
|
|
{
|
||
|
|
return ['wc-licensed-product-checkout-blocks'];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Returns an array of script handles to enqueue in the editor context
|
||
|
|
*/
|
||
|
|
public function get_editor_script_handles(): array
|
||
|
|
{
|
||
|
|
return [];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Returns script data to pass to the frontend scripts
|
||
|
|
*/
|
||
|
|
public function get_script_data(): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
'hasLicensedProducts' => $this->cartHasLicensedProducts(),
|
||
|
|
'fieldLabel' => __('Domain for License Activation', 'wc-licensed-product'),
|
||
|
|
'fieldPlaceholder' => __('example.com', 'wc-licensed-product'),
|
||
|
|
'fieldDescription' => __('Enter the domain where you will use this license (without http:// or www).', 'wc-licensed-product'),
|
||
|
|
'sectionTitle' => __('License Domain', 'wc-licensed-product'),
|
||
|
|
'validationError' => __('Please enter a valid domain for your license activation.', 'wc-licensed-product'),
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Check if cart contains licensed products
|
||
|
|
*/
|
||
|
|
private function cartHasLicensedProducts(): bool
|
||
|
|
{
|
||
|
|
if (!WC()->cart) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
foreach (WC()->cart->get_cart() as $cartItem) {
|
||
|
|
$product = $cartItem['data'];
|
||
|
|
if ($product && $product->is_type('licensed')) {
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|