PluginProbe
HTML Forms – Simple WordPress Forms Plugin / 1.5.6
HTML Forms – Simple WordPress Forms Plugin v1.5.6
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.5.6, at src/actions/class-mailchimp.php

124 lines 3.8 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
45 <p class="description">
46 <?php _e( 'Add an email address to the assigned Mailchimp list when this form is sucessfully submitted.', 'html-forms' ); ?>
47 <a target="_blank" tabindex="-1" class="html-forms-help" href="https://htmlformsplugin.com/kb/add-users-to-a-mailchimp-list/"><span class="dashicons dashicons-editor-help"></span></a>
48 </p>
49
50 <table class="form-table">
51 <tr valign="top">
52 <th scope="row"><?php _e( 'List', 'html-forms' ); ?></th>
53 <td>
54 <?php if ( ! empty( $lists ) ) { ?>
55 <select name="form[settings][actions][<?php echo $index; ?>][list_id]">
56 <option value="" style="color: #AAA;" readonly><?php _e( 'Select Mailchimp list', 'html-forms' ); ?></option>
57 <?php
58 foreach ( $lists as $list ) {
59 $selected = $settings['list_id'] === $list->id ? 'selected' : '';
60 echo sprintf( '<option value="%s" %s>%s</option>', $list->id, $selected, $list->name );
61 }
62 ?>
63 </select>
64 <?php
65 } else {
66 echo '<p><a href="' . admin_url( 'admin.php?page=mailchimp-for-wp' ) . '">' . __( 'Please connect your Mailchimp account first.', 'html-forms' ) . '</a></p>';
67 }
68 ?>
69 </td>
70
71 </tr>
72 </table>
73 <?php
74 }
75
76 public function process( array $settings, Submission $submission, Form $form ) {
77 if ( empty( $settings['list_id'] ) ) {
78 return;
79 }
80
81 $mailchimp_list_id = $settings['list_id'];
82 $email_address = '';
83
84 // find email field
85 foreach ( $submission->data as $field => $value ) {
86 if ( is_string( $value ) && is_email( $value ) ) {
87 $email_address = $value;
88 }
89 }
90
91 // bail if no email address found
92 if ( empty( $email_address ) ) {
93 return;
94 }
95
96 $merge_fields = array();
97 $merge_fields = apply_filters( 'hf_mailchimp_action_merge_fields', $merge_fields, $submission, $form );
98 $mailchimp_data = array(
99 'merge_fields' => $merge_fields,
100 'status' => 'pending',
101 );
102 $mailchimp_data = apply_filters( 'hf_mailchimp_action_subscriber_data', $mailchimp_data, $submission, $form );
103
104 // subscribe the email address to the selected list
105 $mailchimp = new \MC4WP_MailChimp();
106 $result = $mailchimp->list_subscribe( $mailchimp_list_id, $email_address, $mailchimp_data );
107
108 // if result failed, show error message
109 $log = mc4wp_get_debug_log();
110 $name = sprintf( 'HTML Forms: %s', $form->title );
111 if ( ! $result ) {
112 if ( $mailchimp->get_error_code() == 214 ) {
113 $log->warning( sprintf( '%s: %s is already subscribed to the selected list(s)', $name, $email_address ) );
114 } else {
115 $log->error( sprintf( '%s > Mailchimp API Error: %s', $name, $mailchimp->get_error_message() ) );
116 }
117
118 return;
119 }
120
121 $log->info( sprintf( '%s > Successfully subscribed %s', $name, $email_address ) );
122 }
123 }
124