PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.7.7
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.7.7
1.7.7 1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
image-optimization / vendor / elementor / wp-one-package / src / Common / RestError.php

RestError.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.7.7, at vendor/elementor/wp-one-package/src/Common/RestError.php

108 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ElementorOne\Common;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit; // Exit if accessed directly
7 }
8
9 /**
10 * Class RestError
11 * Centralized REST API error handling with proper HTTP status codes
12 */
13 class RestError {
14
15 /**
16 * Create a bad request error (400)
17 *
18 * @param string $message Error message
19 * @param array $data Additional error data
20 * @return \WP_Error
21 */
22 public static function bad_request( string $message, array $data = [] ): \WP_Error {
23 return self::create_error( 'bad_request', $message, \WP_Http::BAD_REQUEST, $data );
24 }
25
26 /**
27 * Create an unauthorized error (401)
28 *
29 * @param string $message Error message
30 * @param array $data Additional error data
31 * @return \WP_Error
32 */
33 public static function unauthorized( string $message, array $data = [] ): \WP_Error {
34 return self::create_error( 'unauthorized', $message, \WP_Http::UNAUTHORIZED, $data );
35 }
36
37 /**
38 * Create a forbidden error (403)
39 *
40 * @param string $message Error message
41 * @param array $data Additional error data
42 * @return \WP_Error
43 */
44 public static function forbidden( string $message, array $data = [] ): \WP_Error {
45 return self::create_error( 'forbidden', $message, \WP_Http::FORBIDDEN, $data );
46 }
47
48 /**
49 * Create a not found error (404)
50 *
51 * @param string $message Error message
52 * @param array $data Additional error data
53 * @return \WP_Error
54 */
55 public static function not_found( string $message, array $data = [] ): \WP_Error {
56 return self::create_error( 'not_found', $message, \WP_Http::NOT_FOUND, $data );
57 }
58
59 /**
60 * Create a conflict error (409)
61 *
62 * @param string $message Error message
63 * @param array $data Additional error data
64 * @return \WP_Error
65 */
66 public static function conflict( string $message, array $data = [] ): \WP_Error {
67 return self::create_error( 'conflict', $message, \WP_Http::CONFLICT, $data );
68 }
69
70 /**
71 * Create an internal server error (500)
72 *
73 * @param string $message Error message
74 * @param array $data Additional error data
75 * @return \WP_Error
76 */
77 public static function internal_server_error( string $message, array $data = [] ): \WP_Error {
78 return self::create_error( 'internal_server_error', $message, \WP_Http::INTERNAL_SERVER_ERROR, $data );
79 }
80
81 /**
82 * Create a custom error with specific code and status
83 *
84 * @param string $code Error code
85 * @param string $message Error message
86 * @param int $status HTTP status code
87 * @param array $data Additional error data
88 * @return \WP_Error
89 */
90 public static function custom_error( string $code, string $message, int $status, array $data = [] ): \WP_Error {
91 return self::create_error( $code, $message, $status, $data );
92 }
93
94 /**
95 * Internal method to create WP_Error with proper structure
96 *
97 * @param string $code Error code
98 * @param string $message Error message
99 * @param int $status HTTP status code
100 * @param array $data Additional error data
101 * @return \WP_Error
102 */
103 private static function create_error( string $code, string $message, int $status, array $data = [] ): \WP_Error {
104 $error_data = array_merge( [ 'status' => $status ], $data );
105 return new \WP_Error( $code, $message, $error_data );
106 }
107 }
108