| 1 |
<?php |
| 2 |
|
| 3 |
namespace Leadin\utils; |
| 4 |
|
| 5 |
/** |
| 6 |
* Static class containing methods used on ajax requests handling. |
| 7 |
*/ |
| 8 |
class RequestUtils { |
| 9 |
/** |
| 10 |
* Send JSON response. |
| 11 |
* |
| 12 |
* @param Mixed $body response to send as JSON. |
| 13 |
* @param Number $code http code to return. |
| 14 |
*/ |
| 15 |
private static function send( $body, $code ) { |
| 16 |
wp_send_json( $body, intval( $code ) ); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Send JSON response with 200 code. |
| 21 |
* |
| 22 |
* @param Mixed $body response to send as JSON. |
| 23 |
*/ |
| 24 |
public static function send_json( $body = array() ) { |
| 25 |
self::send( $body, 200 ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Send error response with message |
| 30 |
* |
| 31 |
* @param String $error Message to be sent on te JSON. |
| 32 |
* @param Number $code Error code to be sent in the error. Default 400. |
| 33 |
*/ |
| 34 |
public static function send_error_message( $error, $code = 400 ) { |
| 35 |
self::send( |
| 36 |
array( |
| 37 |
'error' => $error, |
| 38 |
), |
| 39 |
intval( $code ) |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Send JSON response with a message key. |
| 45 |
* |
| 46 |
* @param String $message Message to be sent on te JSON. |
| 47 |
*/ |
| 48 |
public static function send_message( $message ) { |
| 49 |
self::send( |
| 50 |
array( |
| 51 |
'message' => $message, |
| 52 |
), |
| 53 |
200 |
| 54 |
); |
| 55 |
} |
| 56 |
} |
| 57 |
|