1313define ( 'OSI_API_NAMESPACE ' , 'osi/v1 ' );
1414
1515/**
16- * OSI API Class
16+ * OSI API Class.
1717 */
1818class OSI_API {
1919
@@ -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 ' ), 0 );
34+ add_action ( 'template_redirect ' , array ( $ instance , 'handle_redirects ' ) );
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- 'license_name ' => array (
52+ '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 ( 'license_name ' );
114+ $ name = $ data ->get_param ( 'name ' );
115115
116116 // Check if we have any keyword passed.
117117 $ keyword = $ data ->get_param ( 'keyword ' );
@@ -184,20 +184,14 @@ public function get_license_by_slug( WP_REST_Request $request ) {
184184 }
185185
186186 // Get the license post by slug
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 ) ) {
187+ $ license = get_page_by_path ( $ slug , OBJECT , 'license ' );
188+
189+ if ( ! $ license ) {
196190 return new WP_REST_Response ( array ( 'error ' => 'License not found. ' ), 404 );
197191 }
198192
199193 // Compile the license model
200- $ model = $ this ->get_license_model ( $ licenses [ 0 ] ->ID );
194+ $ model = $ this ->get_license_model ( $ license ->ID );
201195
202196 return new WP_REST_Response ( $ model , 200 );
203197 }
@@ -230,7 +224,7 @@ public function get_license_model( string $id ): ?array {
230224 'submitter_name ' => get_post_meta ( $ license ->ID , 'submitter ' , true ),
231225 'approval_date ' => get_post_meta ( $ license ->ID , 'approval_date ' , true ),
232226 'license_steward_version ' => get_post_meta ( $ license ->ID , 'license_steward_version ' , true ),
233- 'license_steward_url ' => get_post_meta ( $ license ->ID , 'license_steward_version_url ' , true ),
227+ 'licanse_steward_url ' => get_post_meta ( $ license ->ID , 'license_steward_version_url ' , true ),
234228 'board_minutes ' => get_post_meta ( $ license ->ID , 'link_to_board_minutes_url ' , true ),
235229 );
236230
@@ -310,8 +304,9 @@ public function posts_where_title_like( string $where, \WP_Query $query ) {
310304 * @return void
311305 */
312306 public function add_rewrites () {
307+ // This is used to redirect /api/licenses to the REST API endpoint.
313308 add_rewrite_rule (
314- '^api/licenses?/? $ ' ,
309+ '^api/licenses?$ ' , // regex for /api/licenses or /api/licenses
315310 'index.php?osi_api_redirect=1 ' ,
316311 'top '
317312 );
@@ -322,6 +317,8 @@ public function add_rewrites() {
322317 'index.php?osi_api_slug_redirect=1&license_slug=$matches[1] ' ,
323318 'top '
324319 );
320+
321+ flush_rewrite_rules ();
325322 }
326323
327324 /**
@@ -344,24 +341,14 @@ public function add_query_vars( array $vars ): array {
344341 * @return void
345342 */
346343 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-
353344 if ( get_query_var ( 'osi_api_redirect ' ) ) {
354345 // Build REST request
355346 $ request = new WP_REST_Request ( 'GET ' , '/osi/v1/licenses ' );
356347
357348 // Add query parameters if any
358349 if ( ! empty ( $ _GET ) ) { // phpcs:ignore WordPress.Security.NonceVerification
359350 foreach ( $ _GET as $ key => $ value ) { // phpcs:ignore WordPress.Security.NonceVerification
360- // Remap reserved "name" param to avoid canonical redirect
361- if ( $ key === 'name ' ) {
362- $ key = 'license_name ' ;
363- }
364-
351+ // Sanitize key and value
365352 $ sanitized_key = sanitize_key ( $ key );
366353 $ sanitized_value = is_array ( $ value )
367354 ? array_map ( 'sanitize_text_field ' , $ value )
@@ -410,7 +397,6 @@ public function handle_redirects() {
410397 }
411398 }
412399
413-
414400 /**
415401 * Get the License scehema.
416402 *
0 commit comments