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

Change filter names #59

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
38 changes: 30 additions & 8 deletions inc/admin/profile/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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( '<strong>%s</strong>', $client->get_name() );
if ( $is_personal ) {
Expand Down
23 changes: 18 additions & 5 deletions inc/class-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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;
Expand Down
42 changes: 39 additions & 3 deletions inc/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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 */
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand Down
75 changes: 53 additions & 22 deletions inc/types/class-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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 ) {
Expand All @@ -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 ) {
Expand All @@ -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;
}
}