Skip to content

Commit 84dba06

Browse files
committed
added in the means to query do SPDX
1 parent 20698be commit 84dba06

File tree

2 files changed

+124
-76
lines changed

2 files changed

+124
-76
lines changed

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

Lines changed: 40 additions & 5 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,19 @@ 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+
);
148+
// // Add the filter to search by SPDX I
149+
// add_filter( 'posts_where', array( $this, 'posts_where_spdx_like' ), 10, 2 );
150+
// $args['spdx_like'] = $this->cast_wildcard_to_sql_like( $spdx ); // Use the spdx identifier to filter by SPDX ID
135151
} elseif ( ! empty( $keyword ) ) {
136152
// Add a tax query on taxonomy-license-category where passed term is a the slug
137153
$args['tax_query'] = array(
@@ -169,6 +185,26 @@ public function get_licenses( WP_REST_Request $data ) {
169185
return new WP_REST_Response( $all, 200 );
170186
}
171187

188+
/**
189+
* Turns a wildcard string into a LIKE query format.
190+
*
191+
* @param string $spdx The SPDX identifier to search for.
192+
*
193+
* @return string The LIKE query format for the SPDX identifier.
194+
*/
195+
public function cast_wildcard_to_regex( string $spdx ): string {
196+
$escaped = preg_quote( $spdx, '/' );
197+
198+
$pattern = str_replace(
199+
array( '\*', '\?' ),
200+
array( '.*', '.' ),
201+
$escaped
202+
);
203+
204+
// Ensure it matches the whole string
205+
return '^' . $pattern . '$';
206+
}
207+
172208
/**
173209
* Get a license by its slug.
174210
*
@@ -222,8 +258,7 @@ public function get_license_model( string $id ): ?array {
222258
'id' => $license->post_name,
223259
'name' => $license->post_title,
224260
);
225-
226-
$meta = array(
261+
$meta = array(
227262
'version' => get_post_meta( $license->ID, 'version', true ),
228263
'submission_date' => get_post_meta( $license->ID, 'release_date', true ),
229264
'submission_url' => get_post_meta( $license->ID, 'submission_url', true ),
@@ -232,6 +267,7 @@ public function get_license_model( string $id ): ?array {
232267
'license_steward_version' => get_post_meta( $license->ID, 'license_steward_version', true ),
233268
'license_steward_url' => get_post_meta( $license->ID, 'license_steward_version_url', true ),
234269
'board_minutes' => get_post_meta( $license->ID, 'link_to_board_minutes_url', true ),
270+
'spdx_id' => get_post_meta( $license->ID, 'spdx_identifier_display_text', true ),
235271
);
236272

237273
// Get the license stewards (terms)
@@ -405,7 +441,6 @@ public function handle_redirects() {
405441
}
406442
}
407443

408-
409444
/**
410445
* Get the License scehema.
411446
*

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)