2026-01-21 18:55:18 +01:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Main Plugin class
|
|
|
|
|
*
|
|
|
|
|
* @package Jeremias\WcLicensedProduct
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Jeremias\WcLicensedProduct;
|
|
|
|
|
|
|
|
|
|
use Jeremias\WcLicensedProduct\Admin\AdminController;
|
2026-01-21 19:15:19 +01:00
|
|
|
use Jeremias\WcLicensedProduct\Admin\VersionAdminController;
|
2026-01-21 18:55:18 +01:00
|
|
|
use Jeremias\WcLicensedProduct\Api\RestApiController;
|
|
|
|
|
use Jeremias\WcLicensedProduct\Checkout\CheckoutController;
|
2026-01-21 19:15:19 +01:00
|
|
|
use Jeremias\WcLicensedProduct\Email\LicenseEmailController;
|
2026-01-21 18:55:18 +01:00
|
|
|
use Jeremias\WcLicensedProduct\Frontend\AccountController;
|
2026-01-21 19:46:50 +01:00
|
|
|
use Jeremias\WcLicensedProduct\Frontend\DownloadController;
|
2026-01-21 18:55:18 +01:00
|
|
|
use Jeremias\WcLicensedProduct\License\LicenseManager;
|
|
|
|
|
use Jeremias\WcLicensedProduct\Product\LicensedProductType;
|
2026-01-21 19:15:19 +01:00
|
|
|
use Jeremias\WcLicensedProduct\Product\VersionManager;
|
2026-01-21 18:55:18 +01:00
|
|
|
use Twig\Environment;
|
|
|
|
|
use Twig\Loader\FilesystemLoader;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Main plugin controller
|
|
|
|
|
*/
|
|
|
|
|
final class Plugin
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Singleton instance
|
|
|
|
|
*/
|
|
|
|
|
private static ?Plugin $instance = null;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Twig environment
|
|
|
|
|
*/
|
|
|
|
|
private Environment $twig;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* License manager
|
|
|
|
|
*/
|
|
|
|
|
private LicenseManager $licenseManager;
|
|
|
|
|
|
2026-01-21 19:15:19 +01:00
|
|
|
/**
|
|
|
|
|
* Version manager
|
|
|
|
|
*/
|
|
|
|
|
private VersionManager $versionManager;
|
|
|
|
|
|
2026-01-21 19:46:50 +01:00
|
|
|
/**
|
|
|
|
|
* Download controller
|
|
|
|
|
*/
|
|
|
|
|
private DownloadController $downloadController;
|
|
|
|
|
|
2026-01-21 18:55:18 +01:00
|
|
|
/**
|
|
|
|
|
* Get singleton instance
|
|
|
|
|
*/
|
|
|
|
|
public static function instance(): Plugin
|
|
|
|
|
{
|
|
|
|
|
if (self::$instance === null) {
|
|
|
|
|
self::$instance = new self();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self::$instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Private constructor for singleton
|
|
|
|
|
*/
|
|
|
|
|
private function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->initTwig();
|
|
|
|
|
$this->initComponents();
|
|
|
|
|
$this->registerHooks();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize Twig environment
|
|
|
|
|
*/
|
|
|
|
|
private function initTwig(): void
|
|
|
|
|
{
|
|
|
|
|
$loader = new FilesystemLoader(WC_LICENSED_PRODUCT_PLUGIN_DIR . 'templates');
|
|
|
|
|
$this->twig = new Environment($loader, [
|
|
|
|
|
'cache' => WP_CONTENT_DIR . '/cache/wc-licensed-product/twig',
|
|
|
|
|
'auto_reload' => WP_DEBUG,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// Add WordPress functions as Twig functions
|
|
|
|
|
$this->twig->addFunction(new \Twig\TwigFunction('__', function (string $text, string $domain = 'wc-licensed-product'): string {
|
|
|
|
|
return __($text, $domain);
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
$this->twig->addFunction(new \Twig\TwigFunction('esc_html', 'esc_html'));
|
|
|
|
|
$this->twig->addFunction(new \Twig\TwigFunction('esc_attr', 'esc_attr'));
|
|
|
|
|
$this->twig->addFunction(new \Twig\TwigFunction('esc_url', 'esc_url'));
|
|
|
|
|
$this->twig->addFunction(new \Twig\TwigFunction('wp_nonce_field', 'wp_nonce_field', ['is_safe' => ['html']]));
|
|
|
|
|
$this->twig->addFunction(new \Twig\TwigFunction('admin_url', 'admin_url'));
|
|
|
|
|
$this->twig->addFunction(new \Twig\TwigFunction('wc_get_endpoint_url', 'wc_get_endpoint_url'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize plugin components
|
|
|
|
|
*/
|
|
|
|
|
private function initComponents(): void
|
|
|
|
|
{
|
|
|
|
|
$this->licenseManager = new LicenseManager();
|
2026-01-21 19:15:19 +01:00
|
|
|
$this->versionManager = new VersionManager();
|
2026-01-21 18:55:18 +01:00
|
|
|
|
|
|
|
|
// Initialize controllers
|
|
|
|
|
new LicensedProductType();
|
|
|
|
|
new CheckoutController($this->licenseManager);
|
2026-01-21 19:46:50 +01:00
|
|
|
$this->downloadController = new DownloadController($this->licenseManager, $this->versionManager);
|
|
|
|
|
new AccountController($this->twig, $this->licenseManager, $this->versionManager, $this->downloadController);
|
2026-01-21 18:55:18 +01:00
|
|
|
new RestApiController($this->licenseManager);
|
2026-01-21 19:15:19 +01:00
|
|
|
new LicenseEmailController($this->licenseManager);
|
2026-01-21 18:55:18 +01:00
|
|
|
|
|
|
|
|
if (is_admin()) {
|
|
|
|
|
new AdminController($this->twig, $this->licenseManager);
|
2026-01-21 19:15:19 +01:00
|
|
|
new VersionAdminController($this->versionManager);
|
2026-01-21 18:55:18 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Register plugin hooks
|
|
|
|
|
*/
|
|
|
|
|
private function registerHooks(): void
|
|
|
|
|
{
|
|
|
|
|
// Generate license on order completion
|
|
|
|
|
add_action('woocommerce_order_status_completed', [$this, 'onOrderCompleted']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle order completion - generate licenses
|
|
|
|
|
*/
|
|
|
|
|
public function onOrderCompleted(int $orderId): void
|
|
|
|
|
{
|
|
|
|
|
$order = wc_get_order($orderId);
|
|
|
|
|
if (!$order) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($order->get_items() as $item) {
|
|
|
|
|
$product = $item->get_product();
|
|
|
|
|
if ($product && $product->is_type('licensed')) {
|
|
|
|
|
$domain = $order->get_meta('_licensed_product_domain');
|
|
|
|
|
if ($domain) {
|
|
|
|
|
$this->licenseManager->generateLicense(
|
|
|
|
|
$orderId,
|
|
|
|
|
$product->get_id(),
|
|
|
|
|
$order->get_customer_id(),
|
|
|
|
|
$domain
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get Twig environment
|
|
|
|
|
*/
|
|
|
|
|
public function getTwig(): Environment
|
|
|
|
|
{
|
|
|
|
|
return $this->twig;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get license manager
|
|
|
|
|
*/
|
|
|
|
|
public function getLicenseManager(): LicenseManager
|
|
|
|
|
{
|
|
|
|
|
return $this->licenseManager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Render a Twig template
|
|
|
|
|
*/
|
|
|
|
|
public function render(string $template, array $context = []): string
|
|
|
|
|
{
|
|
|
|
|
return $this->twig->render($template, $context);
|
|
|
|
|
}
|
|
|
|
|
}
|