| 1 |
<?php |
| 2 |
|
| 3 |
namespace Texty; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
/** |
| 8 |
* Global notification settings. |
| 9 |
* |
| 10 |
* Stores compliance + global SMS toggles in a single option (`texty_notification_settings`). |
| 11 |
* |
| 12 |
* @since TEXTY_VERSION |
| 13 |
*/ |
| 14 |
class NotificationSettings { |
| 15 |
|
| 16 |
/** |
| 17 |
* Option key for notification compliance settings. |
| 18 |
*/ |
| 19 |
const OPTION_KEY = 'texty_notification_settings'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Default settings. |
| 23 |
* |
| 24 |
* @return array |
| 25 |
*/ |
| 26 |
public function defaults() { |
| 27 |
return [ |
| 28 |
'admin_phone' => '', |
| 29 |
'global_sender_id' => '', |
| 30 |
'pause_all' => false, |
| 31 |
'append_company_name' => false, |
| 32 |
]; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Get all settings (merged with defaults). |
| 37 |
* |
| 38 |
* The `admin_phone` value is read-only and is always derived from the |
| 39 |
* active gateway's "From Number" credential — it is never persisted in |
| 40 |
* this option. |
| 41 |
* |
| 42 |
* @return array |
| 43 |
*/ |
| 44 |
public function all() { |
| 45 |
$stored = get_option( self::OPTION_KEY, [] ); |
| 46 |
$stored = is_array( $stored ) ? $stored : []; |
| 47 |
|
| 48 |
$values = wp_parse_args( $stored, $this->defaults() ); |
| 49 |
|
| 50 |
$values['admin_phone'] = $this->gateway_phone(); |
| 51 |
|
| 52 |
return $values; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Get the active gateway's "From Number". |
| 57 |
* |
| 58 |
* @return string |
| 59 |
*/ |
| 60 |
public function gateway_phone() { |
| 61 |
$gateway = texty()->settings()->gateway(); |
| 62 |
|
| 63 |
if ( ! $gateway ) { |
| 64 |
return ''; |
| 65 |
} |
| 66 |
|
| 67 |
$creds = texty()->settings()->get( $gateway ); |
| 68 |
|
| 69 |
if ( ! is_array( $creds ) || empty( $creds['from'] ) ) { |
| 70 |
return ''; |
| 71 |
} |
| 72 |
|
| 73 |
return (string) $creds['from']; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Get a single setting value. |
| 78 |
* |
| 79 |
* @param string $key Setting key. |
| 80 |
* |
| 81 |
* @return mixed |
| 82 |
*/ |
| 83 |
public function get( $key ) { |
| 84 |
$all = $this->all(); |
| 85 |
|
| 86 |
return isset( $all[ $key ] ) ? $all[ $key ] : null; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Persist settings (merging with existing). |
| 91 |
* |
| 92 |
* @param array $values New values. |
| 93 |
* |
| 94 |
* @return array Final stored values. |
| 95 |
*/ |
| 96 |
public function update( array $values ) { |
| 97 |
// admin_phone is derived from the active gateway, never stored. |
| 98 |
unset( $values['admin_phone'] ); |
| 99 |
|
| 100 |
$existing = get_option( self::OPTION_KEY, [] ); |
| 101 |
$existing = is_array( $existing ) ? $existing : []; |
| 102 |
$existing = wp_parse_args( $existing, $this->defaults() ); |
| 103 |
unset( $existing['admin_phone'] ); |
| 104 |
|
| 105 |
$merged = array_merge( $existing, $values ); |
| 106 |
|
| 107 |
update_option( self::OPTION_KEY, $merged, false ); |
| 108 |
|
| 109 |
return $this->all(); |
| 110 |
} |
| 111 |
} |
| 112 |
|