@@ -119,6 +119,9 @@ public function get_licenses( WP_REST_Request $data ) {
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,13 @@ 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+ // If we have no wildcards, look for a direct match
140+ $ args ['meta_query ' ][] = array (
141+ 'key ' => 'spdx_identifier_display_text ' ,
142+ 'value ' => str_contains ( $ spdx , '* ' ) ? $ this ->cast_wildcard_to_regex ( $ spdx ) : sanitize_text_field ( $ spdx ),
143+ 'compare ' => str_contains ( $ spdx , '* ' ) ? 'REGEXP ' : '== ' ,
144+ );
135145 } elseif ( ! empty ( $ keyword ) ) {
136146 // Add a tax query on taxonomy-license-category where passed term is a the slug
137147 $ args ['tax_query ' ] = array (
@@ -169,6 +179,26 @@ public function get_licenses( WP_REST_Request $data ) {
169179 return new WP_REST_Response ( $ all , 200 );
170180 }
171181
182+ /**
183+ * Turns a wildcard string into a LIKE query format.
184+ *
185+ * @param string $spdx The SPDX identifier to search for.
186+ *
187+ * @return string The LIKE query format for the SPDX identifier.
188+ */
189+ public function cast_wildcard_to_regex ( string $ spdx ): string {
190+ $ escaped = preg_quote ( $ spdx , '/ ' );
191+
192+ $ pattern = str_replace (
193+ array ( '\* ' , '\? ' ),
194+ array ( '.* ' , '. ' ),
195+ $ escaped
196+ );
197+
198+ // Ensure it matches the whole string
199+ return '^ ' . $ pattern . '$ ' ;
200+ }
201+
172202 /**
173203 * Get a license by its slug.
174204 *
@@ -222,12 +252,13 @@ public function get_license_model( string $id ): ?array {
222252 'id ' => $ license ->post_name ,
223253 'name ' => $ license ->post_title ,
224254 );
225-
226- $ meta = array (
255+ $ meta = array (
256+ ' spdx_id ' => get_post_meta ( $ license -> ID , ' spdx_identifier_display_text ' , true ),
227257 'version ' => get_post_meta ( $ license ->ID , 'version ' , true ),
228258 'submission_date ' => get_post_meta ( $ license ->ID , 'release_date ' , true ),
229259 'submission_url ' => get_post_meta ( $ license ->ID , 'submission_url ' , true ),
230260 'submitter_name ' => get_post_meta ( $ license ->ID , 'submitter ' , true ),
261+ 'approved ' => get_post_meta ( $ license ->ID , 'approved ' , true ) === '1 ' ? true : false ,
231262 'approval_date ' => get_post_meta ( $ license ->ID , 'approval_date ' , true ),
232263 'license_steward_version ' => get_post_meta ( $ license ->ID , 'license_steward_version ' , true ),
233264 'license_steward_url ' => get_post_meta ( $ license ->ID , 'license_steward_version_url ' , true ),
@@ -273,13 +304,24 @@ function ( $category ) {
273304
274305 return array_merge (
275306 $ model ,
276- array_map ( ' esc_html ' , $ meta ),
307+ array_map ( array ( $ this , ' sanitize_value ' ) , $ meta ),
277308 array ( 'stewards ' => $ license_stewards ),
278309 array ( 'keywords ' => $ license_categories ),
279310 array ( '_links ' => $ links )
280311 );
281312 }
282313
314+ /**
315+ * Sanitize values to ensure all but bools are escaped.
316+ *
317+ * @param mixed $value The value to sanitize.
318+ *
319+ * @return mixed The sanitized value.
320+ */
321+ public function sanitize_value ( $ value ) { // phpcs:ignore
322+ return is_bool ( $ value ) ? $ value : esc_html ( $ value );
323+ }
324+
283325 /**
284326 * Filter to allow the LIKE search of a post title.
285327 *
@@ -405,7 +447,6 @@ public function handle_redirects() {
405447 }
406448 }
407449
408-
409450 /**
410451 * Get the License scehema.
411452 *
@@ -418,6 +459,11 @@ public function get_license_schema(): array {
418459 'type ' => 'string ' ,
419460 'context ' => array ( 'view ' , 'edit ' ),
420461 ),
462+ 'spdx_id ' => array (
463+ 'description ' => 'The SPDX identifier for the license. ' ,
464+ 'type ' => 'string ' ,
465+ 'context ' => array ( 'view ' , 'edit ' ),
466+ ),
421467 'name ' => array (
422468 'description ' => 'The name of the license. ' ,
423469 'type ' => 'string ' ,
@@ -445,6 +491,12 @@ public function get_license_schema(): array {
445491 'type ' => 'string ' ,
446492 'context ' => array ( 'view ' ),
447493 ),
494+ 'approved ' => array (
495+ 'description ' => 'Whether the license is approved. ' ,
496+ 'type ' => 'boolean ' ,
497+ 'default ' => false ,
498+ 'context ' => array ( 'view ' , 'edit ' ),
499+ ),
448500 'approval_date ' => array (
449501 'description ' => 'Date the license was approved. ' ,
450502 'type ' => 'string ' ,
0 commit comments