Skip to content

Commit

Permalink
chore: simplfy service test
Browse files Browse the repository at this point in the history
  • Loading branch information
Soare-Robert-Daniel committed Aug 31, 2023
1 parent 8c3be5e commit 89f423a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 40 deletions.
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
8 changes: 7 additions & 1 deletion inc/server/class-form-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -655,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

0 comments on commit 89f423a

Please sign in to comment.