Skip to content

Commit

Permalink
Merge pull request #4 from gravitywpcom/beta-version
Browse files Browse the repository at this point in the history
adding a small fix to fix error message for license handler
  • Loading branch information
kimo-works authored Jul 5, 2024
2 parents 7853ad5 + fd5b3e0 commit 2e5b8ab
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions src/pluginUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,14 @@ public function request_is_activate( $field_setting ) {
$json_data = json_decode( $body, true );
if ( isset( $json_data['success'] ) && $json_data['success'] === true ) {
return true;
} elseif ( isset( $json_data['errors'] ) ) {
$this->error_messages = nl2br( $this->generateErrorMessage( $json_data['errors'] ) );
return false;
} elseif ( isset( $json_data['message'] ) ) {
$this->error_messages = nl2br( $this->generateErrorMessage( $json_data['message'] ) );
return false;
} else {
$this->error_messages = nl2br( $this->generateErrorMessage( $json_data['errors'] ) ); // 'Something went wrong: ' . print_r( $json_data['errors'], true );
$this->error_messages = nl2br( $this->generateErrorMessage( 'Please try again later. If the issue persists, please contact support.' ) );
return false;
}
}
Expand All @@ -225,20 +231,27 @@ public function request_is_activate( $field_setting ) {
*
* @return string A formatted error message string.
*/
public function generateErrorMessage( $errorArray ) {
public function generateErrorMessage( $error ) {
// Initialize the error message string with a prefix.
$errorMessage = "Something went wrong:\n";

// Loop through each entry in the error array.
foreach ( $errorArray as $key => $messages ) {
// Loop through each message in the current entry.
foreach ( $messages as $message ) {
// Sanitize the error message to remove any harmful content.
$sanitized_message = sanitize_text_field( $message );

// Append the sanitized message to the error message string.
$errorMessage .= "- $sanitized_message\n";
if ( is_array( $error ) ) {
// Loop through each entry in the error array.
foreach ( $error as $key => $messages ) {
if ( is_array( $messages ) ) {
// Loop through each message in the current entry.
foreach ( $messages as $message ) {
// Sanitize the error message to remove any harmful content.
$sanitized_message = sanitize_text_field( $message );
// Append the sanitized message to the error message string.
$errorMessage .= "- $sanitized_message\n";
}
} else {
$sanitized_message = sanitize_text_field( $messages );
$errorMessage .= "- $sanitized_message\n";
}
}
} else {
$errorMessage .= $error;
}

// Return the formatted error message string.
Expand Down

0 comments on commit 2e5b8ab

Please sign in to comment.