Skip to content

Commit 5952ed3

Browse files
committed
Split function declarations from functional code
1 parent 3dc81e0 commit 5952ed3

File tree

2 files changed

+150
-150
lines changed

2 files changed

+150
-150
lines changed

inc/namespace.php

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
3+
namespace WP\OAuth2;
4+
5+
use WP\OAuth2\Types\Type;
6+
use WP_REST_Response;
7+
8+
function bootstrap() {
9+
// Core authentication hooks.
10+
add_action( 'init', __NAMESPACE__ . '\\Client::register_type' );
11+
add_filter( 'determine_current_user', __NAMESPACE__ . '\\Authentication\\attempt_authentication', 11 );
12+
13+
// REST API integration.
14+
add_filter( 'rest_authentication_errors', __NAMESPACE__ . '\\Authentication\\maybe_report_errors' );
15+
add_filter( 'rest_index', __NAMESPACE__ . '\\register_in_index' );
16+
add_action( 'rest_api_init', __NAMESPACE__ . '\\Endpoints\\register' );
17+
18+
// Internal default hooks.
19+
add_filter( 'oauth2.grant_types', __NAMESPACE__ . '\\register_grant_types', 0 );
20+
21+
// Admin-related.
22+
add_action( 'init', __NAMESPACE__ . '\\rest_oauth2_load_authorize_page' );
23+
add_action( 'admin_menu', __NAMESPACE__ . '\\Admin\\register' );
24+
Admin\Profile\bootstrap();
25+
}
26+
27+
/**
28+
* Register the authorization page
29+
*
30+
* Alas, login_init is too late to register pages, as the action is already
31+
* sanitized before this.
32+
*/
33+
function rest_oauth2_load_authorize_page() {
34+
$authorizer = new Endpoints\Authorization();
35+
$authorizer->register_hooks();
36+
}
37+
38+
/**
39+
* Get valid grant types.
40+
*
41+
* @return Type[] Map of grant type to handler object.
42+
*/
43+
function get_grant_types() {
44+
/**
45+
* Filter valid grant types.
46+
*
47+
* Default supported grant types are added in register_grant_types().
48+
* Note that additional grant types must follow the extension policy in the
49+
* OAuth 2 specification.
50+
*
51+
* @param Type[] $grant_types Map of grant type to handler object.
52+
*/
53+
$grant_types = apply_filters( 'oauth2.grant_types', [] );
54+
foreach ( $grant_types as $type => $handler ) {
55+
if ( ! $handler instanceof Type ) {
56+
/* translators: 1: Grant type name, 2: Grant type interface */
57+
$message = __( 'Skipping invalid grant type "%1$s". Required interface "%1$s" not implemented.', 'oauth2' );
58+
_doing_it_wrong( __FUNCTION__, sprintf( $message, $type, 'WP\\OAuth2\\Types\\Type' ), '0.1.0' );
59+
unset( $grant_types[ $type ] );
60+
}
61+
}
62+
63+
return $grant_types;
64+
}
65+
66+
/**
67+
* Register default grant types.
68+
*
69+
* Callback for the oauth2.grant_types hook.
70+
*
71+
* @param array $types Existing grant types.
72+
* @return array Grant types with additional types registered.
73+
*/
74+
function register_grant_types( $types ) {
75+
$types['authorization_code'] = new Types\AuthorizationCode();
76+
$types['implicit'] = new Types\Implicit();
77+
78+
return $types;
79+
}
80+
81+
/**
82+
* Register the OAuth 2 authentication scheme in the API index.
83+
*
84+
* @param WP_REST_Response $response Index response object.
85+
* @return WP_REST_Response Update index repsonse object.
86+
*/
87+
function register_in_index( WP_REST_Response $response ) {
88+
$data = $response->get_data();
89+
90+
$data['authentication']['oauth2'] = [
91+
'endpoints' => [
92+
'authorization' => get_authorization_url(),
93+
'token' => get_token_url(),
94+
],
95+
'grant_types' => array_keys( get_grant_types() ),
96+
];
97+
98+
$response->set_data( $data );
99+
return $response;
100+
}
101+
102+
/**
103+
* Get the authorization endpoint URL.
104+
*
105+
* @return string URL for the OAuth 2 authorization endpoint.
106+
*/
107+
function get_authorization_url() {
108+
$url = wp_login_url();
109+
$url = add_query_arg( 'action', 'oauth2_authorize', $url );
110+
111+
/**
112+
* Filter the authorization URL.
113+
*
114+
* @param string $url URL for the OAuth 2 authorization endpoint.
115+
*/
116+
return apply_filters( 'oauth2.get_authorization_url', $url );
117+
}
118+
119+
/**
120+
* Get the token endpoint URL.
121+
*
122+
* @return string URL for the OAuth 2 token endpoint.
123+
*/
124+
function get_token_url() {
125+
$url = rest_url( 'oauth2/access_token' );
126+
127+
/**
128+
* Filter the token URL.
129+
*
130+
* @param string $url URL for the OAuth 2 token endpoint.
131+
*/
132+
return apply_filters( 'oauth2.get_token_url', $url );
133+
}

