PluginProbe
HTML Forms – Simple WordPress Forms Plugin / 1.3.22
HTML Forms – Simple WordPress Forms Plugin v1.3.22
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 / class-mailchimp.php

class-mailchimp.php in HTML Forms – Simple WordPress Forms Plugin 1.3.22, at src/actions/class-mailchimp.php

118 lines 3.4 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_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