Skip to content

Commit 05ed163

Browse files
committed
Move all error handling/exiting to separate method
1 parent 4b05d5e commit 05ed163

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

lib/class-wp-json-authentication-oauth1-authorize.php

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,33 @@ class WP_JSON_Authentication_OAuth1_Authorize {
2727
* Register required actions and filters
2828
*/
2929
public function register_hooks() {
30-
add_action( 'login_form_oauth1_authorize', array( $this, 'render_page' ) );
30+
add_action( 'login_form_oauth1_authorize', array( $this, 'handle_request' ) );
3131
add_action( 'oauth1_authorize_form', array( $this, 'page_fields' ) );
3232
}
3333

34+
/**
35+
* Handle request to authorization page
36+
*
37+
* Handles response from {@see render_page}, then exits to avoid output from
38+
* default wp-login handlers.
39+
*/
40+
public function handle_request() {
41+
$response = $this->render_page();
42+
if ( is_wp_error( $response ) ) {
43+
$this->display_error( $response );
44+
}
45+
exit;
46+
}
47+
3448
/**
3549
* Render authorization page
3650
*
37-
* Callback for login form hook. Must exit.
51+
* @return null|WP_Error Null on success, error otherwise
3852
*/
3953
public function render_page() {
4054
// Check required fields
4155
if ( empty( $_REQUEST['oauth_token'] ) ) {
42-
$error = new WP_Error( 'json_oauth1_missing_param', sprintf( __( 'Missing parameter %s' ), 'oauth_token' ), array( 'status' => 400 ) );
43-
$this->display_error( $error );
44-
exit;
56+
return new WP_Error( 'json_oauth1_missing_param', sprintf( __( 'Missing parameter %s' ), 'oauth_token' ), array( 'status' => 400 ) );
4557
}
4658

4759
// Set up fields
@@ -55,8 +67,7 @@ public function render_page() {
5567
$errors = array();
5668
$this->token = $authenticator->get_request_token( $token_key );
5769
if ( is_wp_error( $this->token ) ) {
58-
$this->display_error( $this->token );
59-
exit;
70+
return $this->token;
6071
}
6172

6273
// Fetch consumer
@@ -69,23 +80,20 @@ public function render_page() {
6980
case 'authorize':
7081
$verifier = $authenticator->authorize_request_token( $this->token['key'] );
7182
if ( is_wp_error( $verifier ) ) {
72-
$this->display_error( $error );
73-
exit;
83+
return $verifier;
7484
}
7585

7686
$error = $this->handle_callback_redirect( $verifier );
7787
if ( is_wp_error( $error ) ) {
78-
$this->display_error( $error );
88+
return $error;
7989
}
80-
exit;
90+
return null;
8191

8292
case 'cancel':
8393
exit;
8494

8595
default:
86-
$error = new WP_Error( 'json_oauth1_invalid_action', __( 'Invalid authorization action' ), array( 'status' => 400 ) );
87-
$this->display_error( $error );
88-
exit;
96+
return new WP_Error( 'json_oauth1_invalid_action', __( 'Invalid authorization action' ), array( 'status' => 400 ) );
8997
}
9098
}
9199

@@ -95,8 +103,6 @@ public function render_page() {
95103
}
96104

97105
include $file;
98-
99-
exit;
100106
}
101107

102108
/**

0 commit comments

Comments
 (0)