Skip to content

Commit ab767e9

Browse files
authored
Merge pull request #209 from OpenSourceOrg/fix/add-spdx-filter__dev
Fix/add spdx filter dev
2 parents 20698be + bcaa04d commit ab767e9

File tree

2 files changed

+145
-77
lines changed

2 files changed

+145
-77
lines changed

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

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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',

plugins/osi-features/acf-fields/group_62d0160caa7af.json

Lines changed: 84 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
22
"key": "group_62d0160caa7af",
33
"title": "License Data",
4-
"fields": [
5-
{
4+
"fields": [{
65
"key": "field_62fcd8910c9f7",
76
"label": "Version",
87
"name": "version",
@@ -112,28 +111,26 @@
112111
"id": ""
113112
},
114113
"layout": "block",
115-
"sub_fields": [
116-
{
117-
"key": "field_62fcfcecb4ab2",
118-
"label": "Display Text",
119-
"name": "display_text",
120-
"aria-label": "",
121-
"type": "text",
122-
"instructions": "",
123-
"required": 0,
124-
"conditional_logic": 0,
125-
"wrapper": {
126-
"width": "",
127-
"class": "",
128-
"id": ""
129-
},
130-
"default_value": "",
131-
"placeholder": "",
132-
"prepend": "",
133-
"append": "",
134-
"maxlength": ""
135-
}
136-
]
114+
"sub_fields": [{
115+
"key": "field_62fcfcecb4ab2",
116+
"label": "Display Text",
117+
"name": "display_text",
118+
"aria-label": "",
119+
"type": "text",
120+
"instructions": "",
121+
"required": 0,
122+
"conditional_logic": 0,
123+
"wrapper": {
124+
"width": "",
125+
"class": "",
126+
"id": ""
127+
},
128+
"default_value": "",
129+
"placeholder": "",
130+
"prepend": "",
131+
"append": "",
132+
"maxlength": ""
133+
}]
137134
},
138135
{
139136
"key": "field_62fcda1400450",
@@ -150,25 +147,23 @@
150147
"id": ""
151148
},
152149
"layout": "block",
153-
"sub_fields": [
154-
{
155-
"key": "field_62fcda4d00452",
156-
"label": "URL",
157-
"name": "url",
158-
"aria-label": "",
159-
"type": "url",
160-
"instructions": "",
161-
"required": 0,
162-
"conditional_logic": 0,
163-
"wrapper": {
164-
"width": "",
165-
"class": "",
166-
"id": ""
167-
},
168-
"default_value": "",
169-
"placeholder": ""
170-
}
171-
]
150+
"sub_fields": [{
151+
"key": "field_62fcda4d00452",
152+
"label": "URL",
153+
"name": "url",
154+
"aria-label": "",
155+
"type": "url",
156+
"instructions": "",
157+
"required": 0,
158+
"conditional_logic": 0,
159+
"wrapper": {
160+
"width": "",
161+
"class": "",
162+
"id": ""
163+
},
164+
"default_value": "",
165+
"placeholder": ""
166+
}]
172167
},
173168
{
174169
"key": "field_62fcda6600453",
@@ -186,35 +181,53 @@
186181
},
187182
"relevanssi_exclude": 1,
188183
"layout": "block",
189-
"sub_fields": [
190-
{
191-
"key": "field_62fcda6600455",
192-
"label": "URL",
193-
"name": "url",
194-
"aria-label": "",
195-
"type": "url",
196-
"instructions": "",
197-
"required": 0,
198-
"conditional_logic": 0,
199-
"wrapper": {
200-
"width": "",
201-
"class": "",
202-
"id": ""
203-
},
204-
"default_value": "",
205-
"placeholder": ""
206-
}
207-
]
184+
"sub_fields": [{
185+
"key": "field_62fcda6600455",
186+
"label": "URL",
187+
"name": "url",
188+
"aria-label": "",
189+
"type": "url",
190+
"instructions": "",
191+
"required": 0,
192+
"conditional_logic": 0,
193+
"wrapper": {
194+
"width": "",
195+
"class": "",
196+
"id": ""
197+
},
198+
"default_value": "",
199+
"placeholder": ""
200+
}]
201+
},
202+
{
203+
"key": "field_6855605992d38",
204+
"label": "Approved",
205+
"name": "approved",
206+
"aria-label": "",
207+
"type": "true_false",
208+
"instructions": "",
209+
"required": 0,
210+
"conditional_logic": 0,
211+
"wrapper": {
212+
"width": "",
213+
"class": "",
214+
"id": ""
215+
},
216+
"relevanssi_exclude": 0,
217+
"message": "Denotes if a license has been approved",
218+
"default_value": 0,
219+
"allow_in_bindings": 0,
220+
"ui": 0,
221+
"ui_on_text": "",
222+
"ui_off_text": ""
208223
}
209224
],
210225
"location": [
211-
[
212-
{
213-
"param": "post_type",
214-
"operator": "==",
215-
"value": "license"
216-
}
217-
]
226+
[{
227+
"param": "post_type",
228+
"operator": "==",
229+
"value": "license"
230+
}]
218231
],
219232
"menu_order": 0,
220233
"position": "side",
@@ -225,5 +238,5 @@
225238
"active": true,
226239
"description": "",
227240
"show_in_rest": 1,
228-
"modified": 1749470357
229-
}
241+
"modified": 1750425728
242+
}

0 commit comments

Comments
 (0)