Files
2025-11-18 23:16:28 +01:00

617 lines
23 KiB
PHP

<?php
/**
* Admin Settings
*
* Handles plugin settings page in WordPress admin
*/
// Exit if accessed directly
if (!defined('ABSPATH')) {
exit;
}
class WHMCS_Pricing_Admin {
/**
* Single instance
*/
private static $instance = null;
/**
* Get instance
*/
public static function get_instance() {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor
*/
private function __construct() {
add_action('admin_menu', array($this, 'add_admin_menu'));
add_action('admin_init', array($this, 'register_settings'));
add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_styles'));
add_action('admin_post_whmcs_clear_cache', array($this, 'handle_clear_cache'));
}
/**
* Add admin menu
*/
public function add_admin_menu() {
add_options_page(
__('WHMCS Pricing Settings', 'whmcs-pricing'),
__('WHMCS Pricing', 'whmcs-pricing'),
'manage_options',
'whmcs-pricing',
array($this, 'render_settings_page')
);
}
/**
* Register settings
*/
public function register_settings() {
register_setting(
'whmcs_pricing_settings',
'whmcs_pricing_settings',
array($this, 'sanitize_settings')
);
// API Settings Section
add_settings_section(
'whmcs_pricing_api_section',
__('WHMCS API Configuration', 'whmcs-pricing'),
array($this, 'render_api_section'),
'whmcs-pricing'
);
add_settings_field(
'api_url',
__('WHMCS URL', 'whmcs-pricing'),
array($this, 'render_text_field'),
'whmcs-pricing',
'whmcs_pricing_api_section',
array(
'name' => 'api_url',
'placeholder' => 'https://your-whmcs-domain.com',
'description' => __('Your WHMCS installation URL (without /includes/api.php)', 'whmcs-pricing')
)
);
add_settings_field(
'api_identifier',
__('API Identifier', 'whmcs-pricing'),
array($this, 'render_text_field'),
'whmcs-pricing',
'whmcs_pricing_api_section',
array(
'name' => 'api_identifier',
'description' => __('Your WHMCS API identifier', 'whmcs-pricing')
)
);
add_settings_field(
'api_secret',
__('API Secret', 'whmcs-pricing'),
array($this, 'render_password_field'),
'whmcs-pricing',
'whmcs_pricing_api_section',
array(
'name' => 'api_secret',
'description' => __('Your WHMCS API secret', 'whmcs-pricing')
)
);
// Display Settings Section
add_settings_section(
'whmcs_pricing_display_section',
__('Display Settings', 'whmcs-pricing'),
array($this, 'render_display_section'),
'whmcs-pricing'
);
add_settings_field(
'default_currency',
__('Default Currency', 'whmcs-pricing'),
array($this, 'render_currency_field'),
'whmcs-pricing',
'whmcs_pricing_display_section',
array(
'name' => 'default_currency',
'description' => __('Default currency for pricing display', 'whmcs-pricing')
)
);
add_settings_field(
'show_decimals',
__('Show Decimals', 'whmcs-pricing'),
array($this, 'render_checkbox_field'),
'whmcs-pricing',
'whmcs_pricing_display_section',
array(
'name' => 'show_decimals',
'label' => __('Display prices with decimal places (e.g., €67.00 instead of €67)', 'whmcs-pricing')
)
);
add_settings_field(
'decimal_separator',
__('Decimal Separator', 'whmcs-pricing'),
array($this, 'render_select_field'),
'whmcs-pricing',
'whmcs_pricing_display_section',
array(
'name' => 'decimal_separator',
'options' => array(
'.' => __('Period (.) - Example: 1,234.56', 'whmcs-pricing'),
',' => __('Comma (,) - Example: 1.234,56', 'whmcs-pricing')
),
'description' => __('Choose the character to use as decimal separator', 'whmcs-pricing')
)
);
add_settings_field(
'hide_currency_symbol',
__('Hide Currency Symbol', 'whmcs-pricing'),
array($this, 'render_checkbox_field'),
'whmcs-pricing',
'whmcs_pricing_display_section',
array(
'name' => 'hide_currency_symbol',
'label' => __('Hide currency symbol/prefix from prices', 'whmcs-pricing')
)
);
add_settings_field(
'hide_billing_cycle',
__('Hide Billing Cycle', 'whmcs-pricing'),
array($this, 'render_checkbox_field'),
'whmcs-pricing',
'whmcs_pricing_display_section',
array(
'name' => 'hide_billing_cycle',
'label' => __('Hide billing cycle text (e.g., "per month") from prices', 'whmcs-pricing')
)
);
// Performance Settings Section
add_settings_section(
'whmcs_pricing_performance_section',
__('Performance Settings', 'whmcs-pricing'),
array($this, 'render_performance_section'),
'whmcs-pricing'
);
add_settings_field(
'cache_duration',
__('Cache Duration', 'whmcs-pricing'),
array($this, 'render_number_field'),
'whmcs-pricing',
'whmcs_pricing_performance_section',
array(
'name' => 'cache_duration',
'min' => 300,
'max' => 86400,
'step' => 300,
'description' => __('How long to cache API responses (in seconds). Default: 3600 (1 hour)', 'whmcs-pricing')
)
);
// Advanced Settings Section
add_settings_section(
'whmcs_pricing_advanced_section',
__('Advanced Settings', 'whmcs-pricing'),
array($this, 'render_advanced_section'),
'whmcs-pricing'
);
add_settings_field(
'debug_mode',
__('Debug Mode', 'whmcs-pricing'),
array($this, 'render_checkbox_field'),
'whmcs-pricing',
'whmcs_pricing_advanced_section',
array(
'name' => 'debug_mode',
'label' => __('Enable debug logging and disable cache', 'whmcs-pricing')
)
);
}
/**
* Render settings page
*/
public function render_settings_page() {
if (!current_user_can('manage_options')) {
return;
}
// Show success message
if (isset($_GET['settings-updated'])) {
add_settings_error(
'whmcs_pricing_messages',
'whmcs_pricing_message',
__('Settings saved successfully.', 'whmcs-pricing'),
'updated'
);
}
if (isset($_GET['cache-cleared'])) {
add_settings_error(
'whmcs_pricing_messages',
'whmcs_pricing_message',
__('Cache cleared successfully.', 'whmcs-pricing'),
'updated'
);
}
settings_errors('whmcs_pricing_messages');
?>
<div class="wrap whmcs-pricing-admin">
<h1><?php echo esc_html(get_admin_page_title()); ?></h1>
<div class="whmcs-pricing-container">
<div class="whmcs-pricing-main">
<form action="options.php" method="post">
<?php
settings_fields('whmcs_pricing_settings');
do_settings_sections('whmcs-pricing');
submit_button(__('Save Settings', 'whmcs-pricing'));
?>
</form>
<form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
<?php wp_nonce_field('whmcs_clear_cache', 'whmcs_cache_nonce'); ?>
<input type="hidden" name="action" value="whmcs_clear_cache">
<?php submit_button(__('Clear All Cache', 'whmcs-pricing'), 'secondary', 'clear_cache', false); ?>
</form>
</div>
<div class="whmcs-pricing-sidebar">
<div class="whmcs-pricing-box">
<h3 class="whmcs-toggle" style="cursor: pointer;">
<?php _e('Shortcode Usage', 'whmcs-pricing'); ?> <span style="float: right;">▲</span>
</h3>
<p class="whmcs-preview" style="margin: 10px 0; color: #666; font-size: 13px; display: none;">
<?php _e('Expand to view shortcode examples and parameters...', 'whmcs-pricing'); ?>
</p>
<div class="whmcs-toggle-content">
<p><?php _e('Use these shortcodes in your posts, pages, or Elementor:', 'whmcs-pricing'); ?></p>
<h4><?php _e('Basic Usage:', 'whmcs-pricing'); ?></h4>
<code>[whmcs_price product_id="123"]</code>
<h4><?php _e('With Billing Cycle:', 'whmcs-pricing'); ?></h4>
<code>[whmcs_price product_id="123" billing_cycle="monthly"]</code>
<h4><?php _e('With Currency:', 'whmcs-pricing'); ?></h4>
<code>[whmcs_price product_id="123" currency="EUR"]</code>
<h4><?php _e('Without Decimals:', 'whmcs-pricing'); ?></h4>
<code>[whmcs_price product_id="123" format="no_decimals"]</code>
<h4><?php _e('With Language:', 'whmcs-pricing'); ?></h4>
<code>[whmcs_price product_id="123" lang="dutch"]</code>
<h4><?php _e('Available Billing Cycles:', 'whmcs-pricing'); ?></h4>
<ul>
<li>monthly</li>
<li>quarterly</li>
<li>semiannually</li>
<li>annually</li>
<li>biennially</li>
<li>triennially</li>
</ul>
</div>
</div>
<div class="whmcs-pricing-box">
<h3 class="whmcs-toggle" style="cursor: pointer;">
<?php _e('CSS Styling Guide', 'whmcs-pricing'); ?> <span style="float: right;">▼</span>
</h3>
<p class="whmcs-preview" style="margin: 10px 0; color: #666; font-size: 13px;">
<?php _e('Expand to view CSS examples and styling options...', 'whmcs-pricing'); ?>
</p>
<div class="whmcs-toggle-content" style="display: none;">
<p><?php _e('Add custom CSS to style your prices. Go to Appearance > Customize > Additional CSS:', 'whmcs-pricing'); ?></p>
<h4><?php _e('HTML Structure:', 'whmcs-pricing'); ?></h4>
<code style="white-space: pre-wrap;">&lt;span class="whmcs-price"&gt;
&lt;span class="currency-symbol"&gt;€&lt;/span&gt;
&lt;span class="price-amount"&gt;67.00&lt;/span&gt;
&lt;span class="billing-cycle"&gt;per month&lt;/span&gt;
&lt;/span&gt;</code>
<h4><?php _e('Example CSS:', 'whmcs-pricing'); ?></h4>
<code style="white-space: pre-wrap;">.whmcs-price {
font-size: 24px;
font-weight: bold;
color: #2271b1;
}
.whmcs-price .price-amount {
font-size: 32px;
}
.whmcs-price .billing-cycle {
font-size: 14px;
color: #666;
}</code>
<h4><?php _e('Using Custom Class:', 'whmcs-pricing'); ?></h4>
<code>[whmcs_price product_id="123" class="featured"]</code>
<p style="margin-top: 8px; font-size: 13px;"><?php _e('Then style with: .whmcs-price.featured { }', 'whmcs-pricing'); ?></p>
</div>
</div>
<div class="whmcs-pricing-box">
<h3><?php _e('Plugin Information', 'whmcs-pricing'); ?></h3>
<p><strong><?php _e('Version:', 'whmcs-pricing'); ?></strong> <?php echo WHMCS_PRICING_VERSION; ?></p>
<p><strong><?php _e('Author:', 'whmcs-pricing'); ?></strong> <a href="https://arkhost.com" target="_blank" rel="noopener noreferrer">ArkHost</a></p>
<p><strong><?php _e('Sponsored by:', 'whmcs-pricing'); ?></strong> <a href="https://duplika.com" target="_blank" rel="noopener noreferrer">Duplika</a></p>
</div>
</div>
</div>
</div>
<script>
jQuery(document).ready(function($) {
$('.whmcs-toggle').on('click', function() {
var clickedBox = $(this).parent();
var clickedPreview = clickedBox.find('.whmcs-preview');
var clickedContent = clickedBox.find('.whmcs-toggle-content');
var clickedArrow = $(this).find('span');
// Close all other boxes first
$('.whmcs-pricing-box').not(clickedBox).each(function() {
var otherPreview = $(this).find('.whmcs-preview');
var otherContent = $(this).find('.whmcs-toggle-content');
var otherArrow = $(this).find('.whmcs-toggle span');
if (otherContent.is(':visible')) {
otherPreview.slideDown(200);
otherContent.slideUp(200);
otherArrow.html('▼');
}
});
// Toggle clicked box
clickedPreview.slideToggle(200);
clickedContent.slideToggle(200);
if (clickedContent.is(':visible')) {
clickedArrow.html('▲');
} else {
clickedArrow.html('▼');
}
});
});
</script>
<?php
}
/**
* Section callbacks
*/
public function render_api_section() {
echo '<p>' . esc_html__('Configure your WHMCS API credentials. You can generate API credentials in WHMCS Admin > System Settings > API Credentials.', 'whmcs-pricing') . '</p>';
}
public function render_display_section() {
echo '<p>' . esc_html__('Configure how prices are displayed on your website.', 'whmcs-pricing') . '</p>';
}
public function render_performance_section() {
echo '<p>' . esc_html__('Optimize plugin performance with caching.', 'whmcs-pricing') . '</p>';
}
public function render_advanced_section() {
echo '<p>' . esc_html__('Advanced options for troubleshooting and development.', 'whmcs-pricing') . '</p>';
}
/**
* Field rendering methods
*/
public function render_text_field($args) {
$settings = get_option('whmcs_pricing_settings', array());
$value = isset($settings[$args['name']]) ? $settings[$args['name']] : '';
$placeholder = isset($args['placeholder']) ? $args['placeholder'] : '';
?>
<input type="text"
name="whmcs_pricing_settings[<?php echo esc_attr($args['name']); ?>]"
value="<?php echo esc_attr($value); ?>"
placeholder="<?php echo esc_attr($placeholder); ?>"
class="regular-text">
<?php if (isset($args['description'])): ?>
<p class="description"><?php echo esc_html($args['description']); ?></p>
<?php endif; ?>
<?php
}
public function render_password_field($args) {
$settings = get_option('whmcs_pricing_settings', array());
$value = isset($settings[$args['name']]) ? $settings[$args['name']] : '';
?>
<input type="password"
name="whmcs_pricing_settings[<?php echo esc_attr($args['name']); ?>]"
value="<?php echo esc_attr($value); ?>"
class="regular-text">
<?php if (isset($args['description'])): ?>
<p class="description"><?php echo esc_html($args['description']); ?></p>
<?php endif; ?>
<?php
}
public function render_number_field($args) {
$settings = get_option('whmcs_pricing_settings', array());
$value = isset($settings[$args['name']]) ? $settings[$args['name']] : '';
$min = isset($args['min']) ? $args['min'] : 0;
$max = isset($args['max']) ? $args['max'] : 100000;
$step = isset($args['step']) ? $args['step'] : 1;
?>
<input type="number"
name="whmcs_pricing_settings[<?php echo esc_attr($args['name']); ?>]"
value="<?php echo esc_attr($value); ?>"
min="<?php echo esc_attr($min); ?>"
max="<?php echo esc_attr($max); ?>"
step="<?php echo esc_attr($step); ?>"
class="regular-text">
<?php if (isset($args['description'])): ?>
<p class="description"><?php echo esc_html($args['description']); ?></p>
<?php endif; ?>
<?php
}
public function render_checkbox_field($args) {
$settings = get_option('whmcs_pricing_settings', array());
$checked = isset($settings[$args['name']]) && $settings[$args['name']] ? 'checked' : '';
?>
<label>
<input type="checkbox"
name="whmcs_pricing_settings[<?php echo esc_attr($args['name']); ?>]"
value="1"
<?php echo $checked; ?>>
<?php echo esc_html($args['label']); ?>
</label>
<?php
}
public function render_select_field($args) {
$settings = get_option('whmcs_pricing_settings', array());
$value = isset($settings[$args['name']]) ? $settings[$args['name']] : '';
// Set default value if not set
if (empty($value) && isset($args['default'])) {
$value = $args['default'];
}
?>
<select name="whmcs_pricing_settings[<?php echo esc_attr($args['name']); ?>]" class="regular-text">
<?php foreach ($args['options'] as $option_value => $option_label): ?>
<option value="<?php echo esc_attr($option_value); ?>" <?php selected($value, $option_value); ?>>
<?php echo esc_html($option_label); ?>
</option>
<?php endforeach; ?>
</select>
<?php if (isset($args['description'])): ?>
<p class="description"><?php echo esc_html($args['description']); ?></p>
<?php endif; ?>
<?php
}
public function render_currency_field($args) {
$settings = get_option('whmcs_pricing_settings', array());
$value = isset($settings[$args['name']]) ? $settings[$args['name']] : 'USD';
$currencies = array(
'USD' => 'USD - US Dollar ($)',
'EUR' => 'EUR - Euro (€)',
'GBP' => 'GBP - British Pound (£)',
'AUD' => 'AUD - Australian Dollar (A$)',
'CAD' => 'CAD - Canadian Dollar (C$)',
'JPY' => 'JPY - Japanese Yen (¥)',
'CNY' => 'CNY - Chinese Yuan (¥)',
'INR' => 'INR - Indian Rupee (₹)',
'BRL' => 'BRL - Brazilian Real (R$)',
'RUB' => 'RUB - Russian Ruble (₽)',
'CHF' => 'CHF - Swiss Franc (Fr)',
'SEK' => 'SEK - Swedish Krona (kr)',
'NOK' => 'NOK - Norwegian Krone (kr)',
'DKK' => 'DKK - Danish Krone (kr)',
'PLN' => 'PLN - Polish Zloty (zł)',
'THB' => 'THB - Thai Baht (฿)',
'MYR' => 'MYR - Malaysian Ringgit (RM)',
'SGD' => 'SGD - Singapore Dollar (S$)',
'NZD' => 'NZD - New Zealand Dollar (NZ$)',
'MXN' => 'MXN - Mexican Peso (Mex$)',
'ZAR' => 'ZAR - South African Rand (R)',
'ARS' => 'ARS - Argentine Peso ($)'
);
?>
<select name="whmcs_pricing_settings[<?php echo esc_attr($args['name']); ?>]" class="regular-text">
<?php foreach ($currencies as $code => $label): ?>
<option value="<?php echo esc_attr($code); ?>" <?php selected($value, $code); ?>>
<?php echo esc_html($label); ?>
</option>
<?php endforeach; ?>
</select>
<?php if (isset($args['description'])): ?>
<p class="description"><?php echo esc_html($args['description']); ?></p>
<?php endif; ?>
<?php
}
/**
* Sanitize settings
*/
public function sanitize_settings($input) {
$sanitized = array();
// Sanitize URL
if (isset($input['api_url'])) {
$sanitized['api_url'] = esc_url_raw($input['api_url']);
}
// Sanitize text fields
$text_fields = array('api_identifier', 'api_secret', 'default_currency', 'decimal_separator');
foreach ($text_fields as $field) {
if (isset($input[$field])) {
$sanitized[$field] = sanitize_text_field($input[$field]);
}
}
// Sanitize number
if (isset($input['cache_duration'])) {
$sanitized['cache_duration'] = absint($input['cache_duration']);
}
// Sanitize checkboxes
$sanitized['show_decimals'] = isset($input['show_decimals']) ? true : false;
$sanitized['hide_currency_symbol'] = isset($input['hide_currency_symbol']) ? true : false;
$sanitized['hide_billing_cycle'] = isset($input['hide_billing_cycle']) ? true : false;
$sanitized['debug_mode'] = isset($input['debug_mode']) ? true : false;
return $sanitized;
}
/**
* Handle clear cache action
*/
public function handle_clear_cache() {
if (!current_user_can('manage_options')) {
wp_die(__('Unauthorized', 'whmcs-pricing'));
}
check_admin_referer('whmcs_clear_cache', 'whmcs_cache_nonce');
WHMCS_Pricing_API::clear_all_cache();
wp_redirect(add_query_arg(
array(
'page' => 'whmcs-pricing',
'cache-cleared' => 'true'
),
admin_url('options-general.php')
));
exit;
}
/**
* Enqueue admin styles
*/
public function enqueue_admin_styles($hook) {
if ('settings_page_whmcs-pricing' !== $hook) {
return;
}
wp_enqueue_style(
'whmcs-pricing-admin',
WHMCS_PRICING_PLUGIN_URL . 'assets/css/admin.css',
array(),
WHMCS_PRICING_VERSION
);
}
}