You've already forked wc-licensed-product
143 lines
4.3 KiB
PHP
143 lines
4.3 KiB
PHP
|
|
<?php
|
||
|
|
/**
|
||
|
|
* Settings Controller
|
||
|
|
*
|
||
|
|
* @package Jeremias\WcLicensedProduct\Admin
|
||
|
|
*/
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace Jeremias\WcLicensedProduct\Admin;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Handles WooCommerce settings tab for license defaults
|
||
|
|
*/
|
||
|
|
final class SettingsController
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Settings option name
|
||
|
|
*/
|
||
|
|
public const OPTION_NAME = 'wc_licensed_product_settings';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Constructor
|
||
|
|
*/
|
||
|
|
public function __construct()
|
||
|
|
{
|
||
|
|
$this->registerHooks();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Register WordPress hooks
|
||
|
|
*/
|
||
|
|
private function registerHooks(): void
|
||
|
|
{
|
||
|
|
add_filter('woocommerce_settings_tabs_array', [$this, 'addSettingsTab'], 50);
|
||
|
|
add_action('woocommerce_settings_tabs_licensed_product', [$this, 'renderSettingsTab']);
|
||
|
|
add_action('woocommerce_update_options_licensed_product', [$this, 'saveSettings']);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Add settings tab to WooCommerce settings
|
||
|
|
*/
|
||
|
|
public function addSettingsTab(array $tabs): array
|
||
|
|
{
|
||
|
|
$tabs['licensed_product'] = __('Licensed Products', 'wc-licensed-product');
|
||
|
|
return $tabs;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get settings fields
|
||
|
|
*/
|
||
|
|
public function getSettingsFields(): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
'section_title' => [
|
||
|
|
'name' => __('Default License Settings', 'wc-licensed-product'),
|
||
|
|
'type' => 'title',
|
||
|
|
'desc' => __('These settings serve as defaults for new licensed products. Individual product settings override these defaults.', 'wc-licensed-product'),
|
||
|
|
'id' => 'wc_licensed_product_section_defaults',
|
||
|
|
],
|
||
|
|
'default_max_activations' => [
|
||
|
|
'name' => __('Default Max Activations', 'wc-licensed-product'),
|
||
|
|
'type' => 'number',
|
||
|
|
'desc' => __('Default maximum number of domain activations per license.', 'wc-licensed-product'),
|
||
|
|
'id' => 'wc_licensed_product_default_max_activations',
|
||
|
|
'default' => '1',
|
||
|
|
'custom_attributes' => [
|
||
|
|
'min' => '1',
|
||
|
|
'step' => '1',
|
||
|
|
],
|
||
|
|
],
|
||
|
|
'default_validity_days' => [
|
||
|
|
'name' => __('Default License Validity (Days)', 'wc-licensed-product'),
|
||
|
|
'type' => 'number',
|
||
|
|
'desc' => __('Default number of days a license is valid. Leave empty or set to 0 for lifetime licenses.', 'wc-licensed-product'),
|
||
|
|
'id' => 'wc_licensed_product_default_validity_days',
|
||
|
|
'default' => '',
|
||
|
|
'placeholder' => __('Lifetime', 'wc-licensed-product'),
|
||
|
|
'custom_attributes' => [
|
||
|
|
'min' => '0',
|
||
|
|
'step' => '1',
|
||
|
|
],
|
||
|
|
],
|
||
|
|
'default_bind_to_version' => [
|
||
|
|
'name' => __('Default Bind to Major Version', 'wc-licensed-product'),
|
||
|
|
'type' => 'checkbox',
|
||
|
|
'desc' => __('If enabled, licenses are bound to the major version at purchase time by default.', 'wc-licensed-product'),
|
||
|
|
'id' => 'wc_licensed_product_default_bind_to_version',
|
||
|
|
'default' => 'no',
|
||
|
|
],
|
||
|
|
'section_end' => [
|
||
|
|
'type' => 'sectionend',
|
||
|
|
'id' => 'wc_licensed_product_section_defaults_end',
|
||
|
|
],
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Render settings tab content
|
||
|
|
*/
|
||
|
|
public function renderSettingsTab(): void
|
||
|
|
{
|
||
|
|
woocommerce_admin_fields($this->getSettingsFields());
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Save settings
|
||
|
|
*/
|
||
|
|
public function saveSettings(): void
|
||
|
|
{
|
||
|
|
woocommerce_update_options($this->getSettingsFields());
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get default max activations
|
||
|
|
*/
|
||
|
|
public static function getDefaultMaxActivations(): int
|
||
|
|
{
|
||
|
|
$value = get_option('wc_licensed_product_default_max_activations', 1);
|
||
|
|
return max(1, (int) $value);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get default validity days
|
||
|
|
*/
|
||
|
|
public static function getDefaultValidityDays(): ?int
|
||
|
|
{
|
||
|
|
$value = get_option('wc_licensed_product_default_validity_days', '');
|
||
|
|
if ($value === '' || $value === '0') {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
return (int) $value;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get default bind to version setting
|
||
|
|
*/
|
||
|
|
public static function getDefaultBindToVersion(): bool
|
||
|
|
{
|
||
|
|
return get_option('wc_licensed_product_default_bind_to_version', 'no') === 'yes';
|
||
|
|
}
|
||
|
|
}
|