Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add merge fields for Mailchimp #2323

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
2 changes: 1 addition & 1 deletion src/blocks/blocks/form/common.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ export const selectAllFieldsFromForm = ( children: any[]) : ({ parentClientId: s

export const mappedNameInfo = (
<Fragment>
{__( 'Allow easy identification of the field.', 'otter-blocks' )}
{__( 'Allow easy identification of the field in features like Webhooks.', 'otter-blocks' )}
<ExternalLink href='https://docs.themeisle.com/article/1878-how-to-use-webhooks-in-otter-forms#mapped-name'> { __( 'Learn More', 'otter-blocks' ) } </ExternalLink>
</Fragment>
);
Expand Down
2 changes: 1 addition & 1 deletion src/pro/blocks/form-stripe-field/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const Inspector = ({

<TextControl
label={ __( 'Mapped Name', 'otter-blocks' ) }
help={ __( 'Allow easy identification of the field with features like: webhooks', 'otter-blocks' ) }
help={ __( 'Allow easy identification of the field in features like Webhooks.', 'otter-blocks' ) }
value={ attributes.mappedName }
onChange={ mappedName => setAttributes({ mappedName }) }
placeholder={ __( 'product', 'otter-blocks' ) }
Expand Down
Loading