PluginProbe
JetFormBuilder — Dynamic Blocks Form Builder / 1.1.0
JetFormBuilder — Dynamic Blocks Form Builder v1.1.0
3.6.6.1 3.6.6 3.6.5.4 3.6.5.3 3.6.5.2 3.6.5.1 3.6.5 3.6.4.2 3.6.4.1 3.6.4 3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 All 134 releases
jetformbuilder / includes / integrations / mailchimp-handler.php

mailchimp-handler.php in JetFormBuilder — Dynamic Blocks Form Builder 1.1.0, at includes/integrations/mailchimp-handler.php

118 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Jet_Form_Builder\Integrations;
4
5 /**
6 * MailChimp Handler
7 */
8
9 // If this file is called directly, abort.
10 if ( ! defined( 'WPINC' ) ) {
11 die;
12 }
13
14 /**
15 * Define MailChimp_Handler class
16 */
17 class MailChimp_Handler extends Integration_Base {
18
19 public $success_statuses = array( 'subscribed', 'pending' );
20
21 /**
22 * Constructor for the class
23 *
24 * @param $api_key
25 */
26 public function __construct( $api_key ) {
27 parent::__construct( $api_key );
28
29 $api_key_data = explode( '-', $api_key );
30
31 if ( empty( $api_key_data[1] ) || 0 !== strpos( $api_key_data[1], 'us' ) ) {
32 return new \WP_Error( 'invalid_api_key' );
33 }
34
35 $this->api_base_url = sprintf( 'https://%s.api.mailchimp.com/3.0/', $api_key_data[1] );
36 $this->api_request_args = array(
37 'headers' => array(
38 'Authorization' => 'Basic ' . base64_encode( 'user:' . $this->api_key ),
39 ),
40 );
41
42 }
43
44 public function get_all_data() {
45 $lists = $this->get_lists();
46 $groups = array();
47 $fields = array();
48
49 foreach ( $lists as $list_id => $list_name ) {
50 $groups[ $list_id ] = $this->get_groups( $list_id );
51 $fields[ $list_id ] = $this->get_fields( $list_id );
52 }
53
54 return array(
55 'lists' => $lists,
56 'groups' => $groups,
57 'fields' => $fields,
58 );
59 }
60
61 public function get_lists() {
62 $result = array();
63 $lists = $this->request( 'lists?count=999' );
64
65 if ( ! empty( $lists['lists'] ) ) {
66 foreach ( $lists['lists'] as $list ) {
67 $result[ $list['id'] ] = $list['name'];
68 }
69 }
70
71 return $result;
72 }
73
74 public function get_groups( $list_id ) {
75 $groups = array();
76 $categories = $this->request( 'lists/' . $list_id . '/interest-categories?count=999' );
77
78 if ( ! empty( $categories['categories'] ) ) {
79 foreach ( $categories['categories'] as $category ) {
80 $interests = $this->request( 'lists/' . $list_id . '/interest-categories/' . $category['id'] . '/interests?count=999' );
81
82 foreach ( $interests['interests'] as $interest ) {
83 $groups[] = array(
84 'value' => $interest['id'],
85 'label' => $category['title'] . ' - ' . $interest['name'],
86 );
87 }
88 }
89 }
90
91 return $groups;
92 }
93
94 public function get_fields( $list_id ) {
95 $result = array(
96 'email' => array(
97 'label' => esc_html__( 'Email', 'jet-form-builder' ),
98 'required' => true,
99 )
100 );
101
102 $merge_fields = $this->request( 'lists/' . $list_id . '/merge-fields?count=999' );
103
104 if ( ! empty( $merge_fields['merge_fields'] ) ) {
105 foreach ( $merge_fields['merge_fields'] as $field ) {
106 $result[ $field['tag'] ] = array(
107 'label' => $field['name'],
108 'required' => $field['required'],
109 );
110 }
111 }
112
113 return $result;
114 }
115 }
116
117
118