@@ -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,17 +49,17 @@ public function register_routes() {
4949 'callback ' => array ( $ this , 'get_licenses ' ),
5050 'permission_callback ' => '__return_true ' ,
5151 'args ' => array (
52- 'name ' => array (
52+ 'license_name ' => array (
5353 'required ' => false ,
5454 'type ' => 'string ' ,
5555 'description ' => 'Filter by license name ' ,
5656 ),
57- 'keyword ' => array (
57+ 'keyword ' => array (
5858 'required ' => false ,
5959 'type ' => 'string ' ,
6060 'description ' => 'Filter licenses by keyword ' ,
6161 ),
62- 'steward ' => array (
62+ 'steward ' => array (
6363 'required ' => false ,
6464 'type ' => 'string ' ,
6565 'description ' => 'Filter licenses by steward ' ,
@@ -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+ $ name = $ data ->get_param ( 'license_name ' );
115115
116116 // Check if we have any keyword passed.
117117 $ keyword = $ data ->get_param ( 'keyword ' );
@@ -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,18 +344,28 @@ 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
360+ // Remap reserved "name" param to avoid canonical redirect
361+ if ( $ key === 'name ' ) {
362+ $ key = 'license_name ' ;
363+ }
364+
352365 $ sanitized_key = sanitize_key ( $ key );
353366 $ sanitized_value = is_array ( $ value )
354- ? array_map ( 'sanitize_text_field ' , $ value )
355- : sanitize_text_field ( $ value );
367+ ? array_map ( 'sanitize_text_field ' , $ value )
368+ : sanitize_text_field ( $ value );
356369
357370 $ request ->set_param ( $ sanitized_key , $ sanitized_value );
358371 }
@@ -397,6 +410,7 @@ public function handle_redirects() {
397410 }
398411 }
399412
413+
400414 /**
401415 * Get the License scehema.
402416 *
0 commit comments