validator
3 days ago
class-adbc-common-utils.php
4 months ago
class-adbc-files.php
7 months ago
class-adbc-logging.php
3 months ago
class-adbc-notifications.php
3 days ago
class-adbc-rest.php
3 days ago
class-adbc-rest.php
139 lines
| 1 | <?php |
| 2 | |
| 3 | // Exit if accessed directly |
| 4 | if ( ! defined( 'ABSPATH' ) ) |
| 5 | exit; |
| 6 | |
| 7 | /** |
| 8 | * ADBC response class. |
| 9 | * |
| 10 | * This class provides REST methods. |
| 11 | */ |
| 12 | class ADBC_Rest { |
| 13 | |
| 14 | public const OK = 200; |
| 15 | public const BAD_REQUEST = 400; |
| 16 | public const UNAUTHORIZED = 403; |
| 17 | public const INTERNAL_SERVER_ERROR = 500; |
| 18 | public const NOT_FOUND = 404; |
| 19 | public const UNPROCESSABLE_ENTITY = 422; |
| 20 | |
| 21 | /** |
| 22 | * Return REST success response. |
| 23 | * |
| 24 | * @param string $message Message to return. |
| 25 | * @param mixed $data Data to return. |
| 26 | * @param array $extra_data Extra data to return. |
| 27 | * |
| 28 | * @return WP_REST_Response Response object. |
| 29 | */ |
| 30 | public static function success( $message = "", $data = [], $extra_data = [] ) { |
| 31 | |
| 32 | $response = [ |
| 33 | 'success' => true, |
| 34 | 'message' => $message, |
| 35 | 'data' => $data, |
| 36 | ]; |
| 37 | |
| 38 | if ( ! empty( $extra_data ) ) |
| 39 | $response['extra_data'] = $extra_data; |
| 40 | |
| 41 | return new WP_REST_Response( |
| 42 | $response, |
| 43 | self::OK |
| 44 | ); |
| 45 | |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Return REST error response. |
| 50 | * |
| 51 | * @param string $message Message to return. |
| 52 | * @param int $status_code Status code to return. |
| 53 | * @param int $failure_code Failure code to return. |
| 54 | * @param array $extra_data Extra data to return. |
| 55 | * |
| 56 | * @return WP_REST_Response Response object. |
| 57 | */ |
| 58 | public static function error( $message, $status_code, $failure_code = 0, $extra_data = [] ) { |
| 59 | |
| 60 | $response = [ |
| 61 | 'success' => false, |
| 62 | 'message' => $message, |
| 63 | ]; |
| 64 | |
| 65 | if ( $failure_code !== 0 ) |
| 66 | $response['failure_code'] = $failure_code; |
| 67 | |
| 68 | if ( ! empty( $extra_data ) ) |
| 69 | $response['extra_data'] = $extra_data; |
| 70 | |
| 71 | return new WP_REST_Response( |
| 72 | $response, |
| 73 | $status_code |
| 74 | ); |
| 75 | |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Return REST error response when an exception occurs. |
| 80 | * |
| 81 | * @param string $method_name Method name where the exception occurred. |
| 82 | * @param object $exception Exception object. |
| 83 | * @return WP_REST_Response Response object. |
| 84 | */ |
| 85 | public static function error_for_uncaught_exception( $method_name = "", $exception = null ) { |
| 86 | |
| 87 | // Log exception if exists |
| 88 | if ( $exception !== null ) |
| 89 | ADBC_Logging::log_exception( $method_name, $exception ); |
| 90 | |
| 91 | $failure_code = $exception->getCode(); // Returns 0 if no code is set |
| 92 | |
| 93 | // Attach a default "Check the logs" link. |
| 94 | $extra_data = [ |
| 95 | 'message_links' => [ |
| 96 | [ |
| 97 | 'text' => __( 'Check the logs', 'advanced-database-cleaner' ), |
| 98 | 'tab_id' => 'info_and_logs', |
| 99 | 'sub_tab_id' => 'debug', |
| 100 | ], |
| 101 | ], |
| 102 | ]; |
| 103 | |
| 104 | return self::error( |
| 105 | sprintf( 'Uncaught exception in %s.', $method_name ), |
| 106 | self::INTERNAL_SERVER_ERROR, |
| 107 | $failure_code, |
| 108 | $extra_data |
| 109 | ); |
| 110 | |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Return REST scan heartbeat response. |
| 115 | */ |
| 116 | public static function heartbeat( $message = "", $heartbeat_code = "", $data = [] ) { |
| 117 | |
| 118 | return new WP_REST_Response( |
| 119 | [ |
| 120 | 'success' => true, |
| 121 | 'message' => $message, |
| 122 | 'heartbeat_code' => $heartbeat_code, |
| 123 | 'data' => $data, |
| 124 | "extra_data" => [ |
| 125 | 'message_links' => [ |
| 126 | [ |
| 127 | 'text' => __( 'Check the logs', 'advanced-database-cleaner' ), |
| 128 | 'tab_id' => 'info_and_logs', |
| 129 | 'sub_tab_id' => 'debug', |
| 130 | ], |
| 131 | ], |
| 132 | ], |
| 133 | ], |
| 134 | self::OK |
| 135 | ); |
| 136 | |
| 137 | } |
| 138 | |
| 139 | } |