| 1 |
<?php |
| 2 |
|
| 3 |
namespace JFB_Modules\Actions_V2\Mailchimp; |
| 4 |
|
| 5 |
use Jet_Form_Builder\Actions\Action_Handler; |
| 6 |
use Jet_Form_Builder\Actions\Types\Base; |
| 7 |
use Jet_Form_Builder\Admin\Tabs_Handlers\Tab_Handler_Manager; |
| 8 |
use Jet_Form_Builder\Exceptions\Action_Exception; |
| 9 |
use Jet_Form_Builder\Exceptions\Gateway_Exception; |
| 10 |
use JFB_Modules\Actions_V2\Mailchimp\Api\Action_Factory; |
| 11 |
use JFB_Modules\Actions_V2\Mailchimp\Api\Create_Tags_Action; |
| 12 |
use JFB_Modules\Actions_V2\Mailchimp\Api\Put_Member_In_List_Action; |
| 13 |
|
| 14 |
class Mailchimp_Action extends Base { |
| 15 |
|
| 16 |
public function get_id() { |
| 17 |
return 'mailchimp'; |
| 18 |
} |
| 19 |
|
| 20 |
public function get_name() { |
| 21 |
return __( 'MailChimp', 'jet-form-builder' ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* @param array $request |
| 26 |
* @param Action_Handler $handler |
| 27 |
* |
| 28 |
* @return void |
| 29 |
* @throws Action_Exception |
| 30 |
*/ |
| 31 |
public function do_action( array $request, Action_Handler $handler ) { |
| 32 |
$factory = new Action_Factory(); |
| 33 |
$factory->set_api_key( $this->get_api_key() ); |
| 34 |
|
| 35 |
/** @var Put_Member_In_List_Action $put_member */ |
| 36 |
$put_member = $factory->create( Put_Member_In_List_Action::class ); |
| 37 |
$put_member->set_list_id( $this->settings['list_id'] ?? '' ); |
| 38 |
$put_member->set_opt_in( $this->settings['double_opt_in'] ?? '' ); |
| 39 |
$put_member->set_email_address( $this->get_property_value( 'email' ) ); |
| 40 |
$put_member->set_birthday( $this->get_property_value( 'BIRTHDAY' ) ); |
| 41 |
$put_member->set_interests( $this->settings['groups_ids'] ?? array() ); |
| 42 |
|
| 43 |
$fields_map = $this->settings['fields_map'] ?? array(); |
| 44 |
|
| 45 |
foreach ( $fields_map as $param => $field ) { |
| 46 |
if ( empty( $field ) || empty( $request[ $field ] ) ) { |
| 47 |
continue; |
| 48 |
} |
| 49 |
$put_member->set_merge_fields( $param, $request[ $field ] ); |
| 50 |
} |
| 51 |
|
| 52 |
try { |
| 53 |
$put_member->send_request(); |
| 54 |
} catch ( Gateway_Exception $exception ) { |
| 55 |
// phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 56 |
throw new Action_Exception( |
| 57 |
'internal_error', |
| 58 |
$exception->getMessage(), |
| 59 |
...$exception->get_additional() |
| 60 |
); |
| 61 |
// phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 62 |
} |
| 63 |
|
| 64 |
/** @var Create_Tags_Action $create_tags */ |
| 65 |
$create_tags = $factory->create( Create_Tags_Action::class ); |
| 66 |
$create_tags->set_tags( $this->settings['tags'] ?? array() ); |
| 67 |
$create_tags->set_email_address( $this->get_property_value( 'email' ) ); |
| 68 |
$create_tags->set_list_id( $this->settings['list_id'] ?? '' ); |
| 69 |
|
| 70 |
if ( ! count( $create_tags->get_tags() ) ) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
try { |
| 75 |
$create_tags->request(); |
| 76 |
$create_tags->check_response_code(); |
| 77 |
} catch ( Gateway_Exception $exception ) { |
| 78 |
// phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 79 |
throw new Action_Exception( |
| 80 |
'internal_error', |
| 81 |
$exception->getMessage(), |
| 82 |
...$exception->get_additional() |
| 83 |
); |
| 84 |
// phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
private function get_property_value( string $property ) { |
| 90 |
return jet_fb_context()->get_value( |
| 91 |
(string) ( $this->settings['fields_map'][ $property ] ?? '' ) |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
private function get_api_key(): string { |
| 96 |
if ( empty( $this->settings['use_global'] ) ) { |
| 97 |
return (string) ( $this->settings['api_key'] ?? '' ); |
| 98 |
} |
| 99 |
|
| 100 |
$options = Tab_Handler_Manager::get_options( 'mailchimp-tab' ); |
| 101 |
|
| 102 |
return (string) ( $options['api_key'] ?? '' ); |
| 103 |
} |
| 104 |
|
| 105 |
} |
| 106 |
|