Skip to content

Commit

Permalink
feat: add merge fields for Mailchimp
Browse files Browse the repository at this point in the history
  • Loading branch information
Soare-Robert-Daniel committed Aug 7, 2024
1 parent 4934fce commit 11fc28e
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 4 deletions.
3 changes: 3 additions & 0 deletions inc/integrations/api/form-response-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ public function set_is_credential_error( $is_credential_error ) {
* @since 2.1.7
*/
public function process_error_code() {
if ( ! $this->has_error() ) {
return;
}
$this->add_reason( self::get_error_code_message( $this->response['code'] ) );
}

Expand Down
119 changes: 115 additions & 4 deletions inc/integrations/providers/class-mailchimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ class Mailchimp_Integration implements FormSubscribeServiceInterface {
*/
protected $server_name = '';

/**
* The form data.
*
* @var Form_Data_Request|null
*/
protected $form_data = null;


/**
* The default constructor.
Expand Down Expand Up @@ -110,16 +117,24 @@ function( $item ) {
*
* @param string $email The email address.
* @return array|\WP_Error The response from Mailchimp.
*
* @see https://mailchimp.com/developer/marketing/api/list-members/add-member-to-list/
*/
public function make_subscribe_request( $email ) {
$user_status = $this->get_new_user_status_mailchimp( $this->list_id );

$url = 'https://' . $this->server_name . '.api.mailchimp.com/3.0/lists/' . $this->list_id . '/members/' . md5( strtolower( $email ) );

$payload = array(
'email_address' => $email,
'status' => $user_status,
);

$linked_merge_fields = $this->get_linked_merge_fields();
if ( ! empty( $linked_merge_fields ) ) {
$url = add_query_arg( 'skip_merge_validation', 'true', $url );
$payload['merge_fields'] = $linked_merge_fields;
}

$args = array(
'method' => 'PUT',
Expand All @@ -141,9 +156,10 @@ public function make_subscribe_request( $email ) {
*/
public function subscribe( $form_data ) {

$email = $form_data->get_first_email_from_input_fields();
$response = $this->make_subscribe_request( $email );
$body = json_decode( wp_remote_retrieve_body( $response ), true );
$this->form_data = $form_data;
$email = $form_data->get_first_email_from_input_fields();
$response = $this->make_subscribe_request( $email );
$body = json_decode( wp_remote_retrieve_body( $response ), true );

if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {

Expand Down Expand Up @@ -284,4 +300,99 @@ public function get_information_from_provider( $request ) {
private function is_credential_error( $response_code ) {
return in_array( $response_code, array( 401, 403, 404, 500 ) );
}

/**
* Get the merge fields from Mailchimp.
*
* @return array An array of merge fields, each containing:
* - string $tag The merge field's tag
* - string $type The merge field's type (e.g., 'text')
* @since 2.7.0
*
* @see https://mailchimp.com/developer/marketing/api/list-merges/list-merge-fields/
*/
public function get_merge_fields() {
$url = 'https://' . $this->server_name . '.api.mailchimp.com/3.0/lists/' . $this->list_id . '/merge-fields';
$args = array(
'method' => 'GET',
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'user:' . $this->api_key ),
),
);

if ( function_exists( 'vip_safe_wp_remote_get' ) ) {
$response = vip_safe_wp_remote_get( $url, '', 3, 1, 20, $args );
} else {
$response = wp_remote_get( $url, $args ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get
}

if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
return array();
}

$body = json_decode( wp_remote_retrieve_body( $response ), true );

if ( ! isset( $body['merge_fields'] ) || empty( $body['merge_fields'] ) ) {
return array();
}

return array_filter(
$body['merge_fields'],
function( $item ) {
return ! empty( $item['tag'] );
}
);
}

/**
* Link the form fields with their corresponding merge fields.
*
* @return array The available merge fields to link with the form fields.
* @since 2.7.0
*/
protected function get_linked_merge_fields() {

// Check if it is necessary to link the fields.
$form_fields = $this->form_data->get_fields();
if ( empty( $form_fields ) ) {
return array();
}

$available_fields_tags = array();

foreach ( $form_fields as $field ) {
if ( empty( $field['metadata']['mappedName'] ) ) {
continue;
}

$available_fields_tags[] = array(
'tag' => strtoupper( $field['metadata']['mappedName'] ),
'value' => $field['value'],
);
}

if ( empty( $available_fields_tags ) ) {
return array();
}


$merge_fields = $this->get_merge_fields();
if ( empty( $merge_fields ) ) {
return array();
}

// Link based on the tag of the merge fields.
$linked_fields = array();
foreach ( $available_fields_tags as $field ) {
foreach ( $merge_fields as $merge_field ) {
if ( $field['tag'] !== $merge_field['tag'] ) {
continue;
}

$linked_fields[ $merge_field['tag'] ] = $field['value'];
}
}

return $linked_fields;
}
}

0 comments on commit 11fc28e

Please sign in to comment.