| 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_cached_lists(); |
| 34 |
|
| 35 |
if( ! empty( $settings['list_id'] ) ) { |
| 36 |
$selected_list = $mailchimp->get_cached_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 foreach( $lists as $list ) { |
| 52 |
$selected = $settings['list_id'] === $list->id ? 'selected': ''; |
| 53 |
echo sprintf( '<option value="%s" %s>%s</option>', $list->id, $selected, $list->name ); |
| 54 |
} ?> |
| 55 |
</select> |
| 56 |
<?php } else { |
| 57 |
echo '<p><a href="'. admin_url( 'admin.php?page=mailchimp-for-wp' ) .'">' . __( 'Please connect your MailChimp account first.', 'html-forms' ) . '</a></p>'; |
| 58 |
} ?> |
| 59 |
</td> |
| 60 |
|
| 61 |
</tr> |
| 62 |
</table> |
| 63 |
<?php |
| 64 |
} |
| 65 |
|
| 66 |
public function process( array $settings, Submission $submission, Form $form ) { |
| 67 |
if( empty( $settings['list_id'] ) ) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
$mailchimp_list_id = $settings['list_id']; |
| 72 |
$email_address = ''; |
| 73 |
|
| 74 |
// find email field |
| 75 |
foreach( $submission->data as $field => $value ) { |
| 76 |
if( is_email( $value ) ) { |
| 77 |
$email_address = $value; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
// bail if no email address found |
| 82 |
if( empty( $email_address ) ) { |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
$merge_fields = array(); |
| 87 |
$merge_fields = apply_filters( 'hf_mailchimp_action_merge_fields', $merge_fields, $submission, $form ); |
| 88 |
|
| 89 |
// subscribe the email address to the selected list |
| 90 |
$mailchimp = new \MC4WP_MailChimp(); |
| 91 |
$mailchimp->list_subscribe( $mailchimp_list_id, $email_address, array( |
| 92 |
'merge_fields' => $merge_fields, |
| 93 |
'status' => 'pending', |
| 94 |
) ); |
| 95 |
} |
| 96 |
} |
| 97 |
|