Skip to content

Commit

Permalink
Improve error message handling (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnynews authored Nov 24, 2023
1 parent e23038c commit 0a2901b
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions lib/class-wp-rest-oauth1-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,25 +146,28 @@ class="add-new-h2"><?php echo esc_html_x( 'Add New', 'application', 'rest_oauth1
* @return array|WP_Error
*/
protected static function validate_parameters( $params ) {
$valid = array();

$error = new WP_Error();
if ( empty( $params['name'] ) ) {
return new WP_Error( 'rest_oauth1_missing_name', __( 'Consumer name is required', 'rest_oauth1' ) );
$error->add( 'rest_oauth1_missing_name', __( 'Consumer name is required', 'rest_oauth1' ) );
}
$valid['name'] = wp_filter_post_kses( $params['name'] );

if ( empty( $params['description'] ) ) {
return new WP_Error( 'rest_oauth1_missing_description', __( 'Consumer description is required', 'rest_oauth1' ) );
$error->add( 'rest_oauth1_missing_description', __( 'Consumer description is required', 'rest_oauth1' ) );
}
$valid['description'] = wp_filter_post_kses( $params['description'] );

if ( empty( $params['callback'] ) ) {
return new WP_Error( 'rest_oauth1_missing_callback', __( 'Consumer callback is required and must be a valid URL.', 'rest_oauth1' ) );
$error->add( 'rest_oauth1_missing_callback', __( 'Consumer callback is required and must be a valid URL.', 'rest_oauth1' ) );
}

$valid['callback'] = $params['callback'];
if ( count( $error->get_error_codes() ) > 0 ) {
return $error;
}

return $valid;
return array(
'name' => wp_filter_post_kses( $params['name'] ),
'description' => wp_filter_post_kses( $params['description'] ),
'callback' => $params['description'],
);
}

/**
Expand All @@ -175,7 +178,6 @@ protected static function validate_parameters( $params ) {
* @return array|null List of errors. Issues a redirect and exits on success.
*/
protected static function handle_edit_submit( $consumer ) {
$messages = array();
if ( empty( $consumer ) ) {
$did_action = 'add';
check_admin_referer( 'rest-oauth1-add' );
Expand All @@ -187,8 +189,7 @@ protected static function handle_edit_submit( $consumer ) {
// Check that the parameters are correct first.
$params = self::validate_parameters( wp_unslash( $_POST ) );
if ( is_wp_error( $params ) ) {
$messages[] = $params->get_error_message();
return $messages;
return $params->get_error_messages();
}

if ( empty( $consumer ) ) {
Expand All @@ -215,9 +216,7 @@ protected static function handle_edit_submit( $consumer ) {
}

if ( is_wp_error( $result ) ) {
$messages[] = $result->get_error_message();

return $messages;
return $result->get_error_messages();
}

// Success, redirect to alias page.
Expand Down Expand Up @@ -265,23 +264,29 @@ public static function render_edit_page() {
);
}

// Handle form submission.
$messages = array();
// Handle type of notice.
$notice_type = 'info';
$messages = array();
if ( ! empty( $_POST['submit'] ) ) {
$messages = self::handle_edit_submit( $consumer );
$messages = self::handle_edit_submit( $consumer );
$notice_type = 'error';
}
if ( ! empty( $_GET['did_action'] ) ) {
switch ( $_GET['did_action'] ) {
case 'edit':
$messages[] = __( 'Updated application.', 'rest_oauth1' );
$messages[] = __( 'Updated application.', 'rest_oauth1' );
$notice_type = 'info';

break;

case 'regenerate':
$messages[] = __( 'Regenerated secret.', 'rest_oauth1' );
$messages[] = __( 'Regenerated secret.', 'rest_oauth1' );
$notice_type = 'success';
break;

default:
$messages[] = __( 'Successfully created application.', 'rest_oauth1' );
$messages[] = __( 'Successfully created application.', 'rest_oauth1' );
$notice_type = 'success';
break;
}
}
Expand Down Expand Up @@ -313,7 +318,7 @@ public static function render_edit_page() {
<?php
if ( ! empty( $messages ) ) {
foreach ( $messages as $msg ) {
echo '<div id="message" class="updated"><p>' . esc_html( $msg ) . '</p></div>';
echo '<div id="message" class="notice is-dismissible notice-' . $notice_type . '"><p>' . esc_html( $msg ) . '</p></div>';
}
}
?>
Expand Down

0 comments on commit 0a2901b

Please sign in to comment.