From 8b9e16f5086a2bc4b85bbd58ca77ffdb8acf4e08 Mon Sep 17 00:00:00 2001 From: Jonathan Harris Date: Thu, 18 Jul 2019 13:38:53 +0100 Subject: [PATCH] Change filter names --- inc/admin/profile/namespace.php | 38 +++++++++++++---- inc/class-client.php | 23 +++++++--- inc/namespace.php | 42 ++++++++++++++++-- inc/types/class-base.php | 75 +++++++++++++++++++++++---------- 4 files changed, 140 insertions(+), 38 deletions(-) diff --git a/inc/admin/profile/namespace.php b/inc/admin/profile/namespace.php index 61a2bbb..ecda187 100644 --- a/inc/admin/profile/namespace.php +++ b/inc/admin/profile/namespace.php @@ -97,11 +97,22 @@ function render_token_row( WP_User $user, Access_Token $token ) { /** * Filter details shown for an access token on the profile screen. * - * @param string[] $details List of HTML snippets to render in table. - * @param Access_Token $token Token being displayed. - * @param WP_User $user User whose profile is being rendered. + * @param string[] $details List of HTML snippets to render in table. + * @param Access_Token $token Token being displayed. + * @param WP_User $user User whose profile is being rendered. + * + * @deprecated + */ + $details = apply_filters_deprecated( 'oauth2.admin.profile.render_token_row.details', $details, $token, $user ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores + + /** + * Filter details shown for an access token on the profile screen. + * + * @param string[] $details List of HTML snippets to render in table. + * @param Access_Token $token Token being displayed. + * @param WP_User $user User whose profile is being rendered. */ - $details = apply_filters( 'oauth2.admin.profile.render_token_row.details', $details, $token, $user ); + $details = apply_filters( 'oauth2_admin_profile_render_token_row_details', $details, $token, $user ); // Build actions. if ( $is_personal ) { @@ -130,11 +141,22 @@ function render_token_row( WP_User $user, Access_Token $token ) { /** * Filter actions shown for an access token on the profile screen. * - * @param string[] $actions List of HTML snippets to render in table. - * @param Access_Token $token Token being displayed. - * @param WP_User $user User whose profile is being rendered. + * @param string[] $actions List of HTML snippets to render in table. + * @param Access_Token $token Token being displayed. + * @param WP_User $user User whose profile is being rendered. + * + * @deprecated + */ + $actions = apply_filters_deprecated( 'oauth2.admin.profile.render_token_row.actions', $actions, $token, $user ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores + + /** + * Filter actions shown for an access token on the profile screen. + * + * @param string[] $actions List of HTML snippets to render in table. + * @param Access_Token $token Token being displayed. + * @param WP_User $user User whose profile is being rendered. */ - $actions = apply_filters( 'oauth2.admin.profile.render_token_row.actions', $actions, $token, $user ); + $actions = apply_filters( 'oauth2_admin_profile_render_token_row_actions', $actions, $token, $user ); $name = sprintf( '%s', $client->get_name() ); if ( $is_personal ) { diff --git a/inc/class-client.php b/inc/class-client.php index ff06870..035bdd8 100644 --- a/inc/class-client.php +++ b/inc/class-client.php @@ -198,6 +198,18 @@ public function check_redirect_uri( $uri ) { } } + /** + * Filter whether a callback is counted as valid. + * + * @param boolean $valid True if the callback URL is valid, false otherwise. + * @param string $url Supplied callback URL. + * @param string $registered_uri URI being checked. + * @param Client $client OAuth 2 client object. + * + * @deprecated + */ + $valid = apply_filters_deprecated( 'rest_oauth.check_callback', $valid, $uri, $registered_uri, $this );// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores + /** * Filter whether a callback is counted as valid. * @@ -209,12 +221,13 @@ public function check_redirect_uri( $uri ) { * highly recommended not to change this behaviour, as clients will * expect the same behaviour across all WP sites. * - * @param boolean $valid True if the callback URL is valid, false otherwise. - * @param string $url Supplied callback URL. - * @param string $registered_uri URI being checked. - * @param Client $client OAuth 2 client object. + * @param boolean $valid True if the callback URL is valid, false otherwise. + * @param string $url Supplied callback URL. + * @param string $registered_uri URI being checked. + * @param Client $client OAuth 2 client object. */ - $valid = apply_filters( 'rest_oauth.check_callback', $valid, $uri, $registered_uri, $this ); + $valid = apply_filters( 'rest_oauth_check_callback', $valid, $uri, $registered_uri, $this ); + if ( $valid ) { // Stop checking, we have a match. return true; diff --git a/inc/namespace.php b/inc/namespace.php index fecbad7..e8dad76 100644 --- a/inc/namespace.php +++ b/inc/namespace.php @@ -41,6 +41,19 @@ function rest_oauth2_load_authorize_page() { * @return Type[] Map of grant type to handler object. */ function get_grant_types() { + /** + * Filter valid grant types. + * + * Default supported grant types are added in register_grant_types(). + * Note that additional grant types must follow the extension policy in the + * OAuth 2 specification. + * + * @param Type[] $grant_types Map of grant type to handler object. + * + * @deprecated + */ + $grant_types = apply_filters_deprecated( 'oauth2.grant_types', [] ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores + /** * Filter valid grant types. * @@ -50,7 +63,8 @@ function get_grant_types() { * * @param Type[] $grant_types Map of grant type to handler object. */ - $grant_types = apply_filters( 'oauth2.grant_types', [] ); + $grant_types = apply_filters( 'oauth2_grant_types', $grant_types ); + foreach ( $grant_types as $type => $handler ) { if ( ! $handler instanceof Type ) { /* translators: 1: Grant type name, 2: Grant type interface */ @@ -108,12 +122,23 @@ function get_authorization_url() { $url = wp_login_url(); $url = add_query_arg( 'action', 'oauth2_authorize', $url ); + /** + * Filter the authorization URL. (deprecated). + * + * @param string $url URL for the OAuth 2 authorization endpoint. + * + * @deprecated + */ + $url = apply_filters_deprecated( 'oauth2.get_authorization_url', $url ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores + /** * Filter the authorization URL. * * @param string $url URL for the OAuth 2 authorization endpoint. */ - return apply_filters( 'oauth2.get_authorization_url', $url ); + $url = apply_filters( 'oauth2_get_authorization_url', $url ); + + return $url; } /** @@ -128,8 +153,19 @@ function get_token_url() { * Filter the token URL. * * @param string $url URL for the OAuth 2 token endpoint. + * + * @deprecated */ - return apply_filters( 'oauth2.get_token_url', $url ); + $url = apply_filters_deprecated( 'oauth2.get_token_url', $url ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores + + /** + * Filter the token URL. + * + * @param string $url URL for the OAuth 2 token endpoint. + */ + $url = apply_filters( 'oauth2_get_token_url', $url ); + + return $url; } /** diff --git a/inc/types/class-base.php b/inc/types/class-base.php index d7bc4ad..9c37901 100644 --- a/inc/types/class-base.php +++ b/inc/types/class-base.php @@ -10,14 +10,15 @@ abstract class Base implements Type { /** * Handle submission of authorisation page. * - * @param string $submit Value of the selected button. - * @param Client $client Client being authorised. - * @param array $data Data gathered for the request. { - * @var string $redirect_uri Specified redirection URI. - * @var string $scope Requested scope. - * @var string $state State parameter from the client. - * } + * @param string $submit Value of the selected button. + * @param Client $client Client being authorised. + * @param array $data Data gathered for the request. { + * * @return WP_Error|void Method should output form and exit, or return encountered error. + * @var string $scope Requested scope. + * @var string $state State parameter from the client. + * } + * @var string $redirect_uri Specified redirection URI. */ abstract protected function handle_authorization_submission( $submit, Client $client, $data ); @@ -84,26 +85,29 @@ public function handle_authorisation() { $error = new WP_Error( 'oauth2.types.authorization_code.handle_authorisation.invalid_submit', sprintf( - /* translators: %1$s is the translated "Authorize" button, %2$s is the translated "Cancel" button */ + /* translators: %1$s is the translated "Authorize" button, %2$s is the translated "Cancel" button */ __( 'Select either %1$s or %2$s to continue.', 'oauth2' ), __( 'Authorize', 'oauth2' ), __( 'Cancel', 'oauth2' ) ) ); + return $this->render_form( $client, $error ); } $submit = wp_unslash( $_POST['wp-submit'] ); $data = compact( 'redirect_uri', 'scope', 'state' ); + return $this->handle_authorization_submission( $submit, $client, $data ); } /** * Validate the supplied redirect URI. * - * @param Client $client Client to validate against. + * @param Client $client Client to validate against. * @param string|null $redirect_uri Redirect URI, if supplied. + * * @return string|WP_Error Valid redirect URI on success, error otherwise. */ protected function validate_redirect_uri( Client $client, $redirect_uri = null ) { @@ -133,7 +137,7 @@ protected function validate_redirect_uri( Client $client, $redirect_uri = null ) /** * Render the authorisation form. * - * @param Client $client Client being authorised. + * @param Client $client Client being authorised. * @param WP_Error $errors Errors to display, if any. */ protected function render_form( Client $client, WP_Error $errors = null ) { @@ -149,6 +153,7 @@ protected function render_form( Client $client, WP_Error $errors = null ) { * Get the nonce action for a client. * * @param Client $client Client to generate nonce for. + * * @return string Nonce action for given client. */ protected function get_nonce_action( Client $client ) { @@ -158,30 +163,56 @@ protected function get_nonce_action( Client $client ) { /** * Filter the redirection args. * - * @param array $redirect_args Redirect args. - * @param boolean $authorized True if authorized, false otherwise. - * @param Client $client Client being authorised. - * @param array $data Data for the request. + * @param array $redirect_args Redirect args. + * @param boolean $authorized True if authorized, false otherwise. + * @param Client $client Client being authorised. + * @param array $data Data for the request. */ protected function filter_redirect_args( $redirect_args, $authorized, Client $client, $data ) { if ( ! $authorized ) { /** * Filter the redirect args when the user has cancelled. * - * @param array $redirect_args Redirect args. - * @param Client $client Client being authorised. - * @param array $data Data for the request. + * @param array $redirect_args Redirect args. + * @param Client $client Client being authorised. + * @param array $data Data for the request. + * + * @deprecated */ - return apply_filters( 'oauth2.redirect_args.cancelled', $redirect_args, $client, $data ); + $redirect_args = apply_filters_deprecated( 'oauth2.redirect_args.cancelled', $redirect_args, $client, $data ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores + + /** + * Filter the redirect args when the user has cancelled. + * + * @param array $redirect_args Redirect args. + * @param Client $client Client being authorised. + * @param array $data Data for the request. + */ + $redirect_args = apply_filters( 'oauth2_redirect_args_cancelled', $redirect_args, $client, $data ); + + return $redirect_args; } /** * Filter the redirect args when the user has authorized. * - * @param array $redirect_args Redirect args. - * @param Client $client Client being authorised. - * @param array $data Data for the request. + * @param array $redirect_args Redirect args. + * @param Client $client Client being authorised. + * @param array $data Data for the request. + * + * @deprecated + */ + $redirect_args = apply_filters_deprecated( 'oauth2.redirect_args.authorized', $redirect_args, $client, $data ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores + + /** + * Filter the redirect args when the user has authorized. + * + * @param array $redirect_args Redirect args. + * @param Client $client Client being authorised. + * @param array $data Data for the request. */ - return apply_filters( 'oauth2.redirect_args.authorized', $redirect_args, $client, $data ); + $redirect_args = apply_filters( 'oauth2_redirect_args_authorized', $redirect_args, $client, $data ); + + return $redirect_args; } }