PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.14.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.14.0
2.14.0 2.13.0 2.13.1 2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 All 81 releases
← All changes | includes/blocks/form-builder/subscribe/mailchimp.php +162 -0 2.7.4 → 2.14.0 View file →
@@ -1,0 +1,162 @@
1 +<?php
2 +namespace ABlocks\Blocks\FormBuilder\Subscribe;
3 +
4 +use ABlocks\Classes\Request;
5 +use Exception;
6 +
7 +class Mailchimp {
8 + /** @var $api_key */
9 + private string $api_key;
10 + /** @var $list_id */
11 + private string $list_id;
12 + /** @var $status */
13 + private string $status;
14 +
15 + private Request $req;
16 +
17 + public function __construct( $api_key, $list_id, $status ) {
18 + $this->api_key = $api_key;
19 + $this->list_id = $list_id;
20 + $this->status = $status ? $status : 'subscribed';
21 +
22 + if ( empty( $this->api_key ) ) {
23 + throw new exception( 'api is not defined.' );
24 + }
25 +
26 + $pos = $this->api_key ? strpos( $this->api_key, '-' ) : 0;
27 + if ( intval( $pos ) === 0 ) {
28 + throw new exception( 'api is invalid.' );
29 + }
30 +
31 + $dc = substr( $this->api_key, $pos + 1 );
32 +
33 + $this->req = new Request('https://' . $dc . '.api.mailchimp.com/3.0', [
34 + 'Authorization' => 'Basic ' . base64_encode( 'user:' . $this->api_key ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
35 + 'Content-Type' => 'application/json; charset=utf-8'
36 + ]);
37 + }
38 +
39 + public function get_lists() {
40 + $res = $this->req->get( '/lists?count=100' );
41 +
42 + if ( $res['status_code'] === 401 ) {
43 + return 'invalid_api';
44 + } elseif (
45 + $res['error'] ||
46 + $res['status_code'] === 0 ||
47 + ! is_array( $res['body'] )
48 + ) {
49 + return 'error';
50 + }
51 + return $res['body'];
52 + }
53 +
54 + public function get_groups( $list_id ) {
55 + if ( empty( $list_id ) ) {
56 + return [];
57 + }
58 + $res = $this->req->get( "/lists/{$list_id}/interest-categories?count=999" );
59 +
60 + if ( $res['status_code'] === 401 ) {
61 + return 'invalid_api';
62 + } elseif (
63 + $res['error'] ||
64 + $res['status_code'] === 0 ||
65 + ! is_array( $res['body'] )
66 + ) {
67 + return 'error';
68 + }
69 + $categories = $res['body']['categories'] ?? [];
70 + if ( ! empty( $categories ) ) {
71 + $res['body']['_group_list'] = [];
72 + foreach ( $categories as ['id' => $c_id, 'title' => $c_title] ) {
73 + $detail = $this->get_group_detail( $list_id, $c_id );
74 + foreach ( $detail['interests'] as ['id' => $i_id,'name' => $i_name] ) {
75 + $res['body']['_group_list'][ $i_id ] = $i_name . '~' . $c_title;
76 + }
77 + }
78 + }
79 + $res['body']['_field_list'] = $this->get_fields( $list_id );
80 + return $res['body'];
81 + }
82 +
83 + public function get_fields( $list_id ) {
84 + $res = $this->req->get( "/lists/{$list_id}/merge-fields" );
85 +
86 + if ( $res['status_code'] === 401 ) {
87 + return [];
88 + } elseif (
89 + $res['error'] ||
90 + $res['status_code'] === 0 ||
91 + ! is_array( $res['body'] )
92 + ) {
93 + return [];
94 + }
95 + $data = $res['body']['merge_fields'] ?? [];
96 + return $data;
97 + }
98 +
99 + public function get_group_detail( $list_id, $category_id ) {
100 + $res = $this->req->get( "/lists/{$list_id}/interest-categories/{$category_id}/interests?count=999" );
101 +
102 + if ( $res['status_code'] === 401 ) {
103 + return 'invalid_api';
104 + } elseif (
105 + $res['error'] ||
106 + $res['status_code'] === 0 ||
107 + ! is_array( $res['body'] )
108 + ) {
109 + return 'error';
110 + }
111 + return $res['body'];
112 + }
113 +
114 + public function subscribe( string $user_email, array $groups, array $fields, array $tags ) {
115 +
116 + if ( empty( $this->list_id ) ) {
117 + throw new exception( 'list_id is not defined.' );
118 + }
119 +
120 + $email_hash = md5( strtolower( $user_email ) );
121 +
122 + $post_data = [
123 + 'email_address' => $user_email,
124 + 'status_if_new' => $this->status,
125 + ];
126 +
127 + if ( ! empty( $groups ) ) {
128 + $post_data['interests'] = array_combine( $groups, array_fill( 0, count( $groups ), true ) );
129 + }
130 +
131 + if ( ! empty( $fields ) ) {
132 + $post_data['merge_fields'] = $fields;
133 + }
134 +
135 + $res = $this->req->put(
136 + '/lists/' . $this->list_id . '/members/' . $email_hash,
137 + wp_json_encode( $post_data )
138 + );
139 +
140 + if (
141 + ( $res['error'] ?? true ) === false &&
142 + ! empty( $tags )
143 + ) {
144 + $tags_s = [];
145 + foreach ( $tags as $tag ) {
146 + $tags_s[] = [
147 + 'name' => $tag,
148 + 'status' => 'active',
149 + ];
150 + }
151 + $res_tags = $this->req->post(
152 + '/lists/' . $this->list_id . '/members/' . $email_hash . '/tags',
153 + wp_json_encode([
154 + 'tags' => $tags_s,
155 + ])
156 + );
157 +
158 + }
159 +
160 + return boolval( $res['body']['email_address'] ?? false );
161 + }
162 +}