You've already forked wc-licensed-product
Fix license generation and checkout domain field bugs
- Add WooCommerce Checkout Blocks support for domain field - Create CheckoutBlocksIntegration for block-based checkout - Create StoreApiExtension for Store API domain handling - Add checkout-blocks.js for frontend domain field in blocks - Fix LicenseManager product type check in generateLicense() - Add multiple order status hooks for reliable license generation Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
127
src/Checkout/CheckoutBlocksIntegration.php
Normal file
127
src/Checkout/CheckoutBlocksIntegration.php
Normal file
@@ -0,0 +1,127 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
134
src/Checkout/StoreApiExtension.php
Normal file
134
src/Checkout/StoreApiExtension.php
Normal file
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
/**
|
||||
* WooCommerce Store API Extension
|
||||
*
|
||||
* @package Jeremias\WcLicensedProduct\Checkout
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jeremias\WcLicensedProduct\Checkout;
|
||||
|
||||
use Automattic\WooCommerce\StoreApi\Schemas\V1\CheckoutSchema;
|
||||
use Automattic\WooCommerce\StoreApi\StoreApi;
|
||||
use Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema;
|
||||
use Jeremias\WcLicensedProduct\License\LicenseManager;
|
||||
|
||||
/**
|
||||
* Extends the Store API to handle licensed product domain data
|
||||
*/
|
||||
final class StoreApiExtension
|
||||
{
|
||||
private const IDENTIFIER = 'wc-licensed-product';
|
||||
|
||||
private LicenseManager $licenseManager;
|
||||
|
||||
public function __construct(LicenseManager $licenseManager)
|
||||
{
|
||||
$this->licenseManager = $licenseManager;
|
||||
$this->registerHooks();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register Store API hooks
|
||||
*/
|
||||
private function registerHooks(): void
|
||||
{
|
||||
add_action('woocommerce_blocks_loaded', [$this, 'registerStoreApiExtension']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the Store API extension
|
||||
*/
|
||||
public function registerStoreApiExtension(): void
|
||||
{
|
||||
if (!class_exists('Automattic\WooCommerce\StoreApi\StoreApi')) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Register endpoint data extension
|
||||
woocommerce_store_api_register_endpoint_data([
|
||||
'endpoint' => CheckoutSchema::IDENTIFIER,
|
||||
'namespace' => self::IDENTIFIER,
|
||||
'data_callback' => [$this, 'getExtensionData'],
|
||||
'schema_callback' => [$this, 'getExtensionSchema'],
|
||||
'schema_type' => ARRAY_A,
|
||||
]);
|
||||
|
||||
// Register update callback for the domain field
|
||||
woocommerce_store_api_register_update_callback([
|
||||
'namespace' => self::IDENTIFIER,
|
||||
'callback' => [$this, 'handleExtensionUpdate'],
|
||||
]);
|
||||
|
||||
// Hook into checkout order processing
|
||||
add_action('woocommerce_store_api_checkout_order_processed', [$this, 'processCheckoutOrder']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get extension data for the checkout endpoint
|
||||
*/
|
||||
public function getExtensionData(): array
|
||||
{
|
||||
return [
|
||||
'licensed_product_domain' => WC()->session ? WC()->session->get('licensed_product_domain', '') : '',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get extension schema
|
||||
*/
|
||||
public function getExtensionSchema(): array
|
||||
{
|
||||
return [
|
||||
'licensed_product_domain' => [
|
||||
'description' => __('Domain for license activation', 'wc-licensed-product'),
|
||||
'type' => 'string',
|
||||
'context' => ['view', 'edit'],
|
||||
'readonly' => false,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle extension data updates from the frontend
|
||||
*/
|
||||
public function handleExtensionUpdate(array $data): void
|
||||
{
|
||||
if (isset($data['licensed_product_domain'])) {
|
||||
$domain = sanitize_text_field($data['licensed_product_domain']);
|
||||
$normalizedDomain = $this->licenseManager->normalizeDomain($domain);
|
||||
|
||||
if (WC()->session) {
|
||||
WC()->session->set('licensed_product_domain', $normalizedDomain);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the checkout order - save domain to order meta
|
||||
*/
|
||||
public function processCheckoutOrder(\WC_Order $order): void
|
||||
{
|
||||
$domain = WC()->session ? WC()->session->get('licensed_product_domain', '') : '';
|
||||
|
||||
// Also check in the request data for block checkout
|
||||
if (empty($domain)) {
|
||||
$requestData = json_decode(file_get_contents('php://input'), true);
|
||||
if (isset($requestData['extensions'][self::IDENTIFIER]['licensed_product_domain'])) {
|
||||
$domain = sanitize_text_field($requestData['extensions'][self::IDENTIFIER]['licensed_product_domain']);
|
||||
$domain = $this->licenseManager->normalizeDomain($domain);
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($domain)) {
|
||||
$order->update_meta_data('_licensed_product_domain', $domain);
|
||||
$order->save();
|
||||
|
||||
// Clear session data
|
||||
if (WC()->session) {
|
||||
WC()->session->set('licensed_product_domain', '');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -56,10 +56,15 @@ class LicenseManager
|
||||
}
|
||||
|
||||
$product = wc_get_product($productId);
|
||||
if (!$product instanceof LicensedProduct) {
|
||||
if (!$product || !$product->is_type('licensed')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Ensure we have the LicensedProduct instance for type hints
|
||||
if (!$product instanceof LicensedProduct) {
|
||||
$product = new LicensedProduct($productId);
|
||||
}
|
||||
|
||||
// Generate unique license key
|
||||
$licenseKey = $this->generateLicenseKey();
|
||||
while ($this->getLicenseByKey($licenseKey)) {
|
||||
|
||||
@@ -13,7 +13,9 @@ use Jeremias\WcLicensedProduct\Admin\AdminController;
|
||||
use Jeremias\WcLicensedProduct\Admin\SettingsController;
|
||||
use Jeremias\WcLicensedProduct\Admin\VersionAdminController;
|
||||
use Jeremias\WcLicensedProduct\Api\RestApiController;
|
||||
use Jeremias\WcLicensedProduct\Checkout\CheckoutBlocksIntegration;
|
||||
use Jeremias\WcLicensedProduct\Checkout\CheckoutController;
|
||||
use Jeremias\WcLicensedProduct\Checkout\StoreApiExtension;
|
||||
use Jeremias\WcLicensedProduct\Email\LicenseEmailController;
|
||||
use Jeremias\WcLicensedProduct\Frontend\AccountController;
|
||||
use Jeremias\WcLicensedProduct\Frontend\DownloadController;
|
||||
@@ -110,6 +112,8 @@ final class Plugin
|
||||
// Initialize controllers
|
||||
new LicensedProductType();
|
||||
new CheckoutController($this->licenseManager);
|
||||
new StoreApiExtension($this->licenseManager);
|
||||
$this->registerCheckoutBlocksIntegration();
|
||||
$this->downloadController = new DownloadController($this->licenseManager, $this->versionManager);
|
||||
new AccountController($this->twig, $this->licenseManager, $this->versionManager, $this->downloadController);
|
||||
new RestApiController($this->licenseManager);
|
||||
@@ -122,13 +126,34 @@ final class Plugin
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register WooCommerce Checkout Blocks integration
|
||||
*/
|
||||
private function registerCheckoutBlocksIntegration(): void
|
||||
{
|
||||
add_action('woocommerce_blocks_loaded', function (): void {
|
||||
if (class_exists('Automattic\WooCommerce\Blocks\Integrations\IntegrationRegistry')) {
|
||||
add_action(
|
||||
'woocommerce_blocks_checkout_block_registration',
|
||||
function ($integration_registry): void {
|
||||
$integration_registry->register(new CheckoutBlocksIntegration());
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Register plugin hooks
|
||||
*/
|
||||
private function registerHooks(): void
|
||||
{
|
||||
// Generate license on order completion
|
||||
// Generate license on order completion (multiple hooks for compatibility)
|
||||
add_action('woocommerce_order_status_completed', [$this, 'onOrderCompleted']);
|
||||
add_action('woocommerce_order_status_processing', [$this, 'onOrderCompleted']);
|
||||
|
||||
// Also hook into payment complete for immediate license generation
|
||||
add_action('woocommerce_payment_complete', [$this, 'onOrderCompleted']);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user