Skip to content

Commit 22a86f5

Browse files
authored
Merge pull request #53 from WP-API/filter-redirect-args
Allow plugins to filter redirect arguments
2 parents 4719877 + 1786784 commit 22a86f5

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

inc/types/class-authorization-code.php

+7
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ protected function handle_authorization_submission( $submit, Client $client, $da
6060
$redirect_args['state'] = $data['state'];
6161
}
6262

63+
$redirect_args = $this->filter_redirect_args(
64+
$redirect_args,
65+
$submit === 'authorize',
66+
$client,
67+
$data
68+
);
69+
6370
$generated_redirect = add_query_arg( urlencode_deep( $redirect_args ), $redirect_uri );
6471
wp_redirect( $generated_redirect );
6572
exit;

inc/types/class-base.php

+30
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,34 @@ protected function render_form( Client $client, WP_Error $errors = null ) {
154154
protected function get_nonce_action( Client $client ) {
155155
return sprintf( 'oauth2_authorize:%s', $client->get_id() );
156156
}
157+
158+
/**
159+
* Filter the redirection args.
160+
*
161+
* @param array $redirect_args Redirect args.
162+
* @param boolean $authorized True if authorized, false otherwise.
163+
* @param Client $client Client being authorised.
164+
* @param array $data Data for the request.
165+
*/
166+
protected function filter_redirect_args( $redirect_args, $authorized, Client $client, $data ) {
167+
if ( ! $authorized ) {
168+
/**
169+
* Filter the redirect args when the user has cancelled.
170+
*
171+
* @param array $redirect_args Redirect args.
172+
* @param Client $client Client being authorised.
173+
* @param array $data Data for the request.
174+
*/
175+
return apply_filters( 'oauth2.redirect_args.cancelled', $redirect_args, $client, $data );
176+
}
177+
178+
/**
179+
* Filter the redirect args when the user has authorized.
180+
*
181+
* @param array $redirect_args Redirect args.
182+
* @param Client $client Client being authorised.
183+
* @param array $data Data for the request.
184+
*/
185+
return apply_filters( 'oauth2.redirect_args.authorized', $redirect_args, $client, $data );
186+
}
157187
}

inc/types/class-implicit.php

+7
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ protected function handle_authorization_submission( $submit, Client $client, $da
6161
$redirect_args['state'] = $data['state'];
6262
}
6363

64+
$redirect_args = $this->filter_redirect_args(
65+
$redirect_args,
66+
$submit === 'authorize',
67+
$client,
68+
$data
69+
);
70+
6471
$fragment = build_query( $redirect_args );
6572
$generated_redirect = $redirect_uri . '#' . $fragment;
6673
wp_redirect( $generated_redirect );

0 commit comments

Comments
 (0)