| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Search Utility |
| 5 |
* |
| 6 |
* @package Disco |
| 7 |
* @subpackage App\Utility |
| 8 |
* @since 1.0.0 |
| 9 |
* @category Utility |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Disco\App\Utility; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class Settings |
| 16 |
* |
| 17 |
* @package Disco |
| 18 |
* @subpackage App\Utility |
| 19 |
* @author Ohidul Islam <wahid0003@gmail.com> |
| 20 |
* @link https://webappick.com |
| 21 |
* @license https://opensource.org/licenses/gpl-license.php GNU Public License |
| 22 |
* @category Utility |
| 23 |
*/ |
| 24 |
class Settings { |
| 25 |
|
| 26 |
/** |
| 27 |
* Get the product price from meta according to settings. |
| 28 |
* |
| 29 |
* @param int $product_id Product id. |
| 30 |
* @return float Product Price. |
| 31 |
*/ |
| 32 |
public static function get_price_from_meta( $product_id ): float { |
| 33 |
$price = self::get( 'product_price_type' ); |
| 34 |
|
| 35 |
if ( 'price' === $price ) { |
| 36 |
$meta_value = get_post_meta( $product_id, '_price', true ); |
| 37 |
|
| 38 |
return is_numeric( $meta_value ) ? (float) $meta_value : 0.0; //phpcs:ignore |
| 39 |
} |
| 40 |
|
| 41 |
if ( 'regular_price' === $price ) { |
| 42 |
$meta_value = get_post_meta( $product_id, '_regular_price', true ); |
| 43 |
|
| 44 |
return is_numeric( $meta_value ) ? (float) $meta_value : 0.0; //phpcs:ignore |
| 45 |
} |
| 46 |
|
| 47 |
if ( 'sale_price' === $price ) { |
| 48 |
$meta_value = get_post_meta( $product_id, '_sale_price', true ); |
| 49 |
|
| 50 |
return is_numeric( $meta_value ) ? (float) $meta_value : 0.0; //phpcs:ignore |
| 51 |
} |
| 52 |
|
| 53 |
// @phpstan-ignore-next-line |
| 54 |
return (float) get_post_meta( $product_id, '_price', true ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Get the product price according to settings. |
| 59 |
* |
| 60 |
* @param \WC_Product $product Product Object. |
| 61 |
* @return float Product Price. |
| 62 |
*/ |
| 63 |
public static function get_price( \WC_Product $product ): float { |
| 64 |
$price = self::get( 'product_price_type' ); |
| 65 |
|
| 66 |
if ( 'price' === $price ) { |
| 67 |
return (float) $product->get_price(); |
| 68 |
} |
| 69 |
|
| 70 |
if ( 'regular_price' === $price ) { |
| 71 |
return (float) $product->get_regular_price(); |
| 72 |
} |
| 73 |
|
| 74 |
if ( 'sale_price' === $price ) { |
| 75 |
return (float) $product->get_sale_price(); |
| 76 |
} |
| 77 |
|
| 78 |
return (float) $product->get_price(); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Get settings. |
| 83 |
* If the key is not found, return all settings. |
| 84 |
* If the key is 'all', return all settings. |
| 85 |
* If the key is 'default', return default settings. |
| 86 |
* If the key is found, return the value. |
| 87 |
* If the key is not found, return false. |
| 88 |
* |
| 89 |
* @param string $key Settings Key. |
| 90 |
* @return array|string|\WP_Error Settings. |
| 91 |
*/ |
| 92 |
public static function get( $key = 'all' ) { |
| 93 |
$default = array( |
| 94 |
'product_price_type' => 'price', // price, regular_price, sale_price. |
| 95 |
'min_max_discount_amount' => 'min', // min, max. |
| 96 |
'discount_priority_type' => 'both', // both, disco_campaign |
| 97 |
'on_sale_badge' => 'show_all', // show_all, show_sale. |
| 98 |
'show_strike_through' => array( |
| 99 |
'shop_page' => true, |
| 100 |
'product_pages' => true, |
| 101 |
'category_page' => true, |
| 102 |
'cart_page' => true, |
| 103 |
), |
| 104 |
'live_chat_support' => false, |
| 105 |
); |
| 106 |
|
| 107 |
/** |
| 108 |
* Add defaults without changing the core values. |
| 109 |
* |
| 110 |
* @param array $defaults |
| 111 |
* @since 3.3.11 |
| 112 |
*/ |
| 113 |
$default = wp_parse_args( apply_filters( 'disco_settings', $default ), $default ); |
| 114 |
|
| 115 |
if ( 'default' === $key ) { |
| 116 |
return $default; |
| 117 |
} |
| 118 |
|
| 119 |
$get_settings = get_option( 'disco_settings', array() ); |
| 120 |
|
| 121 |
$settings = wp_parse_args( $get_settings, $default ); |
| 122 |
|
| 123 |
if ( 'all' === $key || empty( $key ) ) { |
| 124 |
return $settings; |
| 125 |
} |
| 126 |
|
| 127 |
if ( array_key_exists( $key, $settings ) ) { |
| 128 |
return $settings[ $key ]; |
| 129 |
} |
| 130 |
|
| 131 |
return new \WP_Error( |
| 132 |
'rest_not_found', |
| 133 |
__( 'Sorry, Invalid Settings Key.', 'disco' ), |
| 134 |
array( 'status' => 400 ) |
| 135 |
); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Set settings. |
| 140 |
* |
| 141 |
* @param string $key Settings Key. |
| 142 |
* @param string $value Settings Value. |
| 143 |
* @return bool|\WP_Error |
| 144 |
* @throws \Exception Invalid Settings Key. |
| 145 |
* @since 1.0.0 |
| 146 |
*/ |
| 147 |
public static function set( $key, $value ) { |
| 148 |
$setting = self::get( 'all' ); |
| 149 |
|
| 150 |
if ( is_wp_error( $setting ) ) { |
| 151 |
return $setting; |
| 152 |
} |
| 153 |
|
| 154 |
if ( is_array( $setting ) && isset( $setting[ $key ] ) ) { |
| 155 |
$setting[ $key ] = $value; |
| 156 |
|
| 157 |
return self::save( $setting ); |
| 158 |
} |
| 159 |
|
| 160 |
return new \WP_Error( |
| 161 |
'rest_not_found', |
| 162 |
__( 'Sorry, Invalid Settings Key.', 'disco' ), |
| 163 |
array( 'status' => 400 ) |
| 164 |
); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Save settings. |
| 169 |
* |
| 170 |
* @param array $args Settings. |
| 171 |
* @return bool |
| 172 |
* @throws \Exception Invalid Settings Key. |
| 173 |
* @since 1.0.0 |
| 174 |
*/ |
| 175 |
public static function save( $args ) { |
| 176 |
$settings = get_option( 'disco_settings', self::Get() ); |
| 177 |
|
| 178 |
$new_settings = wp_parse_args( $args, $settings ); |
| 179 |
|
| 180 |
if ( $new_settings === $settings ) { |
| 181 |
return true; |
| 182 |
} |
| 183 |
|
| 184 |
return update_option( 'disco_settings', $new_settings ); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Get cached WooCommerce tax display settings. |
| 189 |
* Uses static caching to avoid repeated get_option() calls. |
| 190 |
* |
| 191 |
* @return array{enabled: bool, include_tax: bool} Tax settings. |
| 192 |
* @since 1.0.0 |
| 193 |
*/ |
| 194 |
public static function get_tax_settings(): array { |
| 195 |
static $settings = null; |
| 196 |
|
| 197 |
if ( $settings === null ) { |
| 198 |
$settings = array( |
| 199 |
'enabled' => wc_tax_enabled(), |
| 200 |
'include_tax' => get_option( 'woocommerce_tax_display_shop', 'excl' ) === 'incl', |
| 201 |
); |
| 202 |
} |
| 203 |
|
| 204 |
return $settings; |
| 205 |
} |
| 206 |
|
| 207 |
} |
| 208 |
|