| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace Jet_Form_Builder\Blocks\Modules\Fields_Errors; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
class Error_Handler { |
| 9 |
|
| 10 |
private static $instance = null; |
| 11 |
|
| 12 |
private $_types = array(); |
| 13 |
private $_errors = array(); |
| 14 |
|
| 15 |
private function register_field_types() { |
| 16 |
|
| 17 |
$types = array( |
| 18 |
new Text_Field_Error(), |
| 19 |
new Textarea_Field_Error(), |
| 20 |
new Number_Field_Error(), |
| 21 |
new Checkboxes_Field_Error(), |
| 22 |
new Media_Field_Error(), |
| 23 |
new Wysiwyg_Field_Error(), |
| 24 |
new Repeater_Field_Error() |
| 25 |
); |
| 26 |
|
| 27 |
foreach ( $types as $type ) { |
| 28 |
$this->register_field_type( $type ); |
| 29 |
} |
| 30 |
|
| 31 |
do_action( 'jet-form-builder/fields-errors/register', $this ); |
| 32 |
|
| 33 |
} |
| 34 |
|
| 35 |
private function maybe_set_errors() { |
| 36 |
if ( empty( $_REQUEST ) || ! isset( $_REQUEST['fields'] ) ) { |
| 37 |
return; |
| 38 |
} |
| 39 |
$request = stripslashes( $_REQUEST['fields'] ); |
| 40 |
|
| 41 |
if ( is_array( json_decode( $request, true ) ) && json_last_error() == JSON_ERROR_NONE ) { |
| 42 |
|
| 43 |
$this->_errors = json_decode( $request, true ); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
public function register_field_type( $field ) { |
| 48 |
if ( $field instanceof Base_Field_Error ) { |
| 49 |
$this->_types[ $field->get_name() ] = $field; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
/** |
| 55 |
* Instance. |
| 56 |
* |
| 57 |
* Ensures only one instance of the plugin class is loaded or can be loaded. |
| 58 |
* |
| 59 |
* @return Error_Handler An instance of the class. |
| 60 |
* @since 1.0.0 |
| 61 |
* @access public |
| 62 |
* @static |
| 63 |
* |
| 64 |
*/ |
| 65 |
public static function instance() { |
| 66 |
|
| 67 |
if ( is_null( self::$instance ) ) { |
| 68 |
self::$instance = new self(); |
| 69 |
} |
| 70 |
|
| 71 |
return self::$instance; |
| 72 |
} |
| 73 |
|
| 74 |
private function __construct() { |
| 75 |
$this->register_field_types(); |
| 76 |
$this->maybe_set_errors(); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
public function add( $type, $args ) { |
| 81 |
if ( ! isset( $this->_types[ $type ] ) ) { |
| 82 |
return false; |
| 83 |
} |
| 84 |
$field = $this->_types[ $type ]; |
| 85 |
|
| 86 |
$arguments = array_merge( array( |
| 87 |
'name' => 'field_name', |
| 88 |
'message' => false, |
| 89 |
'params' => array() |
| 90 |
), $args ); |
| 91 |
|
| 92 |
$field->set_params( $arguments ); |
| 93 |
|
| 94 |
$message = $arguments['message'] ? $arguments['message'] : $field->error(); |
| 95 |
|
| 96 |
$this->_errors[ $arguments['name'] ] = array( |
| 97 |
'message' => $message |
| 98 |
); |
| 99 |
|
| 100 |
return true; |
| 101 |
} |
| 102 |
|
| 103 |
public function empty_errors() { |
| 104 |
return empty( $this->_errors ); |
| 105 |
} |
| 106 |
|
| 107 |
public function has_error_by_name( $name ) { |
| 108 |
return isset( $this->_errors[ $name ] ); |
| 109 |
} |
| 110 |
|
| 111 |
public function error_by_name( $name ) { |
| 112 |
if ( isset( $this->_errors[ $name ]['message'] ) ) { |
| 113 |
return $this->_errors[ $name ]['message']; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
public function errors() { |
| 118 |
return $this->_errors; |
| 119 |
} |
| 120 |
|
| 121 |
} |