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

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

157 lines 4.0 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 * Nexmo Class
9 *
10 * @see https://developer.nexmo.com/api/sms
11 */
12 class Vonage implements GatewayInterface {
13
14 /**
15 * API Endpoint
16 */
17 const ENDPOINT = 'https://rest.nexmo.com';
18
19 /**
20 * Get the name
21 *
22 * @return string
23 */
24 public function name() {
25 return __( 'Vonage (nexmo)', 'texty' );
26 }
27
28 /**
29 * Get the logo
30 *
31 * @return string
32 */
33 public function logo() {
34 return TEXTY_URL . '/assets/images/vonage.svg';
35 }
36
37 /**
38 * Get the name
39 *
40 * @return string
41 */
42 public function description() {
43 return sprintf(
44 // translators: Vonage dashboard settings URL and help docs
45 __(
46 'Send SMS with Vonage (formerly Nexmo). Follow <a href="%1$s" target="_blank">this link</a> to get the API Key and Secret from Vonage. Follow <a href="%2$s" target="_blank">these instructions</a> to configure the gateway.',
47 'texty'
48 ),
49 'https://dashboard.nexmo.com/settings',
50 'https://github.com/weDevsOfficial/texty/wiki/Vonage'
51 );
52 }
53
54 /**
55 * Get the settings
56 *
57 * @return array
58 */
59 public function get_settings() {
60 $creds = texty()->settings()->get( 'vonage' );
61
62 return [
63 'key' => [
64 'name' => __( 'API Key', 'texty' ),
65 'type' => 'text',
66 'value' => isset( $creds['key'] ) ? $creds['key'] : '',
67 'help' => '',
68 ],
69 'secret' => [
70 'name' => __( 'API Secret', 'texty' ),
71 'type' => 'password',
72 'value' => isset( $creds['secret'] ) ? $creds['secret'] : '',
73 'help' => '',
74 ],
75 'from' => [
76 'name' => __( 'From Number', 'texty' ),
77 'type' => 'text',
78 'value' => isset( $creds['from'] ) ? $creds['from'] : '',
79 'help' => '',
80 ],
81 ];
82 }
83
84 /**
85 * Send SMS
86 *
87 * @param string $to
88 * @param string $message
89 *
90 * @return WP_Error|bool
91 */
92 public function send( $to, $message ) {
93 $creds = texty()->settings()->get( 'vonage' );
94
95 $args = [
96 'body' => [
97 'from' => $creds['from'],
98 'text' => $message,
99 'to' => $to,
100 'api_key' => $creds['key'],
101 'api_secret' => $creds['secret'],
102 ],
103 ];
104
105 $request = wp_remote_post( self::ENDPOINT . '/sms/json', $args );
106 $body = json_decode( wp_remote_retrieve_body( $request ) );
107 $response_code = wp_remote_retrieve_response_code( $request );
108
109 if ( is_wp_error( $request ) ) {
110 return $request;
111 }
112
113 if ( $body->messages[0]->status !== '0' ) {
114 return new WP_Error(
115 $body->messages[0]->status,
116 $body->messages[0]->{'error-text'}
117 );
118 }
119
120 return true;
121 }
122
123 /**
124 * Validate a REST API request
125 *
126 * @param WP_REST_Request $request
127 *
128 * @return WP_Error|true
129 */
130 public function validate( $request ) {
131 $creds = $request->get_param( 'vonage' );
132
133 $args = [
134 'api_key' => $creds['key'],
135 'api_secret' => $creds['secret'],
136 ];
137
138 $endpoint = self::ENDPOINT . '/account/get-balance?' . http_build_query( $args );
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->{'error-code'},
146 $body->{'error-code-label'}
147 );
148 }
149
150 return [
151 'key' => $creds['key'],
152 'secret' => $creds['secret'],
153 'from' => $creds['from'],
154 ];
155 }
156 }
157