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 / Plivo.php

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

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