1 | <?php |
---|
2 | |
---|
3 | if ( ! class_exists( 'WPCF7_Service_OAuth2' ) ) { |
---|
4 | return; |
---|
5 | } |
---|
6 | |
---|
7 | class WPCF7_ConstantContact extends WPCF7_Service_OAuth2 { |
---|
8 | |
---|
9 | const service_name = 'constant_contact'; |
---|
10 | |
---|
11 | const authorization_endpoint |
---|
12 | = 'https://authz.constantcontact.com/oauth2/default/v1/authorize'; |
---|
13 | |
---|
14 | const token_endpoint |
---|
15 | = 'https://authz.constantcontact.com/oauth2/default/v1/token'; |
---|
16 | |
---|
17 | private static $instance; |
---|
18 | protected $contact_lists = array(); |
---|
19 | |
---|
20 | public static function get_instance() { |
---|
21 | if ( empty( self::$instance ) ) { |
---|
22 | self::$instance = new self; |
---|
23 | } |
---|
24 | |
---|
25 | return self::$instance; |
---|
26 | } |
---|
27 | |
---|
28 | private function __construct() { |
---|
29 | $this->authorization_endpoint = self::authorization_endpoint; |
---|
30 | $this->token_endpoint = self::token_endpoint; |
---|
31 | |
---|
32 | $option = (array) WPCF7::get_option( self::service_name ); |
---|
33 | |
---|
34 | if ( isset( $option['client_id'] ) ) { |
---|
35 | $this->client_id = $option['client_id']; |
---|
36 | } |
---|
37 | |
---|
38 | if ( isset( $option['client_secret'] ) ) { |
---|
39 | $this->client_secret = $option['client_secret']; |
---|
40 | } |
---|
41 | |
---|
42 | if ( isset( $option['access_token'] ) ) { |
---|
43 | $this->access_token = $option['access_token']; |
---|
44 | } |
---|
45 | |
---|
46 | if ( isset( $option['refresh_token'] ) ) { |
---|
47 | $this->refresh_token = $option['refresh_token']; |
---|
48 | } |
---|
49 | |
---|
50 | if ( $this->is_active() ) { |
---|
51 | if ( isset( $option['contact_lists'] ) ) { |
---|
52 | $this->contact_lists = $option['contact_lists']; |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | add_action( 'wpcf7_admin_init', array( $this, 'auth_redirect' ) ); |
---|
57 | } |
---|
58 | |
---|
59 | public function auth_redirect() { |
---|
60 | $auth = isset( $_GET['auth'] ) ? trim( $_GET['auth'] ) : ''; |
---|
61 | |
---|
62 | if ( self::service_name === $auth |
---|
63 | and current_user_can( 'wpcf7_manage_integration' ) ) { |
---|
64 | $redirect_to = add_query_arg( |
---|
65 | array( |
---|
66 | 'service' => self::service_name, |
---|
67 | 'action' => 'auth_redirect', |
---|
68 | 'code' => isset( $_GET['code'] ) ? trim( $_GET['code'] ) : '', |
---|
69 | 'state' => isset( $_GET['state'] ) ? trim( $_GET['state'] ) : '', |
---|
70 | ), |
---|
71 | menu_page_url( 'wpcf7-integration', false ) |
---|
72 | ); |
---|
73 | |
---|
74 | wp_safe_redirect( $redirect_to ); |
---|
75 | exit(); |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | protected function save_data() { |
---|
80 | $option = array_merge( |
---|
81 | (array) WPCF7::get_option( self::service_name ), |
---|
82 | array( |
---|
83 | 'client_id' => $this->client_id, |
---|
84 | 'client_secret' => $this->client_secret, |
---|
85 | 'access_token' => $this->access_token, |
---|
86 | 'refresh_token' => $this->refresh_token, |
---|
87 | 'contact_lists' => $this->contact_lists, |
---|
88 | ) |
---|
89 | ); |
---|
90 | |
---|
91 | WPCF7::update_option( self::service_name, $option ); |
---|
92 | } |
---|
93 | |
---|
94 | protected function reset_data() { |
---|
95 | $this->client_id = ''; |
---|
96 | $this->client_secret = ''; |
---|
97 | $this->access_token = ''; |
---|
98 | $this->refresh_token = ''; |
---|
99 | $this->contact_lists = array(); |
---|
100 | |
---|
101 | $this->save_data(); |
---|
102 | } |
---|
103 | |
---|
104 | public function get_title() { |
---|
105 | return __( 'Constant Contact', 'contact-form-7' ); |
---|
106 | } |
---|
107 | |
---|
108 | public function get_categories() { |
---|
109 | return array( 'email_marketing' ); |
---|
110 | } |
---|
111 | |
---|
112 | public function icon() { |
---|
113 | } |
---|
114 | |
---|
115 | public function link() { |
---|
116 | echo 'constantcontact.com'; |
---|
117 | } |
---|
118 | |
---|
119 | protected function get_redirect_uri() { |
---|
120 | return admin_url( '/?auth=' . self::service_name ); |
---|
121 | } |
---|
122 | |
---|
123 | protected function menu_page_url( $args = '' ) { |
---|
124 | $args = wp_parse_args( $args, array() ); |
---|
125 | |
---|
126 | $url = menu_page_url( 'wpcf7-integration', false ); |
---|
127 | $url = add_query_arg( array( 'service' => self::service_name ), $url ); |
---|
128 | |
---|
129 | if ( ! empty( $args ) ) { |
---|
130 | $url = add_query_arg( $args, $url ); |
---|
131 | } |
---|
132 | |
---|
133 | return $url; |
---|
134 | } |
---|
135 | |
---|
136 | public function load( $action = '' ) { |
---|
137 | if ( 'auth_redirect' == $action ) { |
---|
138 | $code = isset( $_GET['code'] ) ? urldecode( $_GET['code'] ) : ''; |
---|
139 | $state = isset( $_GET['state'] ) ? urldecode( $_GET['state'] ) : ''; |
---|
140 | |
---|
141 | if ( $code and $state |
---|
142 | and wpcf7_verify_nonce( $state, 'wpcf7_constant_contact_authorize' ) ) { |
---|
143 | $response = $this->request_token( $code ); |
---|
144 | } |
---|
145 | |
---|
146 | if ( ! empty( $this->access_token ) ) { |
---|
147 | $message = 'success'; |
---|
148 | } else { |
---|
149 | $message = 'failed'; |
---|
150 | } |
---|
151 | |
---|
152 | wp_safe_redirect( $this->menu_page_url( |
---|
153 | array( |
---|
154 | 'action' => 'setup', |
---|
155 | 'message' => $message, |
---|
156 | ) |
---|
157 | ) ); |
---|
158 | |
---|
159 | exit(); |
---|
160 | } |
---|
161 | |
---|
162 | if ( 'setup' == $action and 'POST' == $_SERVER['REQUEST_METHOD'] ) { |
---|
163 | check_admin_referer( 'wpcf7-constant-contact-setup' ); |
---|
164 | |
---|
165 | if ( ! empty( $_POST['reset'] ) ) { |
---|
166 | $this->reset_data(); |
---|
167 | } else { |
---|
168 | $this->client_id = isset( $_POST['client_id'] ) |
---|
169 | ? trim( $_POST['client_id'] ) : ''; |
---|
170 | |
---|
171 | $this->client_secret = isset( $_POST['client_secret'] ) |
---|
172 | ? trim( $_POST['client_secret'] ) : ''; |
---|
173 | |
---|
174 | $this->save_data(); |
---|
175 | $this->authorize( 'contact_data offline_access' ); |
---|
176 | } |
---|
177 | |
---|
178 | wp_safe_redirect( $this->menu_page_url( 'action=setup' ) ); |
---|
179 | exit(); |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | protected function authorize( $scope = '' ) { |
---|
184 | $endpoint = add_query_arg( |
---|
185 | array_map( 'urlencode', array( |
---|
186 | 'response_type' => 'code', |
---|
187 | 'client_id' => $this->client_id, |
---|
188 | 'redirect_uri' => $this->get_redirect_uri(), |
---|
189 | 'scope' => $scope, |
---|
190 | 'state' => wpcf7_create_nonce( 'wpcf7_constant_contact_authorize' ), |
---|
191 | ) ), |
---|
192 | $this->authorization_endpoint |
---|
193 | ); |
---|
194 | |
---|
195 | if ( wp_redirect( sanitize_url( $endpoint ) ) ) { |
---|
196 | exit(); |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | public function email_exists( $email ) { |
---|
201 | $endpoint = add_query_arg( |
---|
202 | array( |
---|
203 | 'email' => $email, |
---|
204 | 'status' => 'all', |
---|
205 | ), |
---|
206 | 'https://api.cc.email/v3/contacts' |
---|
207 | ); |
---|
208 | |
---|
209 | $request = array( |
---|
210 | 'method' => 'GET', |
---|
211 | 'headers' => array( |
---|
212 | 'Accept' => 'application/json', |
---|
213 | 'Content-Type' => 'application/json; charset=utf-8', |
---|
214 | ), |
---|
215 | ); |
---|
216 | |
---|
217 | $response = $this->remote_request( $endpoint, $request ); |
---|
218 | |
---|
219 | if ( 400 <= (int) wp_remote_retrieve_response_code( $response ) ) { |
---|
220 | if ( WP_DEBUG ) { |
---|
221 | $this->log( $endpoint, $request, $response ); |
---|
222 | } |
---|
223 | |
---|
224 | return false; |
---|
225 | } |
---|
226 | |
---|
227 | $response_body = wp_remote_retrieve_body( $response ); |
---|
228 | |
---|
229 | if ( empty( $response_body ) ) { |
---|
230 | return false; |
---|
231 | } |
---|
232 | |
---|
233 | $response_body = json_decode( $response_body, true ); |
---|
234 | |
---|
235 | return ! empty( $response_body['contacts'] ); |
---|
236 | } |
---|
237 | |
---|
238 | public function create_contact( $properties ) { |
---|
239 | $endpoint = 'https://api.cc.email/v3/contacts'; |
---|
240 | |
---|
241 | $request = array( |
---|
242 | 'method' => 'POST', |
---|
243 | 'headers' => array( |
---|
244 | 'Accept' => 'application/json', |
---|
245 | 'Content-Type' => 'application/json; charset=utf-8', |
---|
246 | ), |
---|
247 | 'body' => wp_json_encode( $properties ), |
---|
248 | ); |
---|
249 | |
---|
250 | $response = $this->remote_request( $endpoint, $request ); |
---|
251 | |
---|
252 | if ( 400 <= (int) wp_remote_retrieve_response_code( $response ) ) { |
---|
253 | if ( WP_DEBUG ) { |
---|
254 | $this->log( $endpoint, $request, $response ); |
---|
255 | } |
---|
256 | |
---|
257 | return false; |
---|
258 | } |
---|
259 | } |
---|
260 | |
---|
261 | public function get_contact_lists() { |
---|
262 | $endpoint = 'https://api.cc.email/v3/contact_lists'; |
---|
263 | |
---|
264 | $request = array( |
---|
265 | 'method' => 'GET', |
---|
266 | 'headers' => array( |
---|
267 | 'Accept' => 'application/json', |
---|
268 | 'Content-Type' => 'application/json; charset=utf-8', |
---|
269 | ), |
---|
270 | ); |
---|
271 | |
---|
272 | $response = $this->remote_request( $endpoint, $request ); |
---|
273 | |
---|
274 | if ( 400 <= (int) wp_remote_retrieve_response_code( $response ) ) { |
---|
275 | if ( WP_DEBUG ) { |
---|
276 | $this->log( $endpoint, $request, $response ); |
---|
277 | } |
---|
278 | |
---|
279 | return false; |
---|
280 | } |
---|
281 | |
---|
282 | $response_body = wp_remote_retrieve_body( $response ); |
---|
283 | |
---|
284 | if ( empty( $response_body ) ) { |
---|
285 | return false; |
---|
286 | } |
---|
287 | |
---|
288 | $response_body = json_decode( $response_body, true ); |
---|
289 | |
---|
290 | if ( ! empty( $response_body['lists'] ) ) { |
---|
291 | return (array) $response_body['lists']; |
---|
292 | } else { |
---|
293 | return array(); |
---|
294 | } |
---|
295 | } |
---|
296 | |
---|
297 | public function update_contact_lists( $selection = array() ) { |
---|
298 | $contact_lists = array(); |
---|
299 | $contact_lists_on_api = $this->get_contact_lists(); |
---|
300 | |
---|
301 | if ( false !== $contact_lists_on_api ) { |
---|
302 | foreach ( (array) $contact_lists_on_api as $list ) { |
---|
303 | if ( isset( $list['list_id'] ) ) { |
---|
304 | $list_id = trim( $list['list_id'] ); |
---|
305 | } else { |
---|
306 | continue; |
---|
307 | } |
---|
308 | |
---|
309 | if ( isset( $this->contact_lists[$list_id]['selected'] ) ) { |
---|
310 | $list['selected'] = $this->contact_lists[$list_id]['selected']; |
---|
311 | } else { |
---|
312 | $list['selected'] = array(); |
---|
313 | } |
---|
314 | |
---|
315 | $contact_lists[$list_id] = $list; |
---|
316 | } |
---|
317 | } else { |
---|
318 | $contact_lists = $this->contact_lists; |
---|
319 | } |
---|
320 | |
---|
321 | foreach ( (array) $selection as $key => $ids_or_names ) { |
---|
322 | foreach( $contact_lists as $list_id => $list ) { |
---|
323 | if ( in_array( $list['list_id'], (array) $ids_or_names, true ) |
---|
324 | or in_array( $list['name'], (array) $ids_or_names, true ) ) { |
---|
325 | $contact_lists[$list_id]['selected'][$key] = true; |
---|
326 | } else { |
---|
327 | unset( $contact_lists[$list_id]['selected'][$key] ); |
---|
328 | } |
---|
329 | } |
---|
330 | } |
---|
331 | |
---|
332 | $this->contact_lists = $contact_lists; |
---|
333 | |
---|
334 | if ( $selection ) { |
---|
335 | $this->save_data(); |
---|
336 | } |
---|
337 | |
---|
338 | return $this->contact_lists; |
---|
339 | } |
---|
340 | |
---|
341 | public function admin_notice( $message = '' ) { |
---|
342 | switch ( $message ) { |
---|
343 | case 'success': |
---|
344 | echo sprintf( |
---|
345 | '<div class="notice notice-success"><p>%s</p></div>', |
---|
346 | esc_html( __( "Connection established.", 'contact-form-7' ) ) |
---|
347 | ); |
---|
348 | break; |
---|
349 | case 'failed': |
---|
350 | echo sprintf( |
---|
351 | '<div class="notice notice-error"><p><strong>%1$s</strong>: %2$s</p></div>', |
---|
352 | esc_html( __( "Error", 'contact-form-7' ) ), |
---|
353 | esc_html( __( "Failed to establish connection. Please double-check your configuration.", 'contact-form-7' ) ) |
---|
354 | ); |
---|
355 | break; |
---|
356 | case 'updated': |
---|
357 | echo sprintf( |
---|
358 | '<div class="notice notice-success"><p>%s</p></div>', |
---|
359 | esc_html( __( "Configuration updated.", 'contact-form-7' ) ) |
---|
360 | ); |
---|
361 | break; |
---|
362 | } |
---|
363 | } |
---|
364 | |
---|
365 | public function display( $action = '' ) { |
---|
366 | echo sprintf( |
---|
367 | '<p><strong>%1$s</strong> %2$s</p>', |
---|
368 | esc_html( __( 'Warning:', 'contact-form-7' ) ), |
---|
369 | wpcf7_link( |
---|
370 | __( 'https://contactform7.com/2024/02/02/we-end-the-constant-contact-integration/', 'contact-form-7' ), |
---|
371 | __( "This feature is deprecated. You are not recommended to use it.", 'contact-form-7' ) |
---|
372 | ) |
---|
373 | ); |
---|
374 | |
---|
375 | echo sprintf( |
---|
376 | '<p>%s</p>', |
---|
377 | esc_html( __( "The Constant Contact integration module allows you to send contact data collected through your contact forms to the Constant Contact API. You can create reliable email subscription services in a few easy steps.", 'contact-form-7' ) ) |
---|
378 | ); |
---|
379 | |
---|
380 | echo sprintf( |
---|
381 | '<p><strong>%s</strong></p>', |
---|
382 | wpcf7_link( |
---|
383 | __( 'https://contactform7.com/constant-contact-integration/', 'contact-form-7' ), |
---|
384 | __( 'Constant Contact integration', 'contact-form-7' ) |
---|
385 | ) |
---|
386 | ); |
---|
387 | |
---|
388 | if ( $this->is_active() ) { |
---|
389 | echo sprintf( |
---|
390 | '<p class="dashicons-before dashicons-yes">%s</p>', |
---|
391 | esc_html( __( "This site is connected to the Constant Contact API.", 'contact-form-7' ) ) |
---|
392 | ); |
---|
393 | } |
---|
394 | |
---|
395 | if ( 'setup' == $action ) { |
---|
396 | $this->display_setup(); |
---|
397 | } else { |
---|
398 | echo sprintf( |
---|
399 | '<p><a href="%1$s" class="button">%2$s</a></p>', |
---|
400 | esc_url( $this->menu_page_url( 'action=setup' ) ), |
---|
401 | esc_html( __( 'Setup Integration', 'contact-form-7' ) ) |
---|
402 | ); |
---|
403 | } |
---|
404 | } |
---|
405 | |
---|
406 | private function display_setup() { |
---|
407 | ?> |
---|
408 | <form method="post" action="<?php echo esc_url( $this->menu_page_url( 'action=setup' ) ); ?>"> |
---|
409 | <?php wp_nonce_field( 'wpcf7-constant-contact-setup' ); ?> |
---|
410 | <table class="form-table"> |
---|
411 | <tbody> |
---|
412 | <tr> |
---|
413 | <th scope="row"><label for="client_id"><?php echo esc_html( __( 'API Key', 'contact-form-7' ) ); ?></label></th> |
---|
414 | <td><?php |
---|
415 | if ( $this->is_active() ) { |
---|
416 | echo esc_html( $this->client_id ); |
---|
417 | echo sprintf( |
---|
418 | '<input type="hidden" value="%1$s" id="client_id" name="client_id" />', |
---|
419 | esc_attr( $this->client_id ) |
---|
420 | ); |
---|
421 | } else { |
---|
422 | echo sprintf( |
---|
423 | '<input type="text" aria-required="true" value="%1$s" id="client_id" name="client_id" class="regular-text code" />', |
---|
424 | esc_attr( $this->client_id ) |
---|
425 | ); |
---|
426 | } |
---|
427 | ?></td> |
---|
428 | </tr> |
---|
429 | <tr> |
---|
430 | <th scope="row"><label for="client_secret"><?php echo esc_html( __( 'App Secret', 'contact-form-7' ) ); ?></label></th> |
---|
431 | <td><?php |
---|
432 | if ( $this->is_active() ) { |
---|
433 | echo esc_html( wpcf7_mask_password( $this->client_secret, 4, 4 ) ); |
---|
434 | echo sprintf( |
---|
435 | '<input type="hidden" value="%1$s" id="client_secret" name="client_secret" />', |
---|
436 | esc_attr( $this->client_secret ) |
---|
437 | ); |
---|
438 | } else { |
---|
439 | echo sprintf( |
---|
440 | '<input type="text" aria-required="true" value="%1$s" id="client_secret" name="client_secret" class="regular-text code" />', |
---|
441 | esc_attr( $this->client_secret ) |
---|
442 | ); |
---|
443 | } |
---|
444 | ?></td> |
---|
445 | </tr> |
---|
446 | <tr> |
---|
447 | <th scope="row"><label for="redirect_uri"><?php echo esc_html( __( 'Redirect URI', 'contact-form-7' ) ); ?></label></th> |
---|
448 | <td><?php |
---|
449 | echo sprintf( |
---|
450 | '<input type="text" value="%1$s" id="redirect_uri" name="redirect_uri" class="large-text code" readonly="readonly" onfocus="this.select();" style="font-size: 11px;" />', |
---|
451 | $this->get_redirect_uri() |
---|
452 | ); |
---|
453 | ?> |
---|
454 | <p class="description"><?php echo esc_html( __( "Set this URL as the redirect URI.", 'contact-form-7' ) ); ?></p> |
---|
455 | </td> |
---|
456 | </tr> |
---|
457 | </tbody> |
---|
458 | </table> |
---|
459 | <?php |
---|
460 | if ( $this->is_active() ) { |
---|
461 | submit_button( |
---|
462 | _x( 'Reset Keys', 'API keys', 'contact-form-7' ), |
---|
463 | 'small', 'reset' |
---|
464 | ); |
---|
465 | } else { |
---|
466 | submit_button( |
---|
467 | __( 'Connect to the Constant Contact API', 'contact-form-7' ) |
---|
468 | ); |
---|
469 | } |
---|
470 | ?> |
---|
471 | </form> |
---|
472 | <?php |
---|
473 | } |
---|
474 | |
---|
475 | } |
---|