Files
wc-licensed-product/wc-licensed-product.php
magdev 404083f023 Implement version 0.0.1 - Licensed Product type for WooCommerce
Add complete plugin infrastructure for selling software with license keys:

- New "Licensed Product" WooCommerce product type
- License key generation (XXXX-XXXX-XXXX-XXXX format) on order completion
- Domain-based license validation system
- REST API endpoints (validate, status, activate, deactivate)
- Customer My Account "Licenses" page
- Admin license management under WooCommerce > Licenses
- Checkout domain field for licensed products
- Custom database tables for licenses and product versions
- Twig template engine integration
- Full i18n support with German (de_CH) translation

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-21 18:55:18 +01:00

114 lines
3.1 KiB
PHP

<?php
/**
* Plugin Name: WC Licensed Product
* Plugin URI: https://src.bundespruefstelle.ch/magdev/wc-licensed-product
* Description: WooCommerce plugin to sell software products using license keys with domain-based validation.
* Version: 0.0.1
* Author: Marco Graetsch
* Author URI: https://src.bundespruefstelle.ch/magdev
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: wc-licensed-product
* Domain Path: /languages
* Requires at least: 6.0
* Requires PHP: 8.3
* WC requires at least: 10.0
* WC tested up to: 10.0
*
* @package Jeremias\WcLicensedProduct
*/
declare(strict_types=1);
namespace Jeremias\WcLicensedProduct;
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
// Plugin constants
define('WC_LICENSED_PRODUCT_VERSION', '0.0.1');
define('WC_LICENSED_PRODUCT_PLUGIN_FILE', __FILE__);
define('WC_LICENSED_PRODUCT_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('WC_LICENSED_PRODUCT_PLUGIN_URL', plugin_dir_url(__FILE__));
define('WC_LICENSED_PRODUCT_PLUGIN_BASENAME', plugin_basename(__FILE__));
// Load Composer autoloader
if (file_exists(WC_LICENSED_PRODUCT_PLUGIN_DIR . 'vendor/autoload.php')) {
require_once WC_LICENSED_PRODUCT_PLUGIN_DIR . 'vendor/autoload.php';
}
/**
* Check if WooCommerce is active
*/
function wc_licensed_product_is_woocommerce_active(): bool
{
return class_exists('WooCommerce');
}
/**
* Admin notice for missing WooCommerce
*/
function wc_licensed_product_woocommerce_missing_notice(): void
{
?>
<div class="error">
<p>
<?php
printf(
/* translators: %s: WooCommerce plugin name */
esc_html__('%s requires WooCommerce to be installed and active.', 'wc-licensed-product'),
'<strong>WC Licensed Product</strong>'
);
?>
</p>
</div>
<?php
}
/**
* Initialize the plugin
*/
function wc_licensed_product_init(): void
{
// Check for WooCommerce
if (!wc_licensed_product_is_woocommerce_active()) {
add_action('admin_notices', __NAMESPACE__ . '\\wc_licensed_product_woocommerce_missing_notice');
return;
}
// Load text domain
load_plugin_textdomain(
'wc-licensed-product',
false,
dirname(WC_LICENSED_PRODUCT_PLUGIN_BASENAME) . '/languages'
);
// Initialize the plugin
Plugin::instance();
}
// Hook into plugins_loaded to ensure WooCommerce is loaded first
add_action('plugins_loaded', __NAMESPACE__ . '\\wc_licensed_product_init');
// Register activation hook
register_activation_hook(__FILE__, function (): void {
if (!wc_licensed_product_is_woocommerce_active()) {
deactivate_plugins(WC_LICENSED_PRODUCT_PLUGIN_BASENAME);
wp_die(
esc_html__('WC Licensed Product requires WooCommerce to be installed and active.', 'wc-licensed-product'),
'Plugin Activation Error',
['back_link' => true]
);
}
// Run installation
Installer::activate();
});
// Register deactivation hook
register_deactivation_hook(__FILE__, function (): void {
Installer::deactivate();
});