| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\Admin\Settings; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
class Base { |
| 10 |
|
| 11 |
public static function get_settings_saved_data(): array { |
| 12 |
$settings = get_option( STOREENGINE_SETTINGS_NAME ); |
| 13 |
if ( $settings && ! is_array( $settings ) ) { |
| 14 |
$decoded = json_decode( $settings, true ); |
| 15 |
|
| 16 |
if ( json_last_error() === JSON_ERROR_NONE && is_array( $decoded ) ) { |
| 17 |
return $decoded; |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
if ( is_array( $settings ) ) { |
| 22 |
return $settings; |
| 23 |
} |
| 24 |
|
| 25 |
return []; |
| 26 |
} |
| 27 |
|
| 28 |
public static function get_settings_default_data() { |
| 29 |
return apply_filters( 'storeengine/admin/settings_default_data', [ |
| 30 |
// General |
| 31 |
'store_name' => 'StoreEngine', |
| 32 |
'store_email' => get_option( 'admin_email' ), |
| 33 |
'store_currency' => 'USD', |
| 34 |
'store_currency_position' => 'left', |
| 35 |
'store_currency_thousand_separator' => ',', |
| 36 |
'store_currency_decimal_separator' => '.', |
| 37 |
'store_currency_decimal_limit' => 2, |
| 38 |
'store_country' => '', |
| 39 |
'store_address_1' => '', |
| 40 |
'store_address_2' => '', |
| 41 |
'store_city' => '', |
| 42 |
'store_state' => '', |
| 43 |
'store_postcode' => '', |
| 44 |
// Brand & Style |
| 45 |
'store_logo' => '', |
| 46 |
'global_primary_color' => '#008DFF', |
| 47 |
'global_secondary_color' => '#FF5000', |
| 48 |
'global_text_color' => '#2C3135', |
| 49 |
'global_subtitle_color' => '#646C73', |
| 50 |
'global_input_text_color' => '#434A51', |
| 51 |
'global_border_color' => '#ebebeb', |
| 52 |
'global_background_color' => '#FAFAFA', |
| 53 |
'global_placeholder_color' => '#8E949A', |
| 54 |
// Product |
| 55 |
'default_product_shipping_type' => 'digital', |
| 56 |
'enable_direct_checkout' => false, |
| 57 |
'hide_quantity_selector' => false, |
| 58 |
'hide_add_to_cart' => false, |
| 59 |
'enable_product_reviews' => false, |
| 60 |
'enable_product_comments' => false, |
| 61 |
// Who may leave a product review: 'completed_order' (bought & order |
| 62 |
// completed), 'purchased' (ordered, any status), or 'everyone'. |
| 63 |
'review_permission' => 'completed_order', |
| 64 |
// Max images/videos a reviewer can attach (0 = unlimited). |
| 65 |
'review_media_max' => 5, |
| 66 |
// New reviews: 'auto' approve immediately, or 'pending' for manual |
| 67 |
// approval from Products → Reviews. |
| 68 |
'review_approval' => 'auto', |
| 69 |
'enable_related_products' => false, |
| 70 |
// FAQ system. `faq_mode`: 'global' shows the FAQ Groups library + |
| 71 |
// rule targeting; 'product_only' hides the library and limits the |
| 72 |
// product editor to inline Q&A. |
| 73 |
'enable_faqs' => true, |
| 74 |
'faq_mode' => 'global', |
| 75 |
'enable_product_tax' => false, |
| 76 |
// SKU & Barcode auto-generation |
| 77 |
'auto_generate_sku' => false, |
| 78 |
'sku_pattern' => '{category}-{number}', |
| 79 |
'sku_number_padding' => 4, |
| 80 |
'auto_generate_barcode' => false, |
| 81 |
'barcode_ean_prefix' => '20', |
| 82 |
// Product Archive |
| 83 |
'product_archive_sidebar_position' => 'right', |
| 84 |
'product_archive_filters' => [ |
| 85 |
'search' => [ |
| 86 |
'status' => true, |
| 87 |
'order' => 0, |
| 88 |
], |
| 89 |
'category' => [ |
| 90 |
'status' => true, |
| 91 |
'order' => 1, |
| 92 |
], |
| 93 |
'tags' => [ |
| 94 |
'status' => true, |
| 95 |
'order' => 2, |
| 96 |
], |
| 97 |
], |
| 98 |
'product_archive_products_per_row' => [ |
| 99 |
'desktop' => 3, |
| 100 |
'tablet' => 2, |
| 101 |
'mobile' => 1, |
| 102 |
], |
| 103 |
'product_archive_products_per_page' => 12, |
| 104 |
'product_archive_products_order' => '', |
| 105 |
'product_archive_multi_price_display' => 'dropdown', |
| 106 |
'product_single_price_display' => 'radio', |
| 107 |
'single_product_gallery_layout' => 'carousel', |
| 108 |
// Product archive/shop card enhancements (opt-in). |
| 109 |
'product_archive_card_carousel' => false, |
| 110 |
'product_archive_card_swatches' => false, |
| 111 |
'product_archive_quick_view' => false, |
| 112 |
'quick_view_position' => 'center', // center|left|right |
| 113 |
'quick_view_animation' => 'fade', // fade|zoom|slide|none |
| 114 |
'enable_recently_viewed' => true, |
| 115 |
'enable_size_guide' => false, |
| 116 |
'enable_wishlist' => false, |
| 117 |
'enable_product_compare' => false, |
| 118 |
// Page |
| 119 |
'shop_page' => '', |
| 120 |
'cart_page' => '', |
| 121 |
'checkout_page' => '', |
| 122 |
'thankyou_page' => '', |
| 123 |
'dashboard_page' => '', |
| 124 |
'membership_pricing_page' => '', |
| 125 |
'affiliate_registration_page' => '', |
| 126 |
// Geolocation |
| 127 |
'maxmind_license' => '', |
| 128 |
'default_customer_address' => '', |
| 129 |
'enable_caching_support' => false, |
| 130 |
// Selling / shipping country restrictions |
| 131 |
'allowed_countries' => 'all', |
| 132 |
'all_except_countries' => [], |
| 133 |
'specific_allowed_countries' => [], |
| 134 |
'ship_to_countries' => '', |
| 135 |
'specific_ship_to_countries' => [], |
| 136 |
// Tax settings |
| 137 |
'prices_include_tax' => false, |
| 138 |
'tax_based_on' => 'shipping', |
| 139 |
'shipping_tax_class' => '', |
| 140 |
'tax_round_at_subtotal' => false, |
| 141 |
'tax_classes' => '', |
| 142 |
'tax_display_shop' => 'excl', |
| 143 |
'tax_display_cart' => 'excl', |
| 144 |
'price_display_suffix' => '', |
| 145 |
'tax_total_display' => 'itemized', |
| 146 |
// Stripe Automatic Tax (delegates calculation to Stripe Tax Calculations API). |
| 147 |
'enable_stripe_tax' => false, |
| 148 |
'stripe_tax_default_code' => 'txcd_99999999', |
| 149 |
'stripe_tax_shipping_code' => 'txcd_92010001', |
| 150 |
'stripe_tax_fallback_to_local' => false, |
| 151 |
'auth_redirect_type' => 'storeengine', |
| 152 |
'auth_redirect_url' => '', |
| 153 |
'checkout_default_country' => 'US', |
| 154 |
'enable_floating_cart' => true, |
| 155 |
'auto_open_cart_drawer' => false, |
| 156 |
'sticky_add_to_cart' => true, |
| 157 |
'enable_after_purchase_redirect' => false, |
| 158 |
// checkout customization |
| 159 |
'checkout_fields' => [ |
| 160 |
'email' => [ |
| 161 |
'system' => true, |
| 162 |
'required' => true, |
| 163 |
], |
| 164 |
// billing |
| 165 |
'billing_first_name' => [ |
| 166 |
'required' => true, |
| 167 |
'enabled' => true, |
| 168 |
], |
| 169 |
'billing_last_name' => [ |
| 170 |
'required' => true, |
| 171 |
'enabled' => true, |
| 172 |
], |
| 173 |
'billing_address_line' => [ |
| 174 |
'required' => true, |
| 175 |
'enabled' => true, |
| 176 |
], |
| 177 |
'billing_address_line_2' => [ |
| 178 |
'required' => false, |
| 179 |
'enabled' => false, |
| 180 |
], |
| 181 |
'billing_country' => [ |
| 182 |
'required' => true, |
| 183 |
'enabled' => true, |
| 184 |
], |
| 185 |
'billing_city' => [ |
| 186 |
'required' => true, |
| 187 |
'enabled' => true, |
| 188 |
], |
| 189 |
'billing_state' => [ |
| 190 |
'required' => false, |
| 191 |
'enabled' => false, |
| 192 |
], |
| 193 |
'billing_apt' => [ |
| 194 |
'required' => false, |
| 195 |
'enabled' => false, |
| 196 |
], |
| 197 |
'billing_post_code' => [ |
| 198 |
'required' => false, |
| 199 |
'enabled' => false, |
| 200 |
], |
| 201 |
'billing_phone' => [ |
| 202 |
'required' => false, |
| 203 |
'enabled' => false, |
| 204 |
], |
| 205 |
// Shipping |
| 206 |
'shipping_first_name' => [ |
| 207 |
'system' => true, |
| 208 |
'enabled' => true, |
| 209 |
], |
| 210 |
'shipping_last_name' => [ |
| 211 |
'system' => true, |
| 212 |
'enabled' => true, |
| 213 |
], |
| 214 |
'shipping_address_line' => [ |
| 215 |
'required' => true, |
| 216 |
'enabled' => true, |
| 217 |
], |
| 218 |
'shipping_address_line_2' => [ |
| 219 |
'required' => false, |
| 220 |
'enabled' => false, |
| 221 |
], |
| 222 |
'shipping_country' => [ |
| 223 |
'required' => true, |
| 224 |
'enabled' => true, |
| 225 |
], |
| 226 |
'shipping_city' => [ |
| 227 |
'required' => true, |
| 228 |
'enabled' => true, |
| 229 |
], |
| 230 |
'shipping_state' => [ |
| 231 |
'required' => false, |
| 232 |
'enabled' => false, |
| 233 |
], |
| 234 |
'shipping_apt' => [ |
| 235 |
'required' => false, |
| 236 |
'enabled' => false, |
| 237 |
], |
| 238 |
'shipping_post_code' => [ |
| 239 |
'required' => false, |
| 240 |
'enabled' => false, |
| 241 |
], |
| 242 |
'shipping_phone' => [ |
| 243 |
'required' => false, |
| 244 |
'enabled' => false, |
| 245 |
], |
| 246 |
], |
| 247 |
// Analytics |
| 248 |
'analytics' => [ |
| 249 |
'google' => false, |
| 250 |
'facebook' => false, |
| 251 |
], |
| 252 |
] ); |
| 253 |
} |
| 254 |
|
| 255 |
protected static function prepare_settings_data( array $new_data = [] ): array { |
| 256 |
$default_data = self::get_settings_default_data(); |
| 257 |
$saved_data = self::get_settings_saved_data(); |
| 258 |
$settings_data = wp_parse_args( $saved_data, $default_data ); |
| 259 |
if ( $new_data ) { |
| 260 |
$settings_data = wp_parse_args( $new_data, $settings_data ); |
| 261 |
} |
| 262 |
|
| 263 |
return $settings_data; |
| 264 |
} |
| 265 |
|
| 266 |
public static function save_settings( array $form_data = [] ): bool { |
| 267 |
return update_option( STOREENGINE_SETTINGS_NAME, wp_json_encode( self::prepare_settings_data( $form_data ) ) ); |
| 268 |
} |
| 269 |
|
| 270 |
public static function delete_settings( string $key ): void { |
| 271 |
$settings = self::prepare_settings_data(); |
| 272 |
|
| 273 |
if ( ! array_key_exists( $key, $settings ) ) { |
| 274 |
return; |
| 275 |
} |
| 276 |
|
| 277 |
unset( $settings[ $key ] ); |
| 278 |
|
| 279 |
update_option( STOREENGINE_SETTINGS_NAME, wp_json_encode( $settings ) ); |
| 280 |
} |
| 281 |
} |
| 282 |
|