classes
2 years ago
mail-chimp-api.php
3 weeks ago
mail-chimp-handler.php
3 years ago
mail-chimp.php
3 weeks ago
mail-chimp-api.php
105 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ElementsKit_Lite; |
| 4 | |
| 5 | use \Elementor\ElementsKit_Widget_Mail_Chimp_Handler; |
| 6 | |
| 7 | class ElementsKit_Widget_Mail_Chimp_Api extends Core\Handler_Api { |
| 8 | |
| 9 | public function config(){ |
| 10 | $this->prefix = 'widget/mailchimp'; |
| 11 | } |
| 12 | |
| 13 | public function get_sendmail(){ |
| 14 | // Get only the GET parameters |
| 15 | $params = isset($this->request['GET']) ? $this->request['GET'] : $this->request->get_params(); |
| 16 | |
| 17 | |
| 18 | $return = ['success' => [], 'error' => [] ]; |
| 19 | |
| 20 | $nonce = $this->request->get_header( 'X-WP-Nonce' ); |
| 21 | if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, 'wp_rest' ) ) { |
| 22 | $return['error'] = esc_html__( 'Security check failed. Please refresh the page and try again.', 'elementskit-lite' ); |
| 23 | return $return; |
| 24 | } |
| 25 | |
| 26 | $dataApi = ElementsKit_Widget_Mail_Chimp_Handler::get_data(); |
| 27 | |
| 28 | $token = isset($dataApi['token']) ? $dataApi['token'] : ''; |
| 29 | $listed = $this->request['listed']; |
| 30 | |
| 31 | // Get email - required field |
| 32 | $email = isset($params['email']) ? sanitize_email($params['email']) : ''; |
| 33 | |
| 34 | // Build merge fields dynamically from remaining parameters |
| 35 | $merge_fields = []; |
| 36 | $reserved_fields = ['listed', 'double_opt_in', 'email', 'action']; |
| 37 | |
| 38 | foreach($params as $key => $value) { |
| 39 | if(!in_array($key, $reserved_fields) && !empty($value)) { |
| 40 | // Convert field name to uppercase Mailchimp merge tag |
| 41 | $merge_tag = strtoupper($key); |
| 42 | $merge_fields[$merge_tag] = sanitize_text_field($value); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | $data = [ |
| 47 | 'email_address' => (($email != '') ? $email : ''), |
| 48 | ]; |
| 49 | |
| 50 | if (!empty($merge_fields)) { |
| 51 | $data['merge_fields'] = $merge_fields; |
| 52 | } |
| 53 | |
| 54 | if(!empty($this->request['double_opt_in']) && $this->request['double_opt_in'] === 'yes') { |
| 55 | $data['status'] = 'pending'; |
| 56 | } else { |
| 57 | $data['status'] = 'subscribed'; |
| 58 | } |
| 59 | |
| 60 | $server = explode('-', $token); |
| 61 | if( !is_array($server) || empty($token) || !isset($server[1]) ){ |
| 62 | $return['error'] = esc_html__( 'Please set API Key into Dashboard User Data. ', 'elementskit-lite' ); |
| 63 | return $return; |
| 64 | } |
| 65 | |
| 66 | $url = 'https://'.$server[1].'.api.mailchimp.com/3.0/lists/'.$listed.'/members/'; |
| 67 | |
| 68 | $response = wp_remote_post( $url, [ |
| 69 | 'method' => 'POST', |
| 70 | 'data_format' => 'body', |
| 71 | 'timeout' => 45, |
| 72 | 'headers' => [ |
| 73 | 'Authorization' => 'apikey '.$token, |
| 74 | 'Content-Type' => 'application/json; charset=utf-8' |
| 75 | ], |
| 76 | 'body' => wp_json_encode($data ) |
| 77 | ] |
| 78 | ); |
| 79 | |
| 80 | /* handle Mailchimp response */ |
| 81 | if ( is_wp_error( $response ) ) { |
| 82 | $return['error'] = 'Something went wrong: ' . $response->get_error_message(); |
| 83 | return $return; |
| 84 | } |
| 85 | |
| 86 | $code = wp_remote_retrieve_response_code( $response ); |
| 87 | $body = wp_remote_retrieve_body( $response ); |
| 88 | $decoded = json_decode( $body, true ); |
| 89 | |
| 90 | if ( $code >= 400 ) { |
| 91 | if ( is_array( $decoded ) && ! empty( $decoded['title'] ) ) { |
| 92 | $return['error'] = $decoded['title']; |
| 93 | } else { |
| 94 | $return['error'] = $body; |
| 95 | } |
| 96 | } else { |
| 97 | // keep the original wp_remote_post response so JS can parse response.success.body |
| 98 | $return['success'] = $response; |
| 99 | } |
| 100 | |
| 101 | return $return; |
| 102 | } |
| 103 | } |
| 104 | //https://us20.api.mailchimp.com/3.0/lists?apikey=24550c8cb06076781d51a80274a52878-us20 |
| 105 |