PluginProbe
Texty – SMS Notification for WordPress, WooCommerce, Dokan and more / 2.0.0
Texty – SMS Notification for WordPress, WooCommerce, Dokan and more v2.0.0
2.0.2 trunk 0.2 1.0 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 2.0.0 2.0.1
texty / includes / Api / Gateway.php

Gateway.php in Texty – SMS Notification for WordPress, WooCommerce, Dokan and more 2.0.0, at includes/Api/Gateway.php

255 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Gateway Lifecycle REST Controller.
4 *
5 * Splits the "save credentials" step (handled by Api\SettingsController) from
6 * the activation lifecycle so the UI can offer Activate / Deactivate /
7 * Disconnect actions independent of credential edits.
8 *
9 * Routes:
10 * POST /texty/v1/gateway/activate { gateway: 'twilio' }
11 * POST /texty/v1/gateway/deactivate
12 * POST /texty/v1/gateway/disconnect { gateway: 'twilio' }
13 *
14 * Storage: all three mutate the existing `texty_settings` option in place.
15 *
16 * @package Texty\Api
17 * @since TEXTY_VERSION
18 */
19
20 namespace Texty\Api;
21
22 use WP_Error;
23 use WP_REST_Request;
24 use WP_REST_Response;
25 use WP_REST_Server;
26
27 /**
28 * Gateway Class
29 */
30 class Gateway extends Base {
31
32 /**
33 * Single backing option (mirrors Api\SettingsController::OPTION_KEY).
34 */
35 const OPTION_KEY = 'texty_settings';
36
37 /**
38 * Constructor.
39 *
40 * @since TEXTY_VERSION
41 */
42 public function __construct() {
43 $this->namespace = 'texty/v1';
44 $this->rest_base = 'gateway';
45 }
46
47 /**
48 * Register routes.
49 *
50 * @return void
51 * @since TEXTY_VERSION
52 */
53 public function register_routes() {
54 register_rest_route(
55 $this->namespace,
56 '/' . $this->rest_base . '/activate',
57 [
58 [
59 'methods' => WP_REST_Server::CREATABLE,
60 'callback' => [ $this, 'activate' ],
61 'permission_callback' => [ $this, 'admin_permissions_check' ],
62 'args' => [
63 'gateway' => [
64 'description' => __( 'Gateway key to activate.', 'texty' ),
65 'type' => 'string',
66 'required' => true,
67 ],
68 ],
69 ],
70 ]
71 );
72
73 register_rest_route(
74 $this->namespace,
75 '/' . $this->rest_base . '/deactivate',
76 [
77 [
78 'methods' => WP_REST_Server::CREATABLE,
79 'callback' => [ $this, 'deactivate' ],
80 'permission_callback' => [ $this, 'admin_permissions_check' ],
81 ],
82 ]
83 );
84
85 register_rest_route(
86 $this->namespace,
87 '/' . $this->rest_base . '/disconnect',
88 [
89 [
90 'methods' => WP_REST_Server::CREATABLE,
91 'callback' => [ $this, 'disconnect' ],
92 'permission_callback' => [ $this, 'admin_permissions_check' ],
93 'args' => [
94 'gateway' => [
95 'description' => __( 'Gateway key to disconnect (clears credentials).', 'texty' ),
96 'type' => 'string',
97 'required' => true,
98 ],
99 ],
100 ],
101 ]
102 );
103 }
104
105 /**
106 * Activate a gateway. Sets `texty_settings.gateway` to the given key.
107 *
108 * @param WP_REST_Request $request Request object.
109 *
110 * @return WP_REST_Response|WP_Error
111 * @since TEXTY_VERSION
112 */
113 public function activate( $request ) {
114 $gateway_key = sanitize_key( (string) $request->get_param( 'gateway' ) );
115
116 if ( '' === $gateway_key ) {
117 return new WP_Error(
118 'texty_invalid_gateway',
119 __( 'A gateway key is required.', 'texty' ),
120 [ 'status' => 400 ]
121 );
122 }
123
124 $registered = texty()->gateways()->all();
125 if ( ! isset( $registered[ $gateway_key ] ) ) {
126 return new WP_Error(
127 'texty_unknown_gateway',
128 __( 'Unknown gateway.', 'texty' ),
129 [ 'status' => 400 ]
130 );
131 }
132
133 $stored = $this->load_stored();
134
135 // Gateways that declare no settings (e.g. Fake) need no credentials.
136 $gateway_class = $registered[ $gateway_key ];
137 $gateway_obj = is_object( $gateway_class ) ? $gateway_class : new $gateway_class();
138 $needs_creds = method_exists( $gateway_obj, 'get_settings' )
139 ? ! empty( $gateway_obj->get_settings() )
140 : true;
141
142 if ( $needs_creds && ( empty( $stored[ $gateway_key ] ) || ! is_array( $stored[ $gateway_key ] ) ) ) {
143 return new WP_Error(
144 'texty_no_credentials',
145 __( 'Cannot activate a gateway that has no saved credentials. Save credentials first.', 'texty' ),
146 [ 'status' => 400 ]
147 );
148 }
149
150 $stored['gateway'] = $gateway_key;
151 update_option( self::OPTION_KEY, $stored );
152
153 /**
154 * Fires after a gateway is activated.
155 *
156 * @param string $gateway_key The gateway that was activated.
157 */
158 do_action( 'texty_gateway_activated', $gateway_key );
159
160 return rest_ensure_response(
161 [
162 'success' => true,
163 'active_gateway' => $gateway_key,
164 ]
165 );
166 }
167
168 /**
169 * Deactivate the currently active gateway. Clears `texty_settings.gateway`.
170 *
171 * @param WP_REST_Request $request Request object.
172 *
173 * @return WP_REST_Response
174 * @since TEXTY_VERSION
175 */
176 public function deactivate( $request ) {
177 $stored = $this->load_stored();
178 $previous = isset( $stored['gateway'] ) ? (string) $stored['gateway'] : '';
179 $stored['gateway'] = '';
180
181 update_option( self::OPTION_KEY, $stored );
182
183 /**
184 * Fires after a gateway is deactivated.
185 *
186 * @param string $previous The gateway that was previously active.
187 */
188 do_action( 'texty_gateway_deactivated', $previous );
189
190 return rest_ensure_response(
191 [
192 'success' => true,
193 'active_gateway' => '',
194 ]
195 );
196 }
197
198 /**
199 * Disconnect a gateway — clears its saved credentials. If it was the active
200 * gateway, also deactivates it.
201 *
202 * @param WP_REST_Request $request Request object.
203 *
204 * @return WP_REST_Response|WP_Error
205 * @since TEXTY_VERSION
206 */
207 public function disconnect( $request ) {
208 $gateway_key = sanitize_key( (string) $request->get_param( 'gateway' ) );
209
210 if ( '' === $gateway_key ) {
211 return new WP_Error(
212 'texty_invalid_gateway',
213 __( 'A gateway key is required.', 'texty' ),
214 [ 'status' => 400 ]
215 );
216 }
217
218 $stored = $this->load_stored();
219
220 unset( $stored[ $gateway_key ] );
221
222 if ( isset( $stored['gateway'] ) && $stored['gateway'] === $gateway_key ) {
223 $stored['gateway'] = '';
224 }
225
226 update_option( self::OPTION_KEY, $stored );
227
228 /**
229 * Fires after a gateway's credentials are cleared.
230 *
231 * @param string $gateway_key The gateway that was disconnected.
232 */
233 do_action( 'texty_gateway_disconnected', $gateway_key );
234
235 return rest_ensure_response(
236 [
237 'success' => true,
238 'active_gateway' => isset( $stored['gateway'] ) ? (string) $stored['gateway'] : '',
239 'disconnected' => $gateway_key,
240 ]
241 );
242 }
243
244 /**
245 * Read the backing option safely.
246 *
247 * @return array
248 * @since TEXTY_VERSION
249 */
250 private function load_stored(): array {
251 $stored = get_option( self::OPTION_KEY, [] );
252 return is_array( $stored ) ? $stored : [];
253 }
254 }
255