mailchimp( $email, $first, $last, $api_key, $list_id, $double ); break; case 'brevo': $this->brevo( $email, $first, $last, $api_key, $list_id, $double ); break; case 'cleverreach': $this->cleverreach( $email, $first, $last, $api_key, $list_id ); break; } } private function log_error( $provider, $result ) { if ( ! ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ) { return; } $msg = is_wp_error( $result ) ? $result->get_error_message() : wp_remote_retrieve_body( $result ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log error_log( '[Adventura] Newsletter (' . $provider . '): ' . $msg ); } /** * Mailchimp Marketing API v3. */ private function mailchimp( $email, $first, $last, $api_key, $list_id, $double ) { // The data centre is the part after the dash in the API key. $parts = explode( '-', $api_key ); $dc = isset( $parts[1] ) ? $parts[1] : ''; if ( '' === $dc || '' === $list_id ) { return; } $url = 'https://' . $dc . '.api.mailchimp.com/3.0/lists/' . rawurlencode( $list_id ) . '/members/' . md5( strtolower( $email ) ); $body = array( 'email_address' => $email, 'status_if_new' => $double ? 'pending' : 'subscribed', 'merge_fields' => array( 'FNAME' => $first, 'LNAME' => $last, ), ); $result = wp_remote_request( $url, array( 'method' => 'PUT', 'timeout' => 10, 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( 'anystring:' . $api_key ), 'Content-Type' => 'application/json', ), 'body' => wp_json_encode( $body ), ) ); if ( is_wp_error( $result ) || (int) wp_remote_retrieve_response_code( $result ) >= 400 ) { $this->log_error( 'mailchimp', $result ); } } /** * Brevo (formerly Sendinblue) Contacts API v3. */ private function brevo( $email, $first, $last, $api_key, $list_id, $double ) { $body = array( 'email' => $email, 'updateEnabled' => true, 'attributes' => array( 'FIRSTNAME' => $first, 'LASTNAME' => $last, ), ); if ( '' !== $list_id ) { $body['listIds'] = array( (int) $list_id ); } $result = wp_remote_post( 'https://api.brevo.com/v3/contacts', array( 'timeout' => 10, 'headers' => array( 'api-key' => $api_key, 'Content-Type' => 'application/json', 'Accept' => 'application/json', ), 'body' => wp_json_encode( $body ), ) ); if ( is_wp_error( $result ) || (int) wp_remote_retrieve_response_code( $result ) >= 400 ) { $this->log_error( 'brevo', $result ); } } /** * CleverReach REST API v3 (token in API-key field). */ private function cleverreach( $email, $first, $last, $token, $list_id ) { if ( '' === $list_id ) { return; } $body = array( 'email' => $email, 'registered' => time(), 'activated' => time(), 'source' => 'Adventura', 'global_attributes' => array( 'firstname' => $first, 'lastname' => $last, ), ); $result = wp_remote_post( 'https://rest.cleverreach.com/v3/groups.json/' . rawurlencode( $list_id ) . '/receivers', array( 'timeout' => 10, 'headers' => array( 'Authorization' => 'Bearer ' . $token, 'Content-Type' => 'application/json', ), 'body' => wp_json_encode( $body ), ) ); if ( is_wp_error( $result ) || (int) wp_remote_retrieve_response_code( $result ) >= 400 ) { $this->log_error( 'cleverreach', $result ); } } }