| @@ -6,9 +6,8 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace SureDonation\Inc\API; |
| 9 | 9 | |
| 10 | -use SureDonation\Inc\Emails\Email_Template; | |
| 11 | 10 | use SureDonation\Inc\Helper; |
| 12 | 11 | use SureDonation\Inc\Payments\Payment_Helper; |
| 13 | 12 | use WP_Error; |
| 14 | 13 | use WP_REST_Request; |
| @@ -33,8 +32,29 @@ | ||
| 33 | 32 | */ |
| 34 | 33 | public const EMAIL_OPTION_KEY = 'email_notifications'; |
| 35 | 34 | |
| 36 | 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 spam protection settings within consolidated options. | |
| 44 | + * | |
| 45 | + * @since 1.1.0 | |
| 46 | + */ | |
| 47 | + public const SPAM_OPTION_KEY = 'spam_protection_settings'; | |
| 48 | + | |
| 49 | + /** | |
| 50 | + * Option key for donor management settings within consolidated options. | |
| 51 | + * | |
| 52 | + * @since 1.0.0 | |
| 53 | + */ | |
| 54 | + public const DONOR_OPTION_KEY = 'donor_settings'; | |
| 55 | + | |
| 56 | + /** | |
| 37 | 57 | * Get settings endpoints. |
| 38 | 58 | * |
| 39 | 59 | * @return array<string, mixed> |
| 40 | 60 | * @since 0.0.1 |
| @@ -41,9 +61,9 @@ | ||
| 41 | 61 | */ |
| 42 | 62 | public function get_endpoints() { |
| 43 | 63 | return [ |
| 44 | 64 | // Get currency data for block editor (public endpoint). |
| 45 | - '/settings' => [ | |
| 65 | + '/settings' => [ | |
| 46 | 66 | 'methods' => WP_REST_Server::READABLE, |
| 47 | 67 | 'callback' => [ $this, 'get_currency_settings' ], |
| 48 | 68 | 'permission_callback' => '__return_true', |
| 49 | 69 | ], |
| @@ -48,9 +68,9 @@ | ||
| 48 | 68 | 'permission_callback' => '__return_true', |
| 49 | 69 | ], |
| 50 | 70 | |
| 51 | 71 | // Get and update general settings. |
| 52 | - '/settings/general' => [ | |
| 72 | + '/settings/general' => [ | |
| 53 | 73 | [ |
| 54 | 74 | 'methods' => WP_REST_Server::READABLE, |
| 55 | 75 | 'callback' => [ $this, 'get_settings' ], |
| 56 | 76 | 'permission_callback' => [ $this, 'check_permissions' ], |
| @@ -62,30 +82,100 @@ | ||
| 62 | 82 | ], |
| 63 | 83 | ], |
| 64 | 84 | |
| 65 | 85 | // Get available currencies. |
| 66 | - '/settings/currencies' => [ | |
| 86 | + '/settings/currencies' => [ | |
| 67 | 87 | 'methods' => WP_REST_Server::READABLE, |
| 68 | 88 | 'callback' => [ $this, 'get_currencies' ], |
| 69 | 89 | 'permission_callback' => [ $this, 'check_permissions' ], |
| 70 | 90 | ], |
| 71 | 91 | |
| 72 | - // Email settings. | |
| 73 | - '/settings/email' => [ | |
| 92 | + // Email notifications are managed per-form via post meta. | |
| 93 | + // See inc/form-editor/assets.php for the form-level email system. | |
| 94 | + // AI settings. | |
| 95 | + '/settings/ai' => [ | |
| 74 | 96 | [ |
| 75 | 97 | 'methods' => WP_REST_Server::READABLE, |
| 76 | - 'callback' => [ $this, 'get_email_settings' ], | |
| 98 | + 'callback' => [ $this, 'get_ai_settings' ], | |
| 77 | 99 | 'permission_callback' => [ $this, 'check_permissions' ], |
| 78 | 100 | ], |
| 79 | 101 | [ |
| 80 | 102 | 'methods' => WP_REST_Server::EDITABLE, |
| 81 | - 'callback' => [ $this, 'update_email_settings' ], | |
| 103 | + 'callback' => [ $this, 'update_ai_settings' ], | |
| 82 | 104 | 'permission_callback' => [ $this, 'check_permissions' ], |
| 83 | 105 | ], |
| 84 | 106 | ], |
| 85 | 107 | |
| 108 | + // Spam protection settings. | |
| 109 | + '/settings/spam-protection' => [ | |
| 110 | + [ | |
| 111 | + 'methods' => WP_REST_Server::READABLE, | |
| 112 | + 'callback' => [ $this, 'get_spam_protection_settings' ], | |
| 113 | + 'permission_callback' => [ $this, 'check_permissions' ], | |
| 114 | + ], | |
| 115 | + [ | |
| 116 | + 'methods' => WP_REST_Server::EDITABLE, | |
| 117 | + 'callback' => [ $this, 'update_spam_protection_settings' ], | |
| 118 | + 'permission_callback' => [ $this, 'check_permissions' ], | |
| 119 | + ], | |
| 120 | + ], | |
| 121 | + | |
| 122 | + // Miscellaneous settings (usage tracking, etc.). | |
| 123 | + '/settings/misc' => [ | |
| 124 | + [ | |
| 125 | + 'methods' => WP_REST_Server::READABLE, | |
| 126 | + 'callback' => [ $this, 'get_misc_settings' ], | |
| 127 | + 'permission_callback' => [ $this, 'check_permissions' ], | |
| 128 | + ], | |
| 129 | + [ | |
| 130 | + 'methods' => WP_REST_Server::EDITABLE, | |
| 131 | + 'callback' => [ $this, 'update_misc_settings' ], | |
| 132 | + 'permission_callback' => [ $this, 'check_permissions' ], | |
| 133 | + 'args' => [ | |
| 134 | + 'usage_tracking' => [ | |
| 135 | + 'type' => 'boolean', | |
| 136 | + 'sanitize_callback' => 'rest_sanitize_boolean', | |
| 137 | + ], | |
| 138 | + ], | |
| 139 | + ], | |
| 140 | + ], | |
| 141 | + | |
| 142 | + // Donor management settings. | |
| 143 | + '/settings/donor' => [ | |
| 144 | + [ | |
| 145 | + 'methods' => WP_REST_Server::READABLE, | |
| 146 | + 'callback' => [ $this, 'get_donor_settings' ], | |
| 147 | + 'permission_callback' => [ $this, 'check_permissions' ], | |
| 148 | + ], | |
| 149 | + [ | |
| 150 | + 'methods' => WP_REST_Server::EDITABLE, | |
| 151 | + 'callback' => [ $this, 'update_donor_settings' ], | |
| 152 | + 'permission_callback' => [ $this, 'check_permissions' ], | |
| 153 | + 'args' => [ | |
| 154 | + 'create_wp_user' => [ | |
| 155 | + 'type' => 'boolean', | |
| 156 | + 'sanitize_callback' => 'rest_sanitize_boolean', | |
| 157 | + ], | |
| 158 | + ], | |
| 159 | + ], | |
| 160 | + ], | |
| 161 | + | |
| 162 | + // Form validation default messages. | |
| 163 | + '/settings/validation' => [ | |
| 164 | + [ | |
| 165 | + 'methods' => WP_REST_Server::READABLE, | |
| 166 | + 'callback' => [ $this, 'get_validation_settings' ], | |
| 167 | + 'permission_callback' => [ $this, 'check_permissions' ], | |
| 168 | + ], | |
| 169 | + [ | |
| 170 | + 'methods' => WP_REST_Server::EDITABLE, | |
| 171 | + 'callback' => [ $this, 'update_validation_settings' ], | |
| 172 | + 'permission_callback' => [ $this, 'check_permissions' ], | |
| 173 | + ], | |
| 174 | + ], | |
| 175 | + | |
| 86 | 176 | // Send test email. |
| 87 | - '/settings/email/test' => [ | |
| 177 | + '/settings/email/test' => [ | |
| 88 | 178 | 'methods' => WP_REST_Server::CREATABLE, |
| 89 | 179 | 'callback' => [ $this, 'send_test_email' ], |
| 90 | 180 | 'permission_callback' => [ $this, 'check_permissions' ], |
| 91 | 181 | ], |
| @@ -92,8 +182,86 @@ | ||
| 92 | 182 | ]; |
| 93 | 183 | } |
| 94 | 184 | |
| 95 | 185 | /** |
| 186 | + * Get the form-validation default messages. | |
| 187 | + * | |
| 188 | + * Returns the stored admin overrides merged over the translatable defaults | |
| 189 | + * so every configurable message always has a value in the editor. | |
| 190 | + * | |
| 191 | + * @param WP_REST_Request $request Request object. | |
| 192 | + * @return WP_REST_Response | |
| 193 | + * @since 1.1.0 | |
| 194 | + */ | |
| 195 | + public function get_validation_settings( $request ) { | |
| 196 | + unset( $request ); // Unused parameter. | |
| 197 | + | |
| 198 | + $defaults = \SureDonation\Inc\Field_Validation::default_validation_messages(); | |
| 199 | + $stored = Helper::get_suredonation_option( \SureDonation\Inc\Field_Validation::VALIDATION_MESSAGES_OPTION_KEY, [] ); | |
| 200 | + | |
| 201 | + return new WP_REST_Response( | |
| 202 | + [ | |
| 203 | + 'success' => true, | |
| 204 | + 'settings' => wp_parse_args( is_array( $stored ) ? $stored : [], $defaults ), | |
| 205 | + ], | |
| 206 | + 200 | |
| 207 | + ); | |
| 208 | + } | |
| 209 | + | |
| 210 | + /** | |
| 211 | + * Update the form-validation default messages. | |
| 212 | + * | |
| 213 | + * @param WP_REST_Request $request Request object. | |
| 214 | + * @return WP_REST_Response | |
| 215 | + * @since 1.1.0 | |
| 216 | + */ | |
| 217 | + public function update_validation_settings( $request ) { | |
| 218 | + $current = Helper::get_suredonation_option( \SureDonation\Inc\Field_Validation::VALIDATION_MESSAGES_OPTION_KEY, [] ); | |
| 219 | + | |
| 220 | + if ( ! is_array( $current ) ) { | |
| 221 | + $current = []; | |
| 222 | + } | |
| 223 | + | |
| 224 | + /** | |
| 225 | + * Filter the list of allowed validation-message keys. | |
| 226 | + * | |
| 227 | + * Lets extensions register additional message keys for their own field | |
| 228 | + * types, mirroring the `suredonation.settings.tab.validationFields` and | |
| 229 | + * `suredonation.settings.tab.requiredValidationFields` JS filters. | |
| 230 | + * | |
| 231 | + * @since 1.1.0 | |
| 232 | + * @param array<int, string> $keys Allowed message keys. | |
| 233 | + */ | |
| 234 | + $allowed_keys = apply_filters( | |
| 235 | + 'suredonation_validation_message_keys', | |
| 236 | + array_keys( \SureDonation\Inc\Field_Validation::default_validation_messages() ) | |
| 237 | + ); | |
| 238 | + | |
| 239 | + foreach ( $allowed_keys as $key ) { | |
| 240 | + if ( ! is_string( $key ) ) { | |
| 241 | + continue; | |
| 242 | + } | |
| 243 | + | |
| 244 | + $value = $request->get_param( $key ); | |
| 245 | + // Guard against non-scalar input (array/object) which would make | |
| 246 | + // sanitize_text_field() emit a warning / type error on PHP 8.1+. | |
| 247 | + if ( null !== $value && is_scalar( $value ) ) { | |
| 248 | + $current[ $key ] = sanitize_text_field( (string) $value ); | |
| 249 | + } | |
| 250 | + } | |
| 251 | + | |
| 252 | + Helper::update_suredonation_option( \SureDonation\Inc\Field_Validation::VALIDATION_MESSAGES_OPTION_KEY, $current ); | |
| 253 | + | |
| 254 | + return new WP_REST_Response( | |
| 255 | + [ | |
| 256 | + 'success' => true, | |
| 257 | + 'message' => __( 'Form validation settings saved', 'suredonation' ), | |
| 258 | + ], | |
| 259 | + 200 | |
| 260 | + ); | |
| 261 | + } | |
| 262 | + | |
| 263 | + /** | |
| 96 | 264 | * Get currency settings for block editor. |
| 97 | 265 | * |
| 98 | 266 | * Returns minimal currency data needed for frontend/block previews. |
| 99 | 267 | * |
| @@ -207,20 +375,12 @@ | ||
| 207 | 375 | */ |
| 208 | 376 | public function get_currencies( $request ) { |
| 209 | 377 | unset( $request ); // Unused parameter. |
| 210 | 378 | |
| 211 | - $currencies_data = Payment_Helper::get_all_currencies_data(); | |
| 212 | - | |
| 213 | - // Format for frontend: "CODE - Name". | |
| 214 | - $currencies = []; | |
| 215 | - foreach ( $currencies_data as $code => $data ) { | |
| 216 | - $currencies[ $code ] = $code . ' - ' . $data['name']; | |
| 217 | - } | |
| 218 | - | |
| 219 | 379 | return new WP_REST_Response( |
| 220 | 380 | [ |
| 221 | 381 | 'success' => true, |
| 222 | - 'currencies' => $currencies, | |
| 382 | + 'currencies' => Payment_Helper::get_currencies_list(), | |
| 223 | 383 | ], |
| 224 | 384 | 200 |
| 225 | 385 | ); |
| 226 | 386 | } |
| @@ -225,34 +385,29 @@ | ||
| 225 | 385 | ); |
| 226 | 386 | } |
| 227 | 387 | |
| 228 | 388 | /** |
| 229 | - * Get email settings. | |
| 389 | + * Get AI settings. | |
| 230 | 390 | * |
| 231 | 391 | * @param WP_REST_Request $request Request object. |
| 232 | 392 | * @return WP_REST_Response Response object. |
| 233 | - * @since 0.0.1 | |
| 393 | + * @since 1.0.0 | |
| 234 | 394 | */ |
| 235 | - public function get_email_settings( $request ) { | |
| 395 | + public function get_ai_settings( $request ) { | |
| 236 | 396 | unset( $request ); // Unused parameter. |
| 237 | 397 | |
| 238 | - $saved_notifications = Helper::get_array_value( Helper::get_suredonation_option( self::EMAIL_OPTION_KEY, [] ) ); | |
| 239 | - $default_notifications = $this->get_default_notifications(); | |
| 398 | + $defaults = [ | |
| 399 | + 'enable_abilities' => false, | |
| 400 | + 'allow_updates' => false, | |
| 401 | + 'allow_delete' => false, | |
| 402 | + 'mcp_server' => false, | |
| 403 | + ]; | |
| 404 | + $settings = Helper::get_suredonation_option( self::AI_OPTION_KEY, [] ); | |
| 240 | 405 | |
| 241 | - // Merge saved settings with defaults. | |
| 242 | - $notifications = []; | |
| 243 | - foreach ( $default_notifications as $key => $default ) { | |
| 244 | - $saved_value = isset( $saved_notifications[ $key ] ) && is_array( $saved_notifications[ $key ] ) ? $saved_notifications[ $key ] : []; | |
| 245 | - $notifications[ $key ] = wp_parse_args( | |
| 246 | - $saved_value, | |
| 247 | - $default | |
| 248 | - ); | |
| 249 | - } | |
| 250 | - | |
| 251 | 406 | return new WP_REST_Response( |
| 252 | 407 | [ |
| 253 | - 'success' => true, | |
| 254 | - 'notifications' => $notifications, | |
| 408 | + 'success' => true, | |
| 409 | + 'settings' => wp_parse_args( is_array( $settings ) ? $settings : [], $defaults ), | |
| 255 | 410 | ], |
| 256 | 411 | 200 |
| 257 | 412 | ); |
| 258 | 413 | } |
| @@ -257,50 +412,35 @@ | ||
| 257 | 412 | ); |
| 258 | 413 | } |
| 259 | 414 | |
| 260 | 415 | /** |
| 261 | - * Update email settings. | |
| 416 | + * Update AI settings. | |
| 262 | 417 | * |
| 263 | 418 | * @param WP_REST_Request $request Request object. |
| 264 | - * @return WP_REST_Response|WP_Error Response object. | |
| 265 | - * @since 0.0.1 | |
| 419 | + * @return WP_REST_Response Response object. | |
| 420 | + * @since 1.0.0 | |
| 266 | 421 | */ |
| 267 | - public function update_email_settings( $request ) { | |
| 268 | - $params = $request->get_json_params(); | |
| 422 | + public function update_ai_settings( $request ) { | |
| 423 | + $params = $request->get_json_params(); | |
| 424 | + $current = Helper::get_suredonation_option( self::AI_OPTION_KEY, [] ); | |
| 269 | 425 | |
| 270 | - if ( empty( $params ) || ! isset( $params['notifications'] ) ) { | |
| 271 | - return new WP_Error( | |
| 272 | - 'invalid_settings', | |
| 273 | - __( 'Invalid settings provided', 'suredonation' ), | |
| 274 | - [ 'status' => 400 ] | |
| 275 | - ); | |
| 426 | + if ( ! is_array( $current ) ) { | |
| 427 | + $current = []; | |
| 276 | 428 | } |
| 277 | 429 | |
| 278 | - $current_notifications = Helper::get_array_value( Helper::get_suredonation_option( self::EMAIL_OPTION_KEY, [] ) ); | |
| 279 | - $valid_keys = array_keys( $this->get_default_notifications() ); | |
| 280 | - | |
| 281 | - foreach ( $params['notifications'] as $key => $notification ) { | |
| 282 | - // Only allow known notification types. | |
| 283 | - if ( ! in_array( $key, $valid_keys, true ) ) { | |
| 284 | - continue; | |
| 430 | + $allowed = [ 'enable_abilities', 'allow_updates', 'allow_delete', 'mcp_server' ]; | |
| 431 | + foreach ( $allowed as $key ) { | |
| 432 | + if ( isset( $params[ $key ] ) ) { | |
| 433 | + $current[ $key ] = (bool) $params[ $key ]; | |
| 285 | 434 | } |
| 286 | - | |
| 287 | - $current_notifications[ $key ] = [ | |
| 288 | - 'enabled' => isset( $notification['enabled'] ) ? (bool) $notification['enabled'] : false, | |
| 289 | - 'subject' => isset( $notification['subject'] ) ? sanitize_text_field( $notification['subject'] ) : '', | |
| 290 | - 'from_name' => isset( $notification['from_name'] ) ? sanitize_text_field( $notification['from_name'] ) : '', | |
| 291 | - 'from_email' => isset( $notification['from_email'] ) ? sanitize_email( $notification['from_email'] ) : '', | |
| 292 | - 'reply_to' => isset( $notification['reply_to'] ) ? sanitize_email( $notification['reply_to'] ) : '', | |
| 293 | - 'email_body' => isset( $notification['email_body'] ) ? wp_kses_post( $notification['email_body'] ) : '', | |
| 294 | - ]; | |
| 295 | 435 | } |
| 296 | 436 | |
| 297 | - Helper::update_suredonation_option( self::EMAIL_OPTION_KEY, $current_notifications ); | |
| 437 | + Helper::update_suredonation_option( self::AI_OPTION_KEY, $current ); | |
| 298 | 438 | |
| 299 | 439 | return new WP_REST_Response( |
| 300 | 440 | [ |
| 301 | 441 | 'success' => true, |
| 302 | - 'message' => __( 'Email settings saved', 'suredonation' ), | |
| 442 | + 'message' => __( 'AI settings saved', 'suredonation' ), | |
| 303 | 443 | ], |
| 304 | 444 | 200 |
| 305 | 445 | ); |
| 306 | 446 | } |
| @@ -305,124 +445,61 @@ | ||
| 305 | 445 | ); |
| 306 | 446 | } |
| 307 | 447 | |
| 308 | 448 | /** |
| 309 | - * Send a test email. | |
| 449 | + * Get spam protection settings. | |
| 310 | 450 | * |
| 311 | 451 | * @param WP_REST_Request $request Request object. |
| 312 | - * @return WP_REST_Response|WP_Error Response object. | |
| 313 | - * @since 0.0.1 | |
| 452 | + * @return WP_REST_Response Response object. | |
| 453 | + * @since 1.1.0 | |
| 314 | 454 | */ |
| 315 | - public function send_test_email( $request ) { | |
| 316 | - $params = $request->get_json_params(); | |
| 455 | + public function get_spam_protection_settings( $request ) { | |
| 456 | + unset( $request ); // Unused parameter. | |
| 317 | 457 | |
| 318 | - if ( empty( $params['notification_id'] ) ) { | |
| 319 | - return new WP_Error( | |
| 320 | - 'missing_notification_id', | |
| 321 | - __( 'Notification ID is required', 'suredonation' ), | |
| 322 | - [ 'status' => 400 ] | |
| 323 | - ); | |
| 324 | - } | |
| 458 | + $defaults = [ | |
| 459 | + 'honeypot' => false, | |
| 460 | + ]; | |
| 461 | + $settings = Helper::get_suredonation_option( self::SPAM_OPTION_KEY, [] ); | |
| 325 | 462 | |
| 326 | - $notification_id = sanitize_text_field( $params['notification_id'] ); | |
| 327 | - $admin_email = get_option( 'admin_email' ); | |
| 328 | - $test_email = ! empty( $params['test_email'] ) ? sanitize_email( $params['test_email'] ) : ( is_string( $admin_email ) ? $admin_email : '' ); | |
| 463 | + return new WP_REST_Response( | |
| 464 | + [ | |
| 465 | + 'success' => true, | |
| 466 | + 'settings' => wp_parse_args( is_array( $settings ) ? $settings : [], $defaults ), | |
| 467 | + ], | |
| 468 | + 200 | |
| 469 | + ); | |
| 470 | + } | |
| 329 | 471 | |
| 330 | - if ( ! is_email( $test_email ) ) { | |
| 331 | - return new WP_Error( | |
| 332 | - 'invalid_email', | |
| 333 | - __( 'Invalid email address', 'suredonation' ), | |
| 334 | - [ 'status' => 400 ] | |
| 335 | - ); | |
| 336 | - } | |
| 472 | + /** | |
| 473 | + * Update spam protection settings. | |
| 474 | + * | |
| 475 | + * @param WP_REST_Request $request Request object. | |
| 476 | + * @return WP_REST_Response Response object. | |
| 477 | + * @since 1.1.0 | |
| 478 | + */ | |
| 479 | + public function update_spam_protection_settings( $request ) { | |
| 480 | + $current = Helper::get_suredonation_option( self::SPAM_OPTION_KEY, [] ); | |
| 337 | 481 | |
| 338 | - // Get notification settings. | |
| 339 | - $saved_notifications = Helper::get_array_value( Helper::get_suredonation_option( self::EMAIL_OPTION_KEY, [] ) ); | |
| 340 | - $default_notifications = $this->get_default_notifications(); | |
| 341 | - | |
| 342 | - if ( ! isset( $default_notifications[ $notification_id ] ) ) { | |
| 343 | - return new WP_Error( | |
| 344 | - 'invalid_notification', | |
| 345 | - __( 'Invalid notification type', 'suredonation' ), | |
| 346 | - [ 'status' => 400 ] | |
| 347 | - ); | |
| 482 | + if ( ! is_array( $current ) ) { | |
| 483 | + $current = []; | |
| 348 | 484 | } |
| 349 | 485 | |
| 350 | - $saved_notification_value = isset( $saved_notifications[ $notification_id ] ) && is_array( $saved_notifications[ $notification_id ] ) ? $saved_notifications[ $notification_id ] : []; | |
| 351 | - $notification = wp_parse_args( | |
| 352 | - $saved_notification_value, | |
| 353 | - $default_notifications[ $notification_id ] | |
| 354 | - ); | |
| 355 | - | |
| 356 | - // Use current form data if provided (for preview before save). | |
| 357 | - if ( ! empty( $params['notification_data'] ) && is_array( $params['notification_data'] ) ) { | |
| 358 | - $raw = $params['notification_data']; | |
| 359 | - $sanitized_data = []; | |
| 360 | - if ( isset( $raw['subject'] ) ) { | |
| 361 | - $sanitized_data['subject'] = sanitize_text_field( $raw['subject'] ); | |
| 486 | + // Read each setting via get_param() so the endpoint accepts JSON, body, | |
| 487 | + // or query params (matches the sibling /settings/* update handlers). | |
| 488 | + $allowed = [ 'honeypot' ]; | |
| 489 | + foreach ( $allowed as $key ) { | |
| 490 | + $value = $request->get_param( $key ); | |
| 491 | + if ( null !== $value ) { | |
| 492 | + $current[ $key ] = (bool) $value; | |
| 362 | 493 | } |
| 363 | - if ( isset( $raw['from_name'] ) ) { | |
| 364 | - $sanitized_data['from_name'] = sanitize_text_field( $raw['from_name'] ); | |
| 365 | - } | |
| 366 | - if ( isset( $raw['from_email'] ) ) { | |
| 367 | - $sanitized_data['from_email'] = sanitize_email( $raw['from_email'] ); | |
| 368 | - } | |
| 369 | - if ( isset( $raw['reply_to'] ) ) { | |
| 370 | - $sanitized_data['reply_to'] = sanitize_email( $raw['reply_to'] ); | |
| 371 | - } | |
| 372 | - if ( isset( $raw['email_body'] ) ) { | |
| 373 | - $sanitized_data['email_body'] = wp_kses_post( $raw['email_body'] ); | |
| 374 | - } | |
| 375 | - if ( isset( $raw['enabled'] ) ) { | |
| 376 | - $sanitized_data['enabled'] = (bool) $raw['enabled']; | |
| 377 | - } | |
| 378 | - $notification = wp_parse_args( $sanitized_data, $notification ); | |
| 379 | 494 | } |
| 380 | 495 | |
| 381 | - // Create sample donation data for smart tags. | |
| 382 | - $sample_data = $this->get_sample_donation_data(); | |
| 496 | + Helper::update_suredonation_option( self::SPAM_OPTION_KEY, $current ); | |
| 383 | 497 | |
| 384 | - // Process smart tags. | |
| 385 | - $subject = $this->process_test_smart_tags( $notification['subject'] ?? '', $sample_data ); | |
| 386 | - $email_body = $this->process_test_smart_tags( $notification['email_body'] ?? '', $sample_data ); | |
| 387 | - | |
| 388 | - // Get from name and email. | |
| 389 | - $from_name = ! empty( $notification['from_name'] ) ? $notification['from_name'] : get_bloginfo( 'name' ); | |
| 390 | - $from_email = ! empty( $notification['from_email'] ) ? $notification['from_email'] : get_option( 'admin_email' ); | |
| 391 | - $reply_to = ! empty( $notification['reply_to'] ) ? $notification['reply_to'] : $from_email; | |
| 392 | - | |
| 393 | - // Process smart tags in from name. | |
| 394 | - $from_name = $this->process_test_smart_tags( $from_name, $sample_data ); | |
| 395 | - | |
| 396 | - // Set email headers. | |
| 397 | - $headers = [ | |
| 398 | - 'Content-Type: text/html; charset=UTF-8', | |
| 399 | - sprintf( 'From: %s <%s>', $from_name, $from_email ), | |
| 400 | - sprintf( 'Reply-To: %s', $reply_to ), | |
| 401 | - ]; | |
| 402 | - | |
| 403 | - // Format email body with HTML wrapper. | |
| 404 | - $email_body = $this->format_test_email_body( $email_body ); | |
| 405 | - | |
| 406 | - // Send email. | |
| 407 | - $sent = wp_mail( $test_email, $subject, $email_body, $headers ); | |
| 408 | - | |
| 409 | - if ( ! $sent ) { | |
| 410 | - return new WP_Error( | |
| 411 | - 'email_failed', | |
| 412 | - __( 'Failed to send test email. Please check your email configuration.', 'suredonation' ), | |
| 413 | - [ 'status' => 500 ] | |
| 414 | - ); | |
| 415 | - } | |
| 416 | - | |
| 417 | 498 | return new WP_REST_Response( |
| 418 | 499 | [ |
| 419 | 500 | 'success' => true, |
| 420 | - 'message' => sprintf( | |
| 421 | - /* translators: %s: email address */ | |
| 422 | - __( 'Test email sent to %s', 'suredonation' ), | |
| 423 | - $test_email | |
| 424 | - ), | |
| 501 | + 'message' => __( 'Spam protection settings saved', 'suredonation' ), | |
| 425 | 502 | ], |
| 426 | 503 | 200 |
| 427 | 504 | ); |
| 428 | 505 | } |
| @@ -427,161 +504,128 @@ | ||
| 427 | 504 | ); |
| 428 | 505 | } |
| 429 | 506 | |
| 430 | 507 | /** |
| 431 | - * Check if user has permission to manage settings. | |
| 508 | + * Get miscellaneous settings. | |
| 432 | 509 | * |
| 433 | - * @return bool True if user has permission. | |
| 434 | - * @since 0.0.1 | |
| 510 | + * @param WP_REST_Request $request Request object. | |
| 511 | + * @return WP_REST_Response Response object. | |
| 512 | + * @since 1.0.0 | |
| 435 | 513 | */ |
| 436 | - public function check_permissions() { | |
| 437 | - return current_user_can( 'manage_options' ); | |
| 514 | + public function get_misc_settings( $request ) { | |
| 515 | + unset( $request ); // Unused parameter. | |
| 516 | + | |
| 517 | + return new WP_REST_Response( | |
| 518 | + [ | |
| 519 | + 'success' => true, | |
| 520 | + 'settings' => [ | |
| 521 | + // Site option - the BSF Analytics library reads this via | |
| 522 | + // get_site_option(), so the toggle must use the same | |
| 523 | + // scope to stay in sync on multisite. | |
| 524 | + 'usage_tracking' => 'yes' === get_site_option( 'suredonation_usage_optin', false ), | |
| 525 | + ], | |
| 526 | + ], | |
| 527 | + 200 | |
| 528 | + ); | |
| 438 | 529 | } |
| 439 | 530 | |
| 440 | 531 | /** |
| 441 | - * Get default notification configurations. | |
| 532 | + * Update miscellaneous settings. | |
| 442 | 533 | * |
| 443 | - * @return array<string, array<string, mixed>> Default notifications. | |
| 444 | - * @since 0.0.1 | |
| 534 | + * Stores the usage-tracking opt-in as the standalone 'yes'/'no' | |
| 535 | + * suredonation_usage_optin option read by the BSF Analytics library. | |
| 536 | + * | |
| 537 | + * @param WP_REST_Request $request Request object. | |
| 538 | + * @return WP_REST_Response Response object. | |
| 539 | + * @since 1.0.0 | |
| 445 | 540 | */ |
| 446 | - private function get_default_notifications() { | |
| 447 | - return [ | |
| 448 | - 'donation_receipt' => [ | |
| 449 | - 'id' => 'donation_receipt', | |
| 450 | - 'name' => __( 'Donation Receipt', 'suredonation' ), | |
| 451 | - 'description' => __( 'Sent to donor after a successful donation.', 'suredonation' ), | |
| 452 | - 'recipient' => 'donor', | |
| 453 | - 'enabled' => true, | |
| 454 | - 'subject' => __( 'Thank you for your donation!', 'suredonation' ), | |
| 455 | - 'from_name' => '', | |
| 456 | - 'from_email' => '', | |
| 457 | - 'reply_to' => '', | |
| 458 | - 'email_body' => $this->get_donation_receipt_body(), | |
| 541 | + public function update_misc_settings( $request ) { | |
| 542 | + $usage_tracking = $request->get_param( 'usage_tracking' ); | |
| 543 | + | |
| 544 | + if ( null !== $usage_tracking ) { | |
| 545 | + if ( $usage_tracking ) { | |
| 546 | + update_site_option( 'suredonation_usage_optin', 'yes' ); | |
| 547 | + } else { | |
| 548 | + // Mirror the library's optout() side effects (see | |
| 549 | + // class-bsf-analytics.php) so the cross-product notice | |
| 550 | + // throttle and the send-check transient stay consistent. | |
| 551 | + update_site_option( 'suredonation_usage_optin', 'no' ); | |
| 552 | + update_site_option( 'bsf_usage_last_displayed_time', time() ); | |
| 553 | + delete_site_transient( 'bsf_usage_track' ); | |
| 554 | + } | |
| 555 | + } | |
| 556 | + | |
| 557 | + return new WP_REST_Response( | |
| 558 | + [ | |
| 559 | + 'success' => true, | |
| 560 | + 'message' => __( 'Settings saved', 'suredonation' ), | |
| 459 | 561 | ], |
| 460 | - 'admin_new_donation' => [ | |
| 461 | - 'id' => 'admin_new_donation', | |
| 462 | - 'name' => __( 'New Donation (Admin)', 'suredonation' ), | |
| 463 | - 'description' => __( 'Sent to admin when a new donation is received.', 'suredonation' ), | |
| 464 | - 'recipient' => 'admin', | |
| 465 | - 'enabled' => true, | |
| 466 | - 'subject' => __( 'New donation received!', 'suredonation' ), | |
| 467 | - 'from_name' => '', | |
| 468 | - 'from_email' => '', | |
| 469 | - 'reply_to' => '', | |
| 470 | - 'email_body' => $this->get_admin_new_donation_body(), | |
| 471 | - ], | |
| 472 | - ]; | |
| 562 | + 200 | |
| 563 | + ); | |
| 473 | 564 | } |
| 474 | 565 | |
| 475 | 566 | /** |
| 476 | - * Get donation receipt email body. | |
| 567 | + * Get donor management settings. | |
| 477 | 568 | * |
| 478 | - * @return string Email body in HTML format. | |
| 479 | - * @since 0.0.1 | |
| 569 | + * @param WP_REST_Request $request Request object. | |
| 570 | + * @return WP_REST_Response Response object. | |
| 571 | + * @since 1.0.0 | |
| 480 | 572 | */ |
| 481 | - private function get_donation_receipt_body() { | |
| 482 | - ob_start(); | |
| 483 | - ?> | |
| 484 | - <p><strong><span style="font-size: 18px;"><?php esc_html_e( 'Thank You for Your Donation!', 'suredonation' ); ?></span></strong></p> | |
| 485 | - <p><?php esc_html_e( 'Dear', 'suredonation' ); ?> {donor_name},</p> | |
| 486 | - <p><?php esc_html_e( 'Thank you for your generous donation of', 'suredonation' ); ?> <strong>{amount}</strong> <?php esc_html_e( 'to', 'suredonation' ); ?> <strong>{campaign_name}</strong>.</p> | |
| 487 | - <p><?php esc_html_e( 'Your support means the world to us and helps us continue our mission.', 'suredonation' ); ?></p> | |
| 488 | - <p><strong><?php esc_html_e( 'Donation Details:', 'suredonation' ); ?></strong></p> | |
| 489 | - <ul> | |
| 490 | - <li><?php esc_html_e( 'Amount:', 'suredonation' ); ?> {amount}</li> | |
| 491 | - <li><?php esc_html_e( 'Campaign:', 'suredonation' ); ?> {campaign_name}</li> | |
| 492 | - <li><?php esc_html_e( 'Date:', 'suredonation' ); ?> {donation_date}</li> | |
| 493 | - <li><?php esc_html_e( 'Transaction ID:', 'suredonation' ); ?> {transaction_id}</li> | |
| 494 | - </ul> | |
| 495 | - <p><?php esc_html_e( 'Best regards,', 'suredonation' ); ?><br />{site_title}</p> | |
| 496 | - <?php | |
| 497 | - $output = ob_get_clean(); | |
| 498 | - return trim( false !== $output ? $output : '' ); | |
| 499 | - } | |
| 573 | + public function get_donor_settings( $request ) { | |
| 574 | + unset( $request ); // Unused parameter. | |
| 500 | 575 | |
| 501 | - /** | |
| 502 | - * Get admin new donation email body. | |
| 503 | - * | |
| 504 | - * @return string Email body in HTML format. | |
| 505 | - * @since 0.0.1 | |
| 506 | - */ | |
| 507 | - private function get_admin_new_donation_body() { | |
| 508 | - ob_start(); | |
| 509 | - ?> | |
| 510 | - <p><strong><span style="font-size: 18px;"><?php esc_html_e( 'New Donation Received!', 'suredonation' ); ?></span></strong></p> | |
| 511 | - <p><?php esc_html_e( 'A new donation has been received for your campaign.', 'suredonation' ); ?></p> | |
| 512 | - <p><strong><?php esc_html_e( 'Donation Details:', 'suredonation' ); ?></strong></p> | |
| 513 | - <ul> | |
| 514 | - <li><?php esc_html_e( 'Donor:', 'suredonation' ); ?> {donor_name} ({donor_email})</li> | |
| 515 | - <li><?php esc_html_e( 'Amount:', 'suredonation' ); ?> {amount}</li> | |
| 516 | - <li><?php esc_html_e( 'Campaign:', 'suredonation' ); ?> {campaign_name}</li> | |
| 517 | - <li><?php esc_html_e( 'Date:', 'suredonation' ); ?> {donation_date}</li> | |
| 518 | - <li><?php esc_html_e( 'Transaction ID:', 'suredonation' ); ?> {transaction_id}</li> | |
| 519 | - </ul> | |
| 520 | - <?php | |
| 521 | - $output = ob_get_clean(); | |
| 522 | - return trim( false !== $output ? $output : '' ); | |
| 576 | + $donor_settings = Helper::get_suredonation_option( self::DONOR_OPTION_KEY, [] ); | |
| 577 | + if ( ! is_array( $donor_settings ) ) { | |
| 578 | + $donor_settings = []; | |
| 579 | + } | |
| 580 | + | |
| 581 | + return new WP_REST_Response( | |
| 582 | + [ | |
| 583 | + 'success' => true, | |
| 584 | + 'settings' => [ | |
| 585 | + // Off by default: guest donations never auto-create WP user accounts. | |
| 586 | + 'create_wp_user' => ! empty( $donor_settings['create_wp_user'] ), | |
| 587 | + ], | |
| 588 | + ], | |
| 589 | + 200 | |
| 590 | + ); | |
| 523 | 591 | } |
| 524 | 592 | |
| 525 | 593 | /** |
| 526 | - * Get sample donation data for test emails. | |
| 594 | + * Update donor management settings. | |
| 527 | 595 | * |
| 528 | - * @return array<string, int|string> Sample data. | |
| 529 | - * @since 0.0.1 | |
| 596 | + * @param WP_REST_Request $request Request object. | |
| 597 | + * @return WP_REST_Response Response object. | |
| 598 | + * @since 1.0.0 | |
| 530 | 599 | */ |
| 531 | - private function get_sample_donation_data() { | |
| 532 | - $currency = Payment_Helper::get_currency(); | |
| 533 | - $currency_symbol = Payment_Helper::get_currency_symbol( $currency ); | |
| 534 | - $date_format = get_option( 'date_format' ); | |
| 535 | - $admin_email = get_option( 'admin_email' ); | |
| 600 | + public function update_donor_settings( $request ) { | |
| 601 | + $create_wp_user = $request->get_param( 'create_wp_user' ); | |
| 536 | 602 | |
| 537 | - return [ | |
| 538 | - 'donor_name' => __( 'John Doe', 'suredonation' ), | |
| 539 | - 'donor_email' => '[email protected]', | |
| 540 | - 'amount' => $currency_symbol . '50.00', | |
| 541 | - 'campaign_name' => __( 'Sample Campaign', 'suredonation' ), | |
| 542 | - 'donation_date' => current_time( is_string( $date_format ) ? $date_format : 'Y-m-d' ), | |
| 543 | - 'transaction_id' => 'pi_test_' . wp_generate_password( 16, false ), | |
| 544 | - 'site_title' => get_bloginfo( 'name' ), | |
| 545 | - 'admin_email' => is_string( $admin_email ) ? $admin_email : '', | |
| 546 | - 'site_url' => home_url(), | |
| 547 | - 'admin_url' => admin_url( 'admin.php?page=suredonation' ), | |
| 548 | - ]; | |
| 549 | - } | |
| 603 | + if ( null !== $create_wp_user ) { | |
| 604 | + $donor_settings = Helper::get_suredonation_option( self::DONOR_OPTION_KEY, [] ); | |
| 605 | + if ( ! is_array( $donor_settings ) ) { | |
| 606 | + $donor_settings = []; | |
| 607 | + } | |
| 550 | 608 | |
| 551 | - /** | |
| 552 | - * Process smart tags for test emails. | |
| 553 | - * | |
| 554 | - * @param string $content Content with smart tags. | |
| 555 | - * @param array<string, int|string> $sample_data Sample data for replacement. | |
| 556 | - * @return string Processed content. | |
| 557 | - * @since 0.0.1 | |
| 558 | - */ | |
| 559 | - private function process_test_smart_tags( $content, $sample_data ) { | |
| 560 | - $tags = [ | |
| 561 | - '{donor_name}' => $sample_data['donor_name'], | |
| 562 | - '{donor_email}' => $sample_data['donor_email'], | |
| 563 | - '{amount}' => $sample_data['amount'], | |
| 564 | - '{campaign_name}' => $sample_data['campaign_name'], | |
| 565 | - '{donation_date}' => $sample_data['donation_date'], | |
| 566 | - '{transaction_id}' => $sample_data['transaction_id'], | |
| 567 | - '{site_title}' => $sample_data['site_title'], | |
| 568 | - '{admin_email}' => $sample_data['admin_email'], | |
| 569 | - '{site_url}' => $sample_data['site_url'], | |
| 570 | - '{admin_url}' => $sample_data['admin_url'], | |
| 571 | - ]; | |
| 609 | + $donor_settings['create_wp_user'] = (bool) $create_wp_user; | |
| 610 | + Helper::update_suredonation_option( self::DONOR_OPTION_KEY, $donor_settings ); | |
| 611 | + } | |
| 572 | 612 | |
| 573 | - return str_replace( array_keys( $tags ), array_values( $tags ), $content ); | |
| 613 | + return new WP_REST_Response( | |
| 614 | + [ | |
| 615 | + 'success' => true, | |
| 616 | + 'message' => __( 'Settings saved', 'suredonation' ), | |
| 617 | + ], | |
| 618 | + 200 | |
| 619 | + ); | |
| 574 | 620 | } |
| 575 | 621 | |
| 576 | 622 | /** |
| 577 | - * Format test email body with HTML wrapper. | |
| 623 | + * Check if user has permission to manage settings. | |
| 578 | 624 | * |
| 579 | - * @param string $body Email body content. | |
| 580 | - * @return string Formatted HTML email. | |
| 625 | + * @return bool True if user has permission. | |
| 581 | 626 | * @since 0.0.1 |
| 582 | 627 | */ |
| 583 | - private function format_test_email_body( $body ) { | |
| 584 | - $email_template = Email_Template::get_instance(); | |
| 585 | - return $email_template->render( $body ); | |
| 628 | + public function check_permissions() { | |
| 629 | + return current_user_can( 'manage_options' ); | |
| 586 | 630 | } |
| 587 | 631 | } |