Skip to content

Commit 166d688

Browse files
author
Sebastian Thulin
committed
fix: field sanitization.
1 parent 1185f9d commit 166d688

1 file changed

Lines changed: 87 additions & 3 deletions

File tree

source/php/Api/Submit/Post.php

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,95 @@ public function insertPost($moduleID = null, $fieldMeta = null): WP_Error|int {
119119
],
120120
]);
121121

122-
if ($this->wpService->isWpError($result)) {
123-
return $result;
122+
// Post Successfully created, store the fields
123+
if (!$this->wpService->isWpError($result)) {
124+
$sanitizedFieldMetaKeys = $this->filterUnmappedFieldKeysForPostType(
125+
array_keys($fieldMeta),
126+
'post'
127+
);
128+
$this->storeFields($fieldMeta, $result);
124129
}
125130

126-
return $result;
131+
return $result; // Probobly a Wp_Error
132+
}
133+
134+
/**
135+
* Stores the fields in the database
136+
*
137+
* @param array $fields The fields to store
138+
* @param int $postID The ID of the post to store the fields for
139+
*/
140+
public function storeFields($fields, $postID)
141+
{
142+
$sanitizedFieldMetaKeys = $this->filterUnmappedFieldKeysForPostType(
143+
array_keys($fields),
144+
'post'
145+
);
146+
147+
foreach ($sanitizedFieldMetaKeys as $key) {
148+
if (isset($fields[$key])) {
149+
$this->acfService->updateField(
150+
$key,
151+
$this->santitileFieldValue($fields[$key], $key),
152+
$postID
153+
);
154+
}
155+
}
156+
}
157+
158+
/**
159+
* Removes fields that are not registered in any of the field groups mapped to the post type
160+
*
161+
* @param array $fields The fields to check
162+
* @param string $postType The post type to check against
163+
* @param array $defualtKeys The default keys to include, if any.
164+
*
165+
* @return array The filtered fields
166+
*/
167+
private function filterUnmappedFieldKeysForPostType(array $fieldKeys, string $postType, array $defualtKeys = []): array
168+
{
169+
$fieldGroups = $this->acfService->getFieldGroups($postType);
170+
171+
foreach ($fieldGroups as $group) {
172+
$fields = $this->acfService->getFields($group['key']);
173+
174+
foreach ($fields as $field) {
175+
if (isset($field['key']) && in_array($field['key'], $fieldKeys, true)) {
176+
$validKeys[] = $field['key'];
177+
}
178+
}
179+
}
180+
181+
return array_merge($validKeys, $defualtKeys);
182+
}
183+
184+
/**
185+
* Sanitizes the field value based on its type
186+
*
187+
* @param mixed $value The value to sanitize
188+
* @param string $fieldKey The key of the field
189+
*
190+
* @return mixed The sanitized value
191+
*/
192+
private function santitileFieldValue($value, $fieldKey) {
193+
$field = $this->acfService->getField($fieldKey);
194+
195+
if (isset($field['type'])) {
196+
switch ($field['type']) {
197+
case 'text':
198+
return sanitize_text_field($value);
199+
case 'email':
200+
return sanitize_email($value);
201+
case 'url':
202+
return esc_url_raw($value);
203+
case 'number':
204+
return intval($value);
205+
default:
206+
return $value;
207+
}
208+
}
209+
210+
return $value;
127211
}
128212

129213
/**

0 commit comments

Comments
 (0)