| 1 |
<?php |
| 2 |
|
| 3 |
namespace HTML_Forms\Actions; |
| 4 |
|
| 5 |
use HTML_Forms\Form; |
| 6 |
use HTML_Forms\Submission; |
| 7 |
|
| 8 |
class MailChimp extends Action { |
| 9 |
public $type = 'mailchimp'; |
| 10 |
public $label = 'MailChimp'; |
| 11 |
|
| 12 |
public function __construct() { |
| 13 |
$this->label = __( 'MailChimp', 'html-forms' ); |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* @return array |
| 18 |
*/ |
| 19 |
private function get_default_settings() { |
| 20 |
$defaults = array( |
| 21 |
'list_id' => '', |
| 22 |
); |
| 23 |
return $defaults; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* @param array $settings |
| 28 |
* @param string|int $index |
| 29 |
*/ |
| 30 |
public function page_settings( $settings, $index ) { |
| 31 |
$settings = array_merge( $this->get_default_settings(), $settings ); |
| 32 |
$mailchimp = new \MC4WP_MailChimp(); |
| 33 |
$lists = $mailchimp->get_lists(); |
| 34 |
|
| 35 |
if ( ! empty( $settings['list_id'] ) ) { |
| 36 |
$selected_list = $mailchimp->get_list( $settings['list_id'] ); |
| 37 |
} |
| 38 |
?> |
| 39 |
|
| 40 |
<?php if ( ! empty( $selected_list ) ) { ?> |
| 41 |
<span class="hf-action-summary"><?php printf( __( 'Subscribe to %s', 'html-forms' ), $selected_list->name ); ?></span> |
| 42 |
<?php } ?> |
| 43 |
<input type="hidden" name="form[settings][actions][<?php echo $index; ?>][type]" value="<?php echo $this->type; ?>" /> |
| 44 |
<table class="form-table"> |
| 45 |
<tr valign="top"> |
| 46 |
<th scope="row"><?php _e( 'List', 'html-forms' ); ?></th> |
| 47 |
<td> |
| 48 |
<?php if ( ! empty( $lists ) ) { ?> |
| 49 |
<select name="form[settings][actions][<?php echo $index; ?>][list_id]"> |
| 50 |
<option value="" style="color: #AAA;" readonly><?php _e( 'Select MailChimp list', 'html-forms' ); ?></option> |
| 51 |
<?php |
| 52 |
foreach ( $lists as $list ) { |
| 53 |
$selected = $settings['list_id'] === $list->id ? 'selected' : ''; |
| 54 |
echo sprintf( '<option value="%s" %s>%s</option>', $list->id, $selected, $list->name ); |
| 55 |
} |
| 56 |
?> |
| 57 |
</select> |
| 58 |
<?php |
| 59 |
} else { |
| 60 |
echo '<p><a href="' . admin_url( 'admin.php?page=mailchimp-for-wp' ) . '">' . __( 'Please connect your MailChimp account first.', 'html-forms' ) . '</a></p>'; |
| 61 |
} |
| 62 |
?> |
| 63 |
</td> |
| 64 |
|
| 65 |
</tr> |
| 66 |
</table> |
| 67 |
<?php |
| 68 |
} |
| 69 |
|
| 70 |
public function process( array $settings, Submission $submission, Form $form ) { |
| 71 |
if ( empty( $settings['list_id'] ) ) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
$mailchimp_list_id = $settings['list_id']; |
| 76 |
$email_address = ''; |
| 77 |
|
| 78 |
// find email field |
| 79 |
foreach ( $submission->data as $field => $value ) { |
| 80 |
if ( is_email( $value ) ) { |
| 81 |
$email_address = $value; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
// bail if no email address found |
| 86 |
if ( empty( $email_address ) ) { |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
$merge_fields = array(); |
| 91 |
$merge_fields = apply_filters( 'hf_mailchimp_action_merge_fields', $merge_fields, $submission, $form ); |
| 92 |
$mailchimp_data = array( |
| 93 |
'merge_fields' => $merge_fields, |
| 94 |
'status' => 'pending', |
| 95 |
); |
| 96 |
$mailchimp_data = apply_filters( 'hf_mailchimp_action_subscriber_data', $mailchimp_data, $submission, $form ); |
| 97 |
|
| 98 |
// subscribe the email address to the selected list |
| 99 |
$mailchimp = new \MC4WP_MailChimp(); |
| 100 |
$result = $mailchimp->list_subscribe( $mailchimp_list_id, $email_address, $mailchimp_data ); |
| 101 |
|
| 102 |
// if result failed, show error message |
| 103 |
$log = mc4wp_get_debug_log(); |
| 104 |
$name = sprintf( 'HTML Forms: %s', $form->title ); |
| 105 |
if ( ! $result ) { |
| 106 |
if ( $mailchimp->get_error_code() == 214 ) { |
| 107 |
$log->warning( sprintf( '%s: %s is already subscribed to the selected list(s)', $name, $email_address ) ); |
| 108 |
} else { |
| 109 |
$log->error( sprintf( '%s > Mailchimp API Error: %s', $name, $mailchimp->get_error_message() ) ); |
| 110 |
} |
| 111 |
|
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
$log->info( sprintf( '%s > Successfully subscribed %s', $name, $email_address ) ); |
| 116 |
} |
| 117 |
} |
| 118 |
|