| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace Jet_Form_Builder\Form_Messages; |
| 5 |
|
| 6 |
// If this file is called directly, abort. |
| 7 |
if ( ! defined( 'WPINC' ) ) { |
| 8 |
die; |
| 9 |
} |
| 10 |
|
| 11 |
class Status_Info { |
| 12 |
|
| 13 |
private $raw = ''; |
| 14 |
private $is_dynamic; |
| 15 |
private $is_success; |
| 16 |
private $parsed; |
| 17 |
|
| 18 |
public function __construct( string $status ) { |
| 19 |
$this->raw = $status; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* @return string |
| 24 |
*/ |
| 25 |
public function get_raw_message(): string { |
| 26 |
return $this->raw; |
| 27 |
} |
| 28 |
|
| 29 |
public function is_dynamic(): bool { |
| 30 |
if ( is_null( $this->is_dynamic ) ) { |
| 31 |
$this->set_dynamic_and_success(); |
| 32 |
} |
| 33 |
|
| 34 |
return $this->is_dynamic; |
| 35 |
} |
| 36 |
|
| 37 |
public function is_success(): bool { |
| 38 |
if ( is_null( $this->is_success ) ) { |
| 39 |
$this->set_dynamic_and_success(); |
| 40 |
} |
| 41 |
|
| 42 |
return $this->is_success; |
| 43 |
} |
| 44 |
|
| 45 |
public function get_parsed(): array { |
| 46 |
if ( ! is_array( $this->parsed ) ) { |
| 47 |
$this->parsed = Manager::parse_message( $this->raw ); |
| 48 |
} |
| 49 |
|
| 50 |
return $this->parsed; |
| 51 |
} |
| 52 |
|
| 53 |
public function get_message(): string { |
| 54 |
$parsed = $this->get_parsed(); |
| 55 |
|
| 56 |
if ( $this->is_dynamic() ) { |
| 57 |
return $parsed[1] ?? $parsed[0]; |
| 58 |
} |
| 59 |
|
| 60 |
return $parsed[0]; |
| 61 |
} |
| 62 |
|
| 63 |
private function set_dynamic_and_success(): Status_Info { |
| 64 |
$dynamic = Manager::dynamic_types(); |
| 65 |
$message = $this->get_parsed(); |
| 66 |
$this->is_dynamic = isset( $dynamic[ $message[0] ] ) && ! empty( $message[1] ); |
| 67 |
|
| 68 |
if ( $this->is_dynamic ) { |
| 69 |
$this->is_success = ( 'success' === ( $dynamic[ $message[0] ]['type'] ?? '' ) ); |
| 70 |
} else { |
| 71 |
$this->is_success = in_array( $message[0], array( 'success' ), true ); |
| 72 |
} |
| 73 |
|
| 74 |
do_action( 'jet-form-builder/response-status/init', $this ); |
| 75 |
|
| 76 |
return $this; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
public function get_css_class(): string { |
| 81 |
return $this->is_success() ? 'success' : 'error'; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* @since 3.2.2 |
| 86 |
* @param bool $is_success |
| 87 |
*/ |
| 88 |
public function set_is_success( bool $is_success ) { |
| 89 |
$this->is_success = $is_success; |
| 90 |
} |
| 91 |
|
| 92 |
} |
| 93 |
|