PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.9.1
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.9.1
2.13.0 2.13.1 2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 All 80 releases
ablocks / includes / classes / exceptions / ablocks-exception.php

ablocks-exception.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder 2.9.1, at includes/classes/exceptions/ablocks-exception.php

133 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ABlocks\Classes\Exceptions;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 die();
7 }
8
9 use Exception;
10 use Throwable;
11 use WP_Error;
12
13 /**
14 * WP_Error compatible exception.
15 */
16 class AblocksException extends Exception {
17
18 protected string $wp_code = '';
19
20 protected $data = [];
21
22 /**
23 * @var Throwable|AblocksException|null
24 */
25 protected ?Throwable $previous = null;
26
27 /**
28 * @param string $message
29 * @param string $wp_code
30 * @param mixed $data
31 * @param int $code
32 * @param ?Throwable $previous
33 */
34 public function __construct( string $message, string $wp_code = '', $data = null, int $code = 0, ?Throwable $previous = null ) {
35 parent::__construct( $message, $code, $previous );
36
37 $this->wp_code = $wp_code ?: $this->get_class_code();
38 $this->previous = $previous;
39
40 if ( ! $data ) {
41 $data = [];
42 }
43
44 if ( empty( $data['status'] ) ) {
45 $data['status'] = $code ? $code : 500;
46 }
47
48 $this->data = $data;
49 }
50
51 /**
52 * @throws self
53 */
54 public static function throw( string $message, $data = null, int $code = 0, ?Throwable $previous = null ) {
55 throw new self( $message, '', $data, $code, $previous );
56 }
57
58 protected function get_class_code(): string {
59 $code = str_replace( [ __NAMESPACE__, 'ABlocks', 'Exception' ], '', __CLASS__ );
60 return $code ?: 'UNKNOWN-ERROR';
61 }
62
63 public function get_wp_error_code(): string {
64 return $this->wp_code;
65 }
66
67 public function get_data( string $key = null ) {
68 if ( $key ) {
69 if ( isset( $this->data[ $key ] ) ) {
70 return $this->data[ $key ];
71 }
72
73 return null;
74 }
75
76 return $this->data;
77 }
78
79 public function set_data( array $data ) {
80 $this->data = $data;
81 }
82
83 public function add_data( string $key, $value ) {
84 $this->data[ $key ] = $value;
85 }
86
87 public function get_wp_error(): WP_Error {
88 return $this->toWpError();
89 }
90
91 public function toWpError(): WP_Error {
92 return new WP_Error( $this->wp_code, $this->getMessage(), $this->data );
93 }
94
95 public function get_previous_data(): ?array {
96
97 if ( ! $this->previous ) {
98 return null;
99 }
100
101 if ( is_a( $this->previous, self::class ) ) {
102 return $this->previous->__toArray();
103 }
104
105 return [
106 'code' => $this->previous,
107 'type' => get_class( $this->previous ),
108 'message' => $this->previous->getMessage(),
109 'data' => null,
110 'trace' => $this->previous->getTrace(),
111 ];
112 }
113
114 public function __toArray(): array {
115 return [
116 'code' => $this->wp_code,
117 'type' => get_class( $this ),
118 'message' => $this->getMessage(),
119 'data' => $this->data,
120 'trace' => $this->getTrace(),
121 'previous' => $this->get_previous_data(),
122 ];
123 }
124
125 public function __toJsonString( $flags = 0, $depth = 512 ): string {
126 return wp_json_encode( $this->__toArray(), $flags, $depth );
127 }
128
129 public static function from_wp_error( WP_Error $wp_error ): self {
130 return new self( $wp_error->get_error_message(), $wp_error->get_error_code(), $wp_error->get_error_data() );
131 }
132 }
133