plugin.php

Lines changed: 17 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -10,155 +10,22 @@
1010

1111
namespace WP\OAuth2;
1212

13-
use WP\OAuth2\Types\Type;
14-
use WP_REST_Response;
13+
require __DIR__ . '/inc/namespace.php';
14+
require __DIR__ . '/inc/class-client.php';
15+
require __DIR__ . '/inc/class-scopes.php';
16+
require __DIR__ . '/inc/authentication/namespace.php';
17+
require __DIR__ . '/inc/endpoints/namespace.php';
18+
require __DIR__ . '/inc/endpoints/class-authorization.php';
19+
require __DIR__ . '/inc/endpoints/class-token.php';
20+
require __DIR__ . '/inc/tokens/namespace.php';
21+
require __DIR__ . '/inc/tokens/class-token.php';
22+
require __DIR__ . '/inc/tokens/class-access-token.php';
23+
require __DIR__ . '/inc/tokens/class-authorization-code.php';
24+
require __DIR__ . '/inc/types/class-type.php';
25+
require __DIR__ . '/inc/types/class-base.php';
26+
require __DIR__ . '/inc/types/class-authorization-code.php';
27+
require __DIR__ . '/inc/types/class-implicit.php';
28+
require __DIR__ . '/inc/admin/namespace.php';
29+
require __DIR__ . '/inc/admin/profile/namespace.php';
1530

