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