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

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

150 lines 3.5 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 * Clickatell Class
9 *
10 * @see https://www.clickatell.com/developers/api-documentation/rest-api-send-message/
11 */
12 class Clickatell implements GatewayInterface {
13
14 /**
15 * API Endpoint
16 */
17 const ENDPOINT = 'https://platform.clickatell.com/messages';
18
19 /**
20 * Get the name
21 *
22 * @return string
23 */
24 public function name() {
25 return __( 'Clickatell', '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 Clickatell. Follow <a href="%1$s" target="_blank">this link</a> to get the API. Follow <a href="%2$s" target="_blank">these instructions</a> to configure the gateway.',
38 'texty'
39 ),
40 'https://app.clickatell.com/my-workspace',
41 'https://github.com/weDevsOfficial/texty/wiki/Clickatell'
42 );
43 }
44
45 /**
46 * Get the logo
47 *
48 * @return string
49 */
50 public function logo() {
51 return TEXTY_URL . '/assets/images/clickatell.svg';
52 }
53
54 /**
55 * Get the settings
56 *
57 * @return array
58 */
59 public function get_settings() {
60 $creds = texty()->settings()->get( 'clickatell' );
61
62 return [
63 'key' => [
64 'name' => __( 'API Key', 'texty' ),
65 'type' => 'password',
66 'value' => isset( $creds['key'] ) ? $creds['key'] : '',
67 'help' => '',
68 ],
69 ];
70 }
71
72 /**
73 * Send SMS
74 *
75 * @param string $to
76 * @param string $message
77 *
78 * @return WP_Error|true
79 */
80 public function send( $to, $message ) {
81 $creds = texty()->settings()->get( 'clickatell' );
82
83 $args = [
84 'headers' => [
85 'Authorization' => $creds['key'],
86 'Content-Type' => 'application/json',
87 'Accept' => 'application/json',
88 ],
89 'body' => wp_json_encode(
90 [
91 'to' => [ $to ],
92 'content' => $message,
93 ]
94 ),
95 ];
96
97 $response = wp_remote_post( self::ENDPOINT, $args );
98
99 if ( is_wp_error( $response ) ) {
100 return $response;
101 }
102
103 $body = wp_remote_retrieve_body( $response );
104 $response_code = wp_remote_retrieve_response_code( $response );
105
106 // phpcs:disable
107 if ( 202 !== $response_code ) {
108 switch ( $response_code ) {
109 case 200:
110 $body = json_decode( $body );
111
112 return new WP_Error(
113 $body->errorCode,
114 $body->errorDescription
115 );
116
117 case 400:
118 return new WP_Error(
119 400,
120 'Bad Request'
121 );
122
123 default:
124 return new WP_Error(
125 $response_code,
126 'Bad Request'
127 );
128 }
129 }
130 // phpcs:enable
131
132 return true;
133 }
134
135 /**
136 * Validate a REST API request
137 *
138 * @param WP_REST_Request $request
139 *
140 * @return WP_Error|true
141 */
142 public function validate( $request ) {
143 $creds = $request->get_param( 'clickatell' );
144
145 return [
146 'key' => $creds['key'],
147 ];
148 }
149 }
150