| 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 |
} |
| 72 |
|