| @@ -1,16 +1,124 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | - | |
| 4 | -namespace Jet_Form_Builder\Exceptions; | |
| 5 | - | |
| 6 | - | |
| 7 | -abstract class Handler_Exception extends \Exception { | |
| 8 | - protected $default_type_message = 'failed'; | |
| 9 | - protected $dynamic_message = ''; | |
| 10 | - | |
| 11 | - | |
| 12 | - public function get_form_status() { | |
| 13 | - return $this->message ? $this->message : $this->default_type_message; | |
| 14 | - } | |
| 15 | - | |
| 16 | -} | |
| 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 | + /** | |
| 23 | + * @var Status_Info | |
| 24 | + */ | |
| 25 | + protected $status; | |
| 26 | + | |
| 27 | + public function __construct( $message = '', ...$additional_data ) { | |
| 28 | + parent::__construct( $message, 0, null ); | |
| 29 | + | |
| 30 | + $this->additional_data = $additional_data; | |
| 31 | + | |
| 32 | + $this->check_message_length(); | |
| 33 | + $this->log(); | |
| 34 | + } | |
| 35 | + | |
| 36 | + public function set_code( $code ) { | |
| 37 | + $this->code = $code; | |
| 38 | + | |
| 39 | + return $this; | |
| 40 | + } | |
| 41 | + | |
| 42 | + public function dynamic_success(): Handler_Exception { | |
| 43 | + $this->message = Manager::dynamic_success( $this->message ); | |
| 44 | + | |
| 45 | + return $this; | |
| 46 | + } | |
| 47 | + | |
| 48 | + public function dynamic_error(): Handler_Exception { | |
| 49 | + $this->message = Manager::dynamic_error( $this->message ); | |
| 50 | + | |
| 51 | + return $this; | |
| 52 | + } | |
| 53 | + | |
| 54 | + | |
| 55 | + public function get_form_status() { | |
| 56 | + return $this->message ?: $this->default_type_message; | |
| 57 | + } | |
| 58 | + | |
| 59 | + public function get_additional() { | |
| 60 | + return $this->additional_data; | |
| 61 | + } | |
| 62 | + | |
| 63 | + public function unset_log() { | |
| 64 | + Logger\Module::instance()->unset_last( static::class ); | |
| 65 | + } | |
| 66 | + | |
| 67 | + public function log() { | |
| 68 | + if ( ! jet_form_builder()->has_module( 'dev' ) ) { | |
| 69 | + return; | |
| 70 | + } | |
| 71 | + Logger\Module::instance()->push_log( | |
| 72 | + static::class, | |
| 73 | + array( | |
| 74 | + 'message' => $this->getMessage(), | |
| 75 | + 'file' => $this->make_pretty_filename( $this->getFile() ), | |
| 76 | + 'line' => $this->getLine(), | |
| 77 | + 'data' => $this->get_additional(), | |
| 78 | + 'version' => jet_form_builder()->get_version(), | |
| 79 | + 'action_id' => jet_fb_action_handler()->get_position(), | |
| 80 | + ) | |
| 81 | + ); | |
| 82 | + } | |
| 83 | + | |
| 84 | + private function make_pretty_filename( string $file ) { | |
| 85 | + $path = explode( 'wp-content\\', $file ); | |
| 86 | + | |
| 87 | + return $path[1] ?? ( explode( 'wp-content/', $file )[1] ?? $file ); | |
| 88 | + } | |
| 89 | + | |
| 90 | + public function is_success(): bool { | |
| 91 | + return $this->get_status()->is_success(); | |
| 92 | + } | |
| 93 | + | |
| 94 | + /** | |
| 95 | + * We need to check the status length for cases when it's too long. | |
| 96 | + * If it's long - we store it in `*_jet_fb_records_errors` table | |
| 97 | + * | |
| 98 | + * @since 3.1.8 | |
| 99 | + * | |
| 100 | + * @see https://github.com/Crocoblock/issues-tracker/issues/4994 | |
| 101 | + */ | |
| 102 | + protected function check_message_length() { | |
| 103 | + if ( 255 > strlen( $this->message ) ) { | |
| 104 | + return; | |
| 105 | + } | |
| 106 | + | |
| 107 | + $this->message = $this->get_status()->is_success() ? 'success' : 'failed'; | |
| 108 | + | |
| 109 | + array_unshift( $this->additional_data, $this->get_status()->get_raw_message() ); | |
| 110 | + } | |
| 111 | + | |
| 112 | + /** | |
| 113 | + * @return Status_Info | |
| 114 | + */ | |
| 115 | + protected function get_status(): Status_Info { | |
| 116 | + if ( $this->status ) { | |
| 117 | + return $this->status; | |
| 118 | + } | |
| 119 | + $this->status = new Status_Info( $this->message ); | |
| 120 | + | |
| 121 | + return $this->status; | |
| 122 | + } | |
| 123 | + | |
| 124 | +} | |