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

Improve error message handling #230

Merged
merged 1 commit into from
Nov 24, 2023
Merged
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
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
Loading