Files
wc-licensed-product/src/Admin/SettingsController.php

214 lines
7.5 KiB
PHP
Raw Normal View History

<?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',
],
// Email settings section
'email_section_title' => [
'name' => __('Expiration Warning Schedule', 'wc-licensed-product'),
'type' => 'title',
'desc' => sprintf(
/* translators: %s: URL to WooCommerce email settings */
__('Configure when expiration warning emails are sent. To customize the email template, enable/disable, or change the subject, go to %s.', 'wc-licensed-product'),
'<a href="' . esc_url(admin_url('admin.php?page=wc-settings&tab=email&section=wclp_license_expiration')) . '">' .
__('WooCommerce > Settings > Emails > License Expiration Warning', 'wc-licensed-product') . '</a>'
),
'id' => 'wc_licensed_product_section_email',
],
'expiration_warning_days_first' => [
'name' => __('First Warning (Days Before)', 'wc-licensed-product'),
'type' => 'number',
'desc' => __('Days before expiration to send the first warning email.', 'wc-licensed-product'),
'id' => 'wc_licensed_product_expiration_warning_days_first',
'default' => '7',
'custom_attributes' => [
'min' => '1',
'step' => '1',
],
],
'expiration_warning_days_second' => [
'name' => __('Second Warning (Days Before)', 'wc-licensed-product'),
'type' => 'number',
'desc' => __('Days before expiration to send the second warning email. Set to 0 to disable.', 'wc-licensed-product'),
'id' => 'wc_licensed_product_expiration_warning_days_second',
'default' => '1',
'custom_attributes' => [
'min' => '0',
'step' => '1',
],
],
'email_section_end' => [
'type' => 'sectionend',
'id' => 'wc_licensed_product_section_email_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';
}
/**
* Check if expiration warning emails are enabled
* This checks both the WooCommerce email setting and the old setting for backwards compatibility
*/
public static function isExpirationEmailsEnabled(): bool
{
// Check WooCommerce email enabled status
$emailEnabled = get_option('woocommerce_wclp_license_expiration_settings');
if (is_array($emailEnabled) && isset($emailEnabled['enabled'])) {
return $emailEnabled['enabled'] === 'yes';
}
// Default to enabled if not yet configured
return true;
}
/**
* Get first warning days before expiration
*/
public static function getFirstWarningDays(): int
{
$value = get_option('wc_licensed_product_expiration_warning_days_first', 7);
return max(1, (int) $value);
}
/**
* Get second warning days before expiration
*/
public static function getSecondWarningDays(): int
{
$value = get_option('wc_licensed_product_expiration_warning_days_second', 1);
return max(0, (int) $value);
}
}