PluginProbe
HTML Forms – Simple WordPress Forms Plugin / 1.3.11
HTML Forms – Simple WordPress Forms Plugin v1.3.11
1.7.0 trunk 1.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.2.0 1.3.0 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.14 1.3.15 1.3.16 All 67 releases
html-forms / src / Actions / MailChimp.php

MailChimp.php in HTML Forms – Simple WordPress Forms Plugin 1.3.11, at src/Actions/MailChimp.php

112 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $result = $mailchimp->list_subscribe( $mailchimp_list_id, $email_address, array(
92 'merge_fields' => $merge_fields,
93 'status' => 'pending',
94 ) );
95
96 // if result failed, show error message
97 $log = mc4wp_get_debug_log();
98 $name = sprintf('HTML Forms: %s', $form->title);
99 if (! $result) {
100 if ($mailchimp->get_error_code() == 214) {
101 $log->warning(sprintf("%s: %s is already subscribed to the selected list(s)", $name, $email_address));
102 } else {
103 $log->error(sprintf('%s > Mailchimp API Error: %s', $name, $mailchimp->get_error_message()));
104 }
105
106 return;
107 }
108
109 $log->info(sprintf('%s > Successfully subscribed %s', $name, $email_address));
110 }
111 }
112