Skip to content

Commit 0ea8e13

Browse files
author
Andrei Igna
committed
Updated - sanititze data before saving to DB
1 parent a9cf7c8 commit 0ea8e13

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

example.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
use Layered\Wp\CustomPostType;
66

77

8+
if (!function_exists('add_action')) {
9+
die('This file needs to be included in a WordPress plugin or theme');
10+
}
11+
812
add_action('init', function() {
913

1014
// Add a custom post type

src/CustomPostType.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public function __construct(string $postType, array $args = []) {
1515
$this->postType = sanitize_key($postType);
1616
$args['labels'] = $args['labels'] ?? [];
1717

18-
// TODO use inflector for nice name & pluralize
1918
$args['labels']['singular_name'] = $args['labels']['singular_name'] ?? Inflector::humanize($this->postType);
2019
$args['labels']['name'] = $args['labels']['name'] ?? Inflector::pluralize($args['labels']['singular_name']);
2120

src/MetaFields.php

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public function prepareFieldTypes(array $field): array {
6767
$field = wp_parse_args($field, [
6868
'name' => 'Field',
6969
'type' => 'string',
70+
'sanitize_callback' => null,
7071
'renderValue' => function($obj) {
7172
return $obj;
7273
},
@@ -84,16 +85,22 @@ public function prepareFieldTypes(array $field): array {
8485
/* 1. Register meta fields */
8586

8687
protected function prepareMetaArgs(string $metaKey, array $args = []): array {
88+
$args['type'] = $args['type'] ?? 'text';
8789

8890
if (!isset($args['name']) || !strlen($args['name'])) {
8991
_doing_it_wrong(__FUNCTION__, sprintf(__('Field "%s" is required when registering a custom meta field', 'layered'), $args['name']), null);
9092
}
9193

94+
if (!isset($this->fields[$args['type']])) {
95+
_doing_it_wrong(__FUNCTION__, sprintf(__('Field type "%s" is not available as meta field', 'layered'), $args['type']), null);
96+
}
97+
9298
$args = wp_parse_args($args, [
9399
'type' => 'text',
94100
'description' => '',
95101
'group' => __('Meta Fields', 'layered'),
96102
'single' => true,
103+
'sanitize_callback' => $this->fields[$args['type']]['sanitize_callback'],
97104
'defaultValue' => null,
98105
'initialValue' => null,
99106
'value' => '',
@@ -110,10 +117,6 @@ protected function prepareMetaArgs(string $metaKey, array $args = []): array {
110117
'showInBulkEdit' => false
111118
]);
112119

113-
if (!isset($this->fields[$args['type']])) {
114-
_doing_it_wrong(__FUNCTION__, sprintf(__('Field type "%s" is not available as meta field', 'layered'), $args['type']), null);
115-
}
116-
117120
$args['advancedType'] = $args['type'];
118121
$args['type'] = $this->fields[$args['type']]['type'];
119122

@@ -489,12 +492,19 @@ public function savePostMetaFields(int $postId, \WP_Post $post) {
489492
delete_post_meta($post->ID, $metaKey);
490493

491494
foreach ($_POST[$metaKey] as $i => $value) {
495+
if ($metaField['sanitize_callback']) {
496+
$value = call_user_func($metaField['sanitize_callback'], $value);
497+
}
492498
if (strlen($value)) {
493499
add_post_meta($post->ID, $metaKey, $value);
494500
}
495501
}
496502
} else {
497-
update_post_meta($post->ID, $metaKey, $_POST[$metaKey]);
503+
$value = $_POST[$metaKey];
504+
if ($metaField['sanitize_callback']) {
505+
$value = call_user_func($metaField['sanitize_callback'], $value);
506+
}
507+
update_post_meta($post->ID, $metaKey, $value);
498508
}
499509
} else {
500510
delete_post_meta($post->ID, $metaKey);
@@ -513,12 +523,19 @@ public function saveTaxonomyMetaFields(int $termId, int $termTaxonomyId, string
513523
delete_term_meta($termId, $metaKey);
514524

515525
foreach ($_POST[$metaKey] as $i => $value) {
526+
if ($metaField['sanitize_callback']) {
527+
$value = call_user_func($metaField['sanitize_callback'], $value);
528+
}
516529
if (strlen($value)) {
517530
add_term_meta($termId, $metaKey, $value);
518531
}
519532
}
520533
} else {
521-
update_term_meta($termId, $metaKey, $_POST[$metaKey]);
534+
$value = $_POST[$metaKey];
535+
if ($metaField['sanitize_callback']) {
536+
$value = call_user_func($metaField['sanitize_callback'], $value);
537+
}
538+
update_term_meta($termId, $metaKey, $value);
522539
}
523540
} else {
524541
delete_term_meta($termId, $metaKey);
@@ -534,7 +551,11 @@ public function savePostBulkEdit() {
534551
foreach ($metaFields as $metaKey => $metaField) {
535552
if ($metaField['showInBulkEdit'] && isset($_REQUEST['_' . $metaKey]) && strlen($_REQUEST['_' . $metaKey]) && $_REQUEST['_' . $metaKey] != -1) {
536553
foreach ($_REQUEST['post'] as $postId) {
537-
update_post_meta($postId, $metaKey, $_REQUEST['_' . $metaKey]);
554+
$value = $_REQUEST['_' . $metaKey];
555+
if ($metaField['sanitize_callback']) {
556+
$value = call_user_func($metaField['sanitize_callback'], $value);
557+
}
558+
update_post_meta($postId, $metaKey, $value);
538559
}
539560
}
540561
}
@@ -761,6 +782,7 @@ public static function bulkEditableSelectField(array $metaField, string $metaKey
761782
$fields['url'] = [
762783
'name' => __('URL', 'layered'),
763784
'type' => 'string',
785+
'sanitize_callback' => 'esc_url_raw',
764786
'renderEditableField' => [MetaFields::class, 'editableTextField'],
765787
'renderEditableFieldBulk' => [MetaFields::class, 'bulkEditableTextField']
766788
];
@@ -814,7 +836,7 @@ public static function bulkEditableSelectField(array $metaField, string $metaKey
814836
'name' => __('Attachment', 'layered'),
815837
'type' => 'integer',
816838
'renderValue' => function($metaValue) {
817-
return get_post($metaValue);
839+
return $metaValue ? get_post($metaValue) : null;
818840
},
819841
'renderReadable' => function($metaValue) {
820842
return $metaValue ? wp_get_attachment_image($metaValue->ID, [50, 50], strpos($metaValue->post_mime_type, 'image') === false, ['class' => 'attachment-preview']) : '';

0 commit comments

Comments
 (0)