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 / Gateways / Twilio.php

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

180 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Texty\Gateways;
4
5 use WP_Error;
6 use WP_REST_Request;
7
8 /**
9 * Twilio Class
10 *
11 * @see https://www.twilio.com/docs/sms/api/message
12 */
13 class Twilio implements GatewayInterface {
14
15 /**
16 * API Endpoint
17 */
18 const ENDPOINT = 'https://api.twilio.com/2010-04-01/Accounts/{sid}/Messages.json';
19
20 /**
21 * Get the name
22 *
23 * @return string
24 */
25 public function name() {
26 return __( 'Twilio', 'texty' );
27 }
28
29 /**
30 * Get the name
31 *
32 * @return string
33 */
34 public function description() {
35 return sprintf(
36 // translators: URL to Twilio settings and help docs
37 __(
38 'Send SMS with Twilio. Follow <a href="%1$s" target="_blank">this link</a> to get the Account SID and Token from Twilio. Follow <a href="%2$s" target="_blank">these instructions</a> to configure the gateway.',
39 'texty'
40 ),
41 'https://www.twilio.com/console/project/settings',
42 'https://github.com/weDevsOfficial/texty/wiki/Twilio'
43 );
44 }
45
46 /**
47 * Get the logo
48 *
49 * @return string
50 */
51 public function logo() {
52 return TEXTY_URL . '/assets/images/twilio-logo.png';
53 }
54
55 /**
56 * Get the settings
57 *
58 * @return array
59 */
60 public function get_settings() {
61 $creds = texty()->settings()->get( 'twilio' );
62
63 return [
64 'sid' => [
65 'name' => __( 'Account SID', 'texty' ),
66 'type' => 'text',
67 'value' => isset( $creds['sid'] ) ? $creds['sid'] : '',
68 'help' => '',
69 ],
70 'token' => [
71 'name' => __( 'Auth Token', 'texty' ),
72 'type' => 'password',
73 'value' => isset( $creds['token'] ) ? $creds['token'] : '',
74 'help' => '',
75 ],
76 'from' => [
77 'name' => __( 'From Number', 'texty' ),
78 'type' => 'text',
79 'value' => isset( $creds['from'] ) ? $creds['from'] : '',
80 'help' => __( 'Must be a valid number associated with your Twilio account', 'texty' ),
81 ],
82 ];
83 }
84
85 /**
86 * Send SMS
87 *
88 * @param string $to
89 * @param string $message
90 *
91 * @return WP_Error|array
92 */
93 public function send( $to, $message ) {
94 $creds = texty()->settings()->get( 'twilio' );
95
96 $args = [
97 'headers' => [
98 'Authorization' => 'Basic ' . base64_encode( $creds['sid'] . ':' . $creds['token'] ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
99 ],
100 'body' => [
101 'From' => $creds['from'],
102 'To' => $to,
103 'Body' => $message,
104 ],
105 ];
106
107 $endpoint = str_replace( '{sid}', $creds['sid'], self::ENDPOINT );
108 $response = wp_remote_post( $endpoint, $args );
109
110 if ( is_wp_error( $response ) ) {
111 return $response;
112 }
113
114 $body = json_decode( wp_remote_retrieve_body( $response ) );
115
116 if ( 201 !== $response['response']['code'] ) {
117 return new WP_Error( $body->code, $body->message );
118 }
119
120 return [
121 'success' => true,
122 'reference_id' => isset( $body->sid ) ? $body->sid : null,
123 ];
124 }
125
126 /**
127 * Validate a REST API request
128 *
129 * @param WP_REST_Request $request
130 *
131 * @return WP_Error|mixed
132 */
133 public function validate( $request ) {
134 $creds = $request->get_param( 'twilio' );
135
136 if ( ! is_array( $creds ) || empty( $creds['sid'] ) || empty( $creds['token'] ) ) {
137 return new WP_Error(
138 'texty_missing_credentials',
139 __( 'Twilio Account SID and Auth Token are required.', 'texty' )
140 );
141 }
142
143 $args = [
144 'headers' => [
145 'Authorization' => 'Basic ' . base64_encode( $creds['sid'] . ':' . $creds['token'] ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
146 ],
147 ];
148
149 $endpoint = 'https://api.twilio.com/2010-04-01/Accounts.json';
150 $response = wp_remote_get( $endpoint, $args );
151
152 if ( is_wp_error( $response ) ) {
153 return $response;
154 }
155
156 $body = json_decode( wp_remote_retrieve_body( $response ) );
157 $response_code = wp_remote_retrieve_response_code( $response );
158
159 if ( 401 === $response_code ) {
160 $code = isset( $body->code ) ? $body->code : 'twilio_auth_failed';
161
162 if ( ! empty( $body->detail ) ) {
163 $message = $body->detail;
164 } elseif ( ! empty( $body->message ) ) {
165 $message = $body->message;
166 } else {
167 $message = __( 'Twilio rejected the credentials.', 'texty' );
168 }
169
170 return new WP_Error( $code, $message, $body );
171 }
172
173 return [
174 'sid' => $creds['sid'],
175 'token' => $creds['token'],
176 'from' => isset( $creds['from'] ) ? $creds['from'] : '',
177 ];
178 }
179 }
180