| 1 |
<?php |
| 2 |
/** |
| 3 |
* General Settings REST API endpoints. |
| 4 |
* |
| 5 |
* @package SureDonation |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace SureDonation\Inc\API; |
| 9 |
|
| 10 |
use SureDonation\Inc\Helper; |
| 11 |
use SureDonation\Inc\Payments\Payment_Helper; |
| 12 |
use WP_Error; |
| 13 |
use WP_REST_Request; |
| 14 |
use WP_REST_Response; |
| 15 |
use WP_REST_Server; |
| 16 |
|
| 17 |
// Exit if accessed directly. |
| 18 |
if ( ! defined( 'ABSPATH' ) ) { |
| 19 |
exit; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Settings API class. |
| 24 |
* |
| 25 |
* @since 0.0.1 |
| 26 |
*/ |
| 27 |
class Settings_API { |
| 28 |
/** |
| 29 |
* Option key for email notifications within consolidated options. |
| 30 |
* |
| 31 |
* @since 0.0.1 |
| 32 |
*/ |
| 33 |
public const EMAIL_OPTION_KEY = 'email_notifications'; |
| 34 |
|
| 35 |
/** |
| 36 |
* Option key for AI settings within consolidated options. |
| 37 |
* |
| 38 |
* @since 1.0.0 |
| 39 |
*/ |
| 40 |
public const AI_OPTION_KEY = 'ai_settings'; |
| 41 |
|
| 42 |
/** |
| 43 |
* Option key for donor management settings within consolidated options. |
| 44 |
* |
| 45 |
* @since 1.0.0 |
| 46 |
*/ |
| 47 |
public const DONOR_OPTION_KEY = 'donor_settings'; |
| 48 |
|
| 49 |
/** |
| 50 |
* Get settings endpoints. |
| 51 |
* |
| 52 |
* @return array<string, mixed> |
| 53 |
* @since 0.0.1 |
| 54 |
*/ |
| 55 |
public function get_endpoints() { |
| 56 |
return [ |
| 57 |
// Get currency data for block editor (public endpoint). |
| 58 |
'/settings' => [ |
| 59 |
'methods' => WP_REST_Server::READABLE, |
| 60 |
'callback' => [ $this, 'get_currency_settings' ], |
| 61 |
'permission_callback' => '__return_true', |
| 62 |
], |
| 63 |
|
| 64 |
// Get and update general settings. |
| 65 |
'/settings/general' => [ |
| 66 |
[ |
| 67 |
'methods' => WP_REST_Server::READABLE, |
| 68 |
'callback' => [ $this, 'get_settings' ], |
| 69 |
'permission_callback' => [ $this, 'check_permissions' ], |
| 70 |
], |
| 71 |
[ |
| 72 |
'methods' => WP_REST_Server::EDITABLE, |
| 73 |
'callback' => [ $this, 'update_settings' ], |
| 74 |
'permission_callback' => [ $this, 'check_permissions' ], |
| 75 |
], |
| 76 |
], |
| 77 |
|
| 78 |
// Get available currencies. |
| 79 |
'/settings/currencies' => [ |
| 80 |
'methods' => WP_REST_Server::READABLE, |
| 81 |
'callback' => [ $this, 'get_currencies' ], |
| 82 |
'permission_callback' => [ $this, 'check_permissions' ], |
| 83 |
], |
| 84 |
|
| 85 |
// Email notifications are managed per-form via post meta. |
| 86 |
// See inc/form-editor/assets.php for the form-level email system. |
| 87 |
// AI settings. |
| 88 |
'/settings/ai' => [ |
| 89 |
[ |
| 90 |
'methods' => WP_REST_Server::READABLE, |
| 91 |
'callback' => [ $this, 'get_ai_settings' ], |
| 92 |
'permission_callback' => [ $this, 'check_permissions' ], |
| 93 |
], |
| 94 |
[ |
| 95 |
'methods' => WP_REST_Server::EDITABLE, |
| 96 |
'callback' => [ $this, 'update_ai_settings' ], |
| 97 |
'permission_callback' => [ $this, 'check_permissions' ], |
| 98 |
], |
| 99 |
], |
| 100 |
|
| 101 |
// Miscellaneous settings (usage tracking, etc.). |
| 102 |
'/settings/misc' => [ |
| 103 |
[ |
| 104 |
'methods' => WP_REST_Server::READABLE, |
| 105 |
'callback' => [ $this, 'get_misc_settings' ], |
| 106 |
'permission_callback' => [ $this, 'check_permissions' ], |
| 107 |
], |
| 108 |
[ |
| 109 |
'methods' => WP_REST_Server::EDITABLE, |
| 110 |
'callback' => [ $this, 'update_misc_settings' ], |
| 111 |
'permission_callback' => [ $this, 'check_permissions' ], |
| 112 |
'args' => [ |
| 113 |
'usage_tracking' => [ |
| 114 |
'type' => 'boolean', |
| 115 |
'sanitize_callback' => 'rest_sanitize_boolean', |
| 116 |
], |
| 117 |
], |
| 118 |
], |
| 119 |
], |
| 120 |
|
| 121 |
// Donor management settings. |
| 122 |
'/settings/donor' => [ |
| 123 |
[ |
| 124 |
'methods' => WP_REST_Server::READABLE, |
| 125 |
'callback' => [ $this, 'get_donor_settings' ], |
| 126 |
'permission_callback' => [ $this, 'check_permissions' ], |
| 127 |
], |
| 128 |
[ |
| 129 |
'methods' => WP_REST_Server::EDITABLE, |
| 130 |
'callback' => [ $this, 'update_donor_settings' ], |
| 131 |
'permission_callback' => [ $this, 'check_permissions' ], |
| 132 |
'args' => [ |
| 133 |
'create_wp_user' => [ |
| 134 |
'type' => 'boolean', |
| 135 |
'sanitize_callback' => 'rest_sanitize_boolean', |
| 136 |
], |
| 137 |
], |
| 138 |
], |
| 139 |
], |
| 140 |
|
| 141 |
// Send test email. |
| 142 |
'/settings/email/test' => [ |
| 143 |
'methods' => WP_REST_Server::CREATABLE, |
| 144 |
'callback' => [ $this, 'send_test_email' ], |
| 145 |
'permission_callback' => [ $this, 'check_permissions' ], |
| 146 |
], |
| 147 |
]; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Get currency settings for block editor. |
| 152 |
* |
| 153 |
* Returns minimal currency data needed for frontend/block previews. |
| 154 |
* |
| 155 |
* @param WP_REST_Request $request Request object. |
| 156 |
* @return WP_REST_Response Response object. |
| 157 |
* @since 0.0.1 |
| 158 |
*/ |
| 159 |
public function get_currency_settings( $request ) { |
| 160 |
unset( $request ); // Unused parameter. |
| 161 |
|
| 162 |
$currency = Payment_Helper::get_currency(); |
| 163 |
|
| 164 |
return new WP_REST_Response( |
| 165 |
[ |
| 166 |
'currency' => $currency, |
| 167 |
'currencySymbol' => Payment_Helper::get_currency_symbol( $currency ), |
| 168 |
'isZeroDecimal' => Payment_Helper::is_zero_decimal_currency( $currency ), |
| 169 |
], |
| 170 |
200 |
| 171 |
); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Get general settings. |
| 176 |
* |
| 177 |
* @param WP_REST_Request $request Request object. |
| 178 |
* @return WP_REST_Response Response object. |
| 179 |
* @since 0.0.1 |
| 180 |
*/ |
| 181 |
public function get_settings( $request ) { |
| 182 |
unset( $request ); // Unused parameter. |
| 183 |
|
| 184 |
$settings = Payment_Helper::get_all_payment_settings(); |
| 185 |
|
| 186 |
return new WP_REST_Response( |
| 187 |
[ |
| 188 |
'success' => true, |
| 189 |
'settings' => [ |
| 190 |
'currency' => $settings['currency'] ?? 'USD', |
| 191 |
'payment_mode' => $settings['payment_mode'] ?? 'test', |
| 192 |
], |
| 193 |
], |
| 194 |
200 |
| 195 |
); |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Update general settings. |
| 200 |
* |
| 201 |
* @param WP_REST_Request $request Request object. |
| 202 |
* @return WP_REST_Response|WP_Error Response object. |
| 203 |
* @since 0.0.1 |
| 204 |
*/ |
| 205 |
public function update_settings( $request ) { |
| 206 |
$params = $request->get_json_params(); |
| 207 |
|
| 208 |
if ( empty( $params ) ) { |
| 209 |
return new WP_Error( |
| 210 |
'invalid_settings', |
| 211 |
__( 'Invalid settings provided', 'suredonation' ), |
| 212 |
[ 'status' => 400 ] |
| 213 |
); |
| 214 |
} |
| 215 |
|
| 216 |
$current_settings = Payment_Helper::get_all_payment_settings(); |
| 217 |
|
| 218 |
// Update currency if provided. |
| 219 |
if ( isset( $params['currency'] ) ) { |
| 220 |
$currency = strtoupper( sanitize_text_field( $params['currency'] ) ); |
| 221 |
|
| 222 |
// Validate currency. |
| 223 |
$valid_currencies = array_keys( Payment_Helper::get_all_currencies_data() ); |
| 224 |
if ( in_array( $currency, $valid_currencies, true ) ) { |
| 225 |
$current_settings['currency'] = $currency; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
// Update payment mode if provided. |
| 230 |
if ( isset( $params['payment_mode'] ) ) { |
| 231 |
$mode = sanitize_text_field( $params['payment_mode'] ); |
| 232 |
if ( in_array( $mode, [ 'test', 'live' ], true ) ) { |
| 233 |
$current_settings['payment_mode'] = $mode; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
$success = Payment_Helper::update_all_payment_settings( $current_settings ); |
| 238 |
|
| 239 |
if ( ! $success ) { |
| 240 |
return new WP_Error( |
| 241 |
'update_failed', |
| 242 |
__( 'Failed to update settings', 'suredonation' ), |
| 243 |
[ 'status' => 500 ] |
| 244 |
); |
| 245 |
} |
| 246 |
|
| 247 |
return new WP_REST_Response( |
| 248 |
[ |
| 249 |
'success' => true, |
| 250 |
'message' => __( 'Settings updated successfully', 'suredonation' ), |
| 251 |
], |
| 252 |
200 |
| 253 |
); |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Get available currencies. |
| 258 |
* |
| 259 |
* @param WP_REST_Request $request Request object. |
| 260 |
* @return WP_REST_Response Response object. |
| 261 |
* @since 0.0.1 |
| 262 |
*/ |
| 263 |
public function get_currencies( $request ) { |
| 264 |
unset( $request ); // Unused parameter. |
| 265 |
|
| 266 |
$currencies_data = Payment_Helper::get_all_currencies_data(); |
| 267 |
|
| 268 |
// Format for frontend: "CODE - Name". |
| 269 |
$currencies = []; |
| 270 |
foreach ( $currencies_data as $code => $data ) { |
| 271 |
$currencies[ $code ] = $code . ' - ' . $data['name']; |
| 272 |
} |
| 273 |
|
| 274 |
return new WP_REST_Response( |
| 275 |
[ |
| 276 |
'success' => true, |
| 277 |
'currencies' => $currencies, |
| 278 |
], |
| 279 |
200 |
| 280 |
); |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* Get AI settings. |
| 285 |
* |
| 286 |
* @param WP_REST_Request $request Request object. |
| 287 |
* @return WP_REST_Response Response object. |
| 288 |
* @since 1.0.0 |
| 289 |
*/ |
| 290 |
public function get_ai_settings( $request ) { |
| 291 |
unset( $request ); // Unused parameter. |
| 292 |
|
| 293 |
$defaults = [ |
| 294 |
'enable_abilities' => false, |
| 295 |
'allow_updates' => false, |
| 296 |
'allow_delete' => false, |
| 297 |
'mcp_server' => false, |
| 298 |
]; |
| 299 |
$settings = Helper::get_suredonation_option( self::AI_OPTION_KEY, [] ); |
| 300 |
|
| 301 |
return new WP_REST_Response( |
| 302 |
[ |
| 303 |
'success' => true, |
| 304 |
'settings' => wp_parse_args( is_array( $settings ) ? $settings : [], $defaults ), |
| 305 |
], |
| 306 |
200 |
| 307 |
); |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* Update AI settings. |
| 312 |
* |
| 313 |
* @param WP_REST_Request $request Request object. |
| 314 |
* @return WP_REST_Response Response object. |
| 315 |
* @since 1.0.0 |
| 316 |
*/ |
| 317 |
public function update_ai_settings( $request ) { |
| 318 |
$params = $request->get_json_params(); |
| 319 |
$current = Helper::get_suredonation_option( self::AI_OPTION_KEY, [] ); |
| 320 |
|
| 321 |
if ( ! is_array( $current ) ) { |
| 322 |
$current = []; |
| 323 |
} |
| 324 |
|
| 325 |
$allowed = [ 'enable_abilities', 'allow_updates', 'allow_delete', 'mcp_server' ]; |
| 326 |
foreach ( $allowed as $key ) { |
| 327 |
if ( isset( $params[ $key ] ) ) { |
| 328 |
$current[ $key ] = (bool) $params[ $key ]; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
Helper::update_suredonation_option( self::AI_OPTION_KEY, $current ); |
| 333 |
|
| 334 |
return new WP_REST_Response( |
| 335 |
[ |
| 336 |
'success' => true, |
| 337 |
'message' => __( 'AI settings saved', 'suredonation' ), |
| 338 |
], |
| 339 |
200 |
| 340 |
); |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* Get miscellaneous settings. |
| 345 |
* |
| 346 |
* @param WP_REST_Request $request Request object. |
| 347 |
* @return WP_REST_Response Response object. |
| 348 |
* @since 1.0.0 |
| 349 |
*/ |
| 350 |
public function get_misc_settings( $request ) { |
| 351 |
unset( $request ); // Unused parameter. |
| 352 |
|
| 353 |
return new WP_REST_Response( |
| 354 |
[ |
| 355 |
'success' => true, |
| 356 |
'settings' => [ |
| 357 |
// Site option — the BSF Analytics library reads this via |
| 358 |
// get_site_option(), so the toggle must use the same |
| 359 |
// scope to stay in sync on multisite. |
| 360 |
'usage_tracking' => 'yes' === get_site_option( 'suredonation_usage_optin', false ), |
| 361 |
], |
| 362 |
], |
| 363 |
200 |
| 364 |
); |
| 365 |
} |
| 366 |
|
| 367 |
/** |
| 368 |
* Update miscellaneous settings. |
| 369 |
* |
| 370 |
* Stores the usage-tracking opt-in as the standalone 'yes'/'no' |
| 371 |
* suredonation_usage_optin option read by the BSF Analytics library. |
| 372 |
* |
| 373 |
* @param WP_REST_Request $request Request object. |
| 374 |
* @return WP_REST_Response Response object. |
| 375 |
* @since 1.0.0 |
| 376 |
*/ |
| 377 |
public function update_misc_settings( $request ) { |
| 378 |
$usage_tracking = $request->get_param( 'usage_tracking' ); |
| 379 |
|
| 380 |
if ( null !== $usage_tracking ) { |
| 381 |
if ( $usage_tracking ) { |
| 382 |
update_site_option( 'suredonation_usage_optin', 'yes' ); |
| 383 |
} else { |
| 384 |
// Mirror the library's optout() side effects (see |
| 385 |
// class-bsf-analytics.php) so the cross-product notice |
| 386 |
// throttle and the send-check transient stay consistent. |
| 387 |
update_site_option( 'suredonation_usage_optin', 'no' ); |
| 388 |
update_site_option( 'bsf_usage_last_displayed_time', time() ); |
| 389 |
delete_site_transient( 'bsf_usage_track' ); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
return new WP_REST_Response( |
| 394 |
[ |
| 395 |
'success' => true, |
| 396 |
'message' => __( 'Settings saved', 'suredonation' ), |
| 397 |
], |
| 398 |
200 |
| 399 |
); |
| 400 |
} |
| 401 |
|
| 402 |
/** |
| 403 |
* Get donor management settings. |
| 404 |
* |
| 405 |
* @param WP_REST_Request $request Request object. |
| 406 |
* @return WP_REST_Response Response object. |
| 407 |
* @since 1.0.0 |
| 408 |
*/ |
| 409 |
public function get_donor_settings( $request ) { |
| 410 |
unset( $request ); // Unused parameter. |
| 411 |
|
| 412 |
$donor_settings = Helper::get_suredonation_option( self::DONOR_OPTION_KEY, [] ); |
| 413 |
if ( ! is_array( $donor_settings ) ) { |
| 414 |
$donor_settings = []; |
| 415 |
} |
| 416 |
|
| 417 |
return new WP_REST_Response( |
| 418 |
[ |
| 419 |
'success' => true, |
| 420 |
'settings' => [ |
| 421 |
// Off by default: guest donations never auto-create WP user accounts. |
| 422 |
'create_wp_user' => ! empty( $donor_settings['create_wp_user'] ), |
| 423 |
], |
| 424 |
], |
| 425 |
200 |
| 426 |
); |
| 427 |
} |
| 428 |
|
| 429 |
/** |
| 430 |
* Update donor management settings. |
| 431 |
* |
| 432 |
* @param WP_REST_Request $request Request object. |
| 433 |
* @return WP_REST_Response Response object. |
| 434 |
* @since 1.0.0 |
| 435 |
*/ |
| 436 |
public function update_donor_settings( $request ) { |
| 437 |
$create_wp_user = $request->get_param( 'create_wp_user' ); |
| 438 |
|
| 439 |
if ( null !== $create_wp_user ) { |
| 440 |
$donor_settings = Helper::get_suredonation_option( self::DONOR_OPTION_KEY, [] ); |
| 441 |
if ( ! is_array( $donor_settings ) ) { |
| 442 |
$donor_settings = []; |
| 443 |
} |
| 444 |
|
| 445 |
$donor_settings['create_wp_user'] = (bool) $create_wp_user; |
| 446 |
Helper::update_suredonation_option( self::DONOR_OPTION_KEY, $donor_settings ); |
| 447 |
} |
| 448 |
|
| 449 |
return new WP_REST_Response( |
| 450 |
[ |
| 451 |
'success' => true, |
| 452 |
'message' => __( 'Settings saved', 'suredonation' ), |
| 453 |
], |
| 454 |
200 |
| 455 |
); |
| 456 |
} |
| 457 |
|
| 458 |
/** |
| 459 |
* Check if user has permission to manage settings. |
| 460 |
* |
| 461 |
* @return bool True if user has permission. |
| 462 |
* @since 0.0.1 |
| 463 |
*/ |
| 464 |
public function check_permissions() { |
| 465 |
return current_user_can( 'manage_options' ); |
| 466 |
} |
| 467 |
} |
| 468 |
|