1631
bootstrap();
17-
18-
function bootstrap() {
19-
load();
20-
21-
// Core authentication hooks.
22-
add_action( 'init', __NAMESPACE__ . '\\Client::register_type' );
23-
add_filter( 'determine_current_user', __NAMESPACE__ . '\\Authentication\\attempt_authentication', 11 );
24-
25-
// REST API integration.
26-
add_filter( 'rest_authentication_errors', __NAMESPACE__ . '\\Authentication\\maybe_report_errors' );
27-
add_filter( 'rest_index', __NAMESPACE__ . '\\register_in_index' );
28-
add_action( 'rest_api_init', __NAMESPACE__ . '\\Endpoints\\register' );
29-
30-
// Internal default hooks.
31-
add_filter( 'oauth2.grant_types', __NAMESPACE__ . '\\register_grant_types', 0 );
32-
33-
// Admin-related.
34-
add_action( 'init', __NAMESPACE__ . '\\rest_oauth2_load_authorize_page' );
35-
add_action( 'admin_menu', __NAMESPACE__ . '\\Admin\\register' );
36-
Admin\Profile\bootstrap();
37-
}
38-
39-
function load() {
40-
require __DIR__ . '/inc/class-client.php';
41-
require __DIR__ . '/inc/class-scopes.php';
42-
require __DIR__ . '/inc/authentication/namespace.php';
43-
require __DIR__ . '/inc/endpoints/namespace.php';
44-
require __DIR__ . '/inc/endpoints/class-authorization.php';
45-
require __DIR__ . '/inc/endpoints/class-token.php';
46-
require __DIR__ . '/inc/tokens/namespace.php';
47-
require __DIR__ . '/inc/tokens/class-token.php';
48-
require __DIR__ . '/inc/tokens/class-access-token.php';
49-
require __DIR__ . '/inc/tokens/class-authorization-code.php';
50-
require __DIR__ . '/inc/types/class-type.php';
51-
require __DIR__ . '/inc/types/class-base.php';
52-
require __DIR__ . '/inc/types/class-authorization-code.php';
53-
require __DIR__ . '/inc/types/class-implicit.php';
54-
require __DIR__ . '/inc/admin/namespace.php';
55-
require __DIR__ . '/inc/admin/profile/namespace.php';
56-
}
57-
58-
/**
59-
* Register the authorization page
60-
*
61-
* Alas, login_init is too late to register pages, as the action is already
62-
* sanitized before this.
63-
*/
64-
function rest_oauth2_load_authorize_page() {
65-
$authorizer = new Endpoints\Authorization();
66-
$authorizer->register_hooks();
67-
}
68-
69-
/**
70-
* Get valid grant types.
71-
*
72-
* @return Type[] Map of grant type to handler object.
73-
*/
74-
function get_grant_types() {
75-
/**
76-
* Filter valid grant types.
77-
*
78-
* Default supported grant types are added in register_grant_types().
79-
* Note that additional grant types must follow the extension policy in the
80-
* OAuth 2 specification.
81-
*
82-
* @param Type[] $grant_types Map of grant type to handler object.
83-
*/
84-
$grant_types = apply_filters( 'oauth2.grant_types', [] );
85-
foreach ( $grant_types as $type => $handler ) {
86-
if ( ! $handler instanceof Type ) {
87-
/* translators: 1: Grant type name, 2: Grant type interface */
88-
$message = __( 'Skipping invalid grant type "%s". Required interface "%s" not implemented.', 'oauth2' );
89-
_doing_it_wrong( __FUNCTION__, sprintf( $message, $type, 'WP\\OAuth2\\Types\\Type' ), '0.1.0' );
90-
unset( $grant_types[ $type ] );
91-
}
92-
}
93-
94-
return $grant_types;
95-
}
96-
97-
/**
98-
* Register default grant types.
99-
*
100-
* Callback for the oauth2.grant_types hook.
101-
*
102-
* @param array $types Existing grant types.
103-
* @return array Grant types with additional types registered.
104-
*/
105-
function register_grant_types( $types ) {
106-
$types['authorization_code'] = new Types\AuthorizationCode();
107-
$types['implicit'] = new Types\Implicit();
108-
109-
return $types;
110-
}
111-
112-
/**
113-
* Register the OAuth 2 authentication scheme in the API index.
114-
*
115-
* @param WP_REST_Response $response Index response object.
116-
* @return WP_REST_Response Update index repsonse object.
117-
*/
118-
function register_in_index( WP_REST_Response $response ) {
119-
$data = $response->get_data();
120-
121-
$data['authentication']['oauth2'] = [
122-
'endpoints' => [
123-
'authorization' => get_authorization_url(),
124-
'token' => get_token_url(),
125-
],
126-
'grant_types' => array_keys( get_grant_types() ),
127-
];
128-
129-
$response->set_data( $data );
130-
return $response;
131-
}
132-
133-
/**
134-
* Get the authorization endpoint URL.
135-
*
136-
* @return string URL for the OAuth 2 authorization endpoint.
137-
*/
138-
function get_authorization_url() {
139-
$url = wp_login_url();
140-
$url = add_query_arg( 'action', 'oauth2_authorize', $url );
141-
142-
/**
143-
* Filter the authorization URL.
144-
*
145-
* @param string $url URL for the OAuth 2 authorization endpoint.
146-
*/
147-
return apply_filters( 'oauth2.get_authorization_url', $url );
148-
}
149-
150-
/**
151-
* Get the token endpoint URL.
152-
*
153-
* @return string URL for the OAuth 2 token endpoint.
154-
*/
155-
function get_token_url() {
156-
$url = rest_url( 'oauth2/access_token' );
157-
158-
/**
159-
* Filter the token URL.
160-
*
161-
* @param string $url URL for the OAuth 2 token endpoint.
162-
*/
163-
return apply_filters( 'oauth2.get_token_url', $url );
164-
}

0 commit comments

Comments
 (0)