PluginProbe
JetFormBuilder — Dynamic Blocks Form Builder / 1.0.2
JetFormBuilder — Dynamic Blocks Form Builder v1.0.2
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 1.1.2 1.1.3 1.1.4 1.1.5 All 130 releases
jetformbuilder / includes / integrations / mailchimp-handler.php
mailchimp-handler.php
122 lines 2.8 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
28 if ( empty( $api_key ) ) {
29 return new \WP_Error( 'invalid_api_key' );
30 }
31
32 $api_key_data = explode( '-', $api_key );
33
34 if ( empty( $api_key_data[1] ) || 0 !== strpos( $api_key_data[1], 'us' ) ) {
35 return new \WP_Error( 'invalid_api_key' );
36 }
37
38 $this->api_key = $api_key;
39 $this->api_base_url = sprintf( 'https://%s.api.mailchimp.com/3.0/', $api_key_data[1] );
40 $this->api_request_args = array(
41 'headers' => array(
42 'Authorization' => 'Basic ' . base64_encode( 'user:' . $this->api_key ),
43 ),
44 );
45
46 }
47
48 public function get_all_data() {
49 $lists = $this->get_lists();
50 $groups = array();
51 $fields = array();
52
53 foreach ( $lists as $list_id => $list_name ) {
54 $groups[ $list_id ] = $this->get_groups( $list_id );
55 $fields[ $list_id ] = $this->get_fields( $list_id );
56 }
57
58 return array(
59 'lists' => $lists,
60 'groups' => $groups,
61 'fields' => $fields,
62 );
63 }
64
65 public function get_lists() {
66 $result = array();
67 $lists = $this->request( 'lists?count=999' );
68
69 if ( ! empty( $lists['lists'] ) ) {
70 foreach ( $lists['lists'] as $list ) {
71 $result[ $list['id'] ] = $list['name'];
72 }
73 }
74
75 return $result;
76 }
77
78 public function get_groups( $list_id ) {
79 $groups = array();
80 $categories = $this->request( 'lists/' . $list_id . '/interest-categories?count=999' );
81
82 if ( ! empty( $categories['categories'] ) ) {
83 foreach ( $categories['categories'] as $category ) {
84 $interests = $this->request( 'lists/' . $list_id . '/interest-categories/' . $category['id'] . '/interests?count=999' );
85
86 foreach ( $interests['interests'] as $interest ) {
87 $groups[] = array(
88 'value' => $interest['id'],
89 'label' => $category['title'] . ' - ' . $interest['name'],
90 );
91 }
92 }
93 }
94
95 return $groups;
96 }
97
98 public function get_fields( $list_id ) {
99 $result = array(
100 'email' => array(
101 'label' => esc_html__( 'Email', 'jet-form-builder' ),
102 'required' => true,
103 )
104 );
105
106 $merge_fields = $this->request( 'lists/' . $list_id . '/merge-fields?count=999' );
107
108 if ( ! empty( $merge_fields['merge_fields'] ) ) {
109 foreach ( $merge_fields['merge_fields'] as $field ) {
110 $result[ $field['tag'] ] = array(
111 'label' => $field['name'],
112 'required' => $field['required'],
113 );
114 }
115 }
116
117 return $result;
118 }
119 }
120
121
122