Skip to content

Commit 0a755c3

Browse files
authored
Merge pull request #211 from OpenSourceOrg/fix/add-spdx-filter
Adds the approved meta value for the licences, registered as part of …
2 parents 89171c2 + 99fef5a commit 0a755c3

File tree

2 files changed

+174
-109
lines changed

2 files changed

+174
-109
lines changed

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

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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',
Lines changed: 118 additions & 105 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",
@@ -42,41 +41,41 @@
4241
"first_day": 1
4342
},
4443
{
45-
"key": "field_6783hdda5y544gf",
46-
"label": "Submission URL",
47-
"name": "submission_url",
48-
"aria-label": "",
49-
"type": "url",
50-
"instructions": "",
51-
"required": 0,
52-
"conditional_logic": 0,
53-
"wrapper": {
54-
"width": "",
55-
"class": "",
56-
"id": ""
57-
},
58-
"default_value": "",
59-
"placeholder": ""
44+
"key": "field_6783hdda5y544gf",
45+
"label": "Submission URL",
46+
"name": "submission_url",
47+
"aria-label": "",
48+
"type": "url",
49+
"instructions": "",
50+
"required": 0,
51+
"conditional_logic": 0,
52+
"wrapper": {
53+
"width": "",
54+
"class": "",
55+
"id": ""
56+
},
57+
"default_value": "",
58+
"placeholder": ""
6059
},
6160
{
62-
"key": "field_62fcda3b00451",
63-
"label": "Submitter",
64-
"name": "submitter",
65-
"aria-label": "",
66-
"type": "text",
67-
"instructions": "",
68-
"required": 0,
69-
"conditional_logic": 0,
70-
"wrapper": {
71-
"width": "",
72-
"class": "",
73-
"id": ""
74-
},
75-
"default_value": "",
76-
"placeholder": "",
77-
"prepend": "",
78-
"append": "",
79-
"maxlength": ""
61+
"key": "field_62fcda3b00451",
62+
"label": "Submitter",
63+
"name": "submitter",
64+
"aria-label": "",
65+
"type": "text",
66+
"instructions": "",
67+
"required": 0,
68+
"conditional_logic": 0,
69+
"wrapper": {
70+
"width": "",
71+
"class": "",
72+
"id": ""
73+
},
74+
"default_value": "",
75+
"placeholder": "",
76+
"prepend": "",
77+
"append": "",
78+
"maxlength": ""
8079
},
8180
{
8281
"key": "field_62fcd90c2f885",
@@ -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",
@@ -224,6 +237,6 @@
224237
"hide_on_screen": "",
225238
"active": true,
226239
"description": "",
227-
"show_in_rest": 0,
228-
"modified": 1686668177
229-
}
240+
"show_in_rest": 1,
241+
"modified": 1750425728
242+
}

0 commit comments

Comments
 (0)