| 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 |
* One-time flag: archive product-card buttons used to default on. |
| 18 |
*/ |
| 19 |
private const ARCHIVE_BUTTON_OPT_IN_FLAG = 'king_addons_wishlist_archive_button_opt_in'; |
| 20 |
|
| 21 |
/** |
| 22 |
* One-time flag: single-product auto buttons used to always inject. |
| 23 |
*/ |
| 24 |
private const SINGLE_BUTTON_OPT_IN_FLAG = 'king_addons_wishlist_single_button_opt_in'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Get merged wishlist settings with defaults. |
| 28 |
* |
| 29 |
* @return array<string, mixed> Settings. |
| 30 |
*/ |
| 31 |
public static function get_settings(): array |
| 32 |
{ |
| 33 |
$saved = get_option(self::OPTION_KEY, []); |
| 34 |
return wp_parse_args(is_array($saved) ? $saved : [], self::defaults()); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Turn off auto-injected product-card buttons unless the site opts in. |
| 39 |
* |
| 40 |
* Older versions defaulted this on, so a plugin update put a wishlist |
| 41 |
* button on every shop card. Sites that want it can enable “Show on |
| 42 |
* Product Cards” in King Addons → Wishlist → Buttons. |
| 43 |
* |
| 44 |
* @return void |
| 45 |
*/ |
| 46 |
public static function maybe_disable_archive_buttons(): void |
| 47 |
{ |
| 48 |
if ('1' !== (string) get_option(self::ARCHIVE_BUTTON_OPT_IN_FLAG, '')) { |
| 49 |
$saved = get_option(self::OPTION_KEY, []); |
| 50 |
if (is_array($saved) && array_key_exists('show_in_archives', $saved)) { |
| 51 |
$saved['show_in_archives'] = false; |
| 52 |
update_option(self::OPTION_KEY, $saved, false); |
| 53 |
} |
| 54 |
|
| 55 |
update_option(self::ARCHIVE_BUTTON_OPT_IN_FLAG, '1', false); |
| 56 |
} |
| 57 |
|
| 58 |
self::maybe_disable_single_buttons(); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Turn off auto-injected single-product buttons unless the site opts in. |
| 63 |
* |
| 64 |
* Older versions always hooked next to Add to Cart / Buy Now, including |
| 65 |
* the King Addons Add to Cart widget. Sites that want that can enable |
| 66 |
* “Show on Single Product” in King Addons → Wishlist → Buttons. |
| 67 |
* |
| 68 |
* @return void |
| 69 |
*/ |
| 70 |
public static function maybe_disable_single_buttons(): void |
| 71 |
{ |
| 72 |
if ('1' === (string) get_option(self::SINGLE_BUTTON_OPT_IN_FLAG, '')) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
$saved = get_option(self::OPTION_KEY, []); |
| 77 |
if (!is_array($saved)) { |
| 78 |
$saved = []; |
| 79 |
} |
| 80 |
|
| 81 |
$saved['show_on_single'] = false; |
| 82 |
update_option(self::OPTION_KEY, $saved, false); |
| 83 |
update_option(self::SINGLE_BUTTON_OPT_IN_FLAG, '1', false); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Get a specific setting value. |
| 88 |
* |
| 89 |
* @param string $key Setting key. |
| 90 |
* @param mixed $default Default fallback. |
| 91 |
* @return mixed Setting value. |
| 92 |
*/ |
| 93 |
public static function get(string $key, $default = null) |
| 94 |
{ |
| 95 |
$settings = self::get_settings(); |
| 96 |
return $settings[$key] ?? $default; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Persist wishlist settings. |
| 101 |
* |
| 102 |
* @param array<string, mixed> $settings Settings to store. |
| 103 |
* @return void |
| 104 |
*/ |
| 105 |
public static function save(array $settings): void |
| 106 |
{ |
| 107 |
update_option(self::OPTION_KEY, wp_parse_args($settings, self::defaults())); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Determine if wishlist is enabled. |
| 112 |
* |
| 113 |
* @return bool Whether wishlist is enabled. |
| 114 |
*/ |
| 115 |
public static function is_enabled(): bool |
| 116 |
{ |
| 117 |
return (bool) self::get('enabled', true); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Check if guests can use wishlist. |
| 122 |
* |
| 123 |
* @return bool Whether guests are allowed. |
| 124 |
*/ |
| 125 |
public static function guests_allowed(): bool |
| 126 |
{ |
| 127 |
return (bool) self::get('allow_guests', true); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Get selected wishlist page id. |
| 132 |
* |
| 133 |
* @return int|null Page id or null. |
| 134 |
*/ |
| 135 |
public static function wishlist_page_id(): ?int |
| 136 |
{ |
| 137 |
$page_id = absint(self::get('wishlist_page_id', 0)); |
| 138 |
return $page_id > 0 ? $page_id : null; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Get default settings. |
| 143 |
* |
| 144 |
* @return array<string, mixed> Default settings. |
| 145 |
*/ |
| 146 |
public static function defaults(): array |
| 147 |
{ |
| 148 |
return [ |
| 149 |
'enabled' => true, |
| 150 |
'wishlist_page_id' => 0, |
| 151 |
'allow_guests' => true, |
| 152 |
'guest_block_text' => esc_html__('Please sign in to use wishlist.', 'king-addons'), |
| 153 |
'button_add_text' => esc_html__('Add to wishlist', 'king-addons'), |
| 154 |
'button_added_text' => esc_html__('In wishlist', 'king-addons'), |
| 155 |
'button_display_mode' => 'icon_text', // icon, icon_text |
| 156 |
'button_position' => 'after_add_to_cart', // before_add_to_cart|after_add_to_cart |
| 157 |
'show_in_archives' => false, |
| 158 |
'show_on_single' => false, |
| 159 |
'wishlist_columns' => ['image', 'title', 'price', 'stock', 'notes', 'add_to_cart', 'remove'], |
| 160 |
'cache_enabled' => false, |
| 161 |
'cache_ttl' => 0, |
| 162 |
'icon_choice' => 'fas fa-heart', |
| 163 |
]; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Get available column options. |
| 168 |
* |
| 169 |
* @return array<string, string> Column key => label pairs. |
| 170 |
*/ |
| 171 |
public static function get_column_options(): array |
| 172 |
{ |
| 173 |
return [ |
| 174 |
'image' => esc_html__('Image', 'king-addons'), |
| 175 |
'title' => esc_html__('Title', 'king-addons'), |
| 176 |
'price' => esc_html__('Price', 'king-addons'), |
| 177 |
'stock' => esc_html__('Stock', 'king-addons'), |
| 178 |
'notes' => esc_html__('Notes (Pro)', 'king-addons'), |
| 179 |
'add_to_cart' => esc_html__('Add to cart', 'king-addons'), |
| 180 |
'remove' => esc_html__('Remove', 'king-addons'), |
| 181 |
]; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|