| 1 |
<?php |
| 2 |
|
| 3 |
namespace Texty\Api; |
| 4 |
|
| 5 |
use WP_REST_Request; |
| 6 |
use WP_REST_Server; |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
/** |
| 11 |
* NotificationSettings REST Controller. |
| 12 |
* |
| 13 |
* Exposes global notification compliance settings (admin phone, sender ID, |
| 14 |
* pause-all toggle, company-name footer toggle). |
| 15 |
* |
| 16 |
* Routes: |
| 17 |
* GET /texty/v1/notification-settings — read all settings |
| 18 |
* POST /texty/v1/notification-settings — update settings |
| 19 |
* |
| 20 |
* @since TEXTY_VERSION |
| 21 |
*/ |
| 22 |
class NotificationSettings extends Base { |
| 23 |
|
| 24 |
/** |
| 25 |
* Initialize. |
| 26 |
* |
| 27 |
* @since TEXTY_VERSION |
| 28 |
*/ |
| 29 |
public function __construct() { |
| 30 |
$this->namespace = 'texty/v1'; |
| 31 |
$this->rest_base = 'notification-settings'; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Register routes. |
| 36 |
* |
| 37 |
* @return void |
| 38 |
* @since TEXTY_VERSION |
| 39 |
*/ |
| 40 |
public function register_routes() { |
| 41 |
register_rest_route( |
| 42 |
$this->namespace, |
| 43 |
'/' . $this->rest_base, |
| 44 |
[ |
| 45 |
[ |
| 46 |
'methods' => WP_REST_Server::READABLE, |
| 47 |
'callback' => [ $this, 'get_items' ], |
| 48 |
'permission_callback' => [ $this, 'admin_permissions_check' ], |
| 49 |
'args' => [], |
| 50 |
], |
| 51 |
[ |
| 52 |
'methods' => WP_REST_Server::EDITABLE, |
| 53 |
'callback' => [ $this, 'update_items' ], |
| 54 |
'permission_callback' => [ $this, 'admin_permissions_check' ], |
| 55 |
'args' => $this->get_settings_args(), |
| 56 |
], |
| 57 |
] |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* GET — return current settings. |
| 63 |
* |
| 64 |
* @param WP_REST_Request $request Request object. |
| 65 |
* |
| 66 |
* @return \WP_REST_Response |
| 67 |
* @since TEXTY_VERSION |
| 68 |
*/ |
| 69 |
public function get_items( $request ) { |
| 70 |
unset( $request ); |
| 71 |
|
| 72 |
$settings = texty()->notification_settings(); |
| 73 |
|
| 74 |
return rest_ensure_response( |
| 75 |
[ |
| 76 |
'settings' => $settings->all(), |
| 77 |
] |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* POST — persist settings. |
| 83 |
* |
| 84 |
* @param WP_REST_Request $request Request object. |
| 85 |
* |
| 86 |
* @return \WP_REST_Response |
| 87 |
* @since TEXTY_VERSION |
| 88 |
*/ |
| 89 |
public function update_items( $request ) { |
| 90 |
$service = texty()->notification_settings(); |
| 91 |
|
| 92 |
$values = [ |
| 93 |
'global_sender_id' => sanitize_text_field( (string) $request->get_param( 'global_sender_id' ) ), |
| 94 |
'pause_all' => (bool) $request->get_param( 'pause_all' ), |
| 95 |
'append_company_name' => (bool) $request->get_param( 'append_company_name' ), |
| 96 |
]; |
| 97 |
|
| 98 |
// Allow partial updates — drop keys the request didn't actually send. |
| 99 |
$params = $request->get_params(); |
| 100 |
foreach ( array_keys( $values ) as $key ) { |
| 101 |
if ( ! array_key_exists( $key, $params ) ) { |
| 102 |
unset( $values[ $key ] ); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
$merged = $service->update( $values ); |
| 107 |
|
| 108 |
return rest_ensure_response( |
| 109 |
[ |
| 110 |
'settings' => $merged, |
| 111 |
] |
| 112 |
); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Endpoint args for the EDITABLE schema. |
| 117 |
* |
| 118 |
* @return array |
| 119 |
* @since TEXTY_VERSION |
| 120 |
*/ |
| 121 |
private function get_settings_args() { |
| 122 |
return [ |
| 123 |
'global_sender_id' => [ |
| 124 |
'type' => 'string', |
| 125 |
'sanitize_callback' => 'sanitize_text_field', |
| 126 |
], |
| 127 |
'pause_all' => [ |
| 128 |
'type' => 'boolean', |
| 129 |
], |
| 130 |
'append_company_name' => [ |
| 131 |
'type' => 'boolean', |
| 132 |
], |
| 133 |
]; |
| 134 |
} |
| 135 |
} |
| 136 |
|