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