| 1 |
<?php |
| 2 |
/** |
| 3 |
* Smart Links settings helpers. |
| 4 |
* |
| 5 |
* @package King_Addons |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace King_Addons\Smart_Links; |
| 9 |
|
| 10 |
if (!defined('ABSPATH')) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
class Smart_Links_Settings |
| 15 |
{ |
| 16 |
private const OPTION_KEY = 'king_addons_smart_links_settings'; |
| 17 |
|
| 18 |
public static function get_settings(): array |
| 19 |
{ |
| 20 |
$saved = get_option(self::OPTION_KEY, []); |
| 21 |
return wp_parse_args($saved, self::defaults()); |
| 22 |
} |
| 23 |
|
| 24 |
public static function get(string $key, $default = null) |
| 25 |
{ |
| 26 |
$settings = self::get_settings(); |
| 27 |
return $settings[$key] ?? $default; |
| 28 |
} |
| 29 |
|
| 30 |
public static function defaults(): array |
| 31 |
{ |
| 32 |
return [ |
| 33 |
'base_path' => 'go', |
| 34 |
'default_slug_length' => 6, |
| 35 |
'allow_manual_slug' => true, |
| 36 |
'default_redirect_type' => '302', |
| 37 |
'tracking_enabled' => true, |
| 38 |
'exclude_bots' => false, |
| 39 |
'unique_click_window' => 'daily', |
| 40 |
'retention_days' => 30, |
| 41 |
'pass_query_params' => true, |
| 42 |
'whitelist_query_params' => '', |
| 43 |
'blacklist_query_params' => '', |
| 44 |
'cache_ttl' => 0, |
| 45 |
]; |
| 46 |
} |
| 47 |
|
| 48 |
public static function sanitize(array $settings): array |
| 49 |
{ |
| 50 |
$defaults = self::defaults(); |
| 51 |
$current = self::get_settings(); |
| 52 |
|
| 53 |
$base_path = sanitize_title_with_dashes($settings['base_path'] ?? $defaults['base_path']); |
| 54 |
$base_path = trim($base_path, '/'); |
| 55 |
if ($base_path === '') { |
| 56 |
$base_path = $defaults['base_path']; |
| 57 |
} |
| 58 |
|
| 59 |
$redirect_type = ($settings['default_redirect_type'] ?? $defaults['default_redirect_type']); |
| 60 |
$redirect_type = in_array($redirect_type, ['301', '302'], true) ? $redirect_type : $defaults['default_redirect_type']; |
| 61 |
|
| 62 |
$unique_window = ($settings['unique_click_window'] ?? $defaults['unique_click_window']); |
| 63 |
$unique_window = in_array($unique_window, ['daily', 'rolling'], true) ? $unique_window : $defaults['unique_click_window']; |
| 64 |
|
| 65 |
$sanitized = [ |
| 66 |
'base_path' => $base_path, |
| 67 |
'default_slug_length' => max(4, min(20, absint($settings['default_slug_length'] ?? $defaults['default_slug_length']))), |
| 68 |
'allow_manual_slug' => !empty($settings['allow_manual_slug']), |
| 69 |
'default_redirect_type' => $redirect_type, |
| 70 |
'tracking_enabled' => !empty($settings['tracking_enabled']), |
| 71 |
'exclude_bots' => !empty($settings['exclude_bots']), |
| 72 |
'unique_click_window' => $unique_window, |
| 73 |
'retention_days' => max(0, absint($settings['retention_days'] ?? $defaults['retention_days'])), |
| 74 |
'pass_query_params' => !empty($settings['pass_query_params']), |
| 75 |
'whitelist_query_params' => sanitize_text_field($settings['whitelist_query_params'] ?? ''), |
| 76 |
'blacklist_query_params' => sanitize_text_field($settings['blacklist_query_params'] ?? ''), |
| 77 |
'cache_ttl' => max(0, absint($settings['cache_ttl'] ?? 0)), |
| 78 |
]; |
| 79 |
|
| 80 |
if ($current['base_path'] !== $sanitized['base_path']) { |
| 81 |
update_option('king_addons_smart_links_flush_rewrite', 1); |
| 82 |
} |
| 83 |
|
| 84 |
return wp_parse_args($sanitized, $defaults); |
| 85 |
} |
| 86 |
|
| 87 |
public static function get_base_path(): string |
| 88 |
{ |
| 89 |
return (string) self::get('base_path', 'go'); |
| 90 |
} |
| 91 |
|
| 92 |
public static function get_short_base_url(): string |
| 93 |
{ |
| 94 |
$base = trim(self::get_base_path(), '/'); |
| 95 |
return home_url(user_trailingslashit($base)); |
| 96 |
} |
| 97 |
} |
| 98 |
|