← All changes
|
includes/blocks/form-builder/actions/subscribe.php
+128
-0
2.7.5
→
2.14.0
View file →
| @@ -1,0 +1,128 @@ | ||
| 1 | +<?php | |
| 2 | +namespace ABlocks\Blocks\FormBuilder\Actions; | |
| 3 | + | |
| 4 | +use ABlocks\Blocks\FormBuilder\Actions\Interfaces\FormSubmissionAction; | |
| 5 | +use ABlocks\Blocks\FormBuilder\ValidateFormData; | |
| 6 | +use ABlocks\Blocks\FormBuilder\Subscribe\Mailchimp; | |
| 7 | +use Exception; | |
| 8 | + | |
| 9 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 10 | + exit; | |
| 11 | +} | |
| 12 | +/** | |
| 13 | + * Send email to admin | |
| 14 | + */ | |
| 15 | +final class Subscribe implements FormSubmissionAction { | |
| 16 | + | |
| 17 | + /** @var $validateformdata */ | |
| 18 | + private ValidateFormData $validateformdata; | |
| 19 | + | |
| 20 | + /** @var $user_email */ | |
| 21 | + private string $user_email; | |
| 22 | + | |
| 23 | + /** @var $api_key */ | |
| 24 | + private string $api_key; | |
| 25 | + /** @var $list_id */ | |
| 26 | + private string $list_id; | |
| 27 | + /** @var $settings */ | |
| 28 | + private object $settings; | |
| 29 | + /** @var $config */ | |
| 30 | + private array $config; | |
| 31 | + | |
| 32 | + /** | |
| 33 | + * Contructor | |
| 34 | + * | |
| 35 | + * @param ValidateFormData $obj | |
| 36 | + */ | |
| 37 | + public function __construct( ValidateFormData $obj ) { | |
| 38 | + $this->validateformdata = $obj; | |
| 39 | + $this->user_email = $this->validateformdata->form_info['info']['email']; | |
| 40 | + $this->config = $this->validateformdata->form_info['info']['config']; | |
| 41 | + $this->settings = $GLOBALS['ablocks_settings']; | |
| 42 | + } | |
| 43 | + | |
| 44 | + | |
| 45 | + protected function get_data_by_field_mapping( $fields ) : array { | |
| 46 | + if ( | |
| 47 | + empty( $fields ) || | |
| 48 | + empty( $this->validateformdata->form_info['data'] ) | |
| 49 | + ) { | |
| 50 | + return []; | |
| 51 | + }; | |
| 52 | + | |
| 53 | + $data = []; | |
| 54 | + $value = $this->validateformdata->form_info['data'][ $target ]['value'] ?? ''; | |
| 55 | + foreach ( $fields as $field => $target ) { | |
| 56 | + if ( | |
| 57 | + $target === 'default' || | |
| 58 | + empty( $value ) | |
| 59 | + ) { | |
| 60 | + continue; | |
| 61 | + } | |
| 62 | + $data[ $field ] = $value; | |
| 63 | + } | |
| 64 | + return $data; | |
| 65 | + } | |
| 66 | + | |
| 67 | + /** | |
| 68 | + * Subscribe action | |
| 69 | + * | |
| 70 | + * @return void | |
| 71 | + */ | |
| 72 | + public function action() { | |
| 73 | + | |
| 74 | + $this->mailchimp(); | |
| 75 | + | |
| 76 | + } | |
| 77 | + | |
| 78 | + public function mailchimp() { | |
| 79 | + | |
| 80 | + if ( | |
| 81 | + ! in_array( __FUNCTION__, $this->validateformdata->form_info['info']['actions'], true ) | |
| 82 | + ) { | |
| 83 | + return; | |
| 84 | + } | |
| 85 | + | |
| 86 | + if ( | |
| 87 | + ! array_key_exists( 'mailchimpListId', $this->config ) | |
| 88 | + ) { | |
| 89 | + return; | |
| 90 | + } | |
| 91 | + | |
| 92 | + if ( array_key_exists( 'mailchimpApiKey', $this->config ) ) { | |
| 93 | + $api_key = $this->config['mailchimpApiKey']; | |
| 94 | + } else { | |
| 95 | + $api_key = property_exists( $this->settings, 'mailchimp_api_key' ) ? $this->settings->mailchimp_api_key : ''; | |
| 96 | + } | |
| 97 | + $list_id = $this->config['mailchimpListId']; | |
| 98 | + $mailchimp_groups = $this->config['mailchimpgroupSelects'] ?? []; | |
| 99 | + $status = $this->config['mailchimp_status'] ?? 'subscribed'; | |
| 100 | + | |
| 101 | + $email = $this->validateformdata->form_info['data'][ $this->config['mailchimpEmailSelects'] ?? '' ]['value'] ?? ''; | |
| 102 | + $email = is_email( $email ) ? $email : $this->user_email; | |
| 103 | + if ( | |
| 104 | + empty( $api_key ) || | |
| 105 | + empty( $list_id ) || | |
| 106 | + empty( $email ) | |
| 107 | + ) { | |
| 108 | + return; | |
| 109 | + } | |
| 110 | + | |
| 111 | + try { | |
| 112 | + ( new Mailchimp( | |
| 113 | + $api_key, | |
| 114 | + $list_id, | |
| 115 | + $status | |
| 116 | + ) ) | |
| 117 | + ->subscribe( | |
| 118 | + $email, | |
| 119 | + $mailchimp_groups, | |
| 120 | + $this->get_data_by_field_mapping( $this->config['mailchimpMapSelects'] ?? [] ), | |
| 121 | + explode( ',', $this->config['mailchimpTags'] ?? '' ) | |
| 122 | + ); | |
| 123 | + } catch ( Exception $e ) { | |
| 124 | + return $e->getMessage(); | |
| 125 | + } | |
| 126 | + | |
| 127 | + } | |
| 128 | +} | |