| 1 |
<?php |
| 2 |
/** |
| 3 |
* Store Defaults resolver. |
| 4 |
* |
| 5 |
* @package WCPOS\WooCommercePOS\Services |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WCPOS\WooCommercePOS\Services; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Resolves the effective values for the free single-store properties that the |
| 16 |
* user can override under WP Admin > POS > Settings > General. |
| 17 |
* |
| 18 |
* Resolution cascade (per field): |
| 19 |
* 1. WCPOS general setting (what the user typed in our settings screen) |
| 20 |
* 2. The matching WooCommerce core "Point of Sale" option, if present |
| 21 |
* (woocommerce_pos_store_name etc., shipped with WC 10.5) |
| 22 |
* 3. A WordPress / WooCommerce fallback (get_bloginfo, admin_email, ...) |
| 23 |
* |
| 24 |
* Pro overrides Store::set_pro_property_defaults() and reads from the per-store |
| 25 |
* CPT, so it does not call this service. |
| 26 |
*/ |
| 27 |
class Store_Defaults { |
| 28 |
/** |
| 29 |
* Effective store name. |
| 30 |
*/ |
| 31 |
public static function name(): string { |
| 32 |
$user = self::pos_setting( 'store_name' ); |
| 33 |
return '' !== $user ? $user : self::name_fallback(); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Effective store phone. |
| 38 |
*/ |
| 39 |
public static function phone(): string { |
| 40 |
$user = self::pos_setting( 'store_phone' ); |
| 41 |
return '' !== $user ? $user : self::phone_fallback(); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Effective store email. |
| 46 |
*/ |
| 47 |
public static function email(): string { |
| 48 |
$user = self::pos_setting( 'store_email' ); |
| 49 |
return '' !== $user ? $user : self::email_fallback(); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Effective refund & returns policy. |
| 54 |
* |
| 55 |
* Maps to the Store's policies_and_conditions property; templates render |
| 56 |
* it as a "Refund & Returns Policy" line on receipts. |
| 57 |
*/ |
| 58 |
public static function policies_and_conditions(): string { |
| 59 |
$user = self::pos_setting( 'policies_and_conditions' ); |
| 60 |
return '' !== $user ? $user : self::policies_and_conditions_fallback(); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Sanitized list of structured store tax IDs. |
| 65 |
* |
| 66 |
* @return array<int,array<string,string>> |
| 67 |
*/ |
| 68 |
public static function tax_ids(): array { |
| 69 |
$general = woocommerce_pos_get_settings( 'general' ); |
| 70 |
if ( ! \is_array( $general ) || ! isset( $general['store_tax_ids'] ) ) { |
| 71 |
return array(); |
| 72 |
} |
| 73 |
return Settings::sanitize_store_tax_ids( $general['store_tax_ids'] ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Resolved fallback values used as placeholders in the React settings UI |
| 78 |
* when the corresponding WCPOS setting is empty. |
| 79 |
* |
| 80 |
* @return array<string,string> |
| 81 |
*/ |
| 82 |
public static function fallbacks(): array { |
| 83 |
return array( |
| 84 |
'store_name' => self::name_fallback(), |
| 85 |
'store_phone' => self::phone_fallback(), |
| 86 |
'store_email' => self::email_fallback(), |
| 87 |
'policies_and_conditions' => self::policies_and_conditions_fallback(), |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Fallback for the store name, ignoring the user's own setting. |
| 93 |
*/ |
| 94 |
public static function name_fallback(): string { |
| 95 |
$wc = self::wc_pos_option( 'woocommerce_pos_store_name' ); |
| 96 |
return '' !== $wc ? $wc : (string) \get_bloginfo( 'name' ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Fallback for the store phone, ignoring the user's own setting. |
| 101 |
*/ |
| 102 |
public static function phone_fallback(): string { |
| 103 |
return self::wc_pos_option( 'woocommerce_pos_store_phone' ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Fallback for the store email, ignoring the user's own setting. |
| 108 |
*/ |
| 109 |
public static function email_fallback(): string { |
| 110 |
$wc = self::wc_pos_option( 'woocommerce_pos_store_email' ); |
| 111 |
if ( '' !== $wc ) { |
| 112 |
return $wc; |
| 113 |
} |
| 114 |
$from = (string) \get_option( 'woocommerce_email_from_address', '' ); |
| 115 |
if ( '' !== $from ) { |
| 116 |
return $from; |
| 117 |
} |
| 118 |
return (string) \get_option( 'admin_email', '' ); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Fallback for the refund & returns policy, ignoring the user's own setting. |
| 123 |
*/ |
| 124 |
public static function policies_and_conditions_fallback(): string { |
| 125 |
return self::wc_pos_option( 'woocommerce_pos_refund_returns_policy' ); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Read a string value from the WCPOS general settings. |
| 130 |
* |
| 131 |
* @param string $key Setting key. |
| 132 |
*/ |
| 133 |
private static function pos_setting( string $key ): string { |
| 134 |
$general = woocommerce_pos_get_settings( 'general' ); |
| 135 |
if ( ! \is_array( $general ) || ! isset( $general[ $key ] ) || ! \is_string( $general[ $key ] ) ) { |
| 136 |
return ''; |
| 137 |
} |
| 138 |
return trim( $general[ $key ] ); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Read a trimmed string from a WordPress option. |
| 143 |
* |
| 144 |
* @param string $option_name WordPress option name. |
| 145 |
*/ |
| 146 |
private static function wc_pos_option( string $option_name ): string { |
| 147 |
$value = \get_option( $option_name, '' ); |
| 148 |
return \is_string( $value ) ? trim( $value ) : ''; |
| 149 |
} |
| 150 |
} |
| 151 |
|