2
2
3
3
class MailChimp_API {
4
4
5
- public $ key ;
6
- public $ datacenter ;
7
-
8
- public function __construct ($ api_key ) {
9
- $ api_key = trim ($ api_key );
10
- if (!$ api_key ) {
11
- throw new Exception (__ ('Invalid API Key: ' . $ api_key ));
12
- }
13
-
14
- $ this ->key = $ api_key ;
15
- $ dc = explode ('- ' , $ api_key );
16
- $ this ->datacenter = empty ($ dc [1 ]) ? 'us1 ' : $ dc [1 ];
17
- $ this ->api_url = 'https:// ' . $ this ->datacenter . '.api.mailchimp.com/3.0/ ' ;
18
- return ;
19
- }
20
-
21
- public function get ($ endpoint , $ count = 10 , $ fields = array ())
22
- {
23
- $ query_params = '' ;
24
-
25
- $ url = $ this ->api_url . $ endpoint ;
26
-
27
- if ($ count ) {
28
- $ query_params = 'count= ' . $ count . '& ' ;
29
- }
30
-
31
- if (!empty ($ fields )) {
32
- foreach ($ fields as $ field => $ value ) {
33
- $ query_params .= $ field . '= ' . $ value . '& ' ;
34
- }
35
- }
36
-
37
- if ($ query_params ) {
38
- $ url .= "? {$ query_params }" ;
39
- }
40
-
41
- $ args = array (
42
- 'timeout ' => 5 ,
43
- 'redirection ' => 5 ,
44
- 'httpversion ' => '1.1 ' ,
45
- 'user-agent ' => 'Mailchimp WordPress Plugin/ ' . get_bloginfo ('url ' ),
46
- 'headers ' => array ("Authorization " => 'apikey ' . $ this ->key )
47
- );
48
-
49
- $ request = wp_remote_get ($ url , $ args );
50
-
51
- if (is_array ($ request ) && $ request ['response ' ]['code ' ] == 200 ) {
52
- return json_decode ($ request ['body ' ], true );
53
- } elseif (is_array ($ request ) && $ request ['response ' ]['code ' ]) {
54
- $ error = json_decode ($ request ['body ' ], true );
55
- $ error = new WP_Error ('mailchimp-get-error ' , $ error ['detail ' ]);
56
- return $ error ;
57
- } else {
58
- return false ;
59
- }
60
- }
61
-
62
- public function post ($ endpoint , $ body , $ method = 'POST ' ) {
63
- $ url = $ this ->api_url . $ endpoint ;
64
-
65
- $ args = array (
66
- 'method ' => $ method ,
67
- 'timeout ' => 5 ,
68
- 'redirection ' => 5 ,
69
- 'httpversion ' => '1.1 ' ,
70
- 'user-agent ' => 'Mailchimp WordPress Plugin/ ' . get_bloginfo ( 'url ' ),
71
- 'headers ' => array ("Authorization " => 'apikey ' . $ this ->key ),
72
- 'body ' => json_encode ($ body )
73
- );
74
- $ request = wp_remote_post ($ url , $ args );
75
-
76
- if (is_array ($ request ) && $ request ['response ' ]['code ' ] == 200 ) {
77
- return json_decode ($ request ['body ' ], true );
78
- } else {
79
- if (is_wp_error ($ request )) {
80
- return new WP_Error ('mc-subscribe-error ' , $ request ->get_error_message ());
81
- }
82
-
83
- $ body = json_decode ($ request ['body ' ], true );
84
- $ merges = get_option ('mc_merge_vars ' );
85
- foreach ($ merges as $ merge ) {
86
- if (empty ($ body ['errors ' ])) {
87
- //Email address doesn't come back from the API, so if something's wrong, it's that.
88
- $ field_name = 'Email Address ' ;
89
- $ body ['errors ' ][0 ]['message ' ] = 'Please fill out a valid email address. ' ;
90
- }
91
- elseif ($ merge ['tag ' ] == $ body ['errors ' ][0 ]['field ' ]) {
92
- $ field_name = $ merge ['name ' ];
93
- }
94
- }
95
- $ message = sprintf ($ field_name . ": " . $ body ['errors ' ][0 ]['message ' ]);
96
- return new WP_Error ('mc-subscribe-error-api ' , $ message );
97
- }
98
- }
99
- }
5
+ public $ key ;
6
+ public $ datacenter ;
7
+
8
+ public function __construct ( $ api_key ) {
9
+ $ api_key = trim ( $ api_key );
10
+ if ( ! $ api_key ) {
11
+ throw new Exception ( __ ( 'Invalid API Key: ' . $ api_key ) );
12
+ }
13
+
14
+ $ this ->key = $ api_key ;
15
+ $ dc = explode ( '- ' , $ api_key );
16
+ $ this ->datacenter = empty ( $ dc [1 ] ) ? 'us1 ' : $ dc [1 ];
17
+ $ this ->api_url = 'https:// ' . $ this ->datacenter . '.api.mailchimp.com/3.0/ ' ;
18
+ return ;
19
+ }
20
+
21
+ public function get ( $ endpoint , $ count = 10 , $ fields = array () ) {
22
+ $ query_params = '' ;
23
+
24
+ $ url = $ this ->api_url . $ endpoint ;
25
+
26
+ if ( $ count ) {
27
+ $ query_params = 'count= ' . $ count . '& ' ;
28
+ }
29
+
30
+ if ( ! empty ( $ fields ) ) {
31
+ foreach ( $ fields as $ field => $ value ) {
32
+ $ query_params .= $ field . '= ' . $ value . '& ' ;
33
+ }
34
+ }
35
+
36
+ if ( $ query_params ) {
37
+ $ url .= "? {$ query_params }" ;
38
+ }
39
+
40
+ $ args = array (
41
+ 'timeout ' => 5 ,
42
+ 'redirection ' => 5 ,
43
+ 'httpversion ' => '1.1 ' ,
44
+ 'user-agent ' => 'Mailchimp WordPress Plugin/ ' . get_bloginfo ( 'url ' ),
45
+ 'headers ' => array ( 'Authorization ' => 'apikey ' . $ this ->key ),
46
+ );
47
+
48
+ $ request = wp_remote_get ( $ url , $ args );
49
+
50
+ if ( is_array ( $ request ) && 200 == $ request ['response ' ]['code ' ] ) {
51
+ return json_decode ( $ request ['body ' ], true );
52
+ } elseif ( is_array ( $ request ) && $ request ['response ' ]['code ' ] ) {
53
+ $ error = json_decode ( $ request ['body ' ], true );
54
+ $ error = new WP_Error ( 'mailchimp-get-error ' , $ error ['detail ' ] );
55
+ return $ error ;
56
+ } else {
57
+ return false ;
58
+ }
59
+ }
60
+
61
+ public function post ( $ endpoint , $ body , $ method = 'POST ' ) {
62
+ $ url = $ this ->api_url . $ endpoint ;
63
+
64
+ $ args = array (
65
+ 'method ' => $ method ,
66
+ 'timeout ' => 5 ,
67
+ 'redirection ' => 5 ,
68
+ 'httpversion ' => '1.1 ' ,
69
+ 'user-agent ' => 'Mailchimp WordPress Plugin/ ' . get_bloginfo ( 'url ' ),
70
+ 'headers ' => array ( 'Authorization ' => 'apikey ' . $ this ->key ),
71
+ 'body ' => json_encode ( $ body ),
72
+ );
73
+ $ request = wp_remote_post ( $ url , $ args );
74
+
75
+ if ( is_array ( $ request ) && 200 == $ request ['response ' ]['code ' ] ) {
76
+ return json_decode ( $ request ['body ' ], true );
77
+ } else {
78
+ if ( is_wp_error ( $ request ) ) {
79
+ return new WP_Error ( 'mc-subscribe-error ' , $ request ->get_error_message () );
80
+ }
81
+
82
+ $ body = json_decode ( $ request ['body ' ], true );
83
+ $ merges = get_option ( 'mc_merge_vars ' );
84
+ foreach ( $ merges as $ merge ) {
85
+ if ( empty ( $ body ['errors ' ] ) ) {
86
+ // Email address doesn't come back from the API, so if something's wrong, it's that.
87
+ $ field_name = 'Email Address ' ;
88
+ $ body ['errors ' ][0 ]['message ' ] = 'Please fill out a valid email address. ' ;
89
+ } elseif ( $ merge ['tag ' ] == $ body ['errors ' ][0 ]['field ' ] ) {
90
+ $ field_name = $ merge ['name ' ];
91
+ }
92
+ }
93
+ $ message = sprintf ( $ field_name . ': ' . $ body ['errors ' ][0 ]['message ' ] );
94
+ return new WP_Error ( 'mc-subscribe-error-api ' , $ message );
95
+ }
96
+ }
97
+ }
0 commit comments