PluginProbe
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management / 1.6.0
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management v1.6.0
1.6.0 1.5.1 1.5.0 1.4.0 1.3.0 trunk 0.0.1 1.0.0 1.1.0 1.1.1 1.1.2 1.2.0
suredonation / inc / payments / offline / offline-settings.php

offline-settings.php in SureDonation – Donation Forms, Fundraising Campaigns & Donor Management 1.6.0, at inc/payments/offline/offline-settings.php

162 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Offline Settings - REST API for offline donation configuration
4 *
5 * @package SureDonation
6 */
7
8 namespace SureDonation\Inc\Payments\Offline;
9
10 use SureDonation\Inc\Payments\Payment_Helper;
11 use SureDonation\Inc\Traits\Get_Instance;
12 use WP_REST_Request;
13 use WP_REST_Response;
14 use WP_REST_Server;
15
16 // Exit if accessed directly.
17 if ( ! defined( 'ABSPATH' ) ) {
18 exit;
19 }
20
21 /**
22 * Offline_Settings class
23 * Manages offline donation REST API endpoints
24 *
25 * @since 1.0.0
26 */
27 class Offline_Settings {
28 use Get_Instance;
29
30 /**
31 * Constructor
32 *
33 * @since 1.0.0
34 */
35 public function __construct() {
36 add_action( 'rest_api_init', [ $this, 'register_routes' ] );
37 }
38
39 /**
40 * Register REST API routes
41 *
42 * @return void
43 * @since 1.0.0
44 */
45 public function register_routes() {
46 // Get offline settings.
47 register_rest_route(
48 'suredonation/v1',
49 '/payments/offline/settings',
50 [
51 'methods' => WP_REST_Server::READABLE,
52 'callback' => [ $this, 'get_settings' ],
53 'permission_callback' => [ $this, 'check_permissions' ],
54 ]
55 );
56
57 // Update offline settings.
58 register_rest_route(
59 'suredonation/v1',
60 '/payments/offline/settings',
61 [
62 'methods' => WP_REST_Server::EDITABLE,
63 'callback' => [ $this, 'update_settings' ],
64 'permission_callback' => [ $this, 'check_permissions' ],
65 'args' => [
66 'enabled' => [
67 'type' => 'boolean',
68 'sanitize_callback' => 'rest_sanitize_boolean',
69 ],
70 'instructions' => [
71 'type' => 'string',
72 'sanitize_callback' => 'wp_kses_post',
73 ],
74 ],
75 ]
76 );
77 }
78
79 /**
80 * Get offline settings
81 *
82 * @param WP_REST_Request $request Request object.
83 * @return WP_REST_Response Response object.
84 * @since 1.0.0
85 */
86 public function get_settings( $request ) {
87 unset( $request ); // Unused parameter.
88
89 // get_all_offline_settings() already fills the default template only when
90 // nothing has ever been saved (wp_parse_args on a missing key). We must NOT
91 // coerce an explicitly-stored blank back to the default here: that would show
92 // the default in the editor while the frontend correctly renders blank — an
93 // inconsistent, unrepresentable "blank" state. Editor and frontend read the
94 // same stored value, so they stay in sync.
95 $settings = Offline_Helper::get_all_offline_settings();
96
97 return new WP_REST_Response(
98 [
99 'success' => true,
100 'settings' => $settings,
101 ],
102 200
103 );
104 }
105
106 /**
107 * Update offline settings
108 *
109 * @param WP_REST_Request $request Request object.
110 * @return WP_REST_Response Response object.
111 * @since 1.0.0
112 */
113 public function update_settings( $request ) {
114 $params = $request->get_json_params();
115
116 // Use the raw stored settings as the write baseline — not the coerced
117 // read (get_all_offline_settings), which would otherwise materialize the
118 // locale-frozen default into the DB when only the enable toggle changes.
119 $settings = Payment_Helper::get_gateway_settings( 'offline' );
120
121 if ( isset( $params['enabled'] ) ) {
122 $settings['enabled'] = rest_sanitize_boolean( (bool) $params['enabled'] );
123 }
124
125 if ( isset( $params['instructions'] ) ) {
126 $instructions = is_string( $params['instructions'] ) ? $params['instructions'] : '';
127 $settings['instructions'] = wp_kses_post( $instructions );
128 }
129
130 $updated = Payment_Helper::update_gateway_settings( 'offline', $settings );
131
132 if ( ! $updated ) {
133 return new WP_REST_Response(
134 [
135 'success' => false,
136 'message' => __( 'Failed to update offline donation settings.', 'suredonation' ),
137 ],
138 500
139 );
140 }
141
142 return new WP_REST_Response(
143 [
144 'success' => true,
145 'message' => __( 'Offline donation settings updated successfully.', 'suredonation' ),
146 'settings' => $settings,
147 ],
148 200
149 );
150 }
151
152 /**
153 * Check if user has permission
154 *
155 * @return bool True if user has permission.
156 * @since 1.0.0
157 */
158 public function check_permissions() {
159 return current_user_can( 'manage_options' );
160 }
161 }
162