base-field-error.php
3 years ago
checkboxes-field-error.php
3 years ago
error-handler.php
3 years ago
media-field-error.php
3 years ago
number-field-error.php
3 years ago
repeater-field-error.php
3 years ago
text-field-error.php
3 years ago
textarea-field-error.php
3 years ago
wysiwyg-field-error.php
3 years ago
error-handler.php
124 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Blocks\Modules\Fields_Errors; |
| 5 | |
| 6 | use Jet_Form_Builder\Classes\Tools; |
| 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( Tools::sanitize_recursive( $_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 | public static function instance() { |
| 65 | |
| 66 | if ( is_null( self::$instance ) ) { |
| 67 | self::$instance = new self(); |
| 68 | } |
| 69 | |
| 70 | return self::$instance; |
| 71 | } |
| 72 | |
| 73 | private function __construct() { |
| 74 | $this->register_field_types(); |
| 75 | $this->maybe_set_errors(); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | public function add( $type, $args ) { |
| 80 | if ( ! isset( $this->_types[ $type ] ) ) { |
| 81 | return false; |
| 82 | } |
| 83 | $field = $this->_types[ $type ]; |
| 84 | |
| 85 | $arguments = array_merge( |
| 86 | array( |
| 87 | 'name' => 'field_name', |
| 88 | 'message' => false, |
| 89 | 'params' => array(), |
| 90 | ), |
| 91 | $args |
| 92 | ); |
| 93 | |
| 94 | $field->set_params( $arguments ); |
| 95 | |
| 96 | $message = $arguments['message'] ? $arguments['message'] : $field->error(); |
| 97 | |
| 98 | $this->_errors[ $arguments['name'] ] = array( |
| 99 | 'message' => $message, |
| 100 | ); |
| 101 | |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | public function empty_errors() { |
| 106 | return empty( $this->_errors ); |
| 107 | } |
| 108 | |
| 109 | public function has_error_by_name( $name ) { |
| 110 | return isset( $this->_errors[ $name ] ); |
| 111 | } |
| 112 | |
| 113 | public function error_by_name( $name ) { |
| 114 | if ( isset( $this->_errors[ $name ]['message'] ) ) { |
| 115 | return $this->_errors[ $name ]['message']; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | public function errors() { |
| 120 | return $this->_errors; |
| 121 | } |
| 122 | |
| 123 | } |
| 124 |