| 1 |
<?php |
| 2 |
|
| 3 |
namespace JFB_Modules\Actions_V2\Get_Response; |
| 4 |
|
| 5 |
use Jet_Form_Builder\Actions\Action_Handler; |
| 6 |
use Jet_Form_Builder\Actions\Types\Base; |
| 7 |
use Jet_Form_Builder\Exceptions\Action_Exception; |
| 8 |
use Jet_Form_Builder\Exceptions\Gateway_Exception; |
| 9 |
use JFB_Modules\Actions_V2\Get_Response\Api\Action_Factory; |
| 10 |
use JFB_Modules\Actions_V2\Get_Response\Api\Create_Contact_Action; |
| 11 |
|
| 12 |
class Get_Response_Action extends Base { |
| 13 |
|
| 14 |
public $option_name = 'get-response-tab'; |
| 15 |
|
| 16 |
public function get_name() { |
| 17 |
return __( 'Getresponse', 'jet-form-builder' ); |
| 18 |
} |
| 19 |
|
| 20 |
public function get_id() { |
| 21 |
return 'getresponse'; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Run a hook notification |
| 26 |
* |
| 27 |
* @param array $request |
| 28 |
* @param Action_Handler $handler |
| 29 |
* |
| 30 |
* @return void |
| 31 |
* @throws Action_Exception |
| 32 |
*/ |
| 33 |
public function do_action( array $request, Action_Handler $handler ) { |
| 34 |
$api = $this->global_settings( |
| 35 |
array( |
| 36 |
'api_key' => '', |
| 37 |
) |
| 38 |
); |
| 39 |
|
| 40 |
if ( empty( $api['api_key'] ) || empty( $this->settings['list_id'] ) ) { |
| 41 |
throw new Action_Exception( 'invalid_api_key' ); |
| 42 |
} |
| 43 |
|
| 44 |
$factory = new Action_Factory(); |
| 45 |
$factory->set_api_key( $api['api_key'] ); |
| 46 |
|
| 47 |
/** @var Create_Contact_Action $contact */ |
| 48 |
$contact = $factory->create( Create_Contact_Action::class ); |
| 49 |
$contact->set_list_id( $this->settings['list_id'] ); |
| 50 |
$contact->set_email( $this->get_property_value( 'email' ) ); |
| 51 |
$contact->set_name( $this->get_property_value( 'name' ) ); |
| 52 |
$contact->set_day_of_cycle( $this->settings['day_of_cycle'] ?? '' ); |
| 53 |
|
| 54 |
$fields_map = $this->settings['fields_map'] ?? array(); |
| 55 |
|
| 56 |
foreach ( $fields_map as $param => $field ) { |
| 57 |
if ( ! $field || empty( $request[ $field ] ) ) { |
| 58 |
continue; |
| 59 |
} |
| 60 |
if ( 'name' !== $param && 'email' !== $param ) { |
| 61 |
$contact->add_custom_field( $param, $request[ $field ] ); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
try { |
| 66 |
$contact->request(); |
| 67 |
$contact->check_response_code(); |
| 68 |
} catch ( Gateway_Exception $exception ) { |
| 69 |
// phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 70 |
throw new Action_Exception( |
| 71 |
'internal_error', |
| 72 |
$exception->getMessage(), |
| 73 |
...$exception->get_additional() |
| 74 |
); |
| 75 |
// phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
public function editor_labels() { |
| 80 |
return array( |
| 81 |
'api_key' => __( 'API Key:', 'jet-form-builder' ), |
| 82 |
'validate_api_key' => __( 'Validate API Key', 'jet-form-builder' ), |
| 83 |
'list_id' => __( 'List Id:', 'jet-form-builder' ), |
| 84 |
'update_list_ids' => __( 'Update List', 'jet-form-builder' ), |
| 85 |
'day_of_cycle' => __( 'Day Of Cycle:', 'jet-form-builder' ), |
| 86 |
'fields_map' => __( 'Fields Map:', 'jet-form-builder' ), |
| 87 |
'use_global' => __( 'Use Global Settings', 'jet-form-builder' ), |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
private function get_property_value( string $property ) { |
| 92 |
return jet_fb_context()->get_value( |
| 93 |
(string) ( $this->settings['fields_map'][ $property ] ?? '' ) |
| 94 |
); |
| 95 |
} |
| 96 |
|
| 97 |
} |
| 98 |
|