response.php
67 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Form_Response; |
| 5 | |
| 6 | use Jet_Form_Builder\Form_Response\Types\Response_It; |
| 7 | |
| 8 | class Response { |
| 9 | |
| 10 | public $manager; |
| 11 | private $query_args = array(); |
| 12 | public $args = array(); |
| 13 | |
| 14 | public $default_args = array( |
| 15 | 'status' => 'success', |
| 16 | 'errors' => array(), |
| 17 | ); |
| 18 | |
| 19 | public function __construct( Response_It $instance, $query_args = array() ) { |
| 20 | $this->manager = $instance; |
| 21 | $this->add_query_args( $query_args ); |
| 22 | } |
| 23 | |
| 24 | public function send() { |
| 25 | $this->manager->send( |
| 26 | apply_filters( 'jet-fb/response-handler/query-args', $this->query_args, $this ) |
| 27 | ); |
| 28 | } |
| 29 | |
| 30 | private function init_query_args() { |
| 31 | $this->add_query_args( |
| 32 | array( 'status' => $this->manager->parse_status( $this->args['status'] ) ) |
| 33 | ); |
| 34 | } |
| 35 | |
| 36 | public function init( array $args ) { |
| 37 | $this->args = wp_parse_args( $args, $this->default_args ); |
| 38 | |
| 39 | $this->init_query_args(); |
| 40 | $this->call_on_status(); |
| 41 | |
| 42 | return $this; |
| 43 | } |
| 44 | |
| 45 | private function call_on_status() { |
| 46 | $callable = array( $this, 'on_' . $this->args['status'] ); |
| 47 | |
| 48 | if ( is_callable( $callable ) ) { |
| 49 | call_user_func( $callable ); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | private function add_query_args( $args ) { |
| 54 | $this->query_args = array_merge( $this->query_args, $args ); |
| 55 | } |
| 56 | |
| 57 | public function on_validation_failed() { |
| 58 | $this->add_query_args( |
| 59 | array( |
| 60 | 'fields' => $this->manager->get_field_errors( $this->args['errors'] ), |
| 61 | ) |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | } |
| 67 |