PluginProbe
JetFormBuilder — Dynamic Blocks Form Builder / 3.6.6
JetFormBuilder — Dynamic Blocks Form Builder v3.6.6
3.6.6.1 3.6.6 3.6.5.4 3.6.5.3 3.6.5.2 3.6.5.1 3.6.5 3.6.4.2 3.6.4.1 3.6.4 3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 All 134 releases
jetformbuilder / modules / actions-v2 / get-response / get-response-action.php

get-response-action.php in JetFormBuilder — Dynamic Blocks Form Builder 3.6.6, at modules/actions-v2/get-response/get-response-action.php

98 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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