| 1 |
<?php |
| 2 |
|
| 3 |
namespace King_Addons\Wishlist; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
/** |
| 10 |
* Provides typed access to wishlist options and defaults. |
| 11 |
*/ |
| 12 |
class Wishlist_Settings |
| 13 |
{ |
| 14 |
private const OPTION_KEY = 'king_addons_wishlist_settings'; |
| 15 |
|
| 16 |
/** |
| 17 |
* Get merged wishlist settings with defaults. |
| 18 |
* |
| 19 |
* @return array<string, mixed> Settings. |
| 20 |
*/ |
| 21 |
public static function get_settings(): array |
| 22 |
{ |
| 23 |
$saved = get_option(self::OPTION_KEY, []); |
| 24 |
return wp_parse_args($saved, self::defaults()); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Get a specific setting value. |
| 29 |
* |
| 30 |
* @param string $key Setting key. |
| 31 |
* @param mixed $default Default fallback. |
| 32 |
* @return mixed Setting value. |
| 33 |
*/ |
| 34 |
public static function get(string $key, $default = null) |
| 35 |
{ |
| 36 |
$settings = self::get_settings(); |
| 37 |
return $settings[$key] ?? $default; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Persist wishlist settings. |
| 42 |
* |
| 43 |
* @param array<string, mixed> $settings Settings to store. |
| 44 |
* @return void |
| 45 |
*/ |
| 46 |
public static function save(array $settings): void |
| 47 |
{ |
| 48 |
update_option(self::OPTION_KEY, wp_parse_args($settings, self::defaults())); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Determine if wishlist is enabled. |
| 53 |
* |
| 54 |
* @return bool Whether wishlist is enabled. |
| 55 |
*/ |
| 56 |
public static function is_enabled(): bool |
| 57 |
{ |
| 58 |
return (bool) self::get('enabled', true); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Check if guests can use wishlist. |
| 63 |
* |
| 64 |
* @return bool Whether guests are allowed. |
| 65 |
*/ |
| 66 |
public static function guests_allowed(): bool |
| 67 |
{ |
| 68 |
return (bool) self::get('allow_guests', true); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Get selected wishlist page id. |
| 73 |
* |
| 74 |
* @return int|null Page id or null. |
| 75 |
*/ |
| 76 |
public static function wishlist_page_id(): ?int |
| 77 |
{ |
| 78 |
$page_id = absint(self::get('wishlist_page_id', 0)); |
| 79 |
return $page_id > 0 ? $page_id : null; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Get default settings. |
| 84 |
* |
| 85 |
* @return array<string, mixed> Default settings. |
| 86 |
*/ |
| 87 |
public static function defaults(): array |
| 88 |
{ |
| 89 |
return [ |
| 90 |
'enabled' => true, |
| 91 |
'wishlist_page_id' => 0, |
| 92 |
'allow_guests' => true, |
| 93 |
'guest_block_text' => esc_html__('Please sign in to use wishlist.', 'king-addons'), |
| 94 |
'button_add_text' => esc_html__('Add to wishlist', 'king-addons'), |
| 95 |
'button_added_text' => esc_html__('In wishlist', 'king-addons'), |
| 96 |
'button_display_mode' => 'icon_text', // icon, icon_text |
| 97 |
'button_position' => 'after_add_to_cart', // before_add_to_cart|after_add_to_cart |
| 98 |
'show_in_archives' => true, |
| 99 |
'wishlist_columns' => ['image', 'title', 'price', 'stock', 'notes', 'add_to_cart', 'remove'], |
| 100 |
'cache_enabled' => false, |
| 101 |
'cache_ttl' => 0, |
| 102 |
'icon_choice' => 'eicon-heart', |
| 103 |
]; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Get available column options. |
| 108 |
* |
| 109 |
* @return array<string, string> Column key => label pairs. |
| 110 |
*/ |
| 111 |
public static function get_column_options(): array |
| 112 |
{ |
| 113 |
return [ |
| 114 |
'image' => esc_html__('Image', 'king-addons'), |
| 115 |
'title' => esc_html__('Title', 'king-addons'), |
| 116 |
'price' => esc_html__('Price', 'king-addons'), |
| 117 |
'stock' => esc_html__('Stock', 'king-addons'), |
| 118 |
'notes' => esc_html__('Notes (Pro)', 'king-addons'), |
| 119 |
'add_to_cart' => esc_html__('Add to cart', 'king-addons'), |
| 120 |
'remove' => esc_html__('Remove', 'king-addons'), |
| 121 |
]; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|