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

Fix Form Marketing List ID fetch and improve error handling #1836

Merged
merged 3 commits into from
Aug 31, 2023
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
18 changes: 14 additions & 4 deletions inc/integrations/api/form-request-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class Form_Data_Request {
* Constructor.
*
* @access public
* @param \WP_REST_Request|mixed $request Request Data.
* @param \WP_REST_Request $request Request Data.
* @since 2.0.3
*/
public function __construct( $request = null ) {
Expand All @@ -118,11 +118,21 @@ public function __construct( $request = null ) {
}

$this->request = $request;
$form_data = $request->get_param( 'form_data' );

$form_data = json_decode( $form_data, true );
if ( ! empty( $request->get_param( 'form_data' ) ) ) {
$form_data = $request->get_param( 'form_data' );
$form_data = json_decode( $form_data, true );
$this->request_data = $this->sanitize_request_data( $form_data );
} else {
$body = json_decode( $request->get_body(), true );
if ( null !== $body ) {
$this->request_data = $this->sanitize_request_data( $body );
} else {
$this->error_code = Form_Data_Response::ERROR_MALFORMED_REQUEST;
}
}


$this->request_data = $this->sanitize_request_data( $form_data );
$this->form_options = new Form_Settings_Data( array() );
}

Expand Down
4 changes: 3 additions & 1 deletion inc/integrations/api/form-response-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Form_Data_Response {
const ERROR_MISSING_FILE_FIELD_OPTION = '16';
const ERROR_AUTORESPONDER_MISSING_EMAIL_FIELD = '17';
const ERROR_AUTORESPONDER_COULD_NOT_SEND = '18';
const ERROR_MALFORMED_REQUEST = '19';

// Request validation errors.
const ERROR_MISSING_DATA = '101';
Expand Down Expand Up @@ -151,7 +152,7 @@ public function set_reasons( $reasons ) {

/**
* Set success message.
*
*
* @param string $message The message.
* @since 2.4
*/
Expand Down Expand Up @@ -356,6 +357,7 @@ public static function get_error_code_message( $error_code ) {
self::ERROR_AUTORESPONDER_MISSING_EMAIL_FIELD => __( 'The email field is missing from the Form Block with Autoresponder activated.', 'otter-blocks' ),
self::ERROR_AUTORESPONDER_COULD_NOT_SEND => __( 'The email from Autoresponder could not be sent.', 'otter-blocks' ),
self::ERROR_FILE_MISSING_BINARY => __( 'The file data is missing.', 'otter-blocks' ),
self::ERROR_MALFORMED_REQUEST => __( 'The request is malformed.', 'otter-blocks' ),
);

if ( ! isset( $error_messages[ $error_code ] ) ) {
Expand Down
7 changes: 6 additions & 1 deletion inc/integrations/class-form-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@ public static function generate_test_email() {
'eight',
'nine',
'ten',
'eleven',
'twelve',
'thirteen',
'fourteen',
'fifteen',
);

$name_1 = $words[ wp_rand( 0, count( $words ) ) ];
$name_1 = $words[ wp_rand( 0, count( $words ) - 1 ) ];
$name_2 = $words[ wp_rand( 2, count( $words ) ) - 1 ];

return "Otter-Form-successfully-connected.delete-on-confirmation.$name_1.$name_2@otter-blocks.com";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ public function extract_data_from_integration( $wp_options_form );
public static function validate_api_key( $api_key );

/**
* Test if the service is set up by registering a random email address on the contact list.
* Make a request that add the email to the contact list.
*
* @return mixed
* @since 2.0.3
* @param string $email The email address.
* @return array|\WP_Error The response from Mailchimp.
*/
public function test_subscription();
public function make_subscribe_request( $email );
}
19 changes: 1 addition & 18 deletions inc/integrations/providers/class-mailchimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function( $item ) {
* @param string $email The email address.
* @return array|\WP_Error The response from Mailchimp.
*/
private function make_subscribe_request( $email ) {
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 ) );
Expand Down Expand Up @@ -161,23 +161,6 @@ public function subscribe( $form_data ) {
return $form_data;
}

/**
* Test the subscription by registering a random generated email.
*
* @return Form_Data_Request
* @since 2.0.3
*/
public function test_subscription() {
$req = new Form_Data_Request();
$response = $this->make_subscribe_request( Form_Utils::generate_test_email() );

if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
$req->set_error( Form_Data_Response::get_error_code_message( Form_Data_Response::ERROR_PROVIDER_SUBSCRIBE_ERROR ) );
}

return $req;
}

/**
* Set the API Key.
*
Expand Down
16 changes: 0 additions & 16 deletions inc/integrations/providers/class-sendinblue.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,22 +154,6 @@ public function subscribe( $form_data ) {
return $form_data;
}

/**
* Test the subscription by registering a random generated email.
*
* @return Form_Data_Request
*/
public function test_subscription() {
$req = new Form_Data_Request();
$response = $this->make_subscribe_request( Form_Utils::generate_test_email() );

if ( is_wp_error( $response ) || 400 === wp_remote_retrieve_response_code( $response ) ) {
$req->set_error( Form_Data_Response::get_error_code_message( Form_Data_Response::ERROR_PROVIDER_SUBSCRIBE_ERROR ) );
}

return $req;
}

/**
* Set the API Key
*
Expand Down
14 changes: 12 additions & 2 deletions inc/server/class-form-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public function register_routes() {
* @since 2.0.3
*/
public function editor( $request ) {
$data = new Form_Data_Request( json_decode( $request->get_body(), true ) );
$data = new Form_Data_Request( $request );
$res = new Form_Data_Response();

$form_options = Form_Settings_Data::get_form_setting_from_wordpress_options( $data->get_payload_field( 'formOption' ) );
Expand All @@ -212,6 +212,10 @@ public function editor( $request ) {
}
$provider_handlers = Form_Providers::$instance->get_provider_handlers( $provider, 'editor' );

if ( $data->has_error() ) {
return $res->set_code( $data->get_error_code() )->build_response();
}

if ( $provider_handlers && Form_Providers::provider_has_handler( $provider_handlers, $data->get( 'handler' ) ) ) {
// Send the data to the provider.
return $provider_handlers[ $data->get( 'handler' ) ]( $data );
Expand Down Expand Up @@ -651,7 +655,13 @@ public function test_subscription_service( $form_data ) {
if ( $valid_api_key['valid'] ) {
if ( $form_options->has_list_id() ) {
$service->set_api_key( $form_options->get_api_key() )->set_list_id( $form_options->get_list_id() );
$res = $service->test_subscription();
$response = $service->make_subscribe_request( Form_Utils::generate_test_email() );

if ( is_wp_error( $response ) ) {
$res->set_error( Form_Data_Response::ERROR_RUNTIME_ERROR, $response->get_error_message() );
} else {
$res->mark_as_success();
}
} else {
$res->set_error( __( 'Contact list ID is missing!', 'otter-blocks' ) );
}
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/blocks/form/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ const Edit = ({
} else {
createNotice(
'error',
res?.error,
res?.error ?? res?.reasons?.join( '. ' ) ?? __( 'An error has occurred.', 'otter-blocks' ),
{
isDismissible: true,
type: 'snackbar',
Expand Down
22 changes: 22 additions & 0 deletions src/blocks/test/e2e/blocks/form.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,26 @@ test.describe( 'Form Block', () => {
// check for a element with the attribute data-redirect-url
await expect( await page.$( `[data-redirect="${REDIRECT_URL}"]` ) ).toBeTruthy();
});

test( 'errors on invalid API Key for Market Integration', async({ page, editor, browser }) => {

await editor.insertBlock({ name: 'themeisle-blocks/form' });

let formBlock = ( await editor.getBlocks() ).find( ( block ) => 'themeisle-blocks/form' === block.name );

expect( formBlock ).toBeTruthy();

const { clientId } = formBlock;

await page.click( `#block-${clientId} > div > fieldset > ul > li:nth-child(1) > button` );

await page.getByRole( 'button', { name: 'Marketing Integration' }).click();

// Select the Mailchimp option on the select with label Provider
await page.getByLabel( 'Provider' ).selectOption( 'mailchimp' );

await page.getByLabel( 'API Key' ).fill( 'invalid-api-key' );

await expect( page.getByLabel( 'Dismiss this notice' ) ).toBeVisible();
});
});