| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMail\Models; |
| 4 |
|
| 5 |
use YayMail\Utils\SingletonTrait; |
| 6 |
|
| 7 |
/** |
| 8 |
* Setting Model |
| 9 |
* |
| 10 |
* @method static SettingModel get_instance() |
| 11 |
*/ |
| 12 |
class SettingModel { |
| 13 |
use SingletonTrait; |
| 14 |
|
| 15 |
const OPTION_NAME = 'yaymail_settings'; |
| 16 |
|
| 17 |
public const META_KEYS = [ |
| 18 |
'direction' => 'direction', |
| 19 |
'container_width' => 'container_width', |
| 20 |
'payment_display_mode' => 'payment_display_mode', |
| 21 |
'show_product_image' => 'show_product_image', |
| 22 |
'product_image_position' => 'product_image_position', |
| 23 |
'product_image_height' => 'product_image_height', |
| 24 |
'product_image_width' => 'product_image_width', |
| 25 |
'show_product_sku' => 'show_product_sku', |
| 26 |
'show_product_description' => 'show_product_description', |
| 27 |
'show_product_hyper_links' => 'show_product_hyper_links', |
| 28 |
'show_product_regular_price' => 'show_product_regular_price', |
| 29 |
'show_product_item_cost' => 'show_product_item_cost', |
| 30 |
'enable_custom_css' => 'enable_custom_css', |
| 31 |
'custom_css' => 'custom_css', |
| 32 |
'global_header_footer_enabled' => 'global_header_footer_enabled', |
| 33 |
'wp_global_header_footer_enabled' => 'wp_global_header_footer_enabled', |
| 34 |
]; |
| 35 |
|
| 36 |
// TODO: change variable name to be more meaning in db ( when initialize ) |
| 37 |
const DEFAULT = [ |
| 38 |
self::META_KEYS['direction'] => 'ltr', |
| 39 |
self::META_KEYS['container_width'] => 605, |
| 40 |
self::META_KEYS['payment_display_mode'] => 'yes', |
| 41 |
self::META_KEYS['show_product_image'] => false, |
| 42 |
self::META_KEYS['product_image_position'] => 'top', |
| 43 |
self::META_KEYS['product_image_height'] => 30, |
| 44 |
self::META_KEYS['product_image_width'] => 30, |
| 45 |
self::META_KEYS['show_product_sku'] => true, |
| 46 |
self::META_KEYS['show_product_description'] => false, |
| 47 |
self::META_KEYS['show_product_hyper_links'] => false, |
| 48 |
self::META_KEYS['show_product_regular_price'] => false, |
| 49 |
self::META_KEYS['show_product_item_cost'] => false, |
| 50 |
self::META_KEYS['enable_custom_css'] => false, |
| 51 |
self::META_KEYS['custom_css'] => '', |
| 52 |
self::META_KEYS['global_header_footer_enabled'] => false, |
| 53 |
self::META_KEYS['wp_global_header_footer_enabled'] => false, |
| 54 |
]; |
| 55 |
|
| 56 |
public static function find_by_name( $name ) { |
| 57 |
$settings = self::find_all(); |
| 58 |
if ( isset( $settings[ $name ] ) && ! empty( $settings[ $name ] ) ) { |
| 59 |
return $settings[ $name ]; |
| 60 |
} |
| 61 |
return null; |
| 62 |
} |
| 63 |
|
| 64 |
public static function find_all() { |
| 65 |
$default_settings = self::DEFAULT; |
| 66 |
$settings = get_option( self::OPTION_NAME, [] ); |
| 67 |
if ( ! is_array( $settings ) ) { |
| 68 |
return $default_settings; |
| 69 |
} |
| 70 |
return wp_parse_args( $settings, $default_settings ); |
| 71 |
} |
| 72 |
|
| 73 |
public static function update( $settings ) { |
| 74 |
$settings_option = get_option( self::OPTION_NAME ); |
| 75 |
if ( ! empty( $settings ) && is_array( $settings ) ) { |
| 76 |
return update_option( self::OPTION_NAME, wp_parse_args( $settings, $settings_option ) ); |
| 77 |
} |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
public static function delete() { |
| 82 |
delete_option( self::OPTION_NAME ); |
| 83 |
} |
| 84 |
} |
| 85 |
|