Skip to content

Commit 742067b

Browse files
authored
Merge pull request #206 from OpenSourceOrg/fix/api-redirect-issues
Fix/api redirect issues
2 parents fad54bd + 54625c0 commit 742067b

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

mu-plugins/osi-api/osi-api.php

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static function init() {
3131
// Add all custom rewrites
3232
add_action( 'init', array( $instance, 'add_rewrites' ) );
3333
add_filter( 'query_vars', array( $instance, 'add_query_vars' ), 0 );
34-
add_action( 'template_redirect', array( $instance, 'handle_redirects' ) );
34+
add_action( 'template_redirect', array( $instance, 'handle_redirects' ), 0 );
3535
}
3636

3737
/**
@@ -49,7 +49,7 @@ public function register_routes() {
4949
'callback' => array( $this, 'get_licenses' ),
5050
'permission_callback' => '__return_true',
5151
'args' => array(
52-
'name' => array(
52+
'id' => array(
5353
'required' => false,
5454
'type' => 'string',
5555
'description' => 'Filter by license name',
@@ -111,7 +111,7 @@ public function register_routes() {
111111
public function get_licenses( WP_REST_Request $data ) {
112112

113113
// Check if we have an ID passed.
114-
$name = $data->get_param( 'name' );
114+
$searched_slug = $data->get_param( 'id' );
115115

116116
// Check if we have any keyword passed.
117117
$keyword = $data->get_param( 'keyword' );
@@ -127,11 +127,11 @@ public function get_licenses( WP_REST_Request $data ) {
127127
);
128128

129129
// If we have an id, search for posts with a name LIKE
130-
if ( ! empty( $name ) ) {
130+
if ( ! empty( $searched_slug ) ) {
131131
// Add the filter
132132
add_filter( 'posts_where', array( $this, 'posts_where_title_like' ), 10, 2 );
133133

134-
$args['post_title_like'] = sanitize_text_field( $name ); // Use the post name (slug) to filter by ID
134+
$args['post_title_like'] = sanitize_text_field( $searched_slug ); // Use the post name (slug) to filter by ID
135135
} elseif ( ! empty( $keyword ) ) {
136136
// Add a tax query on taxonomy-license-category where passed term is a the slug
137137
$args['tax_query'] = array(
@@ -184,14 +184,20 @@ public function get_license_by_slug( WP_REST_Request $request ) {
184184
}
185185

186186
// Get the license post by slug
187-
$license = get_page_by_path( $slug, OBJECT, 'license' );
188-
189-
if ( ! $license ) {
187+
$licenses = get_posts(
188+
array(
189+
'name' => $slug,
190+
'post_type' => 'license',
191+
'post_status' => 'publish',
192+
'numberposts' => 1,
193+
)
194+
);
195+
if ( empty( $licenses ) ) {
190196
return new WP_REST_Response( array( 'error' => 'License not found.' ), 404 );
191197
}
192198

193199
// Compile the license model
194-
$model = $this->get_license_model( $license->ID );
200+
$model = $this->get_license_model( $licenses[0]->ID );
195201

196202
return new WP_REST_Response( $model, 200 );
197203
}
@@ -224,7 +230,7 @@ public function get_license_model( string $id ): ?array {
224230
'submitter_name' => get_post_meta( $license->ID, 'submitter', true ),
225231
'approval_date' => get_post_meta( $license->ID, 'approval_date', true ),
226232
'license_steward_version' => get_post_meta( $license->ID, 'license_steward_version', true ),
227-
'licanse_steward_url' => get_post_meta( $license->ID, 'license_steward_version_url', true ),
233+
'license_steward_url' => get_post_meta( $license->ID, 'license_steward_version_url', true ),
228234
'board_minutes' => get_post_meta( $license->ID, 'link_to_board_minutes_url', true ),
229235
);
230236

@@ -304,9 +310,8 @@ public function posts_where_title_like( string $where, \WP_Query $query ) {
304310
* @return void
305311
*/
306312
public function add_rewrites() {
307-
// This is used to redirect /api/licenses to the REST API endpoint.
308313
add_rewrite_rule(
309-
'^api/licenses?$', // regex for /api/licenses or /api/licenses
314+
'^api/licenses?/?$',
310315
'index.php?osi_api_redirect=1',
311316
'top'
312317
);
@@ -317,8 +322,6 @@ public function add_rewrites() {
317322
'index.php?osi_api_slug_redirect=1&license_slug=$matches[1]',
318323
'top'
319324
);
320-
321-
flush_rewrite_rules();
322325
}
323326

324327
/**
@@ -341,14 +344,19 @@ public function add_query_vars( array $vars ): array {
341344
* @return void
342345
*/
343346
public function handle_redirects() {
347+
348+
// Prevent WordPress canonical redirects for custom API endpoints
349+
if ( get_query_var( 'osi_api_redirect' ) || get_query_var( 'osi_api_slug_redirect' ) ) {
350+
remove_filter( 'template_redirect', 'redirect_canonical' );
351+
}
352+
344353
if ( get_query_var( 'osi_api_redirect' ) ) {
345354
// Build REST request
346355
$request = new WP_REST_Request( 'GET', '/osi/v1/licenses' );
347356

348357
// Add query parameters if any
349358
if ( ! empty( $_GET ) ) { // phpcs:ignore WordPress.Security.NonceVerification
350359
foreach ( $_GET as $key => $value ) { // phpcs:ignore WordPress.Security.NonceVerification
351-
// Sanitize key and value
352360
$sanitized_key = sanitize_key( $key );
353361
$sanitized_value = is_array( $value )
354362
? array_map( 'sanitize_text_field', $value )
@@ -397,6 +405,7 @@ public function handle_redirects() {
397405
}
398406
}
399407

408+
400409
/**
401410
* Get the License scehema.
402411
*

0 commit comments

Comments
 (0)