PluginProbe
Texty – SMS Notification for WordPress, WooCommerce, Dokan and more / 2.0.1
Texty – SMS Notification for WordPress, WooCommerce, Dokan and more v2.0.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 / Plivo.php

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

163 lines 4.6 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 * Plivo Class
10 *
11 * @see https://www.plivo.com/docs/sms/api/message#send-a-message
12 */
13 class Plivo implements GatewayInterface {
14
15 /**
16 * API Endpoint
17 */
18 const ENDPOINT = 'https://api.plivo.com/v1/Account/{auth_id}/Message/';
19
20 /**
21 * Get the name
22 *
23 * @return string
24 */
25 public function name() {
26 return __( 'Plivo', 'texty' );
27 }
28
29 /**
30 * Get the name
31 *
32 * @return string
33 */
34 public function description() {
35 return sprintf(
36 // translators: URL to Plivo settings and help docs
37 __(
38 'Send SMS with Plivo. Follow <a href="%1$s" target="_blank">this link</a> to get the Auth ID and Token from Plivo. Follow <a href="%2$s" target="_blank">these instructions</a> to configure the gateway.',
39 'texty'
40 ),
41 'https://console.plivo.com/sms/reporting/',
42 'https://github.com/weDevsOfficial/texty/wiki/Plivo'
43 );
44 }
45
46 /**
47 * Get the logo
48 *
49 * @return string
50 */
51 public function logo() {
52 return TEXTY_URL . '/assets/images/plivo-logo.png';
53 }
54
55 /**
56 * Get the settings
57 *
58 * @return array
59 */
60 public function get_settings() {
61 $creds = texty()->settings()->get( 'plivo' );
62
63 return [
64 'auth_id' => [
65 'name' => __( 'Auth ID', 'texty' ),
66 'type' => 'text',
67 'value' => isset( $creds['auth_id'] ) ? $creds['auth_id'] : '',
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 Plivo 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( 'plivo' );
95
96 $args = [
97 'headers' => [
98 'Authorization' => 'Basic ' . base64_encode( $creds['auth_id'] . ':' . $creds['token'] ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
99 ],
100 'body' => [
101 'src' => $creds['from'],
102 'dst' => $to,
103 'text' => $message,
104 ],
105 ];
106
107 $endpoint = str_replace( '{auth_id}', $creds['auth_id'], self::ENDPOINT );
108 $response = wp_remote_post( $endpoint, $args );
109 $body = json_decode( wp_remote_retrieve_body( $response ) );
110
111 if ( 202 !== $response['response']['code'] ) {
112 return new WP_Error( $body->code, $body->message );
113 }
114
115 return [
116 'success' => true,
117 'reference_id' => isset( $body->message_uuid )
118 ? ( is_array( $body->message_uuid ) ? ( $body->message_uuid[0] ?? null ) : $body->message_uuid )
119 : null,
120 ];
121 }
122
123 /**
124 * Validate a REST API request
125 *
126 * @param WP_REST_Request $request
127 *
128 * @return WP_Error|array
129 */
130 public function validate( $request ) {
131 $creds = $request->get_param( 'plivo' );
132
133 $args = [
134 'headers' => [
135 'Authorization' => 'Basic ' . base64_encode( $creds['auth_id'] . ':' . $creds['token'] ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
136 ],
137 'body' => [
138 'limit' => 5,
139 'offset' => 0,
140 ],
141 ];
142
143 $endpoint = "https://api.plivo.com/v1/Account/{$creds['auth_id']}/Message/";
144 $response = wp_remote_get( $endpoint, $args );
145 $body = json_decode( wp_remote_retrieve_body( $response ) );
146 $response_code = wp_remote_retrieve_response_code( $response );
147
148 if ( 200 !== $response_code ) {
149 return new WP_Error(
150 $body->code,
151 $body->detail ? $body->detail : $body->message,
152 $body
153 );
154 }
155
156 return [
157 'auth_id' => $creds['auth_id'],
158 'token' => $creds['token'],
159 'from' => $creds['from'],
160 ];
161 }
162 }
163