Skip to content

Commit 204d972

Browse files
authored
Post Type Schemas: add internal fields and use in appropriate abilities
1 parent c360919 commit 204d972

File tree

2 files changed

+51
-20
lines changed

2 files changed

+51
-20
lines changed

includes/abilities/class-scf-post-type-abilities.php

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -88,25 +88,29 @@ private function get_scf_identifier_schema() {
8888
}
8989

9090
/**
91-
* Get the post type schema extended with internal fields for GET/LIST operations.
91+
* Get the internal fields schema (ID, _valid, local).
92+
*
93+
* @since 6.6.0
94+
* @return array The internal fields schema.
95+
*/
96+
private function get_internal_fields_schema() {
97+
$validator = new SCF_JSON_Schema_Validator();
98+
$schema = $validator->load_schema( 'internal-fields' );
99+
100+
return json_decode( wp_json_encode( $schema->definitions->internalFields ), true );
101+
}
102+
103+
/**
104+
* Get the post type schema extended with internal fields for GET/LIST/CREATE/UPDATE/IMPORT/DUPLICATE operations.
92105
*
93106
* @since 6.6.0
94107
*
95108
* @return array The extended post type schema with internal fields.
96109
*/
97110
private function get_post_type_with_internal_fields_schema() {
98-
$schema = $this->get_post_type_schema();
99-
100-
// Add internal WordPress/SCF fields that appear in GET/LIST but not EXPORT.
101-
$schema['properties']['ID'] = array(
102-
'type' => 'integer',
103-
'description' => __( 'WordPress post ID (internal field, present in GET/LIST operations only).', 'secure-custom-fields' ),
104-
);
105-
106-
$schema['properties']['_valid'] = array(
107-
'type' => 'boolean',
108-
'description' => __( 'SCF validation cache flag (internal field, present in GET/LIST operations only).', 'secure-custom-fields' ),
109-
);
111+
$schema = $this->get_post_type_schema();
112+
$internal_fields = $this->get_internal_fields_schema();
113+
$schema['properties'] = array_merge( $schema['properties'], $internal_fields['properties'] );
110114

111115
return $schema;
112116
}
@@ -259,7 +263,7 @@ private function register_create_post_type_ability() {
259263
),
260264
'permission_callback' => 'scf_current_user_has_capability',
261265
'input_schema' => $input_schema,
262-
'output_schema' => $this->get_post_type_schema(),
266+
'output_schema' => $this->get_post_type_with_internal_fields_schema(),
263267
)
264268
);
265269
}
@@ -290,7 +294,7 @@ private function register_update_post_type_ability() {
290294
),
291295
'permission_callback' => 'scf_current_user_has_capability',
292296
'input_schema' => $this->get_post_type_schema(),
293-
'output_schema' => $this->get_post_type_schema(),
297+
'output_schema' => $this->get_post_type_with_internal_fields_schema(),
294298
)
295299
);
296300
}
@@ -371,7 +375,7 @@ private function register_duplicate_post_type_ability() {
371375
),
372376
'required' => array( 'identifier' ),
373377
),
374-
'output_schema' => $this->get_post_type_schema(),
378+
'output_schema' => $this->get_post_type_with_internal_fields_schema(),
375379
)
376380
);
377381
}
@@ -419,8 +423,6 @@ private function register_export_post_type_ability() {
419423
* @since 6.6.0
420424
*/
421425
private function register_import_post_type_ability() {
422-
$post_type_schema = $this->get_post_type_schema();
423-
424426
wp_register_ability(
425427
'scf/import-post-type',
426428
array(
@@ -440,8 +442,8 @@ private function register_import_post_type_ability() {
440442
),
441443
),
442444
'permission_callback' => 'scf_current_user_has_capability',
443-
'input_schema' => $this->get_post_type_schema(),
444-
'output_schema' => $this->get_post_type_schema(),
445+
'input_schema' => $this->get_post_type_with_internal_fields_schema(),
446+
'output_schema' => $this->get_post_type_with_internal_fields_schema(),
445447
)
446448
);
447449
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"$id": "https://raw.githubusercontent.com/WordPress/secure-custom-fields/trunk/schemas/internal-fields.schema.json",
4+
"title": "SCF Internal Fields",
5+
"description": "Internal fields present in SCF entities (post types, taxonomies, field groups, options pages). These fields are managed by the system and appear in GET/LIST/CREATE/UPDATE/IMPORT/DUPLICATE outputs, but are stripped during EXPORT.",
6+
"definitions": {
7+
"internalFields": {
8+
"type": "object",
9+
"description": "Internal system-managed fields that appear in entity responses",
10+
"properties": {
11+
"ID": {
12+
"type": "integer",
13+
"minimum": 1,
14+
"description": "WordPress post ID. Present in GET/LIST/CREATE/UPDATE/IMPORT/DUPLICATE outputs. Optional in IMPORT input - if provided, updates existing entity; if omitted, creates new entity."
15+
},
16+
"_valid": {
17+
"type": "boolean",
18+
"description": "Validation cache flag . Indicates whether the entity passed validation."
19+
},
20+
"local": {
21+
"type": "string",
22+
"enum": ["json"],
23+
"description": "Source indicator. Present only if entity is locally-defined (e.g., from local JSON files). Stripped during export."
24+
}
25+
},
26+
"additionalProperties": false
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)