@@ -49,7 +49,7 @@ public function register_routes() {
4949 'callback ' => array ( $ this , 'get_licenses ' ),
5050 'permission_callback ' => '__return_true ' ,
5151 'args ' => array (
52- 'id ' => array (
52+ 'name ' => array (
5353 'required ' => false ,
5454 'type ' => 'string ' ,
5555 'description ' => 'Filter by license name ' ,
@@ -111,14 +111,17 @@ public function register_routes() {
111111 public function get_licenses ( WP_REST_Request $ data ) {
112112
113113 // Check if we have an ID passed.
114- $ searched_slug = $ data ->get_param ( 'id ' );
114+ $ searched_slug = $ data ->get_param ( 'name ' );
115115
116116 // Check if we have any keyword passed.
117117 $ keyword = $ data ->get_param ( 'keyword ' );
118118
119119 // Check if we have any steward passed.
120120 $ steward = $ data ->get_param ( 'steward ' );
121121
122+ // Check the SPDX parameter.
123+ $ spdx = $ data ->get_param ( 'spdx ' );
124+
122125 // Get all public posts from the 'osi_license' post type
123126 $ args = array (
124127 'post_type ' => 'license ' ,
@@ -132,6 +135,16 @@ public function get_licenses( WP_REST_Request $data ) {
132135 add_filter ( 'posts_where ' , array ( $ this , 'posts_where_title_like ' ), 10 , 2 );
133136
134137 $ args ['post_title_like ' ] = sanitize_text_field ( $ searched_slug ); // Use the post name (slug) to filter by ID
138+ } elseif ( ! empty ( $ spdx ) ) {
139+ // Cast the term to a regex pattern
140+ $ regex = $ this ->cast_wildcard_to_regex ( $ spdx );
141+
142+ // If we have no wildcards, look for a direct match
143+ $ args ['meta_query ' ][] = array (
144+ 'key ' => 'spdx_identifier_display_text ' ,
145+ 'value ' => $ regex ,
146+ 'compare ' => str_contains ( $ spdx , '* ' ) ? 'REGEXP ' : '== ' ,
147+ );
135148 } elseif ( ! empty ( $ keyword ) ) {
136149 // Add a tax query on taxonomy-license-category where passed term is a the slug
137150 $ args ['tax_query ' ] = array (
@@ -169,6 +182,26 @@ public function get_licenses( WP_REST_Request $data ) {
169182 return new WP_REST_Response ( $ all , 200 );
170183 }
171184
185+ /**
186+ * Turns a wildcard string into a LIKE query format.
187+ *
188+ * @param string $spdx The SPDX identifier to search for.
189+ *
190+ * @return string The LIKE query format for the SPDX identifier.
191+ */
192+ public function cast_wildcard_to_regex ( string $ spdx ): string {
193+ $ escaped = preg_quote ( $ spdx , '/ ' );
194+
195+ $ pattern = str_replace (
196+ array ( '\* ' , '\? ' ),
197+ array ( '.* ' , '. ' ),
198+ $ escaped
199+ );
200+
201+ // Ensure it matches the whole string
202+ return '^ ' . $ pattern . '$ ' ;
203+ }
204+
172205 /**
173206 * Get a license by its slug.
174207 *
@@ -222,12 +255,13 @@ public function get_license_model( string $id ): ?array {
222255 'id ' => $ license ->post_name ,
223256 'name ' => $ license ->post_title ,
224257 );
225-
226- $ meta = array (
258+ $ meta = array (
259+ ' spdx_id ' => get_post_meta ( $ license -> ID , ' spdx_identifier_display_text ' , true ),
227260 'version ' => get_post_meta ( $ license ->ID , 'version ' , true ),
228261 'submission_date ' => get_post_meta ( $ license ->ID , 'release_date ' , true ),
229262 'submission_url ' => get_post_meta ( $ license ->ID , 'submission_url ' , true ),
230263 'submitter_name ' => get_post_meta ( $ license ->ID , 'submitter ' , true ),
264+ 'approved ' => get_post_meta ( $ license ->ID , 'approved ' , true ) === '1 ' ? true : false ,
231265 'approval_date ' => get_post_meta ( $ license ->ID , 'approval_date ' , true ),
232266 'license_steward_version ' => get_post_meta ( $ license ->ID , 'license_steward_version ' , true ),
233267 'license_steward_url ' => get_post_meta ( $ license ->ID , 'license_steward_version_url ' , true ),
@@ -273,13 +307,24 @@ function ( $category ) {
273307
274308 return array_merge (
275309 $ model ,
276- array_map ( ' esc_html ' , $ meta ),
310+ array_map ( array ( $ this , ' sanitize_value ' ) , $ meta ),
277311 array ( 'stewards ' => $ license_stewards ),
278312 array ( 'keywords ' => $ license_categories ),
279313 array ( '_links ' => $ links )
280314 );
281315 }
282316
317+ /**
318+ * Sanitize values to ensure all but bools are escaped.
319+ *
320+ * @param mixed $value The value to sanitize.
321+ *
322+ * @return mixed The sanitized value.
323+ */
324+ public function sanitize_value ( $ value ) { // phpcs:ignore
325+ return is_bool ( $ value ) ? $ value : esc_html ( $ value );
326+ }
327+
283328 /**
284329 * Filter to allow the LIKE search of a post title.
285330 *
@@ -405,7 +450,6 @@ public function handle_redirects() {
405450 }
406451 }
407452
408-
409453 /**
410454 * Get the License scehema.
411455 *
@@ -418,6 +462,11 @@ public function get_license_schema(): array {
418462 'type ' => 'string ' ,
419463 'context ' => array ( 'view ' , 'edit ' ),
420464 ),
465+ 'spdx_id ' => array (
466+ 'description ' => 'The SPDX identifier for the license. ' ,
467+ 'type ' => 'string ' ,
468+ 'context ' => array ( 'view ' , 'edit ' ),
469+ ),
421470 'name ' => array (
422471 'description ' => 'The name of the license. ' ,
423472 'type ' => 'string ' ,
@@ -445,6 +494,12 @@ public function get_license_schema(): array {
445494 'type ' => 'string ' ,
446495 'context ' => array ( 'view ' ),
447496 ),
497+ 'approved ' => array (
498+ 'description ' => 'Whether the license is approved. ' ,
499+ 'type ' => 'boolean ' ,
500+ 'default ' => false ,
501+ 'context ' => array ( 'view ' , 'edit ' ),
502+ ),
448503 'approval_date ' => array (
449504 'description ' => 'Date the license was approved. ' ,
450505 'type ' => 'string ' ,
0 commit comments