action-exception.php
2 years ago
condition-exception.php
2 years ago
condition-silence-exception.php
2 years ago
gateway-exception.php
2 years ago
handler-exception.php
2 years ago
integration-exception.php
2 years ago
invalid-macro-exception.php
2 years ago
not-router-request.php
2 years ago
parse-exception.php
2 years ago
plain-default-exception.php
2 years ago
preset-exception.php
2 years ago
query-builder-exception.php
2 years ago
repository-exception.php
2 years ago
request-exception.php
2 years ago
silence-exception.php
2 years ago
handler-exception.php
113 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Exceptions; |
| 5 | |
| 6 | use Jet_Form_Builder\Form_Messages\Manager; |
| 7 | use Jet_Form_Builder\Form_Messages\Status_Info; |
| 8 | use JFB_Modules\Dev; |
| 9 | use JFB_Modules\Logger; |
| 10 | |
| 11 | // If this file is called directly, abort. |
| 12 | if ( ! defined( 'WPINC' ) ) { |
| 13 | die; |
| 14 | } |
| 15 | |
| 16 | abstract class Handler_Exception extends \Exception { |
| 17 | |
| 18 | protected $default_type_message = 'failed'; |
| 19 | |
| 20 | protected $additional_data; |
| 21 | |
| 22 | public function __construct( $message = '', ...$additional_data ) { |
| 23 | parent::__construct( $message, 0, null ); |
| 24 | |
| 25 | $this->additional_data = $additional_data; |
| 26 | |
| 27 | $this->check_message_length(); |
| 28 | $this->log(); |
| 29 | } |
| 30 | |
| 31 | public function set_code( $code ) { |
| 32 | $this->code = $code; |
| 33 | |
| 34 | return $this; |
| 35 | } |
| 36 | |
| 37 | public function dynamic_success(): Handler_Exception { |
| 38 | $this->message = Manager::dynamic_success( $this->message ); |
| 39 | |
| 40 | return $this; |
| 41 | } |
| 42 | |
| 43 | public function dynamic_error(): Handler_Exception { |
| 44 | $this->message = Manager::dynamic_error( $this->message ); |
| 45 | |
| 46 | return $this; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | public function get_form_status() { |
| 51 | return $this->message ?: $this->default_type_message; |
| 52 | } |
| 53 | |
| 54 | public function get_additional() { |
| 55 | return $this->additional_data; |
| 56 | } |
| 57 | |
| 58 | public function unset_log() { |
| 59 | Logger\Module::instance()->unset_last( static::class ); |
| 60 | } |
| 61 | |
| 62 | public function log() { |
| 63 | if ( ! jet_form_builder()->has_module( 'dev' ) ) { |
| 64 | return; |
| 65 | } |
| 66 | Logger\Module::instance()->push_log( |
| 67 | static::class, |
| 68 | array( |
| 69 | 'message' => $this->getMessage(), |
| 70 | 'file' => $this->make_pretty_filename( $this->getFile() ), |
| 71 | 'line' => $this->getLine(), |
| 72 | 'data' => $this->get_additional(), |
| 73 | 'version' => jet_form_builder()->get_version(), |
| 74 | 'action_id' => jet_fb_action_handler()->get_position(), |
| 75 | ) |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | private function make_pretty_filename( string $file ) { |
| 80 | $path = explode( 'wp-content\\', $file ); |
| 81 | |
| 82 | return $path[1] ?? ( explode( 'wp-content/', $file )[1] ?? $file ); |
| 83 | } |
| 84 | |
| 85 | public function is_success(): bool { |
| 86 | if ( 'success' === $this->message ) { |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | return is_string( $this->message ) && 0 === strpos( $this->message, Manager::DYNAMIC_SUCCESS_PREF ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * We need to check the status length for cases when it's too long. |
| 95 | * If it's long - we store it in `*_jet_fb_records_errors` table |
| 96 | * |
| 97 | * @since 3.1.8 |
| 98 | * |
| 99 | * @see https://github.com/Crocoblock/issues-tracker/issues/4994 |
| 100 | */ |
| 101 | protected function check_message_length() { |
| 102 | if ( 255 > strlen( $this->message ) ) { |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | $status = new Status_Info( $this->message ); |
| 107 | $this->message = $status->is_success() ? 'success' : 'failed'; |
| 108 | |
| 109 | array_unshift( $this->additional_data, $status->get_raw_message() ); |
| 110 | } |
| 111 | |
| 112 | } |
| 113 |