| 1 |
<?php |
| 2 |
|
| 3 |
namespace Texty\Api; |
| 4 |
|
| 5 |
use WP_REST_Server; |
| 6 |
|
| 7 |
class Tools extends Base { |
| 8 |
|
| 9 |
/** |
| 10 |
* Initialize |
| 11 |
* |
| 12 |
* @return void |
| 13 |
*/ |
| 14 |
public function __construct() { |
| 15 |
$this->namespace = 'texty/v1'; |
| 16 |
$this->rest_base = 'tools'; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Registers the routes for the objects of the controller. |
| 21 |
* |
| 22 |
* @return void |
| 23 |
*/ |
| 24 |
public function register_routes() { |
| 25 |
register_rest_route( |
| 26 |
$this->namespace, |
| 27 |
'/' . $this->rest_base . '/test', |
| 28 |
[ |
| 29 |
[ |
| 30 |
'methods' => WP_REST_Server::CREATABLE, |
| 31 |
'callback' => [ $this, 'test_send' ], |
| 32 |
'permission_callback' => [ $this, 'admin_permissions_check' ], |
| 33 |
'args' => [ |
| 34 |
'to' => [ |
| 35 |
'description' => __( 'The to phone number', 'texty' ), |
| 36 |
'type' => 'string', |
| 37 |
'required' => true, |
| 38 |
], |
| 39 |
], |
| 40 |
], |
| 41 |
] |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
/* |
| 46 |
* Send a test. |
| 47 |
* |
| 48 |
* @param WP_Rest_Request $request |
| 49 |
* |
| 50 |
* @return WP_Rest_Response|WP_Error |
| 51 |
*/ |
| 52 |
public function test_send( $request ) { |
| 53 |
$to = $request->get_param( 'to' ); |
| 54 |
$message = 'This is a test message from Texty.'; |
| 55 |
|
| 56 |
$status = texty()->gateways()->send( $to, $message ); |
| 57 |
|
| 58 |
$response = [ |
| 59 |
'success' => is_wp_error( $status ) ? false : true, |
| 60 |
'message' => is_wp_error( $status ) ? $status->get_error_message() : '', |
| 61 |
]; |
| 62 |
|
| 63 |
return rest_ensure_response( $response ); |
| 64 |
} |
| 65 |
} |
| 66 |
|