PluginProbe
Texty – SMS Notification for WordPress, WooCommerce, Dokan and more / 1.1
Texty – SMS Notification for WordPress, WooCommerce, Dokan and more v1.1
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 1.1, at includes/Gateways/Twilio.php

158 lines 4.3 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
7 /**
8 * Twilio Class
9 *
10 * @see https://www.twilio.com/docs/sms/api/message
11 */
12 class Twilio implements GatewayInterface {
13
14 /**
15 * API Endpoint
16 */
17 const ENDPOINT = 'https://api.twilio.com/2010-04-01/Accounts/{sid}/Messages.json';
18
19 /**
20 * Get the name
21 *
22 * @return string
23 */
24 public function name() {
25 return __( 'Twilio', 'texty' );
26 }
27
28 /**
29 * Get the name
30 *
31 * @return string
32 */
33 public function description() {
34 return sprintf(
35 // translators: URL to Twilio settings and help docs
36 __(
37 '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.',
38 'texty'
39 ),
40 'https://www.twilio.com/console/project/settings',
41 'https://github.com/weDevsOfficial/texty/wiki/Twilio'
42 );
43 }
44
45 /**
46 * Get the logo
47 *
48 * @return string
49 */
50 public function logo() {
51 return TEXTY_URL . '/assets/images/twilio.svg';
52 }
53
54 /**
55 * Get the settings
56 *
57 * @return array
58 */
59 public function get_settings() {
60 $creds = texty()->settings()->get( 'twilio' );
61
62 return [
63 'sid' => [
64 'name' => __( 'Account SID', 'texty' ),
65 'type' => 'text',
66 'value' => isset( $creds['sid'] ) ? $creds['sid'] : '',
67 'help' => '',
68 ],
69 'token' => [
70 'name' => __( 'Auth Token', 'texty' ),
71 'type' => 'password',
72 'value' => isset( $creds['token'] ) ? $creds['token'] : '',
73 'help' => '',
74 ],
75 'from' => [
76 'name' => __( 'From Number', 'texty' ),
77 'type' => 'text',
78 'value' => isset( $creds['from'] ) ? $creds['from'] : '',
79 'help' => __( 'Must be a valid number associated with your Twilio account', 'texty' ),
80 ],
81 ];
82 }
83
84 /**
85 * Send SMS
86 *
87 * @param string $to
88 * @param string $message
89 *
90 * @return WP_Error|true
91 */
92 public function send( $to, $message ) {
93 $creds = texty()->settings()->get( 'twilio' );
94
95 $args = [
96 'headers' => [
97 'Authorization' => 'Basic ' . base64_encode( $creds['sid'] . ':' . $creds['token'] ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
98 ],
99 'body' => [
100 'From' => $creds['from'],
101 'To' => $to,
102 'Body' => $message,
103 ],
104 ];
105
106 $endpoint = str_replace( '{sid}', $creds['sid'], self::ENDPOINT );
107 $response = wp_remote_post( $endpoint, $args );
108
109 if ( is_wp_error( $response ) ) {
110 return $response;
111 }
112
113 $body = json_decode( wp_remote_retrieve_body( $response ) );
114
115 if ( 201 !== $response['response']['code'] ) {
116 return new WP_Error( $body->code, $body->message );
117 }
118
119 return true;
120 }
121
122 /**
123 * Validate a REST API request
124 *
125 * @param WP_REST_Request $request
126 *
127 * @return WP_Error|true
128 */
129 public function validate( $request ) {
130 $creds = $request->get_param( 'twilio' );
131
132 $args = [
133 'headers' => [
134 'Authorization' => 'Basic ' . base64_encode( $creds['sid'] . ':' . $creds['token'] ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
135 ],
136 ];
137
138 $endpoint = 'https://api.twilio.com/2010-04-01/Accounts.json';
139 $response = wp_remote_get( $endpoint, $args );
140 $body = json_decode( wp_remote_retrieve_body( $response ) );
141 $response_code = wp_remote_retrieve_response_code( $response );
142
143 if ( 401 === $response_code ) {
144 return new WP_Error(
145 $body->code,
146 $body->detail ? $body->detail : $body->message,
147 $body
148 );
149 }
150
151 return [
152 'sid' => $creds['sid'],
153 'token' => $creds['token'],
154 'from' => $creds['from'],
155 ];
156 }
157 }
158